Skip to content

Commit 4c653d0

Browse files
arboleyapetertonysmith94nedsalkdanielbate
authored
feat!: adding abi transpiler (#2856)
* Upgrading forc to temp branch * Adding ABI transpiler and integrating it with related areas * Updating typegen fixtures * Fixing broken typegen tests; updating outdated IDs * Re-instating the order * Loading forc projects the standard and proper way * Updating coder’s types, regexes, and fixing type matching everywhere * Updating test using callpath-based naming for Option * Fixing test, intercepting ABI loading so it can be transpiled * Adding changeset * Adjusting changeset * Excluding sway-repo source files in `forc:check` script * Ensuring branches are up to date before building * Building internal packages in `--release` mode * Fixing directory path for new build target * Merging duplicated variables, standardizing terminologies * Improving validation to handle strings / non-objects as well * fix: option coder decoding incorrect value (#2870) * Revert "Improving validation to handle strings / non-objects as well" This reverts commit 45f3ef2. * chore: fixing types on OptionCoder * fix: match full qualifying type name with regex * chore: added test groups Co-authored-by: Daniel Bate <[email protected]> * chore: revert StructCoder name change --------- Co-authored-by: Daniel Bate <[email protected]> * Standardizing timeout limits across workflows * Ignoring `sway-repo`in links check when building things from source * Fixing ignore path * Stop deleting `concreteTypeId`, used in public API * Update .changeset/odd-horses-cheer.md * Ignoring also `fuel-core-repo` for the same reasons as `sway-repo` * Update .changeset/odd-horses-cheer.md * Updating more fixtures * Updating snippet * Fixing random lint warning * Incrasing test timeout limit * Fixing ignored paths * chore!: internalize old `JsonAbi` format (#2862) * use `transpileAbi` in `Interface` * add new interfaces to typegen * no need for `debug` build anymore * fix `getTypegenForcProject` return type * added `as JsonAbi` type assertions to fix type errors * fix typegen tests * rename variable * stop exporting `JsonAbi` from typegen * revert changes that should be in another PR * contttinue exporting * rename to match `master` * fix encode-and-decode test * Renamed `JsonAbiNew` to `JsonAbi` and the old format to `JsonAbiOld` * fix compilation errors * Stop exporting `AbiCoder` and update `encode-and-decode` example * Removed `Interface.findTypeById` method * stop casting `as unknown as JsonAbi` * use `argument` directly * fix import * fix: `Interface.encodeType/decodeType` * Update packages/abi-coder/src/types/JsonAbi.ts Co-authored-by: Peter Smith <[email protected]> * Update packages/abi-coder/src/types/JsonAbiNew.ts Co-authored-by: Peter Smith <[email protected]> * Update packages/abi-typegen/src/types/interfaces/JsonAbi.ts Co-authored-by: Peter Smith <[email protected]> * Update packages/account/test/fuel-wallet-connector.test.ts Co-authored-by: Peter Smith <[email protected]> * Update packages/abi-typegen/src/utils/transpile-abi.ts Co-authored-by: Peter Smith <[email protected]> * copy/paste transpile-abi * fix compilation error * eslint disable todo type * use `{ transpile: T }` params object * update `encode-and-decode.md` docs * fix: spellcheck * Update apps/docs/src/guide/encoding/encode-and-decode.md Co-authored-by: Anderson Arboleya <[email protected]> * Update apps/docs/src/guide/encoding/encode-and-decode.md Co-authored-by: Anderson Arboleya <[email protected]> * Update apps/docs/src/guide/encoding/encode-and-decode.md Co-authored-by: Anderson Arboleya <[email protected]> --------- Co-authored-by: Anderson Arboleya <[email protected]> Co-authored-by: Peter Smith <[email protected]> * Getting around non-null assertions * Adjusting changeset * Adjusting scripts * Updating template fixtures/snapshots * Triggering CI * The branch is gone - temporarily switching to `master` * Temporarily patching sway std lib * Temporarily skipping problematic sway projects and related tests * Lintfix - deprecations, types mismatch, and broken imports * DRYing test setup/teardown * Fixing broken tests * Patching base library individually * Unskipping problematic workspace members * Adjusting predicate data handling * Undoing undesired changed * Adjuting more projects * Revert "Temporarily skipping problematic sway projects and related tests" This reverts commit dc054ff. * Formatting * Revert "Temporarily patching sway std lib" This reverts commit 48b0ab3. * Revert "Patching base library individually" This reverts commit b97c944. * Replacing sway `branch` by `0.63.0` version * Update .changeset/odd-horses-cheer.md Co-authored-by: Peter Smith <[email protected]> * Updating forc version --------- Co-authored-by: Peter Smith <[email protected]> Co-authored-by: Nedim Salkić <[email protected]> Co-authored-by: Daniel Bate <[email protected]>
1 parent f6b12bd commit 4c653d0

File tree

66 files changed

+1764
-1610
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1764
-1610
lines changed

.changeset/odd-horses-cheer.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@fuel-ts/abi-coder": minor
3+
"@fuel-ts/abi-typegen": minor
4+
"@fuel-ts/versions": minor
5+
"@internal/forc": minor
6+
"create-fuels": minor
7+
---
8+
9+
feat!: adding `abi` transpiler

apps/docs-snippets/src/guide/encoding/encode-and-decode.test.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { AbiCoder, Script, ReceiptType, arrayify, buildFunctionResult } from 'fuels';
2-
import type { JsonAbi, JsonAbiArgument, TransactionResultReturnDataReceipt } from 'fuels';
1+
import type { JsonAbi, TransactionResultReturnDataReceipt } from 'fuels';
2+
import { buildFunctionResult, ReceiptType, arrayify, Script, Interface } from 'fuels';
33
import { launchTestNode } from 'fuels/test-utils';
44

5+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
6+
// @ts-ignore JSONC extension requires mapping but behaves fine
57
import abiSnippet from '../../../test/fixtures/abi/encode-and-decode.jsonc';
68
import { SumScript as factory } from '../../../test/typegen/scripts/SumScript';
79

@@ -27,7 +29,7 @@ describe('encode and decode', () => {
2729
// First we need to build out the transaction via the script that we want to encode.
2830
// For that we'll need the ABI and the bytecode of the script
2931
const abi: JsonAbi = factory.abi;
30-
const bytecode: string = factory.bytecode;
32+
const bytecode = factory.bytecode;
3133

3234
// Create the invocation scope for the script call, passing the initial
3335
// value for the configurable constant
@@ -42,18 +44,21 @@ describe('encode and decode', () => {
4244
// #endregion encode-and-decode-3
4345

4446
// #region encode-and-decode-4
45-
// #import { JsonAbiArgument, AbiCoder};
47+
// #import { Interface };
4648

4749
// Now we can encode the argument we want to pass to the function. The argument is required
48-
// as a function parameter for all `AbiCoder` functions and we can extract it from the ABI itself
49-
const argument: JsonAbiArgument = abi.functions
50+
// as a function parameter for all abi functions and we can extract it from the ABI itself
51+
const argument = abi.functions
5052
.find((f) => f.name === 'main')
51-
?.inputs.find((i) => i.name === 'inputted_amount') as JsonAbiArgument;
53+
?.inputs.find((i) => i.name === 'inputted_amount')?.concreteTypeId as string;
5254

53-
// Using the `AbiCoder`'s `encode` method, we can now create the encoding required for
54-
// a u32 which takes 4 bytes up of property space
55+
// The `Interface` class is the entry point for encoding and decoding all things abi-related.
56+
// We will use its `encodeType` method and create the encoding required for
57+
// a u32 which takes 4 bytes up of property space.
58+
59+
const abiInterface = new Interface(abi);
5560
const argumentToAdd = 10;
56-
const encodedArguments = AbiCoder.encode(abi, argument, [argumentToAdd]);
61+
const encodedArguments = abiInterface.encodeType(argument, [argumentToAdd]);
5762
// Therefore the value of 10 will be encoded to:
5863
// Uint8Array([0, 0, 0, 10]
5964

@@ -72,7 +77,7 @@ describe('encode and decode', () => {
7277
// #endregion encode-and-decode-4
7378

7479
// #region encode-and-decode-5
75-
// #import { AbiCoder, ReceiptType, TransactionResultReturnDataReceipt, arrayify, buildFunctionResult };
80+
// #import { ReceiptType, TransactionResultReturnDataReceipt, arrayify, buildFunctionResult };
7681

7782
// Get result of the transaction, including the contract call result. For this we'll need
7883
// the previously created invocation scope, the transaction response and the script
@@ -99,8 +104,8 @@ describe('encode and decode', () => {
99104
// returnData = new Uint8Array([0, 0, 0, 20]
100105

101106
// And now we can decode the returned bytes in a similar fashion to how they were
102-
// encoded, via the `AbiCoder`
103-
const [decodedReturnData] = AbiCoder.decode(abi, argument, returnData, 0);
107+
// encoded, via the `Interface`
108+
const [decodedReturnData] = abiInterface.decodeType(argument, returnData);
104109
// 20
105110
// #endregion encode-and-decode-5
106111

apps/docs-snippets/src/guide/transactions/transaction-request.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ describe('Transaction Request', () => {
210210
// #endregion transaction-request-7
211211

212212
expect(transactionId).toBe(
213-
'0x5e12f588de0cbf2ec0f085078880d5eeb3e18cd239a288d4a06ee4247a97e4f2'
213+
'0xf62e682e05fa17455999758d00875449bd6f06cdbe003a6431aa944f2d652642'
214214
);
215215
});
216216
});
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
// #region encode-and-decode-2
22
{
3-
"encoding": "1",
4-
"types": [
3+
"programType": "script",
4+
"specVersion": "1",
5+
"encodingVersion": "1",
6+
"concreteTypes": [
57
{
6-
"typeId": 0,
78
"type": "u32",
8-
"components": null,
9-
"typeParameters": null,
9+
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
1010
},
1111
],
12+
"metadataTypes": [],
1213
"functions": [
1314
{
1415
"inputs": [
1516
{
1617
"name": "inputted_amount",
17-
"type": 0,
18-
"typeArguments": null,
18+
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
1919
},
2020
],
2121
"name": "main",
22-
"output": {
23-
"name": "",
24-
"type": 0,
25-
"typeArguments": null,
26-
},
22+
"output": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
2723
"attributes": null,
2824
},
2925
],
@@ -32,14 +28,9 @@
3228
"configurables": [
3329
{
3430
"name": "AMOUNT",
35-
"configurableType": {
36-
"name": "",
37-
"type": 0,
38-
"typeArguments": null,
39-
},
40-
"offset": 856,
31+
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
32+
"offset": 920,
4133
},
4234
],
4335
}
44-
4536
// #endregion encode-and-decode-2

apps/docs-snippets/test/fixtures/forc-projects/predicate-signing/src/main.sw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ predicate;
44
use std::{b512::B512, ecr::ec_recover_address, tx::{tx_id, tx_witness_data}};
55

66
fn main(signer: b256) -> bool {
7-
let witness_data: B512 = tx_witness_data(1);
7+
let witness_data: B512 = tx_witness_data(1).unwrap();
88
let address: b256 = ec_recover_address(witness_data, tx_id()).unwrap().bits();
99
return address == signer;
1010
}

apps/docs-snippets/test/fixtures/forc-projects/script-signing/src/main.sw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ script;
44
use std::{b512::B512, ecr::ec_recover_address, tx::{tx_id, tx_witness_data}};
55

66
fn main(signer: b256) -> bool {
7-
let witness_data: B512 = tx_witness_data(1);
7+
let witness_data: B512 = tx_witness_data(1).unwrap();
88
let address: b256 = ec_recover_address(witness_data, tx_id()).unwrap().bits();
99
return address == signer;
1010
}

apps/docs/spell-check-custom-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
ABI
22
ABIs
3+
ABI's
34
ASM
45
AssetId
56
IDE

apps/docs/src/guide/encoding/encode-and-decode.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
# Encode and Decode
22

3-
In order to interact with the FuelVM, types must be encoded and decoded as 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.
3+
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.
44

5-
It has three static methods:
5+
The relevant methods of `Interface` are:
66

7-
- `encode`
8-
- `decode`
9-
- `getCoder`
7+
- `encodeType`
8+
- `decodeType`
109

11-
The methods `encode` and `decode` describe the aforementioned process, while `getCoder` returns an instance of the internal coder required to serialize the passed type. This coder is then used internally by the `encode` and `decode` methods.
12-
13-
All methods expect you to pass the [ABI](https://docs.fuel.network/docs/specs/abi/json-abi-format/) and ABI Argument as function parameters to deduce the specific type coders that will be required to parse the data.
10+
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.
1411

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

@@ -26,7 +23,7 @@ It will produce the following ABI:
2623

2724
<<< @/../../docs-snippets/test/fixtures/abi/encode-and-decode.jsonc#encode-and-decode-2{json:line-numbers}
2825

29-
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`.
26+
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`.
3027

3128
First, let's prepare the transaction:
3229

internal/forc/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.62.0
1+
0.63.1

internal/forc/lib/shared.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,27 @@ const swayRepoUrl = 'https://github.com/fuellabs/sway.git';
5252

5353
export const buildFromGitBranch = (branchName) => {
5454
const swayRepoDir = join(__dirname, '..', 'sway-repo');
55-
const swayRepoDebugDir = join(swayRepoDir, 'target', 'debug');
55+
const swayRepoReleaseDir = join(swayRepoDir, 'target', 'release');
5656
const stdioOpts = { stdio: 'inherit' };
5757

5858
if (existsSync(swayRepoDir)) {
59-
execSync(`cd ${swayRepoDir} && git fetch origin && git checkout ${branchName}`, stdioOpts);
60-
execSync(`cd ${swayRepoDir} && cargo build`, stdioOpts);
59+
execSync(
60+
[
61+
`cd ${swayRepoDir}`,
62+
`git fetch origin`,
63+
`git checkout ${branchName}`,
64+
`git pull origin ${branchName}`,
65+
].join('&&'),
66+
stdioOpts
67+
);
68+
69+
execSync(`cd ${swayRepoDir} && cargo build --release`, stdioOpts);
6170
} else {
6271
execSync(`git clone --branch ${branchName} ${swayRepoUrl} ${swayRepoDir}`, stdioOpts);
63-
execSync(`cd ${swayRepoDir} && cargo build`, stdioOpts);
72+
execSync(`cd ${swayRepoDir} && cargo build --release`, stdioOpts);
6473
}
6574

66-
const [from, to] = [swayRepoDebugDir, forcBinDirPath];
75+
const [from, to] = [swayRepoReleaseDir, forcBinDirPath];
6776

6877
rmSync(to, { recursive: true, force: true });
6978
mkdirSync(to, { recursive: true });

0 commit comments

Comments
 (0)