The asset ID of a token on the Fuel network is determined by two factors:
Both of which are bits256 strings.
The process involves applying a SHA-256 hash algorithm to the combination of the Contract ID and the Sub ID, to derive an Asset ID - as explained here .
Consider the following simplified token contract:
contract;
use std::asset::{burn, mint, transfer};
abi Token {
fn transfer_to_address(target: Address, asset_id: AssetId, coins: u64);
fn transfer_to_contract(recipient: ContractId, asset_id: AssetId, coins: u64);
fn mint_coins(sub_id: b256, mint_amount: u64);
fn burn_coins(sub_id: b256, burn_amount: u64);
}
impl Token for Contract {
fn transfer_to_address(recipient: Address, asset_id: AssetId, amount: u64) {
transfer(Identity::Address(recipient), asset_id, amount);
}
fn transfer_to_contract(target: ContractId, asset_id: AssetId, amount: u64) {
transfer(Identity::ContractId(target), asset_id, amount);
}
fn mint_coins(sub_id: b256, mint_amount: u64) {
mint(sub_id, mint_amount);
}
fn burn_coins(sub_id: b256, burn_amount: u64) {
burn(sub_id, burn_amount);
}
}
Imagine that this contract is already deployed and we are about to mint some coins:
// #import { bn, getMintedAssetId };
// Any valid bits256 string can be used as a sub ID
const subID = '0xc7fd1d987ada439fc085cfa3c49416cf2b504ac50151e3c2335d60595cb90745';
const mintAmount = bn(1000);
const { waitForResult } = await contract.functions.mint_coins(subID, mintAmount).call();
const txResult = await waitForResult();
const mintedAssetId = getMintedAssetId(contract.id.toB256(), subID);
Since the asset ID depends on the contract ID, which is always dynamic (unlike the sub ID, which can be set to a fixed value), the helper getMintedAssetId
can be used to easily obtain the asset ID for a given contract ID and sub ID.
The SDK provides a helper named createAssetId
which takes the contract ID and sub ID as parameters. This helper internally calls getMintedAssetId
and returns the Sway native parameter AssetId , ready to be used in a Sway program invocation:
// #import { createAssetId, AssetId };
const subID = '0xc7fd1d987ada439fc085cfa3c49416cf2b504ac50151e3c2335d60595cb90745';
const assetId: AssetId = createAssetId(contract.id.toB256(), subID);