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

feat: ABI refactor #3085

Closed
wants to merge 55 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
c041f20
chore: added @fuel-ts/abi package
petertonysmith94 Sep 3, 2024
cb48d9c
chore: added basic folder structure
petertonysmith94 Sep 3, 2024
74a6f50
chore: added ignore to changeset
petertonysmith94 Sep 3, 2024
0736344
chore: changeset
petertonysmith94 Sep 3, 2024
584d354
chore: changeset
petertonysmith94 Sep 3, 2024
f63a171
chore: added missing index file
petertonysmith94 Sep 3, 2024
a618c89
scaffold cleanup
nedsalk Sep 3, 2024
d74786a
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into np…
petertonysmith94 Sep 4, 2024
8e58b23
chore: added basic test infrastructure (#3090)
petertonysmith94 Sep 4, 2024
a5eeaef
Merge branch 'master' into np/feat/abi-refactor
petertonysmith94 Sep 10, 2024
ed8e598
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Sep 12, 2024
882a0a2
feat: sway type matchers (#3137)
nedsalk Sep 12, 2024
c3a9b6f
chore: added start of line to ABI regex matchers (#3163)
petertonysmith94 Sep 13, 2024
09cddf0
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into np…
petertonysmith94 Sep 16, 2024
8eb9316
chore: consolidate all ABI Sway programs (#3171)
petertonysmith94 Sep 16, 2024
8235b4e
chore: separate minor and major versions (#3170)
petertonysmith94 Sep 16, 2024
5e55da8
chore: inform users if their `fuels` version is outdated (#3108)
Dhaiwat10 Sep 16, 2024
7096791
fix: some assets got assetId, contractId and decimals incorrect (#3167)
LuizAsFight Sep 16, 2024
dc0db20
build(deps): bump the deps-patch group (#3173)
dependabot[bot] Sep 16, 2024
e6b6af9
fix: add missing receipts properties (#3156)
Torres-ssf Sep 16, 2024
7e3b66c
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Sep 17, 2024
bd79318
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Sep 25, 2024
540d4e2
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Sep 28, 2024
3659e50
update proxy contract
nedsalk Oct 3, 2024
976c1bd
feat: add missing matcher (#3247)
nedsalk Oct 6, 2024
3605a1c
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Oct 11, 2024
052e7d7
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Oct 15, 2024
43e6c6e
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Oct 23, 2024
0e5e9cb
Merge branch 'master' into np/feat/abi-refactor
nedsalk Oct 24, 2024
691f3c0
Merge branch 'master' into np/feat/abi-refactor
petertonysmith94 Nov 18, 2024
bd647b3
Merge branch 'master' of github.com:FuelLabs/fuels-ts into np/feat/ab…
petertonysmith94 Nov 25, 2024
7611c55
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Nov 27, 2024
97c03f4
chore: added errors for matchers
petertonysmith94 Nov 27, 2024
ee79c21
chore: added matcher to spell check
petertonysmith94 Nov 27, 2024
c5bfa0a
chore: added errors dep
petertonysmith94 Nov 27, 2024
fb5e04d
Merge branch 'master' of github.com:FuelLabs/fuels-ts into np/feat/ab…
petertonysmith94 Nov 29, 2024
0d33661
Merge branch 'master' of github.com:FuelLabs/fuels-ts into np/feat/ab…
petertonysmith94 Dec 2, 2024
8ce4810
Merge branch 'master' of github.com:FuelLabs/fuels-ts into np/feat/ab…
petertonysmith94 Dec 3, 2024
57014d5
Merge branch 'master' of github.com:FuelLabs/fuels-ts into np/feat/ab…
petertonysmith94 Dec 9, 2024
7ca0689
Merge branch 'master' of github.com:FuelLabs/fuels-ts into np/feat/ab…
petertonysmith94 Dec 11, 2024
ed95764
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Dec 12, 2024
bff8061
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Dec 12, 2024
1390116
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Dec 16, 2024
74ad20c
Merge branch 'master' into np/feat/abi-refactor
nedsalk Dec 31, 2024
31ddb12
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Jan 2, 2025
508991b
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Jan 8, 2025
5bd8378
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Jan 8, 2025
86b7109
Merge remote-tracking branch 'origin/master' into np/feat/abi-refactor
nedsalk Jan 13, 2025
9a86b31
remove unused dependency
nedsalk Jan 13, 2025
21ac698
fix lock file
nedsalk Jan 14, 2025
6962abb
Merge branch 'master' of github.com:FuelLabs/fuels-ts into np/feat/ab…
petertonysmith94 Jan 22, 2025
44f6590
feat: ABI parser (#3089)
nedsalk Jan 22, 2025
9ec45f9
feat!: ABI Gen (#3249)
petertonysmith94 Jan 22, 2025
9266d3d
feat: ABI coder (#3402)
petertonysmith94 Jan 23, 2025
6c51751
Merge branch 'master' of github.com:FuelLabs/fuels-ts into np/feat/ab…
petertonysmith94 Jan 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions .changeset/flat-roses-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

feat: ABI refactor
Copy link
Contributor Author

@petertonysmith94 petertonysmith94 Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

This is not to be merged until we've concluded development

2 changes: 2 additions & 0 deletions .changeset/nice-books-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
6 changes: 6 additions & 0 deletions .changeset/poor-years-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/abi": minor
"fuels": minor
---

feat!: ABI Gen
7 changes: 7 additions & 0 deletions .changeset/tender-tigers-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fuel-ts/abi": patch
"fuels": patch
"@fuel-ts/errors": patch
---

feat: ABI parser
6 changes: 6 additions & 0 deletions .github/actions/test-setup/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ runs:
with:
bun-version: ${{ inputs.bun-version }}

- name: Set forc and fuel-core paths
shell: bash
run: |
echo "$GITHUB_WORKSPACE/internal/forc/forc-binaries" >> $GITHUB_PATH
echo "$GITHUB_WORKSPACE/internal/fuel-core/fuel-core-binaries" >> $GITHUB_PATH

- name: Build
run: pnpm build
shell: bash
1 change: 1 addition & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jobs:
PUBLISHED_NPM_TAG: next

e2e:
if: false
runs-on: ubuntu-latest
timeout-minutes: 25
needs: [environments]
Expand Down
2 changes: 1 addition & 1 deletion .knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"fuels",
"bun",
"@types/rimraf",
"@fuel-ts/abi-typegen",
"@fuel-ts/abi",
"@internal/fuel-core",
"get-graphql-schema",
"events",
Expand Down
4 changes: 2 additions & 2 deletions apps/demo-typegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"forc:predicate": "pnpm fuels-forc build -p demo-predicate --release",
"build:types": "run-p types:*",
"types:contract": "pnpm fuels typegen -i demo-contract/out/release/demo-contract-abi.json -o src/contract-types",
"types:script": "pnpm fuels typegen -i demo-script/out/release/demo-script-abi.json -o src/script-types --script",
"types:predicate": "pnpm fuels typegen -i demo-predicate/out/release/demo-predicate-abi.json -o src/predicate-types --predicate"
"types:script": "pnpm fuels typegen -i demo-script/out/release/demo-script-abi.json -o src/script-types",
"types:predicate": "pnpm fuels typegen -i demo-predicate/out/release/demo-predicate-abi.json -o src/predicate-types"
},
"license": "Apache-2.0",
"dependencies": {
Expand Down
6 changes: 2 additions & 4 deletions apps/demo-typegen/src/demo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
import { toHex, Address, Wallet, FuelError, ErrorCode } from 'fuels';
import { expectToThrowFuelError, launchTestNode } from 'fuels/test-utils';

import storageSlots from '../demo-contract/out/release/demo-contract-storage_slots.json';

import { DemoContract, DemoContractFactory } from './contract-types';
import { DemoPredicate } from './predicate-types';
import type { DemoPredicateInputs } from './predicate-types/DemoPredicate';
import type { DemoPredicateInputs } from './predicate-types/predicates/DemoPredicate';
import { DemoScript } from './script-types';

/**
Expand All @@ -25,7 +23,7 @@ describe('ExampleContract', () => {
// #context import { DemoContractFactory } from './sway-programs-api';

const { waitForResult } = await DemoContractFactory.deploy(wallet, {
storageSlots,
storageSlots: DemoContractFactory.storageSlots,
});

const { contract } = await waitForResult();
Expand Down
3 changes: 3 additions & 0 deletions apps/docs-api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

# Modules

<!-- TODO: uncomment once deployed -->
<!-- - [abi](https://fuels-ts-docs-api.vercel.app/modules/_fuel_ts_abi.html) -->

- [abi-coder](https://fuels-ts-docs-api.vercel.app/modules/_fuel_ts_abi_coder.html)
- [abi-typegen](https://fuels-ts-docs-api.vercel.app/modules/_fuel_ts_abi_typegen.html)
- [account](https://fuels-ts-docs-api.vercel.app/modules/_fuel_ts_account.html)
Expand Down
1 change: 1 addition & 0 deletions apps/docs-api/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://typedoc.org/schema.json",
"entryPointStrategy": "packages",
"entryPoints": [
"../../packages/abi",
"../../packages/abi-coder",
"../../packages/abi-typegen",
"../../packages/address",
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 @@ -441,6 +441,10 @@ export default defineConfig({
text: 'Optimized React Example',
link: '/guide/cookbook/optimized-react-example',
},
{
text: 'Working with the ABI',
link: '/guide/cookbook/working-with-the-abi',
},
],
},
{
Expand Down
3 changes: 2 additions & 1 deletion apps/docs/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,5 @@ Workspaces
WSL
XOR
XORs
YAML
YAML
matcher
9 changes: 2 additions & 7 deletions apps/docs/src/guide/contracts/snippets/proxy-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// #region proxy-2
import {
Provider,
Wallet,
Src14OwnedProxy,
Src14OwnedProxyFactory,
} from 'fuels';
import { Provider, Wallet, Src14OwnedProxyFactory } from 'fuels';

import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../env';
import {
Expand All @@ -28,7 +23,7 @@ const { contract: counterContract } = await deploy.waitForResult();
* initialize the storage slots.
*/
const storageSlots = counterContractFactory.storageSlots.concat(
Src14OwnedProxy.storageSlots
Src14OwnedProxyFactory.storageSlots
);
/**
* These configurables are specific to our recommended SRC14 compliant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
import { Provider, Wallet } from 'fuels';

import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../../env';
import {
StorageTestContract,
StorageTestContractFactory,
} from '../../../../typegend';
import { StorageTestContractFactory } from '../../../../typegend';

const provider = new Provider(LOCAL_NETWORK_URL);
const deployer = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);

const deploymentTx = await StorageTestContractFactory.deploy(deployer, {
storageSlots: StorageTestContract.storageSlots,
storageSlots: StorageTestContractFactory.storageSlots,
});

await deploymentTx.waitForResult();
Expand Down
9 changes: 9 additions & 0 deletions apps/docs/src/guide/cookbook/snippets/parsing-the-abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// #region full
import { AbiParser } from 'fuels';
import type { Abi, AbiSpecificationV1 } from 'fuels';

import { Counter } from '../../../typegend';

const parsedAbi: Abi = AbiParser.parse(Counter.abi as AbiSpecificationV1);
// #endregion full
console.log('Parsed ABI:', parsedAbi);
11 changes: 11 additions & 0 deletions apps/docs/src/guide/cookbook/working-with-the-abi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Working with the ABI

Building a Sway program with `forc build` outputs multiple files, one of which is a JSON representation of the program's ABI. Because ABI specifications can change from one `forc` version to another, working directly with the ABI is cumbersome due to having to manage all ABI specification versions in order to ensure proper functionality.

<!-- TODO: fix links once it's live -->
<!-- AbiParser: https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_abi.AbiParser.html-->
<!-- ABI: https://fuels-ts-docs-api.vercel.app/interfaces/_fuel_ts_abi.Abi.html -->

To mitigate this, The SDK provides [`AbiParser`](#working-with-the-abi) which can parse all ABI specification versions and output an object that conforms to the [`Abi`](#working-with-the-abi) interface. The SDK also internally uses this `Abi` interface for implementing its encoding/decoding and TS type generation.

<<< @./snippets/parsing-the-abi.ts#full{ts:line-numbers}
12 changes: 6 additions & 6 deletions apps/docs/src/guide/encoding/encode-and-decode.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Encode and Decode

To interact with the FuelVM, types must be encoded and decoded per the [argument encoding specification](https://docs.fuel.network/docs/specs/abi/argument-encoding/). The SDK provides the `Interface` class to encode and decode data.
To interact with the FuelVM, types must be encoded and decoded per the [argument encoding specification](https://docs.fuel.network/docs/specs/abi/argument-encoding/). The SDK provides the `AbiCoder` class to encode and decode data.

The relevant methods of `Interface` are:
To encode and decode types, the `AbiCoder` class provides the `getType` method which returns a `AbiCoderType` instance that provides the following methods:

- `encodeType`
- `decodeType`
- `encode`
- `decode`

The `Interface` class requires you to pass the [ABI](https://docs.fuel.network/docs/specs/abi/json-abi-format/) on initialization. Both methods accept a `concreteTypeId`, which must exist in the ABI's `concreteTypes` array. After that, a suitable coder will be assigned to encode/decode that type.
The `AbiCoder` class requires you to pass the [ABI](https://docs.fuel.network/docs/specs/abi/json-abi-format/) on initialization. Both methods accept a `concreteTypeId`, which must exist in the ABI's `concreteTypes` array. After that, a suitable coder will be assigned to encode/decode that type.

Imagine we are working with the following script that returns the sum of two `u32` integers:

Expand All @@ -23,7 +23,7 @@ It will produce the following ABI:

<<< @./snippets/encode-and-decode.jsonc#encode-and-decode-2{json:line-numbers}

Now, let's prepare some data to pass to the `main` function to retrieve the combined integer. The function expects and returns a `u32` integer. So here, we will encode the `u32` to pass it to the function and receive the same `u32` back, as bytes, that we'll use for decoding. We can do both of these with the `Interface`.
Now, let's prepare some data to pass to the `main` function to retrieve the combined integer. The function expects and returns a `u32` integer. So here, we will encode the `u32` to pass it to the function and receive the same `u32` back, as bytes, that we'll use for decoding. We can do both of these with the `AbiCoder`.

First, let's prepare the transaction:

Expand Down
26 changes: 16 additions & 10 deletions apps/docs/src/guide/encoding/snippets/encode-and-decode.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// #region full
import type { JsonAbi, TransactionResultReturnDataReceipt } from 'fuels';
import type {
AbiSpecification,
TransactionResultReturnDataReceipt,
} from 'fuels';
import {
buildFunctionResult,
ReceiptType,
arrayify,
Script,
Interface,
AbiCoder,
Provider,
Wallet,
} from 'fuels';
Expand All @@ -20,7 +23,7 @@ const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);

// First we need to build out the transaction via the script that we want to encode.
// For that we'll need the ABI and the bytecode of the script
const abi: JsonAbi = ScriptSum.abi;
const abi: AbiSpecification = ScriptSum.abi;
const bytecode = ScriptSum.bytecode;

// Create the invocation scope for the script call, passing the initial
Expand All @@ -43,13 +46,13 @@ const argument = abi.functions
.find((f) => f.name === 'main')
?.inputs.find((i) => i.name === 'inputted_amount')?.concreteTypeId as string;

// The `Interface` class (imported from `fuels`) is the entry point for encoding and decoding all things abi-related.
// We will use its `encodeType` method and create the encoding required for
// a u32 which takes 4 bytes up of property space.
// The `AbiCoder` class (imported from `fuels`) is the entry point for encoding and decoding all things abi-related.
// We will use its `getType` method to get the coder for the argument and then call its `encode` method to
// create the encoding required for a u32 which takes 4 bytes up of property space.

const abiInterface = new Interface(abi);
const abiCoder = AbiCoder.fromAbi(abi);
const argumentToAdd = 10;
const encodedArguments = abiInterface.encodeType(argument, [argumentToAdd]);
const encodedArguments = abiCoder.getType(argument).encode([argumentToAdd]);
// Therefore the value of 10 will be encoded to:
// Uint8Array([0, 0, 0, 10]

Expand Down Expand Up @@ -91,10 +94,13 @@ const returnData = arrayify(returnDataReceipt.data);
// returnData = new Uint8Array([0, 0, 0, 20]

// And now we can decode the returned bytes in a similar fashion to how they were
// encoded, via the `Interface`
const [decodedReturnData] = abiInterface.decodeType(argument, returnData);
// encoded, via the `AbiCoder`
const decodedReturnData = abiCoder.getType(argument).decode(returnData);
// 20

const totalValue = argumentToAdd + initialValue;
// #endregion encode-and-decode-5
// #endregion full

console.log('decodedReturnData should be 20', decodedReturnData === 20);
console.log('totalValue should be 20', totalValue === 20);
Loading
Loading