Skip to content

Asset ID

An Asset ID can be represented using the AssetId type. It's definition matches the Sway standard library type being a Struct wrapper around an inner Bits256 value.

ts
import type { AssetId } from 'fuels';

const assetId: AssetId = {
  bits: Bits256,
};
See code in context

Using an Asset ID

The AssetId type can be integrated with your contract calls. Consider the following contract that can compares and return an Asset ID:

ts
contract;

abi EvmTest {
    fn echo_asset_id() -> AssetId;
    fn echo_asset_id_comparison(asset_id: AssetId) -> bool;
    fn echo_asset_id_input(asset_id: AssetId) -> AssetId;
}

const ASSET_ID: AssetId = AssetId::from(0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c);

impl EvmTest for Contract {
    fn echo_asset_id() -> AssetId {
        ASSET_ID
    }

    fn echo_asset_id_comparison(asset_id: AssetId) -> bool {
        asset_id == ASSET_ID
    }

    fn echo_asset_id_input(asset_id: AssetId) -> AssetId {
        asset_id
    }
}
See code in context

The AssetId type can be used with the SDK and passed to the contract function as follows:

ts
import type { AssetId } from 'fuels';

const assetId: AssetId = {
  bits: Bits256,
};

const { value } = await contract.functions.echo_asset_id_comparison(assetId).simulate();

expect(value).toBeTruthy();
See code in context

And to validate the returned value:

ts
import type { AssetId } from 'fuels';

const assetId: AssetId = {
  bits: Bits256,
};

const { value } = await contract.functions.echo_asset_id().simulate();

expect(value).toEqual(assetId);
See code in context