Skip to content

Commit

Permalink
Merge branch 'ps/feat/abi-coder' into ps/feat/transaction-coder
Browse files Browse the repository at this point in the history
  • Loading branch information
petertonysmith94 authored Dec 9, 2024
2 parents aec848a + 51b0ac8 commit bb4bbf2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/abi/src/coder/encoding/encoding-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const U256_TYPE = 'u256';
export const B256_TYPE = 'b256';
export const B512_TYPE = 'b512';
export const VOID_TYPE = 'void';
export const BYTE_TYPE = 'byte';
export const BYTES_TYPE = 'bytes';
export const RAW_SLICE_TYPE = 'raw slice';
export const STD_STRING_TYPE = 'std string';
export const STR_SLICE_TYPE = 'str';
Expand All @@ -29,7 +29,7 @@ export const ENCODING_TYPES = [
B256_TYPE,
B512_TYPE,
VOID_TYPE,
BYTE_TYPE,
BYTES_TYPE,
RAW_SLICE_TYPE,
STD_STRING_TYPE,
STR_SLICE_TYPE,
Expand Down
2 changes: 1 addition & 1 deletion packages/abi/src/coder/encoding/matching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const createCoderMatcher = (coders: SupportedCoders) =>
tuple: coders.tuple,
vector: coders.vector,
struct: coders.struct,
bytes: coders.byte,
bytes: coders.bytes,
stdString: coders.stdString,
str: coders.str,
enum: coders.enum,
Expand Down
20 changes: 4 additions & 16 deletions packages/abi/src/coder/encoding/v1/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { concat, toUtf8Bytes, toUtf8String } from '@fuel-ts/utils';

import { MAX_BYTES } from '../../constants';
import {
BYTE_TYPE,
BYTES_TYPE,
RAW_SLICE_TYPE,
STD_STRING_TYPE,
STR_SLICE_TYPE,
Expand Down Expand Up @@ -56,29 +56,20 @@ const createHeapType = <TEncoded extends { length: number }, TDecoded>({
},
});

/**
* Byte
*/
const byteTransformer: Coder<Uint8Array | number[], Uint8Array> = {
type: BYTE_TYPE,
const bytesTransformer: Coder<Uint8Array | number[], Uint8Array> = {
type: BYTES_TYPE,
encode: (value: Uint8Array | number[]) => (Array.isArray(value) ? new Uint8Array(value) : value),
decode: (data: Uint8Array, offset: number): [Uint8Array, number] => [data, offset],
};
export const byte: Coder<Uint8Array | number[], Uint8Array> = createHeapType(byteTransformer);
export const bytes: Coder<Uint8Array | number[], Uint8Array> = createHeapType(bytesTransformer);

/**
* Raw slice
*/
const rawSliceTransformer: Coder<number[]> = {
type: RAW_SLICE_TYPE,
encode: (value: number[]) => new Uint8Array(value),
decode: (data: Uint8Array, offset: number): [number[], number] => [Array.from(data), offset],
};
export const rawSlice: Coder<number[]> = createHeapType(rawSliceTransformer);

/**
* Dynamic Length String based coders
*/
const createStringCoder = (type: string): Coder<string, string> =>
createHeapType({
type,
Expand All @@ -89,9 +80,6 @@ const createStringCoder = (type: string): Coder<string, string> =>
export const stdString = createStringCoder(STD_STRING_TYPE);
export const str = createStringCoder(STR_SLICE_TYPE);

/**
* Vec
*/
type VecEncodedValue<TCoder extends Coder = Coder> =
| Array<ReturnType<TCoder['encode']>[0]>
| Uint8Array;
Expand Down
4 changes: 2 additions & 2 deletions packages/abi/src/coder/encoding/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { arrayCoder } from './array';
import { enumCoder } from './enum';
import { voidCoder, u16, u32, u8, u64, u256, b256, b512, bool } from './fixed';
import { byte, rawSlice, stdString, str, vector } from './heap';
import { bytes, rawSlice, stdString, str, vector } from './heap';
import { option } from './option';
import { string } from './string';
import { struct } from './struct';
Expand All @@ -20,7 +20,7 @@ export const v1: SupportedCodersV1 = {
b512,
bool,
void: voidCoder,
byte,
bytes,
rawSlice,
str,
stdString,
Expand Down
4 changes: 2 additions & 2 deletions packages/abi/src/coder/encoding/v1/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { arrayCoder } from './array';
import type { enumCoder } from './enum';
import type { voidCoder, u16, u32, u8, u64, u256, b256, b512, bool } from './fixed';
import type { byte, rawSlice, stdString, str, vector } from './heap';
import type { bytes, rawSlice, stdString, str, vector } from './heap';
import type { option } from './option';
import type { string } from './string';
import type { struct } from './struct';
Expand All @@ -23,7 +23,7 @@ export interface SupportedCodersV1 {
option: typeof option;
struct: typeof struct;
tuple: typeof tuple;
byte: typeof byte;
bytes: typeof bytes;
rawSlice: typeof rawSlice;
stdString: typeof stdString;
str: typeof str;
Expand Down
7 changes: 2 additions & 5 deletions packages/fuel-gauge/src/abi/abi-coder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,10 +1142,7 @@ describe('AbiCoder', () => {
});

describe('types_struct_with_array', () => {
/**
* TODO This is causing a generic to be left into the parsed format, ask Nedim about this.
*/
it.skip('should encode/decode just fine', async () => {
it('should encode/decode just fine', async () => {
// Inputs
const inputB256: string =
'0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
Expand Down Expand Up @@ -1406,7 +1403,7 @@ describe('AbiCoder', () => {
});

describe('types_struct_with_complex_nested_struct', () => {
it('should encode/decode just fine');
it.todo('should encode/decode just fine');

it('should have function properties', () => {
const fn = contract.interface.getFunction('types_struct_with_complex_nested_struct');
Expand Down

0 comments on commit bb4bbf2

Please sign in to comment.