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 8 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
4 changes: 4 additions & 0 deletions .changeset/free-birds-soar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

chore: implement a helper createAssetId function
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Contract } from 'fuels';
import { bn, getMintedAssetId } from 'fuels';
import { bn, getMintedAssetId, createAssetId } from 'fuels';

import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects';
import { createAndDeployContractFromProject } from '../../utils';
Expand Down Expand Up @@ -30,4 +30,16 @@
expect(mintedAssetId).toBeDefined();
expect(txResult.transactionResult.isStatusSuccess).toBeTruthy();
});

it('should create valid asset ID', async () => {
// #region create-asset-id-1
const subID = '0xc7fd1d987ada439fc085cfa3c49416cf2b504ac50151e3c2335d60595cb90745';

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

Check failure on line 40 in apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts

View workflow job for this annotation

GitHub Actions / node@18

apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts > /home/runner/work/fuels-ts/fuels-ts/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts > should create valid asset ID

TypeError: contract.functions.echo_asset_id_comparison is not a function ❯ apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts:40:48

Check failure on line 40 in apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts

View workflow job for this annotation

GitHub Actions / node@20

apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts > /home/runner/work/fuels-ts/fuels-ts/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts > should create valid asset ID

TypeError: contract.functions.echo_asset_id_comparison is not a function ❯ apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts:40:48

Check failure on line 40 in apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts

View workflow job for this annotation

GitHub Actions / node@22

apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts > /home/runner/work/fuels-ts/fuels-ts/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts > should create valid asset ID

TypeError: contract.functions.echo_asset_id_comparison is not a function ❯ apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts:40:48
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved

expect(assetId).toBeDefined();
expect(value).toBeTruthy();
});
});
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