-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fix
AssetID
encoding and improve support (#1476)
* 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
1 parent
870e4b5
commit 9b552fa
Showing
19 changed files
with
199 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
apps/docs-snippets/test/fixtures/forc-projects/echo-asset-id/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
22 changes: 22 additions & 0 deletions
22
apps/docs-snippets/test/fixtures/forc-projects/echo-asset-id/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.