Skip to main content

Transactions on erc1155

tip

Transactions are operations that are executed on the blockchain.

Mine

Create a mineable asset

A minable asset is a utility class that allows you to pass a javascript File object and the properties of the asset to be created as arguments.

const file = new File(); // File file
const assetData = {
type: AssetType.ERC1155,
address: '0x7C6ac312c08b975a0e74Be8Cd8C6ad8b8b4A8DEB', // Contract address
chainId: 80001,
amount: 200, // Default will be set to 1
metadata: {
name: 'My erc1155',
description: 'Description of my erc1155',
attributes: [
{
trait_type: 'Color',
value: 'green'
},
{
trait_type: 'Year',
value: '1952'
}
], // An array with the asset's attributes
decimal: 5
}
}
const mintableAsset = sdk.utils.mintableERC1155FromPrimitive(assetData, file);
Asset Types

AssetType is a typescript enum that is imported from @alfabc/sdk

Primitives of an Asset

In the example above, the constant assetData satisfies the interface Primitives<Asset>

The File object

In the example above for demonstration purposes only, an instance of the File class is made. But this object can also come from an input in a form.

Node.js File

The File class does not exist in Node.js . If you use the sdk on the server you will need to use the class SdkFile utility available. This complies with the interface of the original File class.

import { SdkFile } from "@alfabc/sdk";

Prepare the asset

After having created the minable asset, you have to prepare it for mining. The class will upload the File to the ipfs service to make it available on the ipfs network and automatically add the image hash to the asset's metadata. In addition, some special attributes will be added to the asset that will be used for the operation of the transaction on the server or locally.

await mintableAsset.prepare();

Execute mining transaction

To execute a transaction you need to use one of the apis available in the sdk in its transactions property. In this case, the api for the erc1155 is needed.

const erc1155Api = sdk.transactions.erc1155Api;

And we also get the caller.

const caller = sdk.transactions.callers.getCaller();

We then use its mint method to finally execute the mining.

const owner = new Owner('0xa73a3b8ACa335855EeaC2f9Fb505BB0360A1B703')
const transaction = await erc1155Api.mint(mintableAsset, owner, caller);

Transfer

Parameters to transfer

To transfer an asset of type ERC1155 it is necessary to send an object with the transfer parameters.

const payload: TransferERC1155Payload = {
asset: Asset,
to: new Owner("0xa73a3b8ACa335855EeaC2f9Fb505BB0360A1B703"),
amount: new Amount("1"),
assetId: newID("1"),
};
Amount to transfer

ERC1155 assets are assets that perform two functions, they are like NFT when they only have 1 unit. If they have more than one unit then they are more like ERC20, so when transferring them you have to specify the amount of the asset you want to transfer, this value is defined in wei

Execute transfer transaction

We use the api for the erc1155.

const erc1155Api = sdk.transactions.erc1155Api;

And we send the data.

const caller = sdk.transactions.callers.getCaller();
const transaction = await erc1155Api.transfer(payload, caller);