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 coder #3402

Open
wants to merge 24 commits into
base: ns/feat/abi-parser
Choose a base branch
from
Open

Conversation

petertonysmith94
Copy link
Contributor

@petertonysmith94 petertonysmith94 commented Nov 18, 2024

Summary

  • This PR encompasses the refactoring and revamping of the encoding.
  • The following diagram outlines all the different encoding types and how the encoded types look in bytes.

image

Breaking Changes

Note

These will need to be combined into the final PR (#3085)

  • The Interface class has been removed in favour of the AbiCoder.
// Before
const abiInterface = new Interface({ ...abi });
// After
const abiCoder = new AbiCoder({ ...abi });
  • The Interface.decodeFunctionResult has moved to the AbiCoderFunction.decodeOutput
// Before
const data = new Uint8Array(...);
abiInterface.decodeFunctionResult('fn_name', data)
// After
const data = new Uint8Array(...);
abiCoder.getFunction('fn_name').decodeOutput(data)
  • The Interface.decodeLog has moved to the AbiCoderLog.decode
// Before
const data = new Uint8Array(...);
abiInterface.decodeLog(data, 'log_id')
// After
const data = new Uint8Array(...);
abiCoder.getLog('log_id').decode(data);
  • The Interface.encodeConfigurable has moved to the AbiCoderConfigurable.encode
// Before
const value = 'Configurable Value';
abiInterface.encodeConfigurable('configurable_name', value);
// After
const value = 'Configurable Value';
abiInterface.getConfigurable('configurable_name').encode(value);
  • The Interface.encodeType and Interface.encodeType has moved to the AbiCoderType
// Before
abiInterface.encodeType('concrete_type_id', 'Type value');
abiInterface.decodeType('concrete_type_id', new Uint8Array(...));
// After
abiInterface.getType('concrete_type_id').encode('Type value');
abiInterface.getType('concrete_type_id').decode(new Uint8Array(...));
  • Accessing underlying coders is now achieved through a convenient class, AbiEncoding.
// Before
import { NumberCoder, BigNumberCoder, B256Coder, B512Coder, VoidCoder, BooleanCoder, RawSliceCoder, StrSliceCoder, StdStringCoder } from '@fuel-ts/abi-coder';

const u8Coder = new NumberCoder('u8');
const u16Coder = new NumberCoder('u16');
const u32Coder = new NumberCoder('u32');
const u64Coder = new BigNumberCoder('u64');
const u256Coder = new BigNumberCoder('u256');
const b256Coder = new B256Coder();
const b512Coder = new B512Coder();
const boolCoder = new BooleanCoder();
const voidCoder = new VoidCoder();

const byteCoder = new ByteCoder();
const rawSliceCoder = new RawSliceCoder();
const strSliceCoder = new StrSliceCoder();
const stdStringCoder = new StdStringCoder();
const stringCoder = new StringCoder(4);

const arrayCoder = new ArrayCoder(u8Coder, 3);
const tupleCoder = new TupleCoder([u8Coder, u8Coder]);
const vectorCoder = new VecCoder(u8Coder);

const enumCoder = new EnumCoder('name of enum', { u8: u8Coder });
const optionCoder = new OptionCoder('name of option', { None: voidCoder, Some: voidCoder });
const structCoder = new StructCoder('name of struct', { u8: u8Coder });
// After
import { encoding } from '@fuel-ts/abi';

const u8Coder = encoding.u8;
const u16Coder = encoding.u16;
const u32Coder = encoding.u32;
const u64Coder = encoding.u64;
const u256Coder = encoding.u256;
const b256Coder = encoding.b256;
const b512Coder = encoding.b512;
const boolCoder = encoding.bool;
const voidCoder = encoding.void;

const byteCoder = encoding.byte;
const rawSliceCoder = encoding.rawSlice;
const strSliceCoder = encoding.str;
const stdStringCoder = encoding.stdString;
const stringCoder = encoding.string(4);

const arrayCoder = encoding.array(u8Coder, 3);
const tupleCoder = encoding.tuple([u8Coder, u8Coder]);
const vectorCoder = encoding.vector(u8Coder);

const enumCoder = encoding.enum({ u8: u8Coder });
const optionCoder = encoding.option({ None: voidCoder, Some: voidCoder });
const structCoder = encoding.struct({ u8: u8Coder });
  • Removal of the constant INPUT_COIN_FIXED_SIZE
import { INPUT_COIN_FIXED_SIZE } from 'fuels';
  • Renamed Encoding from the @fuel-ts/crypto package to BufferEncoding.
// Before
import { Encoding } from '@fuel-ts/crypto';
// After
import { BufferEncoding } from '@fuel-ts/crypto';

Checklist

  • All changes are covered by tests (or not applicable)
  • All changes are documented (or not applicable)
  • I reviewed the entire PR myself (preferably, on GH UI)
  • I described all Breaking Changes (or there's none)

@petertonysmith94 petertonysmith94 added the feat Issue is a feature label Nov 18, 2024
@petertonysmith94 petertonysmith94 self-assigned this Nov 18, 2024
Copy link

vercel bot commented Nov 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
fuels-template ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 13, 2025 11:38am
ts-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 13, 2025 11:38am
ts-docs-api ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 13, 2025 11:38am

@petertonysmith94 petertonysmith94 changed the base branch from master to ns/feat/abi-parser November 18, 2024 12:27
@nedsalk nedsalk mentioned this pull request Nov 28, 2024
4 tasks
@@ -47,7 +47,7 @@ export class Predicate<
> extends Account {
bytes: Uint8Array;
predicateData: TData = [] as unknown as TData;
interface: Interface;
interface: AbiCoder;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
interface: AbiCoder;
abiCoder: AbiCoder;

and all the other places where this renaming would break them.

@@ -54,7 +54,7 @@ export type DeployContractResult<TContract extends Contract = Contract> = {
*/
export default class ContractFactory<TContract extends Contract = Contract> {
bytecode: BytesLike;
interface: Interface;
interface: AbiCoder;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
interface: AbiCoder;
abiCoder: AbiCoder;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Abi - Refactor / Coder
6 participants