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!: v1 encoding and encoding improvements #1780

Merged
merged 36 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
cc4035a
chore: reorganise ABI Coder package, including correct file naming fo…
danielbate Feb 15, 2024
1a27076
feat: rename and correctly type encoding
danielbate Feb 15, 2024
0e7aa34
feat: implement encoding strategies
danielbate Feb 15, 2024
dc85889
feat: populate versioned get coder functions
danielbate Feb 15, 2024
d8836d6
feat: add v1 bytes encode
danielbate Feb 15, 2024
92e2e63
feat: add v1 raw slice encode
danielbate Feb 15, 2024
71b5467
feat: add v1 std string encode
danielbate Feb 15, 2024
7897712
feat: add v1 str slice encode
danielbate Feb 15, 2024
50fe9bf
feat: add v1 struct encode
danielbate Feb 15, 2024
def6cc3
feat: add v1 tuple encode
danielbate Feb 15, 2024
21f5057
feat: add v1 vec encode
danielbate Feb 15, 2024
aba8466
feat: add v1 enum encode
danielbate Feb 15, 2024
8b57dc7
feat: utils tests
danielbate Feb 19, 2024
f0107c7
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Feb 20, 2024
a7a25b0
feat: remove redundant ABI Coder methods
danielbate Feb 20, 2024
33cc23b
chore: rename interface -> Interface
danielbate Feb 20, 2024
59617fa
chore: linting
danielbate Feb 20, 2024
5aceff0
chore: changeset
danielbate Feb 20, 2024
7e81999
chore: fix interface export
danielbate Feb 20, 2024
2d08ea7
chore: add missing test groups
danielbate Feb 20, 2024
4dd675f
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Feb 20, 2024
871b709
feat: add missing async to str tests
danielbate Feb 20, 2024
a7b0f22
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Feb 21, 2024
1c10719
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Feb 22, 2024
1a65e50
feat: remove T and I type prefixing
danielbate Feb 22, 2024
5330da2
feat: remove get coder interface
danielbate Feb 22, 2024
1ef0ae8
chore: import fixes
danielbate Feb 22, 2024
cb23fa5
Merge branch 'master' into db/feat/v1-encoding-improvements
danielbate Feb 22, 2024
489a777
chore: linting
danielbate Feb 22, 2024
5cf26c5
Merge branch 'db/feat/v1-encoding-improvements' of https://github.com…
danielbate Feb 22, 2024
f6e6228
Merge branch 'master' into db/feat/v1-encoding-improvements
danielbate Feb 22, 2024
0db6026
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Feb 23, 2024
1145ceb
chore: remove rednundant link
danielbate Feb 23, 2024
6b0ea22
chore: use assert fuel error in enum coder tests
danielbate Feb 23, 2024
212fe9a
chore: alter comment format for abi coder constants
danielbate Feb 23, 2024
5cc2471
Merge branch 'master' into db/feat/v1-encoding-improvements
arboleya Feb 23, 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
80 changes: 80 additions & 0 deletions packages/abi-coder/src/AbiCoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
import { ErrorCode, FuelError } from '@fuel-ts/errors';

Check warning on line 2 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'ErrorCode' is defined but never used

Check warning on line 2 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'FuelError' is defined but never used

Check warning on line 2 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'ErrorCode' is defined but never used

Check warning on line 2 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'FuelError' is defined but never used

Check warning on line 2 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'ErrorCode' is defined but never used

Check warning on line 2 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'FuelError' is defined but never used

import { ResolvedAbiType } from './ResolvedAbiType';
import type { DecodedValue, InputValue, Coder } from './encoding/coders/AbstractCoder';
import { ArrayCoder } from './encoding/coders/v0/ArrayCoder';

Check warning on line 6 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'ArrayCoder' is defined but never used

Check warning on line 6 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'ArrayCoder' is defined but never used

Check warning on line 6 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'ArrayCoder' is defined but never used
import { B256Coder } from './encoding/coders/v0/B256Coder';

Check warning on line 7 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'B256Coder' is defined but never used

Check warning on line 7 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'B256Coder' is defined but never used

Check warning on line 7 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'B256Coder' is defined but never used
import { B512Coder } from './encoding/coders/v0/B512Coder';

Check warning on line 8 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'B512Coder' is defined but never used

Check warning on line 8 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'B512Coder' is defined but never used

Check warning on line 8 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'B512Coder' is defined but never used
import { BooleanCoder } from './encoding/coders/v0/BooleanCoder';

Check warning on line 9 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'BooleanCoder' is defined but never used

Check warning on line 9 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'BooleanCoder' is defined but never used

Check warning on line 9 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'BooleanCoder' is defined but never used
import { ByteCoder } from './encoding/coders/v0/ByteCoder';

Check warning on line 10 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'ByteCoder' is defined but never used

Check warning on line 10 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'ByteCoder' is defined but never used

Check warning on line 10 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'ByteCoder' is defined but never used
import { EnumCoder } from './encoding/coders/v0/EnumCoder';

Check warning on line 11 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'EnumCoder' is defined but never used

Check warning on line 11 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'EnumCoder' is defined but never used

Check warning on line 11 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'EnumCoder' is defined but never used
import { NumberCoder } from './encoding/coders/v0/NumberCoder';

Check warning on line 12 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'NumberCoder' is defined but never used

Check warning on line 12 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'NumberCoder' is defined but never used

Check warning on line 12 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'NumberCoder' is defined but never used
import { OptionCoder } from './encoding/coders/v0/OptionCoder';

Check warning on line 13 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@20

'OptionCoder' is defined but never used

Check warning on line 13 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / browser

'OptionCoder' is defined but never used

Check warning on line 13 in packages/abi-coder/src/AbiCoder.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

'OptionCoder' is defined but never used
import { RawSliceCoder } from './encoding/coders/v0/RawSliceCoder';
import { StdStringCoder } from './encoding/coders/v0/StdStringCoder';
import { StringCoder } from './encoding/coders/v0/StringCoder';
import { StructCoder } from './encoding/coders/v0/StructCoder';
import { TupleCoder } from './encoding/coders/v0/TupleCoder';
import { U64Coder } from './encoding/coders/v0/U64Coder';
import { VecCoder } from './encoding/coders/v0/VecCoder';
import { BooleanCoder as BooleanCoderV1 } from './encoding/coders/v1/BooleanCoder';
import { ByteCoder as ByteCoderV1 } from './encoding/coders/v1/ByteCoder';
import { EnumCoder as EnumCoderV1 } from './encoding/coders/v1/EnumCoder';
import { NumberCoder as NumberCoderV1 } from './encoding/coders/v1/NumberCoder';
import { RawSliceCoder as RawSliceCoderV1 } from './encoding/coders/v1/RawSliceCoder';
import { StdStringCoder as StdStringCoderV1 } from './encoding/coders/v1/StdStringCoder';
import { StringCoder as StringCoderV1 } from './encoding/coders/v1/StringCoder';
import { StructCoder as StructCoderV1 } from './encoding/coders/v1/StructCoder';
import { TupleCoder as TupleCoderV1 } from './encoding/coders/v1/TupleCoder';
import { VecCoder as VecCoderV1 } from './encoding/coders/v1/VecCoder';
import { getEncodingStrategy } from './encoding/strategies/getEncodingStrategy';
import type { JsonAbi, JsonAbiArgument } from './types/JsonAbi';
import type { TEncodingOptions } from './types/TEncodingOptions';
import {
arrayRegEx,
enumRegEx,
stringRegEx,
structRegEx,
tupleRegEx,
OPTION_CODER_TYPE,
VEC_CODER_TYPE,
BYTES_CODER_TYPE,
STD_STRING_CODER_TYPE,
} from './utils/constants';
import { findOrThrow } from './utils/utilities';

export abstract class AbiCoder {
static getCoder(
abi: JsonAbi,
argument: JsonAbiArgument,
options: TEncodingOptions = {
isSmallBytes: false,
}
): Coder {
const resolvedAbiType = new ResolvedAbiType(abi, argument);
return getEncodingStrategy(options.encoding).getCoder(resolvedAbiType, options);
}

static encode(
abi: JsonAbi,
argument: JsonAbiArgument,
value: InputValue,
options?: TEncodingOptions
) {
return this.getCoder(abi, argument, options).encode(value);
}

static decode(
abi: JsonAbi,
argument: JsonAbiArgument,
data: Uint8Array,
offset: number,
options?: TEncodingOptions
): [DecodedValue | undefined, number] {
return this.getCoder(abi, argument, options).decode(data, offset) as [
DecodedValue | undefined,
number,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import type { BytesLike } from 'ethers';
import { sha256, getBytesCopy } from 'ethers';

import { AbiCoder } from './abi-coder';
import type { DecodedValue, InputValue } from './coders/abstract-coder';
import { ByteCoder } from './coders/v0/byte';
import { TupleCoder } from './coders/v0/tuple';
import { VecCoder } from './coders/v0/vec';
import { OPTION_CODER_TYPE } from './constants';
import { AbiCoder } from './AbiCoder';
import type { DecodedValue, InputValue } from './encoding/coders/AbstractCoder';
import { ByteCoder } from './encoding/coders/v0/ByteCoder';
import { TupleCoder } from './encoding/coders/v0/TupleCoder';
import { VecCoder } from './encoding/coders/v0/VecCoder';
import { OPTION_CODER_TYPE } from './utils/constants';
import type {

Check failure on line 13 in packages/abi-coder/src/FunctionFragment.ts

View workflow job for this annotation

GitHub Actions / node@20

`./types/JsonAbi` type import should occur before import of `./utils/constants`

Check failure on line 13 in packages/abi-coder/src/FunctionFragment.ts

View workflow job for this annotation

GitHub Actions / browser

`./types/JsonAbi` type import should occur before import of `./utils/constants`

Check failure on line 13 in packages/abi-coder/src/FunctionFragment.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

`./types/JsonAbi` type import should occur before import of `./utils/constants`
JsonAbi,
JsonAbiArgument,
JsonAbiFunction,
JsonAbiFunctionAttribute,
} from './json-abi';
import { ResolvedAbiType } from './resolved-abi-type';
import type { Uint8ArrayWithDynamicData } from './utilities';
import { isPointerType, unpackDynamicData, findOrThrow, isHeapType } from './utilities';
} from './types/JsonAbi';
import { ResolvedAbiType } from './ResolvedAbiType';

Check failure on line 19 in packages/abi-coder/src/FunctionFragment.ts

View workflow job for this annotation

GitHub Actions / node@20

`./ResolvedAbiType` import should occur before type import of `./encoding/coders/AbstractCoder`

Check failure on line 19 in packages/abi-coder/src/FunctionFragment.ts

View workflow job for this annotation

GitHub Actions / browser

`./ResolvedAbiType` import should occur before type import of `./encoding/coders/AbstractCoder`

Check failure on line 19 in packages/abi-coder/src/FunctionFragment.ts

View workflow job for this annotation

GitHub Actions / node@18.18.2

`./ResolvedAbiType` import should occur before type import of `./encoding/coders/AbstractCoder`
import type { Uint8ArrayWithDynamicData } from './utils/utilities';
import { isPointerType, unpackDynamicData, findOrThrow, isHeapType } from './utils/utilities';

export class FunctionFragment<
TAbi extends JsonAbi = JsonAbi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { getBytesCopy, type BytesLike } from 'ethers';

import { AbiCoder } from './abi-coder';
import type { InputValue } from './coders/abstract-coder';
import { FunctionFragment } from './function-fragment';
import type { JsonAbi, JsonAbiConfigurable } from './json-abi';
import { findOrThrow } from './utilities';
import { AbiCoder } from './AbiCoder';
import { FunctionFragment } from './FunctionFragment';
import type { InputValue } from './encoding/coders/AbstractCoder';
import type { JsonAbi, JsonAbiConfigurable } from './types/JsonAbi';
import { findOrThrow } from './utils/utilities';

export class Interface<TAbi extends JsonAbi = JsonAbi> {
readonly functions!: Record<string, FunctionFragment>;
Expand Down Expand Up @@ -100,7 +100,7 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {
const { loggedType } = findOrThrow(this.jsonAbi.loggedTypes, (type) => type.logId === logId);

return AbiCoder.decode(this.jsonAbi, loggedType, getBytesCopy(data), 0, {
version: this.jsonAbi.encoding,
encoding: this.jsonAbi.encoding,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';

import { arrayRegEx, enumRegEx, genericRegEx, stringRegEx, structRegEx } from './constants';
import type { JsonAbi, JsonAbiArgument } from './json-abi';
import { findOrThrow } from './utilities';
import type { JsonAbi, JsonAbiArgument } from './types/JsonAbi';
import { arrayRegEx, enumRegEx, genericRegEx, stringRegEx, structRegEx } from './utils/constants';
import { findOrThrow } from './utils/utilities';

export class ResolvedAbiType {
readonly abi: JsonAbi;
Expand Down
205 changes: 0 additions & 205 deletions packages/abi-coder/src/abi-coder.ts

This file was deleted.

Loading
Loading