From c81710961399da6039c5c991aa45099240768f8c Mon Sep 17 00:00:00 2001 From: Bolaji Ahmad <56865496+bolajahmad@users.noreply.github.com> Date: Mon, 1 Jul 2024 00:50:42 +0100 Subject: [PATCH 1/7] draft: included a new createAssetId fn in receipt; added documentation --- apps/docs/src/guide/types/native-parameters.md | 8 ++++++++ packages/transactions/src/coders/receipt.ts | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/apps/docs/src/guide/types/native-parameters.md b/apps/docs/src/guide/types/native-parameters.md index 3ec894d081c..dc8a7dfd9bc 100644 --- a/apps/docs/src/guide/types/native-parameters.md +++ b/apps/docs/src/guide/types/native-parameters.md @@ -74,3 +74,11 @@ To pass an `AssetId` as an input parameter to a Sway program, you can define the For a Sway program that returns an `AssetId` type, you can convert the returned value to a `string` as shown below: <<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#asset-id-output + +### `CreateAssetId` + +To create an `AssetId` from a `contractId` and `subId`. You can create the `assetId` as shown below: + +``` + const assetId = createAssetId(contractId, subId); +``` diff --git a/packages/transactions/src/coders/receipt.ts b/packages/transactions/src/coders/receipt.ts index 19c4b1970f8..518258bba05 100644 --- a/packages/transactions/src/coders/receipt.ts +++ b/packages/transactions/src/coders/receipt.ts @@ -774,6 +774,18 @@ export const getMintedAssetId = (contractId: string, subId: string): string => { return sha256(concat([contractIdBytes, subIdBytes])); }; +type AssetIdInput = { + bits: string; +}; + +export const createAssetId = (contractId: string, subId: string): AssetIdInput => { + const bits = getMintedAssetId(contractId, subId); + const assetId: AssetIdInput = { + bits, + }; + return assetId; +}; + export class ReceiptMintCoder extends Coder { constructor() { super('ReceiptMint', 'struct ReceiptMint', 0); From 075ef73515d1062811a099838480d311088d78dd Mon Sep 17 00:00:00 2001 From: Bolaji Ahmad <56865496+bolajahmad@users.noreply.github.com> Date: Tue, 2 Jul 2024 15:22:06 +0100 Subject: [PATCH 2/7] chore: implemented tests for new createAssetId function --- .changeset/free-birds-soar.md | 4 ++++ .../guide/contracts/minted-token-asset-id.test.ts | 14 +++++++++++++- .../src/guide/contracts/minted-token-asset-id.md | 8 ++++++++ apps/docs/src/guide/types/native-parameters.md | 8 -------- packages/transactions/src/coders/receipt.ts | 15 ++++----------- 5 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 .changeset/free-birds-soar.md diff --git a/.changeset/free-birds-soar.md b/.changeset/free-birds-soar.md new file mode 100644 index 00000000000..c46ea83ebd9 --- /dev/null +++ b/.changeset/free-birds-soar.md @@ -0,0 +1,4 @@ +--- +--- + +chore: implement a helper createAssetId function diff --git a/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts b/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts index c2635e6751f..1675afcf8a1 100644 --- a/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts +++ b/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts @@ -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'; @@ -30,4 +30,16 @@ describe(__filename, () => { 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); + const { value } = await contract.functions.echo_asset_id_comparison(assetId).simulate(); + // #endregion create-asset-id-1 + + expect(assetId).toBeDefined(); + expect(value).toBeTruthy(); + }); }); diff --git a/apps/docs/src/guide/contracts/minted-token-asset-id.md b/apps/docs/src/guide/contracts/minted-token-asset-id.md index a97a6d0157b..9c211786d92 100644 --- a/apps/docs/src/guide/contracts/minted-token-asset-id.md +++ b/apps/docs/src/guide/contracts/minted-token-asset-id.md @@ -18,3 +18,11 @@ 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} 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` + +This is an extension of the Minted Token Asset ID, but it returns an [AssetId](https://github.com/FuelLabs/fuels-ts/blob/1fb529bf03e5751e61afac69bfbd488e05577d10/packages/interfaces/src/index.ts#L33). Under the hood, the `createAssetId` calls the `getMintedAssetId` + +A sample usage is as shown below: + +<<< @/../../docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts#create-asset-id-1{ts:line-numbers} diff --git a/apps/docs/src/guide/types/native-parameters.md b/apps/docs/src/guide/types/native-parameters.md index dc8a7dfd9bc..3ec894d081c 100644 --- a/apps/docs/src/guide/types/native-parameters.md +++ b/apps/docs/src/guide/types/native-parameters.md @@ -74,11 +74,3 @@ To pass an `AssetId` as an input parameter to a Sway program, you can define the For a Sway program that returns an `AssetId` type, you can convert the returned value to a `string` as shown below: <<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#asset-id-output - -### `CreateAssetId` - -To create an `AssetId` from a `contractId` and `subId`. You can create the `assetId` as shown below: - -``` - const assetId = createAssetId(contractId, subId); -``` diff --git a/packages/transactions/src/coders/receipt.ts b/packages/transactions/src/coders/receipt.ts index 518258bba05..9e8f54881e0 100644 --- a/packages/transactions/src/coders/receipt.ts +++ b/packages/transactions/src/coders/receipt.ts @@ -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'; @@ -774,17 +775,9 @@ export const getMintedAssetId = (contractId: string, subId: string): string => { return sha256(concat([contractIdBytes, subIdBytes])); }; -type AssetIdInput = { - bits: string; -}; - -export const createAssetId = (contractId: string, subId: string): AssetIdInput => { - const bits = getMintedAssetId(contractId, subId); - const assetId: AssetIdInput = { - bits, - }; - return assetId; -}; +export const createAssetId = (contractId: string, subId: string): AssetId => ({ + bits: getMintedAssetId(contractId, subId), +}); export class ReceiptMintCoder extends Coder { constructor() { From 9a24fdc60aa723977d91b9e93365a4475d8902f0 Mon Sep 17 00:00:00 2001 From: Bolaji Ahmad <56865496+bolajahmad@users.noreply.github.com> Date: Wed, 3 Jul 2024 14:00:52 +0100 Subject: [PATCH 3/7] docs: improved documentation for obtaining and minting new asset --- apps/docs/src/guide/contracts/minted-token-asset-id.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/docs/src/guide/contracts/minted-token-asset-id.md b/apps/docs/src/guide/contracts/minted-token-asset-id.md index 9c211786d92..6b3551656d6 100644 --- a/apps/docs/src/guide/contracts/minted-token-asset-id.md +++ b/apps/docs/src/guide/contracts/minted-token-asset-id.md @@ -17,12 +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} -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. +## Obtaining the Asset ID -## `Create 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. -This is an extension of the Minted Token Asset ID, but it returns an [AssetId](https://github.com/FuelLabs/fuels-ts/blob/1fb529bf03e5751e61afac69bfbd488e05577d10/packages/interfaces/src/index.ts#L33). Under the hood, the `createAssetId` calls the `getMintedAssetId` +## Create Asset Id -A sample usage is as shown below: +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://github.com/FuelLabs/fuels-ts/blob/1fb529bf03e5751e61afac69bfbd488e05577d10/packages/interfaces/src/index.ts#L33), 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} From 476b9081813fda517dd2f21171836cab6ae278b2 Mon Sep 17 00:00:00 2001 From: Bolaji Ahmad <56865496+bolajahmad@users.noreply.github.com> Date: Thu, 4 Jul 2024 12:39:39 +0100 Subject: [PATCH 4/7] docs: replaced source code reference for createAssetId docs --- .../src/guide/contracts/minted-token-asset-id.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts b/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts index 1675afcf8a1..4d77781590d 100644 --- a/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts +++ b/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts @@ -36,8 +36,8 @@ describe(__filename, () => { const subID = '0xc7fd1d987ada439fc085cfa3c49416cf2b504ac50151e3c2335d60595cb90745'; const assetId = createAssetId(contract.id.toB256(), subID); - const { value } = await contract.functions.echo_asset_id_comparison(assetId).simulate(); // #endregion create-asset-id-1 + const { value } = await contract.functions.echo_asset_id_comparison(assetId).simulate(); expect(assetId).toBeDefined(); expect(value).toBeTruthy(); From 8504c3a7ca43bf7ef8ec805977b6fadaff828a0b Mon Sep 17 00:00:00 2001 From: Bolaji Ahmad <56865496+bolajahmad@users.noreply.github.com> Date: Sat, 6 Jul 2024 11:21:43 +0100 Subject: [PATCH 5/7] docs: improved create assetId docs; updated spell checks --- apps/docs/spell-check-custom-words.txt | 1 + apps/docs/src/guide/contracts/minted-token-asset-id.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/docs/spell-check-custom-words.txt b/apps/docs/spell-check-custom-words.txt index 61def69ded2..3302da23501 100644 --- a/apps/docs/spell-check-custom-words.txt +++ b/apps/docs/spell-check-custom-words.txt @@ -1,6 +1,7 @@ ABI ABIs ASM +AssetId IDE IDEs LSP diff --git a/apps/docs/src/guide/contracts/minted-token-asset-id.md b/apps/docs/src/guide/contracts/minted-token-asset-id.md index 6b3551656d6..091e7a26eaa 100644 --- a/apps/docs/src/guide/contracts/minted-token-asset-id.md +++ b/apps/docs/src/guide/contracts/minted-token-asset-id.md @@ -23,6 +23,6 @@ Since the asset ID depends on the contract ID, which is always dynamic (unlike t ## 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://github.com/FuelLabs/fuels-ts/blob/1fb529bf03e5751e61afac69bfbd488e05577d10/packages/interfaces/src/index.ts#L33), ready to be used in a Sway program invocation: +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} From a012f5e889762cc8f4893a487b28ce1ce4c441ac Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 5 Aug 2024 10:46:50 +0100 Subject: [PATCH 6/7] chore: added missing changeset package --- .changeset/free-birds-soar.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/free-birds-soar.md b/.changeset/free-birds-soar.md index c46ea83ebd9..20e0e647c0c 100644 --- a/.changeset/free-birds-soar.md +++ b/.changeset/free-birds-soar.md @@ -1,4 +1,5 @@ --- +"@fuel-ts/transactions": patch --- chore: implement a helper createAssetId function From 059d5e272023d4d70e4df63f1387085e6b26bb25 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 5 Aug 2024 11:06:31 +0100 Subject: [PATCH 7/7] chore: fixed test --- .../guide/contracts/minted-token-asset-id.test.ts | 14 +++++++++----- .../forc-projects/echo-asset-id/src/main.sw | 5 +++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts b/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts index facaa8e2d71..fac50856e2d 100644 --- a/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts +++ b/apps/docs-snippets/src/guide/contracts/minted-token-asset-id.test.ts @@ -1,7 +1,9 @@ +import type { AssetId } from 'fuels'; import { bn, getMintedAssetId, createAssetId } from 'fuels'; import { launchTestNode } from 'fuels/test-utils'; -import { TokenAbi__factory } from '../../../test/typegen'; +import { EchoAssetIdAbi__factory, TokenAbi__factory } from '../../../test/typegen'; +import EchoAssetIdAbiHex from '../../../test/typegen/contracts/EchoAssetIdAbi.hex'; import TokenAbiHex from '../../../test/typegen/contracts/TokenAbi.hex'; /** @@ -36,7 +38,7 @@ describe(__filename, () => { it('should create valid asset ID', async () => { using launched = await launchTestNode({ - contractsConfigs: [{ deployer: TokenAbi__factory, bytecode: TokenAbiHex }], + contractsConfigs: [{ deployer: EchoAssetIdAbi__factory, bytecode: EchoAssetIdAbiHex }], }); const { @@ -44,13 +46,15 @@ describe(__filename, () => { } = launched; // #region create-asset-id-1 + // #import { createAssetId, AssetId }; + const subID = '0xc7fd1d987ada439fc085cfa3c49416cf2b504ac50151e3c2335d60595cb90745'; - const assetId = createAssetId(contract.id.toB256(), subID); + const assetId: AssetId = createAssetId(contract.id.toB256(), subID); // #endregion create-asset-id-1 - const { value } = await contract.functions.echo_asset_id_comparison(assetId).simulate(); + const { value } = await contract.functions.echo_asset_id_input(assetId).simulate(); expect(assetId).toBeDefined(); - expect(value).toBeTruthy(); + expect(value).toEqual(assetId); }); }); diff --git a/apps/docs-snippets/test/fixtures/forc-projects/echo-asset-id/src/main.sw b/apps/docs-snippets/test/fixtures/forc-projects/echo-asset-id/src/main.sw index f7dd31b148d..ab01dd6f207 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/echo-asset-id/src/main.sw +++ b/apps/docs-snippets/test/fixtures/forc-projects/echo-asset-id/src/main.sw @@ -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); @@ -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