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 = {
  value: 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;
}

const ASSET_ID: AssetId = AssetId {
    value: 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
    }
}
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 = {
  value: 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 = {
  value: Bits256,
};

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

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