Skip to content

Commit

Permalink
feat: fix AssetID encoding and improve support (#1476)
Browse files Browse the repository at this point in the history
* feat: remove asset id coder and unit test

* feat: remove asset id coder from ABI coder switch

* feat: implement asset id address helper

* feat: implement docs snippet for asset id

* feat: fix existing asset id tests

* feat: add docs

* chore: changeset

* chore: linting

* chore: force rebuild

* feat: enable release to PR

* feat: add echo evm address project to docs snippets workspace

* feat: fix failing docs snippets tests

* feat: remove PR release

---------

Co-authored-by: Anderson Arboleya <[email protected]>
  • Loading branch information
danielbate and arboleya authored Dec 5, 2023
1 parent 870e4b5 commit 9b552fa
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 150 deletions.
7 changes: 7 additions & 0 deletions .changeset/friendly-cobras-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fuel-ts/abi-coder": minor
"@fuel-ts/address": minor
"@fuel-ts/interfaces": minor
---

Improve support of Asset ID
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Contract } from 'fuels';
import type { Contract, AssetId } from 'fuels';
import { Wallet, BN, BaseAssetId, Provider, FUEL_NETWORK_URL } from 'fuels';

import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects';
Expand All @@ -15,6 +15,7 @@ describe(__filename, () => {

it('should successfully get a contract balance', async () => {
// #region contract-balance-3
// #context import type { AssetId } from 'fuels';
// #context import { Wallet, BN, BaseAssetId } from 'fuels';

const amountToForward = 40;
Expand All @@ -26,8 +27,12 @@ describe(__filename, () => {

const { minGasPrice, maxGasPerTx } = provider.getGasConfig();

const asset: AssetId = {
value: BaseAssetId,
};

await contract.functions
.transfer(amountToTransfer, BaseAssetId, recipient.address.toB256())
.transfer(amountToTransfer, asset, recipient.address.toB256())
.callParams({
forward: [amountToForward, BaseAssetId],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { safeExec } from '@fuel-ts/errors/test-utils';
import type { AssetId } from 'fuels';
import { BaseAssetId, Wallet, BN, Contract } from 'fuels';

import { DocSnippetProjectsEnum, getDocsSnippetsForcProject } from '../../../test/fixtures/forc-projects';
import {
DocSnippetProjectsEnum,
getDocsSnippetsForcProject,
} from '../../../test/fixtures/forc-projects';
import { createAndDeployContractFromProject } from '../../utils';

describe(__filename, () => {
Expand All @@ -19,9 +23,13 @@ describe(__filename, () => {
provider,
}).address.toB256();

const assetId: AssetId = {
value: BaseAssetId,
};

// #region simulate-transactions-1
const { gasUsed } = await contract.functions
.transfer(amountToTransfer, BaseAssetId, someAddress)
.transfer(amountToTransfer, assetId, someAddress)
.callParams({
forward: [amountToForward, BaseAssetId],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { BN, ContractFactory, BaseAssetId, ScriptTransactionRequest } from 'fuels';
import type { CoinQuantityLike, Contract, WalletUnlocked } from 'fuels';

import { DocSnippetProjectsEnum, getDocsSnippetsForcProject } from '../../../test/fixtures/forc-projects';
import {
DocSnippetProjectsEnum,
getDocsSnippetsForcProject,
} from '../../../test/fixtures/forc-projects';
import { defaultTxParams, getTestWallet } from '../../utils';

describe(__filename, () => {
Expand Down Expand Up @@ -53,7 +56,13 @@ describe(__filename, () => {
});

// 2. Instantiate the script main arguments
const scriptArguments = [contract.id.toB256(), assetIdA, new BN(1000), assetIdB, new BN(500)];
const scriptArguments = [
contract.id.toB256(),
{ value: assetIdA },
new BN(1000),
{ value: assetIdB },
new BN(500),
];

// 3. Get the resources for inputs and outpoints
const fee = request.calculateFee(gasPriceFactor);
Expand Down
73 changes: 73 additions & 0 deletions apps/docs-snippets/src/guide/types/asset-id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { AssetId, Contract, B256Address } from 'fuels';
import { Address } from 'fuels';

import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects';
import { createAndDeployContractFromProject } from '../../utils';

describe('AssetId', () => {
let contract: Contract;
const Bits256: B256Address = '0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c';

beforeAll(async () => {
contract = await createAndDeployContractFromProject(DocSnippetProjectsEnum.ECHO_ASSET_ID);
});

it('should demonstrate typed asset id example', () => {
// #region asset-id-1
// #context import type { AssetId } from 'fuels';

const assetId: AssetId = {
value: Bits256,
};
// #endregion asset-id-1

expect(assetId.value).toBe(Bits256);
});

it('should create an AssetId from a B256Address', async () => {
// #region asset-id-2
// #context import type { AssetId } from 'fuels';
// #context import { AssetId } from 'fuels';

const b256Address = '0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c';

const address = Address.fromB256(b256Address);

const assetId: AssetId = address.toAssetId();
// #endregion asset-id-2

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

expect(value).toBeTruthy();
});

it('should pass an asset id to a contract', async () => {
// #region asset-id-3
// #context import type { AssetId } from 'fuels';

const assetId: AssetId = {
value: Bits256,
};

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

expect(value).toBeTruthy();
// #endregion asset-id-3
});

it('should retrieve an asset id from a contract', async () => {
// #region asset-id-4
// #context import type { AssetId } from 'fuels';

const assetId: AssetId = {
value: Bits256,
};

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

expect(value).toEqual(assetId);
// #endregion asset-id-4

expect(value.value).toEqual(Bits256);
});
});
1 change: 1 addition & 0 deletions apps/docs-snippets/test/fixtures/forc-projects/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"echo-employee-data-vector",
"whitelisted-address-predicate",
"echo-evm-address",
"echo-asset-id",
"script-transfer-to-contract",
"echo-bytes",
"echo-raw-slice",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "echo-asset-id"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// #region asset-id-1
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
}
}
// #endregion asset-id-1
1 change: 1 addition & 0 deletions apps/docs-snippets/test/fixtures/forc-projects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum DocSnippetProjectsEnum {
ECHO_BYTES = 'echo-bytes',
ECHO_RAW_SLICE = 'echo-raw-slice',
ECHO_STD_STRING = 'echo-std-string',
ECHO_ASSET_ID = 'echo-asset-id',
SCRIPT_TRANSFER_TO_CONTRACT = 'script-transfer-to-contract',
}

Expand Down
4 changes: 4 additions & 0 deletions apps/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export default defineConfig({
text: 'Evm Address',
link: '/guide/types/evm-address',
},
{
text: 'Asset Id',
link: '/guide/types/asset-id',
},
{
text: 'Arrays',
link: '/guide/types/arrays',
Expand Down
19 changes: 19 additions & 0 deletions apps/docs/src/guide/types/asset-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

<<< @/../../docs-snippets/src/guide/types/asset-id.test.ts#asset-id-1{ts:line-numbers}

## 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:

<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-asset-id/src/main.sw#asset-id-1{ts:line-numbers}

The `AssetId` type can be used with the SDK and passed to the contract function as follows:

<<< @/../../docs-snippets/src/guide/types/asset-id.test.ts#asset-id-3{ts:line-numbers}

And to validate the returned value:

<<< @/../../docs-snippets/src/guide/types/asset-id.test.ts#asset-id-4{ts:line-numbers}
3 changes: 0 additions & 3 deletions packages/abi-coder/src/abi-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors';

import type { DecodedValue, InputValue, Coder } from './coders/abstract-coder';
import { ArrayCoder } from './coders/array';
import { AssetIdCoder } from './coders/asset-id';
import { B256Coder } from './coders/b256';
import { B512Coder } from './coders/b512';
import { BooleanCoder } from './coders/boolean';
Expand Down Expand Up @@ -70,8 +69,6 @@ export abstract class AbiCoder {
return new B256Coder();
case 'struct B512':
return new B512Coder();
case 'struct AssetId':
return new AssetIdCoder();
case BYTES_CODER_TYPE:
return new ByteCoder();
case STD_STRING_CODER_TYPE:
Expand Down
94 changes: 0 additions & 94 deletions packages/abi-coder/src/coders/asset-id.test.ts

This file was deleted.

36 changes: 0 additions & 36 deletions packages/abi-coder/src/coders/asset-id.ts

This file was deleted.

Loading

0 comments on commit 9b552fa

Please sign in to comment.