Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: implement a helper createAssetId function #2665

Merged
merged 14 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/free-birds-soar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/transactions": patch
---

chore: implement a helper createAssetId function
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { bn, getMintedAssetId } from 'fuels';
import type { AssetId } from 'fuels';
import { bn, getMintedAssetId, createAssetId } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import { TokenFactory } from '../../../test/typegen';
import { EchoAssetIdFactory, TokenFactory } from '../../../test/typegen';

/**
* @group node
Expand Down Expand Up @@ -32,4 +33,26 @@ describe(__filename, () => {
expect(mintedAssetId).toBeDefined();
expect(txResult.transactionResult.isStatusSuccess).toBeTruthy();
});

it('should create valid asset ID', async () => {
using launched = await launchTestNode({
contractsConfigs: [{ factory: EchoAssetIdFactory }],
});

const {
contracts: [contract],
} = launched;

// #region create-asset-id-1
// #import { createAssetId, AssetId };

const subID = '0xc7fd1d987ada439fc085cfa3c49416cf2b504ac50151e3c2335d60595cb90745';

const assetId: AssetId = createAssetId(contract.id.toB256(), subID);
// #endregion create-asset-id-1
const { value } = await contract.functions.echo_asset_id_input(assetId).simulate();

expect(assetId).toBeDefined();
expect(value).toEqual(assetId);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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);
Expand All @@ -16,5 +17,9 @@ impl EvmTest for Contract {
fn echo_asset_id_comparison(asset_id: AssetId) -> bool {
asset_id == ASSET_ID
}

fn echo_asset_id_input(asset_id: AssetId) -> AssetId {
asset_id
}
}
// #endregion asset-id-1
1 change: 1 addition & 0 deletions apps/docs/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ABI
ABIs
ASM
AssetId
IDE
IDEs
LSP
Expand Down
8 changes: 8 additions & 0 deletions apps/docs/src/guide/contracts/minted-token-asset-id.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ Imagine that this contract is already deployed and we are about to mint some coi

<<< @/../../docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts#minted-token-asset-id-2{ts:line-numbers}

## Obtaining the Asset ID

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.

## Create Asset 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](https://docs.fuel.network/docs/fuels-ts/interfaces/#assetid), ready to be used in a Sway program invocation:

<<< @/../../docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts#create-asset-id-1{ts:line-numbers}
5 changes: 5 additions & 0 deletions packages/transactions/src/coders/receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Coder, BigNumberCoder, B256Coder, NumberCoder } from '@fuel-ts/abi-coder';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { sha256 } from '@fuel-ts/hasher';
import type { AssetId } from '@fuel-ts/interfaces';
import type { BN } from '@fuel-ts/math';
import { arrayify, concat } from '@fuel-ts/utils';

Expand Down Expand Up @@ -774,6 +775,10 @@ export const getMintedAssetId = (contractId: string, subId: string): string => {
return sha256(concat([contractIdBytes, subIdBytes]));
};

export const createAssetId = (contractId: string, subId: string): AssetId => ({
bits: getMintedAssetId(contractId, subId),
});

export class ReceiptMintCoder extends Coder<ReceiptMint, ReceiptMint> {
constructor() {
super('ReceiptMint', 'struct ReceiptMint', 0);
Expand Down
Loading