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!: adding abi transpiler #2856

Merged
merged 80 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
7585782
Upgrading forc to temp branch
arboleya Jul 29, 2024
bb3a168
Adding ABI transpiler and integrating it with related areas
arboleya Jul 29, 2024
d08b334
Updating typegen fixtures
arboleya Jul 29, 2024
81ec4cc
Fixing broken typegen tests; updating outdated IDs
arboleya Jul 29, 2024
3caff6d
Re-instating the order
arboleya Jul 29, 2024
5816eaf
Loading forc projects the standard and proper way
arboleya Jul 29, 2024
f04bc3f
Updating coder’s types, regexes, and fixing type matching everywhere
arboleya Jul 29, 2024
a34f676
Updating test using callpath-based naming for Option
arboleya Jul 29, 2024
bf760a2
Fixing test, intercepting ABI loading so it can be transpiled
arboleya Jul 29, 2024
27a1e61
Adding changeset
arboleya Jul 29, 2024
05c45be
Adjusting changeset
arboleya Jul 29, 2024
2a00ce3
Excluding sway-repo source files in `forc:check` script
arboleya Jul 29, 2024
ddb3cfe
Ensuring branches are up to date before building
arboleya Jul 29, 2024
bdcf220
Building internal packages in `--release` mode
arboleya Jul 29, 2024
045adc8
Fixing directory path for new build target
arboleya Jul 29, 2024
7de6c06
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Jul 30, 2024
3b62ef4
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Jul 30, 2024
0b7d477
Merge branch 'master' into aa/feat/adding-abi-transpiler
petertonysmith94 Jul 30, 2024
45929ee
Merge branch 'master' into aa/feat/adding-abi-transpiler
petertonysmith94 Jul 30, 2024
188a5e2
Merge branch 'master' into aa/feat/adding-abi-transpiler
nedsalk Jul 30, 2024
fcfe0df
Merge branch 'master' into aa/feat/adding-abi-transpiler
nedsalk Jul 30, 2024
38973b1
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Jul 31, 2024
8198c53
Merging duplicated variables, standardizing terminologies
arboleya Jul 31, 2024
45f3ef2
Improving validation to handle strings / non-objects as well
arboleya Jul 31, 2024
fed8d3a
fix: option coder decoding incorrect value (#2870)
petertonysmith94 Jul 31, 2024
a42e859
Standardizing timeout limits across workflows
arboleya Jul 31, 2024
bc70ce0
Ignoring `sway-repo`in links check when building things from source
arboleya Jul 31, 2024
b4ee165
Fixing ignore path
arboleya Jul 31, 2024
7b4ce7b
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Jul 31, 2024
e0417bf
Stop deleting `concreteTypeId`, used in public API
arboleya Jul 31, 2024
d5ae163
Merge branch 'aa/feat/adding-abi-transpiler' of github.com:FuelLabs/f…
arboleya Jul 31, 2024
8d1795c
Update .changeset/odd-horses-cheer.md
arboleya Jul 31, 2024
a175a6f
Ignoring also `fuel-core-repo` for the same reasons as `sway-repo`
arboleya Jul 31, 2024
5626954
Update .changeset/odd-horses-cheer.md
arboleya Jul 31, 2024
67013cb
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 1, 2024
306738f
Updating more fixtures
arboleya Aug 1, 2024
d9f9d30
Updating snippet
arboleya Aug 1, 2024
00e2b8e
Fixing random lint warning
arboleya Aug 1, 2024
a203ff6
Incrasing test timeout limit
arboleya Aug 1, 2024
e725b1d
Fixing ignored paths
arboleya Aug 1, 2024
942e5cc
chore!: internalize old `JsonAbi` format (#2862)
nedsalk Aug 5, 2024
5621ccf
Getting around non-null assertions
arboleya Aug 5, 2024
98a0c1a
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 5, 2024
1e4996f
Adjusting changeset
arboleya Aug 5, 2024
265ee88
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 6, 2024
4241143
Adjusting scripts
arboleya Aug 6, 2024
47f54f2
Updating template fixtures/snapshots
arboleya Aug 6, 2024
84d50b3
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 6, 2024
e81dfde
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 11, 2024
d2bb381
Triggering CI
arboleya Aug 11, 2024
0877d5e
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 13, 2024
2c9d14f
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 13, 2024
d4962de
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 13, 2024
a1efe88
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 13, 2024
d8f7d5c
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 14, 2024
ac8f02d
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 14, 2024
14c8505
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 15, 2024
85b20ad
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 15, 2024
6b75b30
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 15, 2024
29d6ba3
The branch is gone - temporarily switching to `master`
arboleya Aug 16, 2024
48b0ab3
Temporarily patching sway std lib
arboleya Aug 16, 2024
dc054ff
Temporarily skipping problematic sway projects and related tests
arboleya Aug 16, 2024
76b9a9c
Lintfix - deprecations, types mismatch, and broken imports
arboleya Aug 16, 2024
d7a5100
DRYing test setup/teardown
arboleya Aug 16, 2024
e239137
Fixing broken tests
arboleya Aug 16, 2024
23e3e7e
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 16, 2024
b97c944
Patching base library individually
arboleya Aug 17, 2024
b741c93
Unskipping problematic workspace members
arboleya Aug 17, 2024
f035e4c
Adjusting predicate data handling
arboleya Aug 17, 2024
b8451a3
Undoing undesired changed
arboleya Aug 17, 2024
b36d444
Adjuting more projects
arboleya Aug 17, 2024
d233464
Revert "Temporarily skipping problematic sway projects and related te…
arboleya Aug 17, 2024
5ae0e32
Formatting
arboleya Aug 17, 2024
edc3f34
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 19, 2024
84954af
Revert "Temporarily patching sway std lib"
arboleya Aug 19, 2024
2d88c07
Revert "Patching base library individually"
arboleya Aug 19, 2024
bd174ca
Replacing sway `branch` by `0.63.0` version
arboleya Aug 19, 2024
28734a9
Update .changeset/odd-horses-cheer.md
arboleya Aug 19, 2024
7ef4e88
Merge branch 'master' into aa/feat/adding-abi-transpiler
arboleya Aug 19, 2024
f019cba
Updating forc version
arboleya Aug 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/odd-horses-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@fuel-ts/abi-coder": minor
"@fuel-ts/abi-typegen": minor
"@fuel-ts/versions": minor
"@internal/forc": minor
"create-fuels": minor
---
arboleya marked this conversation as resolved.
Show resolved Hide resolved

feat!: adding `abi` transpiler
31 changes: 18 additions & 13 deletions apps/docs-snippets/src/guide/encoding/encode-and-decode.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { AbiCoder, Script, ReceiptType, arrayify, buildFunctionResult } from 'fuels';
import type { JsonAbi, JsonAbiArgument, TransactionResultReturnDataReceipt } from 'fuels';
import type { JsonAbi, TransactionResultReturnDataReceipt } from 'fuels';
import { buildFunctionResult, ReceiptType, arrayify, Script, Interface } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

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

Expand All @@ -27,7 +29,7 @@ describe('encode and decode', () => {
// 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 = factory.abi;
const bytecode: string = factory.bytecode;
const bytecode = factory.bytecode;

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

// #region encode-and-decode-4
// #import { JsonAbiArgument, AbiCoder};
// #import { Interface };

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

// Using the `AbiCoder`'s `encode` method, we can now create the encoding required for
// a u32 which takes 4 bytes up of property space
// The `Interface` class 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.

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

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

// #region encode-and-decode-5
// #import { AbiCoder, ReceiptType, TransactionResultReturnDataReceipt, arrayify, buildFunctionResult };
// #import { ReceiptType, TransactionResultReturnDataReceipt, arrayify, buildFunctionResult };

// Get result of the transaction, including the contract call result. For this we'll need
// the previously created invocation scope, the transaction response and the script
Expand All @@ -99,8 +104,8 @@ describe('encode and decode', () => {
// 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 `AbiCoder`
const [decodedReturnData] = AbiCoder.decode(abi, argument, returnData, 0);
// encoded, via the `Interface`
const [decodedReturnData] = abiInterface.decodeType(argument, returnData);
// 20
// #endregion encode-and-decode-5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('Transaction Request', () => {
// #endregion transaction-request-7

expect(transactionId).toBe(
'0x5e12f588de0cbf2ec0f085078880d5eeb3e18cd239a288d4a06ee4247a97e4f2'
'0xf62e682e05fa17455999758d00875449bd6f06cdbe003a6431aa944f2d652642'
);
});
});
29 changes: 10 additions & 19 deletions apps/docs-snippets/test/fixtures/abi/encode-and-decode.jsonc
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
// #region encode-and-decode-2
{
"encoding": "1",
"types": [
"programType": "script",
"specVersion": "1",
"encodingVersion": "1",
"concreteTypes": [
{
"typeId": 0,
"type": "u32",
"components": null,
"typeParameters": null,
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
},
],
"metadataTypes": [],
"functions": [
{
"inputs": [
{
"name": "inputted_amount",
"type": 0,
"typeArguments": null,
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
},
],
"name": "main",
"output": {
"name": "",
"type": 0,
"typeArguments": null,
},
"output": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"attributes": null,
},
],
Expand All @@ -32,14 +28,9 @@
"configurables": [
{
"name": "AMOUNT",
"configurableType": {
"name": "",
"type": 0,
"typeArguments": null,
},
"offset": 856,
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"offset": 920,
},
],
}

// #endregion encode-and-decode-2
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ predicate;
use std::{b512::B512, ecr::ec_recover_address, tx::{tx_id, tx_witness_data}};

fn main(signer: b256) -> bool {
let witness_data: B512 = tx_witness_data(1);
let witness_data: B512 = tx_witness_data(1).unwrap();
let address: b256 = ec_recover_address(witness_data, tx_id()).unwrap().bits();
return address == signer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ script;
use std::{b512::B512, ecr::ec_recover_address, tx::{tx_id, tx_witness_data}};

fn main(signer: b256) -> bool {
let witness_data: B512 = tx_witness_data(1);
let witness_data: B512 = tx_witness_data(1).unwrap();
let address: b256 = ec_recover_address(witness_data, tx_id()).unwrap().bits();
return address == signer;
}
Expand Down
1 change: 1 addition & 0 deletions apps/docs/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ABI
ABIs
ABI's
ASM
AssetId
IDE
Expand Down
15 changes: 6 additions & 9 deletions apps/docs/src/guide/encoding/encode-and-decode.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# Encode and Decode

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.
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.

It has three static methods:
The relevant methods of `Interface` are:

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

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.

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.
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.

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

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

<<< @/../../docs-snippets/test/fixtures/abi/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 `AbiCoder`.
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`.

First, let's prepare the transaction:

Expand Down
2 changes: 1 addition & 1 deletion internal/forc/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.62.0
0.63.1
19 changes: 14 additions & 5 deletions internal/forc/lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,27 @@ const swayRepoUrl = 'https://github.com/fuellabs/sway.git';

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

if (existsSync(swayRepoDir)) {
execSync(`cd ${swayRepoDir} && git fetch origin && git checkout ${branchName}`, stdioOpts);
execSync(`cd ${swayRepoDir} && cargo build`, stdioOpts);
execSync(
[
`cd ${swayRepoDir}`,
`git fetch origin`,
`git checkout ${branchName}`,
`git pull origin ${branchName}`,
].join('&&'),
Dismissed Show dismissed Hide dismissed
stdioOpts
);

execSync(`cd ${swayRepoDir} && cargo build --release`, stdioOpts);
Dismissed Show dismissed Hide dismissed
} else {
execSync(`git clone --branch ${branchName} ${swayRepoUrl} ${swayRepoDir}`, stdioOpts);
execSync(`cd ${swayRepoDir} && cargo build`, stdioOpts);
execSync(`cd ${swayRepoDir} && cargo build --release`, stdioOpts);
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
}

const [from, to] = [swayRepoDebugDir, forcBinDirPath];
const [from, to] = [swayRepoReleaseDir, forcBinDirPath];

rmSync(to, { recursive: true, force: true });
mkdirSync(to, { recursive: true });
Expand Down
10 changes: 5 additions & 5 deletions internal/fuel-core/lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ const fuelCoreRepoUrl = 'https://github.com/fuellabs/fuel-core.git';

export const buildFromGitBranch = (branchName) => {
const fuelCoreRepoDir = join(__dirname, '..', 'fuel-core-repo');
const fuelCoreRepoDebugDir = join(fuelCoreRepoDir, 'target', 'debug');
const fuelCoreRepoReleaseDir = join(fuelCoreRepoDir, 'target', 'release');
const stdioOpts = { stdio: 'inherit' };

if (existsSync(fuelCoreRepoDir)) {
spawnSync('git', ['pull'], { cwd: fuelCoreRepoDir, ...stdioOpts });
spawnSync('git', ['checkout', branchName], { cwd: fuelCoreRepoDir, ...stdioOpts });
spawnSync('cargo', ['build'], { cwd: fuelCoreRepoDir, ...stdioOpts });
spawnSync('git', ['pull'], { cwd: fuelCoreRepoDir, ...stdioOpts });
spawnSync('cargo', ['build', '--release'], { cwd: fuelCoreRepoDir, ...stdioOpts });
} else {
spawnSync(
'git',
['clone', '--branch', branchName, fuelCoreRepoUrl, fuelCoreRepoDir],
stdioOpts
);
spawnSync('cargo', ['build'], { cwd: fuelCoreRepoDir, ...stdioOpts });
spawnSync('cargo', ['build', '--release'], { cwd: fuelCoreRepoDir, ...stdioOpts });
}

const [from, to] = [fuelCoreRepoDebugDir, fuelCoreBinDirPath];
const [from, to] = [fuelCoreRepoReleaseDir, fuelCoreBinDirPath];

rmSync(to, { recursive: true, force: true });
mkdirSync(to, { recursive: true });
Expand Down
8 changes: 4 additions & 4 deletions packages/abi-coder/src/AbiCoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { ResolvedAbiType } from './ResolvedAbiType';
import type { DecodedValue, InputValue, Coder } from './encoding/coders/AbstractCoder';
import { getCoderForEncoding } from './encoding/strategies/getCoderForEncoding';
import type { EncodingOptions } from './types/EncodingOptions';
import type { JsonAbi, JsonAbiArgument } from './types/JsonAbi';
import type { JsonAbiOld, JsonAbiArgument } from './types/JsonAbi';

export abstract class AbiCoder {
static getCoder(
abi: JsonAbi,
abi: JsonAbiOld,
argument: JsonAbiArgument,
options: EncodingOptions = {
padToWordSize: false,
Expand All @@ -17,7 +17,7 @@ export abstract class AbiCoder {
}

static encode(
abi: JsonAbi,
abi: JsonAbiOld,
argument: JsonAbiArgument,
value: InputValue,
options?: EncodingOptions
Expand All @@ -26,7 +26,7 @@ export abstract class AbiCoder {
}

static decode(
abi: JsonAbi,
abi: JsonAbiOld,
argument: JsonAbiArgument,
data: Uint8Array,
offset: number,
Expand Down
Loading
Loading