From 38f67def49058e6ed71648f690722a454f16e1cc Mon Sep 17 00:00:00 2001 From: nedsalk Date: Tue, 10 Sep 2024 09:27:57 +0200 Subject: [PATCH 01/16] add matchers and PoC --- .../src/coder/encoding/v1/coder-matcher.ts | 35 ++++++++ .../coder/encoding/v1/coders/empty-coder.ts | 3 + .../coder/encoding/v1/coders/enum-coder.ts | 3 + .../coder/encoding/v1/coders/option-coder.ts | 3 + .../abi/src/coder/encoding/v1/coders/types.ts | 1 + .../src/coder/encoding/v1/coders/u8-coder.ts | 3 + packages/abi/src/gen/typer-matcher.ts | 35 ++++++++ packages/abi/src/gen/typers/empty-typer.ts | 3 + packages/abi/src/gen/typers/enum-typer.ts | 3 + packages/abi/src/gen/typers/option-typer.ts | 3 + packages/abi/src/gen/typers/result-typer.ts | 3 + packages/abi/src/gen/typers/types.ts | 1 + packages/abi/src/gen/typers/u8-typer.ts | 3 + .../src/matchers/sway-type-matchers.test.ts | 48 ++++++++++ .../abi/src/matchers/sway-type-matchers.ts | 89 +++++++++++++++++++ 15 files changed, 236 insertions(+) create mode 100644 packages/abi/src/coder/encoding/v1/coder-matcher.ts create mode 100644 packages/abi/src/coder/encoding/v1/coders/empty-coder.ts create mode 100644 packages/abi/src/coder/encoding/v1/coders/enum-coder.ts create mode 100644 packages/abi/src/coder/encoding/v1/coders/option-coder.ts create mode 100644 packages/abi/src/coder/encoding/v1/coders/types.ts create mode 100644 packages/abi/src/coder/encoding/v1/coders/u8-coder.ts create mode 100644 packages/abi/src/gen/typer-matcher.ts create mode 100644 packages/abi/src/gen/typers/empty-typer.ts create mode 100644 packages/abi/src/gen/typers/enum-typer.ts create mode 100644 packages/abi/src/gen/typers/option-typer.ts create mode 100644 packages/abi/src/gen/typers/result-typer.ts create mode 100644 packages/abi/src/gen/typers/types.ts create mode 100644 packages/abi/src/gen/typers/u8-typer.ts create mode 100644 packages/abi/src/matchers/sway-type-matchers.test.ts create mode 100644 packages/abi/src/matchers/sway-type-matchers.ts diff --git a/packages/abi/src/coder/encoding/v1/coder-matcher.ts b/packages/abi/src/coder/encoding/v1/coder-matcher.ts new file mode 100644 index 00000000000..2bfca3e5958 --- /dev/null +++ b/packages/abi/src/coder/encoding/v1/coder-matcher.ts @@ -0,0 +1,35 @@ +import type { swayTypeMatchers } from '../../../matchers/sway-type-matchers'; + +import { EmptyCoder } from './coders/empty-coder'; +import { EnumCoder } from './coders/enum-coder'; +import { OptionCoder } from './coders/option-coder'; +import type { Coder } from './coders/types'; +import { U8Coder } from './coders/u8-coder'; + +// Coder | undefined for proof-of-concept, it'll become only Coder later +export const coderMatcher: Record = { + empty: EmptyCoder, + u8: U8Coder, + enum: EnumCoder, + result: EnumCoder, + option: OptionCoder, + string: undefined, + bool: undefined, + u16: undefined, + u32: undefined, + u64: undefined, + u256: undefined, + b256: undefined, + generic: undefined, + stdString: undefined, + struct: undefined, + b512: undefined, + bytes: undefined, + vector: undefined, + tuple: undefined, + array: undefined, + assetId: undefined, + evmAddress: undefined, + rawUntypedPtr: undefined, + rawUntypedSlice: undefined, +}; diff --git a/packages/abi/src/coder/encoding/v1/coders/empty-coder.ts b/packages/abi/src/coder/encoding/v1/coders/empty-coder.ts new file mode 100644 index 00000000000..d9e1e211e30 --- /dev/null +++ b/packages/abi/src/coder/encoding/v1/coders/empty-coder.ts @@ -0,0 +1,3 @@ +import type { Coder } from './types'; + +export class EmptyCoder implements Coder {} diff --git a/packages/abi/src/coder/encoding/v1/coders/enum-coder.ts b/packages/abi/src/coder/encoding/v1/coders/enum-coder.ts new file mode 100644 index 00000000000..65073698fd4 --- /dev/null +++ b/packages/abi/src/coder/encoding/v1/coders/enum-coder.ts @@ -0,0 +1,3 @@ +import type { Coder } from './types'; + +export class EnumCoder implements Coder {} diff --git a/packages/abi/src/coder/encoding/v1/coders/option-coder.ts b/packages/abi/src/coder/encoding/v1/coders/option-coder.ts new file mode 100644 index 00000000000..2cc5c3a00c3 --- /dev/null +++ b/packages/abi/src/coder/encoding/v1/coders/option-coder.ts @@ -0,0 +1,3 @@ +import type { Coder } from './types'; + +export class OptionCoder implements Coder {} diff --git a/packages/abi/src/coder/encoding/v1/coders/types.ts b/packages/abi/src/coder/encoding/v1/coders/types.ts new file mode 100644 index 00000000000..6f640be46ef --- /dev/null +++ b/packages/abi/src/coder/encoding/v1/coders/types.ts @@ -0,0 +1 @@ +export interface Coder {} diff --git a/packages/abi/src/coder/encoding/v1/coders/u8-coder.ts b/packages/abi/src/coder/encoding/v1/coders/u8-coder.ts new file mode 100644 index 00000000000..70ca4b8397d --- /dev/null +++ b/packages/abi/src/coder/encoding/v1/coders/u8-coder.ts @@ -0,0 +1,3 @@ +import type { Coder } from './types'; + +export class U8Coder implements Coder {} diff --git a/packages/abi/src/gen/typer-matcher.ts b/packages/abi/src/gen/typer-matcher.ts new file mode 100644 index 00000000000..a102b4c9ec8 --- /dev/null +++ b/packages/abi/src/gen/typer-matcher.ts @@ -0,0 +1,35 @@ +import type { swayTypeMatchers } from '../matchers/sway-type-matchers'; + +import { EmptyTyper } from './typers/empty-typer'; +import { EnumTyper } from './typers/enum-typer'; +import { OptionTyper } from './typers/option-typer'; +import { ResultTyper } from './typers/result-typer'; +import type { Typer } from './typers/types'; +import { U8Typer } from './typers/u8-typer'; + +export const coderMatcher: Record = { + empty: EmptyTyper, + u8: U8Typer, + enum: EnumTyper, + result: ResultTyper, + option: OptionTyper, + string: undefined, + bool: undefined, + u16: undefined, + u32: undefined, + u64: undefined, + u256: undefined, + b256: undefined, + generic: undefined, + stdString: undefined, + struct: undefined, + b512: undefined, + bytes: undefined, + vector: undefined, + tuple: undefined, + array: undefined, + assetId: undefined, + evmAddress: undefined, + rawUntypedPtr: undefined, + rawUntypedSlice: undefined, +}; diff --git a/packages/abi/src/gen/typers/empty-typer.ts b/packages/abi/src/gen/typers/empty-typer.ts new file mode 100644 index 00000000000..b0a7a900bda --- /dev/null +++ b/packages/abi/src/gen/typers/empty-typer.ts @@ -0,0 +1,3 @@ +import type { Typer } from './types'; + +export class EmptyTyper implements Typer {} diff --git a/packages/abi/src/gen/typers/enum-typer.ts b/packages/abi/src/gen/typers/enum-typer.ts new file mode 100644 index 00000000000..d91804109b0 --- /dev/null +++ b/packages/abi/src/gen/typers/enum-typer.ts @@ -0,0 +1,3 @@ +import type { Typer } from './types'; + +export class EnumTyper implements Typer {} diff --git a/packages/abi/src/gen/typers/option-typer.ts b/packages/abi/src/gen/typers/option-typer.ts new file mode 100644 index 00000000000..56e0ef31281 --- /dev/null +++ b/packages/abi/src/gen/typers/option-typer.ts @@ -0,0 +1,3 @@ +import type { Typer } from './types'; + +export class OptionTyper implements Typer {} diff --git a/packages/abi/src/gen/typers/result-typer.ts b/packages/abi/src/gen/typers/result-typer.ts new file mode 100644 index 00000000000..e57ec0fc2a3 --- /dev/null +++ b/packages/abi/src/gen/typers/result-typer.ts @@ -0,0 +1,3 @@ +import type { Typer } from './types'; + +export class ResultTyper implements Typer {} diff --git a/packages/abi/src/gen/typers/types.ts b/packages/abi/src/gen/typers/types.ts new file mode 100644 index 00000000000..25b53b4f143 --- /dev/null +++ b/packages/abi/src/gen/typers/types.ts @@ -0,0 +1 @@ +export interface Typer {} diff --git a/packages/abi/src/gen/typers/u8-typer.ts b/packages/abi/src/gen/typers/u8-typer.ts new file mode 100644 index 00000000000..924a83da79f --- /dev/null +++ b/packages/abi/src/gen/typers/u8-typer.ts @@ -0,0 +1,3 @@ +import type { Typer } from './types'; + +export class U8Typer implements Typer {} diff --git a/packages/abi/src/matchers/sway-type-matchers.test.ts b/packages/abi/src/matchers/sway-type-matchers.test.ts new file mode 100644 index 00000000000..e86bfacc4b6 --- /dev/null +++ b/packages/abi/src/matchers/sway-type-matchers.test.ts @@ -0,0 +1,48 @@ +import { swayTypeMatchers } from './sway-type-matchers'; + +const testCases: Record = { + empty: '()', + generic: 'generic T', + bool: 'bool', + u8: 'u8', + u16: 'u16', + u32: 'u32', + u64: 'u64', + u256: 'u256', + b256: 'b256', + + string: 'str[5]', + array: '[_; 2]', + tuple: '(_, _, _)', + + struct: 'struct MyStruct', + assetId: 'struct std::asset_id::AssetId', + b512: 'struct std::b512::B512', + bytes: 'struct std::bytes::Bytes', + evmAddress: 'struct std::vm::evm::evm_address::EvmAddress', + stdString: 'struct std::string::String', + vector: 'struct std::vec::Vec', + + enum: 'enum MyEnum', + option: 'enum std::option::Option', + result: 'enum std::result::Result', + + rawUntypedPtr: 'raw untyped ptr', + rawUntypedSlice: 'raw untyped slice', +}; + +describe('sway type matchers', () => { + test.each(Object.entries(testCases))('%s - %s', (type, value) => { + expect(swayTypeMatchers[type as keyof typeof swayTypeMatchers](value)).toEqual(true); + + // verify that it doesn't match any other type + const allOtherMatchers = Object.entries(swayTypeMatchers).filter(([key]) => key !== type); + allOtherMatchers.forEach(([, matcher]) => { + expect(matcher(value)).toEqual(false); + }); + }); + + test('raw vector is not interpreted as vector', () => { + expect(swayTypeMatchers.vector('struct std::vec::RawVec')).toEqual(false); + }); +}); diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts new file mode 100644 index 00000000000..536191f359a --- /dev/null +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -0,0 +1,89 @@ +type SwayType = + | 'empty' + | 'bool' + | 'u8' + | 'u16' + | 'u32' + | 'u64' + | 'u256' + | 'b256' + | 'generic' + | 'string' + | 'stdString' + | 'option' + | 'result' + | 'enum' + | 'struct' + | 'b512' + | 'bytes' + | 'vector' + | 'tuple' + | 'array' + | 'assetId' + | 'evmAddress' + | 'rawUntypedPtr' // might not need it + | 'rawUntypedSlice'; // might not need it + +type Matcher = (type: string) => boolean; + +const empty: Matcher = (type) => type === '()'; +const bool: Matcher = (type) => type === 'bool'; +const u8: Matcher = (type) => type === 'u8'; +const u16: Matcher = (type) => type === 'u16'; +const u32: Matcher = (type) => type === 'u32'; +const u64: Matcher = (type) => type === 'u64'; +const u256: Matcher = (type) => type === 'u256'; +const b256: Matcher = (type) => type === 'b256'; +const generic: Matcher = (type) => /^generic ([^\s]+)$/m.test(type); +const string: Matcher = (type) => /^str\[(.+)\]$/m.test(type); +const tuple: Matcher = (type) => /^\([_,\s]+\)$/m.test(type); +const array: Matcher = (type) => /^\[_; ([0-9]+)\]$/m.test(type); + +const struct: Matcher = (type) => + /^struct (.+::)?(.+)$/m.test(type) && + !/^struct (std::.*)?(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m.test(type); +const assetId: Matcher = (type) => type === 'struct std::asset_id::AssetId'; +const b512: Matcher = (type) => type === 'struct std::b512::B512'; +const bytes: Matcher = (type) => type === 'struct std::bytes::Bytes'; +const evmAddress: Matcher = (type) => type === 'struct std::vm::evm::evm_address::EvmAddress'; +const stdString: Matcher = (type) => type === 'struct std::string::String'; +const vector: Matcher = (type) => type === 'struct std::vec::Vec'; + +const option: Matcher = (type) => type === 'enum std::option::Option'; +const result: Matcher = (type) => type === 'enum std::result::Result'; +const enumMatcher: Matcher = (type) => + !option(type) && !result(type) && /^enum (.+::)?(.+)$/m.test(type); + +const rawUntypedPtr: Matcher = (type) => type === 'raw untyped ptr'; +const rawUntypedSlice: Matcher = (type) => type === 'raw untyped slice'; + +export const swayTypeMatchers: Record = { + empty, + generic, + bool, + u8, + u16, + u32, + u64, + u256, + b256, + + string, + tuple, + array, + + struct, + assetId, + b512, + bytes, + evmAddress, + stdString, + vector, + + enum: enumMatcher, + option, + result, + + rawUntypedPtr, + rawUntypedSlice, +}; From ee83ffd1c72c5c19560ff7da980038d46c2baacf Mon Sep 17 00:00:00 2001 From: nedsalk Date: Tue, 10 Sep 2024 09:39:44 +0200 Subject: [PATCH 02/16] add "full" flow --- packages/abi/src/coder/abi-coder.ts | 11 ++++++++++- .../abi/src/coder/encoding/v1/coder-matcher.ts | 11 +++++++++++ packages/abi/src/gen/typer-matcher.ts | 14 ++++++++++++-- packages/abi/src/matchers/sway-type-matchers.ts | 2 ++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packages/abi/src/coder/abi-coder.ts b/packages/abi/src/coder/abi-coder.ts index 38325e4eff3..49cd447eff9 100644 --- a/packages/abi/src/coder/abi-coder.ts +++ b/packages/abi/src/coder/abi-coder.ts @@ -1,2 +1,11 @@ +import { getCoder } from './encoding/v1/coder-matcher'; +import type { Coder } from './encoding/v1/coders/types'; + // Placeholder -export class AbiCoder {} +export class AbiCoder { + public encode(type: string, value: unknown) { + const coder = getCoder(type) as Coder & { encode: (value: unknown) => unknown }; + + return coder.encode(value); + } +} diff --git a/packages/abi/src/coder/encoding/v1/coder-matcher.ts b/packages/abi/src/coder/encoding/v1/coder-matcher.ts index 2bfca3e5958..d25bdcfbbc1 100644 --- a/packages/abi/src/coder/encoding/v1/coder-matcher.ts +++ b/packages/abi/src/coder/encoding/v1/coder-matcher.ts @@ -1,4 +1,5 @@ import type { swayTypeMatchers } from '../../../matchers/sway-type-matchers'; +import { swayTypeMatcherEntries } from '../../../matchers/sway-type-matchers'; import { EmptyCoder } from './coders/empty-coder'; import { EnumCoder } from './coders/enum-coder'; @@ -33,3 +34,13 @@ export const coderMatcher: Record = { +export const typerMatcher: Record = { empty: EmptyTyper, u8: U8Typer, enum: EnumTyper, @@ -33,3 +33,13 @@ export const coderMatcher: Record = { rawUntypedPtr, rawUntypedSlice, }; + +export const swayTypeMatcherEntries = Object.entries(swayTypeMatchers); From d14d69a5a60e10966771564e3832be21446da850 Mon Sep 17 00:00:00 2001 From: nedsalk Date: Tue, 10 Sep 2024 10:14:09 +0200 Subject: [PATCH 03/16] extract relevant regexes into variables --- packages/abi/src/matchers/sway-type-matchers.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index acaf7f8c9c0..8fb2e19604f 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -34,10 +34,18 @@ const u32: Matcher = (type) => type === 'u32'; const u64: Matcher = (type) => type === 'u64'; const u256: Matcher = (type) => type === 'u256'; const b256: Matcher = (type) => type === 'b256'; -const generic: Matcher = (type) => /^generic ([^\s]+)$/m.test(type); -const string: Matcher = (type) => /^str\[(.+)\]$/m.test(type); -const tuple: Matcher = (type) => /^\([_,\s]+\)$/m.test(type); -const array: Matcher = (type) => /^\[_; ([0-9]+)\]$/m.test(type); + +export const genericRegEx = /^generic ([^\s]+)$/m; +const generic: Matcher = (type) => genericRegEx.test(type); + +export const stringRegEx = /^str\[(.+)\]$/m; +const string: Matcher = (type) => stringRegEx.test(type); + +export const tupleRegEx = /^\([_,\s]+\)$/m; +const tuple: Matcher = (type) => tupleRegEx.test(type); + +export const arrayRegEx = /^\[_; ([0-9]+)\]$/m; +const array: Matcher = (type) => arrayRegEx.test(type); const struct: Matcher = (type) => /^struct (.+::)?(.+)$/m.test(type) && From 8e59b8355b3f8ff24c2949eb013a2ac1fccf90b5 Mon Sep 17 00:00:00 2001 From: nedsalk Date: Tue, 10 Sep 2024 10:33:26 +0200 Subject: [PATCH 04/16] update generics --- packages/abi/src/matchers/sway-type-matchers.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index 8fb2e19604f..161eee0fb45 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -35,20 +35,20 @@ const u64: Matcher = (type) => type === 'u64'; const u256: Matcher = (type) => type === 'u256'; const b256: Matcher = (type) => type === 'b256'; -export const genericRegEx = /^generic ([^\s]+)$/m; +export const genericRegEx = /^generic.+$/; const generic: Matcher = (type) => genericRegEx.test(type); -export const stringRegEx = /^str\[(.+)\]$/m; +export const stringRegEx = /str\[(?[0-9]+)\]/; const string: Matcher = (type) => stringRegEx.test(type); -export const tupleRegEx = /^\([_,\s]+\)$/m; +export const tupleRegEx = /^\((?.*)\)$/m; const tuple: Matcher = (type) => tupleRegEx.test(type); -export const arrayRegEx = /^\[_; ([0-9]+)\]$/m; +export const arrayRegEx = /\[(?[\w\s\\[\]]+);\s*(?[0-9]+)\]/; const array: Matcher = (type) => arrayRegEx.test(type); const struct: Matcher = (type) => - /^struct (.+::)?(.+)$/m.test(type) && + /^struct .+$/m.test(type) && !/^struct (std::.*)?(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m.test(type); const assetId: Matcher = (type) => type === 'struct std::asset_id::AssetId'; const b512: Matcher = (type) => type === 'struct std::b512::B512'; @@ -59,8 +59,7 @@ const vector: Matcher = (type) => type === 'struct std::vec::Vec'; const option: Matcher = (type) => type === 'enum std::option::Option'; const result: Matcher = (type) => type === 'enum std::result::Result'; -const enumMatcher: Matcher = (type) => - !option(type) && !result(type) && /^enum (.+::)?(.+)$/m.test(type); +const enumMatcher: Matcher = (type) => !option(type) && !result(type) && /^enum .*$/m.test(type); const rawUntypedPtr: Matcher = (type) => type === 'raw untyped ptr'; const rawUntypedSlice: Matcher = (type) => type === 'raw untyped slice'; From d7ea3deab6c3ed7bf9478feff49611b4d143c70e Mon Sep 17 00:00:00 2001 From: nedsalk Date: Tue, 10 Sep 2024 10:34:21 +0200 Subject: [PATCH 05/16] fix tupleRegex --- packages/abi/src/matchers/sway-type-matchers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index 161eee0fb45..24ecb19113c 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -41,7 +41,7 @@ const generic: Matcher = (type) => genericRegEx.test(type); export const stringRegEx = /str\[(?[0-9]+)\]/; const string: Matcher = (type) => stringRegEx.test(type); -export const tupleRegEx = /^\((?.*)\)$/m; +export const tupleRegEx = /^\((?.+)\)$/m; const tuple: Matcher = (type) => tupleRegEx.test(type); export const arrayRegEx = /\[(?[\w\s\\[\]]+);\s*(?[0-9]+)\]/; @@ -49,7 +49,7 @@ const array: Matcher = (type) => arrayRegEx.test(type); const struct: Matcher = (type) => /^struct .+$/m.test(type) && - !/^struct (std::.*)?(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m.test(type); + !/^struct std::.*(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m.test(type); const assetId: Matcher = (type) => type === 'struct std::asset_id::AssetId'; const b512: Matcher = (type) => type === 'struct std::b512::B512'; const bytes: Matcher = (type) => type === 'struct std::bytes::Bytes'; From d2b17071d617041c43d2c39fd0fd585c0fdd9678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Salki=C4=87?= Date: Tue, 10 Sep 2024 11:31:30 +0200 Subject: [PATCH 06/16] Update sway-type-matchers.ts --- packages/abi/src/matchers/sway-type-matchers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index 24ecb19113c..72e7ecaf083 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -35,7 +35,7 @@ const u64: Matcher = (type) => type === 'u64'; const u256: Matcher = (type) => type === 'u256'; const b256: Matcher = (type) => type === 'b256'; -export const genericRegEx = /^generic.+$/; +export const genericRegEx = /^generic ([^\s]+)$/m; const generic: Matcher = (type) => genericRegEx.test(type); export const stringRegEx = /str\[(?[0-9]+)\]/; From 19f793df60c5e9eecfe44d13b8c2a6130da13dc9 Mon Sep 17 00:00:00 2001 From: nedsalk Date: Tue, 10 Sep 2024 13:59:16 +0200 Subject: [PATCH 07/16] refactor out parameterized tests --- .../src/matchers/sway-type-matchers.test.ts | 245 +++++++++++++++--- 1 file changed, 206 insertions(+), 39 deletions(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.test.ts b/packages/abi/src/matchers/sway-type-matchers.test.ts index e86bfacc4b6..7be02d3334e 100644 --- a/packages/abi/src/matchers/sway-type-matchers.test.ts +++ b/packages/abi/src/matchers/sway-type-matchers.test.ts @@ -1,45 +1,212 @@ -import { swayTypeMatchers } from './sway-type-matchers'; - -const testCases: Record = { - empty: '()', - generic: 'generic T', - bool: 'bool', - u8: 'u8', - u16: 'u16', - u32: 'u32', - u64: 'u64', - u256: 'u256', - b256: 'b256', - - string: 'str[5]', - array: '[_; 2]', - tuple: '(_, _, _)', - - struct: 'struct MyStruct', - assetId: 'struct std::asset_id::AssetId', - b512: 'struct std::b512::B512', - bytes: 'struct std::bytes::Bytes', - evmAddress: 'struct std::vm::evm::evm_address::EvmAddress', - stdString: 'struct std::string::String', - vector: 'struct std::vec::Vec', - - enum: 'enum MyEnum', - option: 'enum std::option::Option', - result: 'enum std::result::Result', - - rawUntypedPtr: 'raw untyped ptr', - rawUntypedSlice: 'raw untyped slice', -}; +import { swayTypeMatcherEntries, swayTypeMatchers } from './sway-type-matchers'; -describe('sway type matchers', () => { - test.each(Object.entries(testCases))('%s - %s', (type, value) => { - expect(swayTypeMatchers[type as keyof typeof swayTypeMatchers](value)).toEqual(true); - - // verify that it doesn't match any other type - const allOtherMatchers = Object.entries(swayTypeMatchers).filter(([key]) => key !== type); - allOtherMatchers.forEach(([, matcher]) => { +function verifyOtherMatchersDontMatch(type: keyof typeof swayTypeMatchers, value: string) { + swayTypeMatcherEntries + .filter(([key]) => key !== type) + .forEach(([, matcher]) => { expect(matcher(value)).toEqual(false); }); +} + +describe('sway type matchers', () => { + test('empty', () => { + const key = 'empty'; + const value = '()'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('bool', () => { + const key = 'bool'; + const value = 'bool'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('u8', () => { + const key = 'u8'; + const value = 'u8'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('u16', () => { + const key = 'u16'; + const value = 'u16'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('u32', () => { + const key = 'u32'; + const value = 'u32'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('u64', () => { + const key = 'u64'; + const value = 'u64'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('u256', () => { + const key = 'u256'; + const value = 'u256'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('b256', () => { + const key = 'b256'; + const value = 'b256'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('string', () => { + const key = 'string'; + const value = 'str[5]'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('array', () => { + const key = 'array'; + const value = '[_; 3]'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('tuple', () => { + const key = 'tuple'; + const value = '(_, _, _)'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('struct', () => { + const key = 'struct'; + const value = 'struct MyStruct'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('assetId', () => { + const key = 'assetId'; + const value = 'struct std::asset_id::AssetId'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('b512', () => { + const key = 'b512'; + const value = 'struct std::b512::B512'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('assetId', () => { + const key = 'assetId'; + const value = 'struct std::asset_id::AssetId'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('bytes', () => { + const key = 'bytes'; + const value = 'struct std::bytes::Bytes'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('stdString', () => { + const key = 'stdString'; + const value = 'struct std::string::String'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('evmAddress', () => { + const key = 'evmAddress'; + const value = 'struct std::vm::evm::evm_address::EvmAddress'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('vector', () => { + const key = 'vector'; + const value = 'struct std::vec::Vec'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('enum', () => { + const key = 'enum'; + const value = 'enum MyEnum'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('option', () => { + const key = 'option'; + const value = 'enum std::option::Option'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('result', () => { + const key = 'result'; + const value = 'enum std::result::Result'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('rawUntypedPtr', () => { + const key = 'rawUntypedPtr'; + const value = 'raw untyped ptr'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('rawUntypedSlice', () => { + const key = 'rawUntypedSlice'; + const value = 'raw untyped slice'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); + }); + + test('generic', () => { + const key = 'generic'; + const value = 'generic T'; + + expect(swayTypeMatchers[key](value)).toEqual(true); + verifyOtherMatchersDontMatch(key, value); }); test('raw vector is not interpreted as vector', () => { From 7efdf8d9d5f40b54fe3a36f41c944d0a94884e78 Mon Sep 17 00:00:00 2001 From: nedsalk Date: Tue, 10 Sep 2024 15:18:55 +0200 Subject: [PATCH 08/16] add test tags --- packages/abi/src/matchers/sway-type-matchers.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/abi/src/matchers/sway-type-matchers.test.ts b/packages/abi/src/matchers/sway-type-matchers.test.ts index 7be02d3334e..a7b45caafdd 100644 --- a/packages/abi/src/matchers/sway-type-matchers.test.ts +++ b/packages/abi/src/matchers/sway-type-matchers.test.ts @@ -8,6 +8,10 @@ function verifyOtherMatchersDontMatch(type: keyof typeof swayTypeMatchers, value }); } +/** + * @group node + * @group browser + */ describe('sway type matchers', () => { test('empty', () => { const key = 'empty'; From 7ef45a33cd6e8c1ffc9bf5572cfb21034379c7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Salki=C4=87?= Date: Wed, 11 Sep 2024 09:08:01 +0200 Subject: [PATCH 09/16] Update packages/abi/src/matchers/sway-type-matchers.ts Co-authored-by: Peter Smith --- packages/abi/src/matchers/sway-type-matchers.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index 72e7ecaf083..3bc5b3c6b3c 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -35,17 +35,17 @@ const u64: Matcher = (type) => type === 'u64'; const u256: Matcher = (type) => type === 'u256'; const b256: Matcher = (type) => type === 'b256'; -export const genericRegEx = /^generic ([^\s]+)$/m; -const generic: Matcher = (type) => genericRegEx.test(type); +export const GENERIC_REGEX = /^generic ([^\s]+)$/m; +const generic: Matcher = (type) => GENERIC_REGEX.test(type); -export const stringRegEx = /str\[(?[0-9]+)\]/; -const string: Matcher = (type) => stringRegEx.test(type); +export const STRING_REGEX = /str\[(?[0-9]+)\]/; +const string: Matcher = (type) => STRING_REGEX.test(type); -export const tupleRegEx = /^\((?.+)\)$/m; -const tuple: Matcher = (type) => tupleRegEx.test(type); +export const TUPLE_REGEX = /^\((?.+)\)$/m; +const tuple: Matcher = (type) => TUPLE_REGEX.test(type); -export const arrayRegEx = /\[(?[\w\s\\[\]]+);\s*(?[0-9]+)\]/; -const array: Matcher = (type) => arrayRegEx.test(type); +export const ARRAY_REGEX = /\[(?[\w\s\\[\]]+);\s*(?[0-9]+)\]/; +const array: Matcher = (type) => ARRAY_REGEX.test(type); const struct: Matcher = (type) => /^struct .+$/m.test(type) && From 530a28d3b747af340eb0bbd8c60f5b13c7dfa6b6 Mon Sep 17 00:00:00 2001 From: nedsalk Date: Wed, 11 Sep 2024 09:38:31 +0200 Subject: [PATCH 10/16] add `createMatcher` utility --- .../src/matchers/sway-type-matchers.test.ts | 187 ++++++++++-------- .../abi/src/matchers/sway-type-matchers.ts | 16 +- 2 files changed, 122 insertions(+), 81 deletions(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.test.ts b/packages/abi/src/matchers/sway-type-matchers.test.ts index a7b45caafdd..95ea3a1ae28 100644 --- a/packages/abi/src/matchers/sway-type-matchers.test.ts +++ b/packages/abi/src/matchers/sway-type-matchers.test.ts @@ -1,10 +1,37 @@ -import { swayTypeMatcherEntries, swayTypeMatchers } from './sway-type-matchers'; +import { createMatcher, swayTypeMatchers } from './sway-type-matchers'; + +const matcher = createMatcher<`${string}-matched`>({ + string: 'string-matched', + empty: 'empty-matched', + bool: 'bool-matched', + u8: 'u8-matched', + u16: 'u16-matched', + u32: 'u32-matched', + u64: 'u64-matched', + u256: 'u256-matched', + b256: 'b256-matched', + generic: 'generic-matched', + stdString: 'stdString-matched', + option: 'option-matched', + result: 'result-matched', + enum: 'enum-matched', + struct: 'struct-matched', + b512: 'b512-matched', + bytes: 'bytes-matched', + vector: 'vector-matched', + tuple: 'tuple-matched', + array: 'array-matched', + assetId: 'assetId-matched', + evmAddress: 'evmAddress-matched', + rawUntypedPtr: 'rawUntypedPtr-matched', + rawUntypedSlice: 'rawUntypedSlice-matched', +}); -function verifyOtherMatchersDontMatch(type: keyof typeof swayTypeMatchers, value: string) { - swayTypeMatcherEntries +function verifyOtherMatchersDontMatch(type: keyof typeof swayTypeMatchers) { + Object.entries(swayTypeMatchers) .filter(([key]) => key !== type) - .forEach(([, matcher]) => { - expect(matcher(value)).toEqual(false); + .forEach(([key]) => { + expect(matcher({ swayType: key })).throws(`Matcher not found for ${key}`); }); } @@ -15,202 +42,202 @@ function verifyOtherMatchersDontMatch(type: keyof typeof swayTypeMatchers, value describe('sway type matchers', () => { test('empty', () => { const key = 'empty'; - const value = '()'; + const swayType = '()'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('bool', () => { const key = 'bool'; - const value = 'bool'; + const swayType = 'bool'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('u8', () => { const key = 'u8'; - const value = 'u8'; + const swayType = 'u8'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('u16', () => { const key = 'u16'; - const value = 'u16'; + const swayType = 'u16'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('u32', () => { const key = 'u32'; - const value = 'u32'; + const swayType = 'u32'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('u64', () => { const key = 'u64'; - const value = 'u64'; + const swayType = 'u64'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('u256', () => { const key = 'u256'; - const value = 'u256'; + const swayType = 'u256'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('b256', () => { const key = 'b256'; - const value = 'b256'; + const swayType = 'b256'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('string', () => { const key = 'string'; - const value = 'str[5]'; + const swayType = 'str[5]'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('array', () => { const key = 'array'; - const value = '[_; 3]'; + const swayType = '[_; 3]'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('tuple', () => { const key = 'tuple'; - const value = '(_, _, _)'; + const swayType = '(_, _, _)'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('struct', () => { const key = 'struct'; - const value = 'struct MyStruct'; + const swayType = 'struct MyStruct'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('assetId', () => { const key = 'assetId'; - const value = 'struct std::asset_id::AssetId'; + const swayType = 'struct std::asset_id::AssetId'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('b512', () => { const key = 'b512'; - const value = 'struct std::b512::B512'; + const swayType = 'struct std::b512::B512'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('assetId', () => { const key = 'assetId'; - const value = 'struct std::asset_id::AssetId'; + const swayType = 'struct std::asset_id::AssetId'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('bytes', () => { const key = 'bytes'; - const value = 'struct std::bytes::Bytes'; + const swayType = 'struct std::bytes::Bytes'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('stdString', () => { const key = 'stdString'; - const value = 'struct std::string::String'; + const swayType = 'struct std::string::String'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('evmAddress', () => { const key = 'evmAddress'; - const value = 'struct std::vm::evm::evm_address::EvmAddress'; + const swayType = 'struct std::vm::evm::evm_address::EvmAddress'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('vector', () => { const key = 'vector'; - const value = 'struct std::vec::Vec'; + const swayType = 'struct std::vec::Vec'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('enum', () => { const key = 'enum'; - const value = 'enum MyEnum'; + const swayType = 'enum MyEnum'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('option', () => { const key = 'option'; - const value = 'enum std::option::Option'; + const swayType = 'enum std::option::Option'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('result', () => { const key = 'result'; - const value = 'enum std::result::Result'; + const swayType = 'enum std::result::Result'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('rawUntypedPtr', () => { const key = 'rawUntypedPtr'; - const value = 'raw untyped ptr'; + const swayType = 'raw untyped ptr'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('rawUntypedSlice', () => { const key = 'rawUntypedSlice'; - const value = 'raw untyped slice'; + const swayType = 'raw untyped slice'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('generic', () => { const key = 'generic'; - const value = 'generic T'; + const swayType = 'generic T'; - expect(swayTypeMatchers[key](value)).toEqual(true); - verifyOtherMatchersDontMatch(key, value); + expect(matcher({ swayType })).toEqual(`${key}-matched`); + verifyOtherMatchersDontMatch(key); }); test('raw vector is not interpreted as vector', () => { diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index 3bc5b3c6b3c..15043a17d6e 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -95,4 +95,18 @@ export const swayTypeMatchers: Record = { rawUntypedSlice, }; -export const swayTypeMatcherEntries = Object.entries(swayTypeMatchers); +const swayTypeMatcherEntries = Object.entries(swayTypeMatchers); + +export function createMatcher(mappings: Record) { + return (opts: { swayType: string }): T => { + const { swayType } = opts; + + for (const [key, matcher] of swayTypeMatcherEntries) { + if (matcher(swayType)) { + return mappings[key as SwayType]; + } + } + + throw new Error(`Matcher not found for ${swayType}`); + }; +} From 8c4c90613ea5f04c08f662a98f9867e94bfd8f49 Mon Sep 17 00:00:00 2001 From: nedsalk Date: Wed, 11 Sep 2024 09:38:43 +0200 Subject: [PATCH 11/16] remove typers and coders --- packages/abi/src/coder/encoding/v1/coders/empty-coder.ts | 3 --- packages/abi/src/coder/encoding/v1/coders/enum-coder.ts | 3 --- packages/abi/src/coder/encoding/v1/coders/option-coder.ts | 3 --- packages/abi/src/coder/encoding/v1/coders/types.ts | 1 - packages/abi/src/coder/encoding/v1/coders/u8-coder.ts | 3 --- packages/abi/src/gen/abi-gen-types.ts | 1 - packages/abi/src/gen/typers/empty-typer.ts | 3 --- packages/abi/src/gen/typers/enum-typer.ts | 3 --- packages/abi/src/gen/typers/option-typer.ts | 3 --- packages/abi/src/gen/typers/result-typer.ts | 3 --- packages/abi/src/gen/typers/types.ts | 1 - packages/abi/src/gen/typers/u8-typer.ts | 3 --- 12 files changed, 30 deletions(-) delete mode 100644 packages/abi/src/coder/encoding/v1/coders/empty-coder.ts delete mode 100644 packages/abi/src/coder/encoding/v1/coders/enum-coder.ts delete mode 100644 packages/abi/src/coder/encoding/v1/coders/option-coder.ts delete mode 100644 packages/abi/src/coder/encoding/v1/coders/types.ts delete mode 100644 packages/abi/src/coder/encoding/v1/coders/u8-coder.ts delete mode 100644 packages/abi/src/gen/abi-gen-types.ts delete mode 100644 packages/abi/src/gen/typers/empty-typer.ts delete mode 100644 packages/abi/src/gen/typers/enum-typer.ts delete mode 100644 packages/abi/src/gen/typers/option-typer.ts delete mode 100644 packages/abi/src/gen/typers/result-typer.ts delete mode 100644 packages/abi/src/gen/typers/types.ts delete mode 100644 packages/abi/src/gen/typers/u8-typer.ts diff --git a/packages/abi/src/coder/encoding/v1/coders/empty-coder.ts b/packages/abi/src/coder/encoding/v1/coders/empty-coder.ts deleted file mode 100644 index d9e1e211e30..00000000000 --- a/packages/abi/src/coder/encoding/v1/coders/empty-coder.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { Coder } from './types'; - -export class EmptyCoder implements Coder {} diff --git a/packages/abi/src/coder/encoding/v1/coders/enum-coder.ts b/packages/abi/src/coder/encoding/v1/coders/enum-coder.ts deleted file mode 100644 index 65073698fd4..00000000000 --- a/packages/abi/src/coder/encoding/v1/coders/enum-coder.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { Coder } from './types'; - -export class EnumCoder implements Coder {} diff --git a/packages/abi/src/coder/encoding/v1/coders/option-coder.ts b/packages/abi/src/coder/encoding/v1/coders/option-coder.ts deleted file mode 100644 index 2cc5c3a00c3..00000000000 --- a/packages/abi/src/coder/encoding/v1/coders/option-coder.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { Coder } from './types'; - -export class OptionCoder implements Coder {} diff --git a/packages/abi/src/coder/encoding/v1/coders/types.ts b/packages/abi/src/coder/encoding/v1/coders/types.ts deleted file mode 100644 index 6f640be46ef..00000000000 --- a/packages/abi/src/coder/encoding/v1/coders/types.ts +++ /dev/null @@ -1 +0,0 @@ -export interface Coder {} diff --git a/packages/abi/src/coder/encoding/v1/coders/u8-coder.ts b/packages/abi/src/coder/encoding/v1/coders/u8-coder.ts deleted file mode 100644 index 70ca4b8397d..00000000000 --- a/packages/abi/src/coder/encoding/v1/coders/u8-coder.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { Coder } from './types'; - -export class U8Coder implements Coder {} diff --git a/packages/abi/src/gen/abi-gen-types.ts b/packages/abi/src/gen/abi-gen-types.ts deleted file mode 100644 index 10051c76805..00000000000 --- a/packages/abi/src/gen/abi-gen-types.ts +++ /dev/null @@ -1 +0,0 @@ -// Placeholder diff --git a/packages/abi/src/gen/typers/empty-typer.ts b/packages/abi/src/gen/typers/empty-typer.ts deleted file mode 100644 index b0a7a900bda..00000000000 --- a/packages/abi/src/gen/typers/empty-typer.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { Typer } from './types'; - -export class EmptyTyper implements Typer {} diff --git a/packages/abi/src/gen/typers/enum-typer.ts b/packages/abi/src/gen/typers/enum-typer.ts deleted file mode 100644 index d91804109b0..00000000000 --- a/packages/abi/src/gen/typers/enum-typer.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { Typer } from './types'; - -export class EnumTyper implements Typer {} diff --git a/packages/abi/src/gen/typers/option-typer.ts b/packages/abi/src/gen/typers/option-typer.ts deleted file mode 100644 index 56e0ef31281..00000000000 --- a/packages/abi/src/gen/typers/option-typer.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { Typer } from './types'; - -export class OptionTyper implements Typer {} diff --git a/packages/abi/src/gen/typers/result-typer.ts b/packages/abi/src/gen/typers/result-typer.ts deleted file mode 100644 index e57ec0fc2a3..00000000000 --- a/packages/abi/src/gen/typers/result-typer.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { Typer } from './types'; - -export class ResultTyper implements Typer {} diff --git a/packages/abi/src/gen/typers/types.ts b/packages/abi/src/gen/typers/types.ts deleted file mode 100644 index 25b53b4f143..00000000000 --- a/packages/abi/src/gen/typers/types.ts +++ /dev/null @@ -1 +0,0 @@ -export interface Typer {} diff --git a/packages/abi/src/gen/typers/u8-typer.ts b/packages/abi/src/gen/typers/u8-typer.ts deleted file mode 100644 index 924a83da79f..00000000000 --- a/packages/abi/src/gen/typers/u8-typer.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { Typer } from './types'; - -export class U8Typer implements Typer {} From 43f35b32f00725ac5fb5431c957d1e92922c7450 Mon Sep 17 00:00:00 2001 From: nedsalk Date: Wed, 11 Sep 2024 09:39:54 +0200 Subject: [PATCH 12/16] remove irrelevant changes --- packages/abi/src/coder/abi-coder.ts | 11 +---- .../src/coder/encoding/v1/coder-matcher.ts | 46 ------------------- packages/abi/src/gen/abi-gen-types.ts | 1 + packages/abi/src/gen/typer-matcher.ts | 45 ------------------ 4 files changed, 2 insertions(+), 101 deletions(-) delete mode 100644 packages/abi/src/coder/encoding/v1/coder-matcher.ts create mode 100644 packages/abi/src/gen/abi-gen-types.ts delete mode 100644 packages/abi/src/gen/typer-matcher.ts diff --git a/packages/abi/src/coder/abi-coder.ts b/packages/abi/src/coder/abi-coder.ts index 49cd447eff9..38325e4eff3 100644 --- a/packages/abi/src/coder/abi-coder.ts +++ b/packages/abi/src/coder/abi-coder.ts @@ -1,11 +1,2 @@ -import { getCoder } from './encoding/v1/coder-matcher'; -import type { Coder } from './encoding/v1/coders/types'; - // Placeholder -export class AbiCoder { - public encode(type: string, value: unknown) { - const coder = getCoder(type) as Coder & { encode: (value: unknown) => unknown }; - - return coder.encode(value); - } -} +export class AbiCoder {} diff --git a/packages/abi/src/coder/encoding/v1/coder-matcher.ts b/packages/abi/src/coder/encoding/v1/coder-matcher.ts deleted file mode 100644 index d25bdcfbbc1..00000000000 --- a/packages/abi/src/coder/encoding/v1/coder-matcher.ts +++ /dev/null @@ -1,46 +0,0 @@ -import type { swayTypeMatchers } from '../../../matchers/sway-type-matchers'; -import { swayTypeMatcherEntries } from '../../../matchers/sway-type-matchers'; - -import { EmptyCoder } from './coders/empty-coder'; -import { EnumCoder } from './coders/enum-coder'; -import { OptionCoder } from './coders/option-coder'; -import type { Coder } from './coders/types'; -import { U8Coder } from './coders/u8-coder'; - -// Coder | undefined for proof-of-concept, it'll become only Coder later -export const coderMatcher: Record = { - empty: EmptyCoder, - u8: U8Coder, - enum: EnumCoder, - result: EnumCoder, - option: OptionCoder, - string: undefined, - bool: undefined, - u16: undefined, - u32: undefined, - u64: undefined, - u256: undefined, - b256: undefined, - generic: undefined, - stdString: undefined, - struct: undefined, - b512: undefined, - bytes: undefined, - vector: undefined, - tuple: undefined, - array: undefined, - assetId: undefined, - evmAddress: undefined, - rawUntypedPtr: undefined, - rawUntypedSlice: undefined, -}; - -export function getCoder(type: string) { - for (const [key, matcher] of swayTypeMatcherEntries) { - if (matcher(type)) { - return coderMatcher[key as keyof typeof coderMatcher]; - } - } - - throw new Error("couldn't find the coder"); -} diff --git a/packages/abi/src/gen/abi-gen-types.ts b/packages/abi/src/gen/abi-gen-types.ts new file mode 100644 index 00000000000..10051c76805 --- /dev/null +++ b/packages/abi/src/gen/abi-gen-types.ts @@ -0,0 +1 @@ +// Placeholder diff --git a/packages/abi/src/gen/typer-matcher.ts b/packages/abi/src/gen/typer-matcher.ts deleted file mode 100644 index 33985a647b5..00000000000 --- a/packages/abi/src/gen/typer-matcher.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { swayTypeMatcherEntries, type swayTypeMatchers } from '../matchers/sway-type-matchers'; - -import { EmptyTyper } from './typers/empty-typer'; -import { EnumTyper } from './typers/enum-typer'; -import { OptionTyper } from './typers/option-typer'; -import { ResultTyper } from './typers/result-typer'; -import type { Typer } from './typers/types'; -import { U8Typer } from './typers/u8-typer'; - -export const typerMatcher: Record = { - empty: EmptyTyper, - u8: U8Typer, - enum: EnumTyper, - result: ResultTyper, - option: OptionTyper, - string: undefined, - bool: undefined, - u16: undefined, - u32: undefined, - u64: undefined, - u256: undefined, - b256: undefined, - generic: undefined, - stdString: undefined, - struct: undefined, - b512: undefined, - bytes: undefined, - vector: undefined, - tuple: undefined, - array: undefined, - assetId: undefined, - evmAddress: undefined, - rawUntypedPtr: undefined, - rawUntypedSlice: undefined, -}; - -export function getTyper(type: string) { - for (const [key, matcher] of swayTypeMatcherEntries) { - if (matcher(type)) { - return typerMatcher[key as keyof typeof typerMatcher]; - } - } - - throw new Error("couldn't find the coder"); -} From 185c46b6dc903cdde701efdab2651afca64d6e6a Mon Sep 17 00:00:00 2001 From: nedsalk Date: Wed, 11 Sep 2024 10:22:28 +0200 Subject: [PATCH 13/16] fix test --- .../src/matchers/sway-type-matchers.test.ts | 77 ++++++++++--------- .../abi/src/matchers/sway-type-matchers.ts | 10 ++- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.test.ts b/packages/abi/src/matchers/sway-type-matchers.test.ts index 95ea3a1ae28..739861c3b13 100644 --- a/packages/abi/src/matchers/sway-type-matchers.test.ts +++ b/packages/abi/src/matchers/sway-type-matchers.test.ts @@ -1,6 +1,7 @@ -import { createMatcher, swayTypeMatchers } from './sway-type-matchers'; +import type { SwayType, swayTypeMatchers } from './sway-type-matchers'; +import { createMatcher } from './sway-type-matchers'; -const matcher = createMatcher<`${string}-matched`>({ +const testMappings: Record = { string: 'string-matched', empty: 'empty-matched', bool: 'bool-matched', @@ -25,14 +26,18 @@ const matcher = createMatcher<`${string}-matched`>({ evmAddress: 'evmAddress-matched', rawUntypedPtr: 'rawUntypedPtr-matched', rawUntypedSlice: 'rawUntypedSlice-matched', -}); +}; + +const matcher = createMatcher(testMappings); + +function verifyOtherMatchersDontMatch(key: keyof typeof testMappings, swayType: string) { + const testMappingsWithoutKey = Object.fromEntries( + Object.entries(testMappings).filter(([k]) => k !== key) + ); -function verifyOtherMatchersDontMatch(type: keyof typeof swayTypeMatchers) { - Object.entries(swayTypeMatchers) - .filter(([key]) => key !== type) - .forEach(([key]) => { - expect(matcher({ swayType: key })).throws(`Matcher not found for ${key}`); - }); + const verifier = createMatcher(testMappingsWithoutKey as Record); + + expect(() => verifier({ swayType })).toThrow(`Matcher not found for sway type ${swayType}.`); } /** @@ -45,7 +50,7 @@ describe('sway type matchers', () => { const swayType = '()'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('bool', () => { @@ -53,7 +58,7 @@ describe('sway type matchers', () => { const swayType = 'bool'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('u8', () => { @@ -61,7 +66,7 @@ describe('sway type matchers', () => { const swayType = 'u8'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('u16', () => { @@ -69,7 +74,7 @@ describe('sway type matchers', () => { const swayType = 'u16'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('u32', () => { @@ -77,7 +82,7 @@ describe('sway type matchers', () => { const swayType = 'u32'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('u64', () => { @@ -85,7 +90,7 @@ describe('sway type matchers', () => { const swayType = 'u64'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('u256', () => { @@ -93,7 +98,7 @@ describe('sway type matchers', () => { const swayType = 'u256'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('b256', () => { @@ -101,7 +106,7 @@ describe('sway type matchers', () => { const swayType = 'b256'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('string', () => { @@ -109,7 +114,7 @@ describe('sway type matchers', () => { const swayType = 'str[5]'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('array', () => { @@ -117,7 +122,7 @@ describe('sway type matchers', () => { const swayType = '[_; 3]'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('tuple', () => { @@ -125,7 +130,7 @@ describe('sway type matchers', () => { const swayType = '(_, _, _)'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('struct', () => { @@ -133,7 +138,7 @@ describe('sway type matchers', () => { const swayType = 'struct MyStruct'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('assetId', () => { @@ -141,7 +146,7 @@ describe('sway type matchers', () => { const swayType = 'struct std::asset_id::AssetId'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('b512', () => { @@ -149,7 +154,7 @@ describe('sway type matchers', () => { const swayType = 'struct std::b512::B512'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('assetId', () => { @@ -157,7 +162,7 @@ describe('sway type matchers', () => { const swayType = 'struct std::asset_id::AssetId'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('bytes', () => { @@ -165,7 +170,7 @@ describe('sway type matchers', () => { const swayType = 'struct std::bytes::Bytes'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('stdString', () => { @@ -173,7 +178,7 @@ describe('sway type matchers', () => { const swayType = 'struct std::string::String'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('evmAddress', () => { @@ -181,7 +186,7 @@ describe('sway type matchers', () => { const swayType = 'struct std::vm::evm::evm_address::EvmAddress'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('vector', () => { @@ -189,7 +194,7 @@ describe('sway type matchers', () => { const swayType = 'struct std::vec::Vec'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('enum', () => { @@ -197,7 +202,7 @@ describe('sway type matchers', () => { const swayType = 'enum MyEnum'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('option', () => { @@ -205,7 +210,7 @@ describe('sway type matchers', () => { const swayType = 'enum std::option::Option'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('result', () => { @@ -213,7 +218,7 @@ describe('sway type matchers', () => { const swayType = 'enum std::result::Result'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('rawUntypedPtr', () => { @@ -221,7 +226,7 @@ describe('sway type matchers', () => { const swayType = 'raw untyped ptr'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('rawUntypedSlice', () => { @@ -229,7 +234,7 @@ describe('sway type matchers', () => { const swayType = 'raw untyped slice'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); + verifyOtherMatchersDontMatch(key, swayType); }); test('generic', () => { @@ -237,10 +242,6 @@ describe('sway type matchers', () => { const swayType = 'generic T'; expect(matcher({ swayType })).toEqual(`${key}-matched`); - verifyOtherMatchersDontMatch(key); - }); - - test('raw vector is not interpreted as vector', () => { - expect(swayTypeMatchers.vector('struct std::vec::RawVec')).toEqual(false); + verifyOtherMatchersDontMatch(key, swayType); }); }); diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index 15043a17d6e..3c2216159f2 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -1,4 +1,4 @@ -type SwayType = +export type SwayType = | 'empty' | 'bool' | 'u8' @@ -103,10 +103,14 @@ export function createMatcher(mappings: Record) { for (const [key, matcher] of swayTypeMatcherEntries) { if (matcher(swayType)) { - return mappings[key as SwayType]; + const mapping = mappings[key as SwayType]; + if (mapping) { + return mapping; + } + break; } } - throw new Error(`Matcher not found for ${swayType}`); + throw new Error(`Matcher not found for sway type ${swayType}.`); }; } From 8fee7a25909434b8a5151e92599ebcbdfc9f29be Mon Sep 17 00:00:00 2001 From: nedsalk Date: Wed, 11 Sep 2024 12:12:38 +0200 Subject: [PATCH 14/16] rename `empty` to `void` --- packages/abi/src/matchers/sway-type-matchers.test.ts | 6 +++--- packages/abi/src/matchers/sway-type-matchers.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.test.ts b/packages/abi/src/matchers/sway-type-matchers.test.ts index 739861c3b13..314d01e70af 100644 --- a/packages/abi/src/matchers/sway-type-matchers.test.ts +++ b/packages/abi/src/matchers/sway-type-matchers.test.ts @@ -3,7 +3,7 @@ import { createMatcher } from './sway-type-matchers'; const testMappings: Record = { string: 'string-matched', - empty: 'empty-matched', + void: 'void-matched', bool: 'bool-matched', u8: 'u8-matched', u16: 'u16-matched', @@ -45,8 +45,8 @@ function verifyOtherMatchersDontMatch(key: keyof typeof testMappings, swayType: * @group browser */ describe('sway type matchers', () => { - test('empty', () => { - const key = 'empty'; + test('void', () => { + const key = 'void'; const swayType = '()'; expect(matcher({ swayType })).toEqual(`${key}-matched`); diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index 3c2216159f2..43eec196937 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -1,5 +1,5 @@ export type SwayType = - | 'empty' + | 'void' | 'bool' | 'u8' | 'u16' @@ -26,7 +26,7 @@ export type SwayType = type Matcher = (type: string) => boolean; -const empty: Matcher = (type) => type === '()'; +const voidMatcher: Matcher = (type) => type === '()'; const bool: Matcher = (type) => type === 'bool'; const u8: Matcher = (type) => type === 'u8'; const u16: Matcher = (type) => type === 'u16'; @@ -65,7 +65,7 @@ const rawUntypedPtr: Matcher = (type) => type === 'raw untyped ptr'; const rawUntypedSlice: Matcher = (type) => type === 'raw untyped slice'; export const swayTypeMatchers: Record = { - empty, + void: voidMatcher, generic, bool, u8, From 98debae9e2af55ccfcc8b2dd16df82e83cc5cf29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Salki=C4=87?= Date: Wed, 11 Sep 2024 12:51:58 +0200 Subject: [PATCH 15/16] Update packages/abi/src/matchers/sway-type-matchers.ts Co-authored-by: Anderson Arboleya --- packages/abi/src/matchers/sway-type-matchers.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index 43eec196937..5989b0fa7dc 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -47,7 +47,9 @@ const tuple: Matcher = (type) => TUPLE_REGEX.test(type); export const ARRAY_REGEX = /\[(?[\w\s\\[\]]+);\s*(?[0-9]+)\]/; const array: Matcher = (type) => ARRAY_REGEX.test(type); -const struct: Matcher = (type) => +const STRUCT_REGEX = /^struct .+$/m; +const STRUCT_STD_REGEX = /^struct std::.*(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m; +const struct: Matcher = (type) => STRUCT_REGEX.test(type) && !STRUCT_STD_REGEX.test(type); /^struct .+$/m.test(type) && !/^struct std::.*(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m.test(type); const assetId: Matcher = (type) => type === 'struct std::asset_id::AssetId'; From 199550157d236bb423e725b4e806467cba0c05d0 Mon Sep 17 00:00:00 2001 From: nedsalk Date: Wed, 11 Sep 2024 13:00:47 +0200 Subject: [PATCH 16/16] fix compilation --- packages/abi/src/matchers/sway-type-matchers.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/abi/src/matchers/sway-type-matchers.ts b/packages/abi/src/matchers/sway-type-matchers.ts index 5989b0fa7dc..22d68d6804a 100644 --- a/packages/abi/src/matchers/sway-type-matchers.ts +++ b/packages/abi/src/matchers/sway-type-matchers.ts @@ -48,10 +48,9 @@ export const ARRAY_REGEX = /\[(?[\w\s\\[\]]+);\s*(?[0-9]+)\]/; const array: Matcher = (type) => ARRAY_REGEX.test(type); const STRUCT_REGEX = /^struct .+$/m; -const STRUCT_STD_REGEX = /^struct std::.*(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m; +const STRUCT_STD_REGEX = + /^struct std::.*(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m; const struct: Matcher = (type) => STRUCT_REGEX.test(type) && !STRUCT_STD_REGEX.test(type); - /^struct .+$/m.test(type) && - !/^struct std::.*(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m.test(type); const assetId: Matcher = (type) => type === 'struct std::asset_id::AssetId'; const b512: Matcher = (type) => type === 'struct std::b512::B512'; const bytes: Matcher = (type) => type === 'struct std::bytes::Bytes';