Transactions on Erc721
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.NFT,
address: '0x7C6ac312c08b975a0e74Be8Cd8C6ad8b8b4A8DEB', // Contract address
chainId: 80001,
metadata: {
name: 'My NFT',
description: 'Description of my nft',
attributes: [
{
trait_type: 'Color',
value: 'green'
},
{
trait_type: 'Year',
value: '1952'
}
], // An array with the asset's attributes
}
}
const mintableAsset = sdk.utils.mintableAssetFromPrimitive(file, assetData);
AssetType is a typescript enum that is imported from @alfabc/sdk
In the example above, the constant assetData satisfies the interface Primitives<Asset>
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.
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 erc721 is needed.
const erc721api = sdk.transactions.erc721Api;
And we also get the caller.
const caller = sdk.transactions.callers.getCaller();
We then use its minar method to finally execute the mining.
const owner = new Owner('0xa73a3b8ACa335855EeaC2f9Fb505BB0360A1B703')
const transaction = await erc721api.mint(mintableAsset, owner, caller);
Transfer
To transfer we only need to send the asset of type NFT and the address of the new owner.
We use the api for the erc721.
const erc721api = sdk.transactions.erc721Api;
We use the transfer method of the api.
const to = new Owner('0xa73a3b8ACa335855EeaC2f9Fb505BB0360A1B703');
const caller = sdk.transactions.callers.getCaller();
const transaction = await erc721api.transfer(asset, to, caller);
Delete
To delete, we only need to send the NFT type asset that we want to delete.
We use the api for the erc721.
const erc721api = sdk.transactions.erc721Api;
We use the burn method of the api.
const caller = sdk.transactions.callers.getCaller();
const transaction = await erc721api.burn(asset, caller)