Skip to content

Commit

Permalink
chore: implemented tests for new createAssetId function
Browse files Browse the repository at this point in the history
  • Loading branch information
bolajahmad committed Jul 2, 2024
1 parent c817109 commit 075ef73
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
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 @@ 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();
});
});
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 @@ -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}
8 changes: 0 additions & 8 deletions apps/docs/src/guide/types/native-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
15 changes: 4 additions & 11 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,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<ReceiptMint, ReceiptMint> {
constructor() {
Expand Down

0 comments on commit 075ef73

Please sign in to comment.