diff --git a/src/types/convertor/argsConvertor.ts b/src/types/convertor/argsConvertor.ts index c4c639c..36932ed 100644 --- a/src/types/convertor/argsConvertor.ts +++ b/src/types/convertor/argsConvertor.ts @@ -2,9 +2,13 @@ * Convert Move type to TypeScript type, * for input arguments of view function or entry function. */ - import { AnyNumber, UnknownStruct } from '../common.js'; -import { MoveNonStructTypes, MovePrimitive } from '../moveTypes.js'; +import { + MoveObject, + MoveOption, + MovePrimitive, + MovePrimitiveMap, +} from '../moveTypes.js'; /** * Convert an array of input arguments type. @@ -16,46 +20,11 @@ export type ConvertArgs = T extends readonly [ ? [ConvertArgType, ...ConvertArgs] : []; -/** - * Internal - */ -type ConvertArgType = - TMoveType extends MoveNonStructTypes - ? // it's a non-struct type - ConvertNonStructArgType - : // it's a struct type - UnknownStruct; - -type ConvertPrimitiveArgType = - TMoveType extends 'bool' - ? boolean - : TMoveType extends 'u8' - ? number - : TMoveType extends 'u16' - ? number - : TMoveType extends 'u32' - ? number - : TMoveType extends 'u64' - ? AnyNumber - : TMoveType extends 'u128' - ? AnyNumber - : TMoveType extends 'u256' - ? AnyNumber - : TMoveType extends 'address' - ? `0x${string}` - : TMoveType extends '0x1::string::String' - ? string - : never; - -type ConvertNonStructArgType = - TMoveType extends MovePrimitive - ? ConvertPrimitiveArgType - : TMoveType extends `vector` - ? string | number[] | Uint8Array - : TMoveType extends `vector<${infer TInner}>` - ? ConvertArgType[] - : TMoveType extends `0x1::object::Object<${string}>` - ? `0x${string}` - : TMoveType extends `0x1::option::Option<${infer TInner}>` - ? ConvertArgType | undefined - : UnknownStruct; +// Internal +type ConvertArgType = TMoveType extends MovePrimitive + ? MovePrimitiveMap[TMoveType] + : TMoveType extends MoveObject + ? `0x${string}` + : TMoveType extends MoveOption + ? ConvertArgType | undefined + : UnknownStruct; diff --git a/src/types/convertor/returnConvertor.ts b/src/types/convertor/returnConvertor.ts index 46e1620..fff6ced 100644 --- a/src/types/convertor/returnConvertor.ts +++ b/src/types/convertor/returnConvertor.ts @@ -5,7 +5,12 @@ import { UnknownStruct } from '../common.js'; import { DefaultABITable } from '../defaultABITable.js'; -import { MoveNonStructTypes, MovePrimitive } from '../moveTypes.js'; +import { + MoveObject, + MoveOption, + MovePrimitive, + MovePrimitiveMap, +} from '../moveTypes.js'; import { ConvertStructFieldOptionType } from './structConvertor.js'; /** @@ -22,40 +27,10 @@ export type ConvertReturns = T extends readonly [ * Internal */ type ConvertReturnType = - TMoveType extends MoveNonStructTypes - ? // it's a non-struct type - ConvertNonStructReturnType - : // it's a struct type - UnknownStruct; - -type ConvertPrimitiveReturnType = - TMoveType extends 'bool' - ? boolean - : TMoveType extends 'u8' - ? number - : TMoveType extends 'u16' - ? number - : TMoveType extends 'u32' - ? number - : TMoveType extends 'u64' - ? string - : TMoveType extends 'u128' - ? string - : TMoveType extends 'u256' - ? string - : TMoveType extends 'address' - ? `0x${string}` - : TMoveType extends '0x1::string::String' - ? string - : never; - -type ConvertNonStructReturnType = TMoveType extends MovePrimitive - ? ConvertPrimitiveReturnType - : TMoveType extends `vector<${infer TInner}>` - ? ConvertReturnType[] - : TMoveType extends `0x1::object::Object<${string}>` - ? { inner: `0x${string}` } - : TMoveType extends `0x1::option::Option<${infer TInner}>` - ? ConvertStructFieldOptionType - : UnknownStruct; + ? MovePrimitiveMap[TMoveType] + : TMoveType extends MoveObject + ? { inner: `0x${string}` } + : TMoveType extends MoveOption + ? ConvertStructFieldOptionType + : UnknownStruct; diff --git a/src/types/convertor/structConvertor.ts b/src/types/convertor/structConvertor.ts index 15b2bbd..d4227ec 100644 --- a/src/types/convertor/structConvertor.ts +++ b/src/types/convertor/structConvertor.ts @@ -7,7 +7,13 @@ import { ExtractStructType, ResourceStructName, } from '../extractor/structExtractor.js'; -import { MoveNonStructTypes, MovePrimitive } from '../moveTypes.js'; +import { + MoveNonStructTypes, + MoveObject, + MovePrimitive, + MovePrimitiveMap, + MoveVector, +} from '../moveTypes.js'; // Convert a struct field Move type to a TypeScript type export type ConvertStructFieldType< @@ -19,38 +25,15 @@ export type ConvertStructFieldType< : // it's a struct type ConvertStructFieldStructType; -/** - * Internal - */ -type ConvertPrimitiveStructField = T extends 'bool' - ? boolean - : T extends 'u8' - ? number - : T extends 'u16' - ? number - : T extends 'u32' - ? number - : T extends 'u64' - ? string - : T extends 'u128' - ? string - : T extends 'u256' - ? string - : T extends 'address' - ? `0x${string}` - : T extends '0x1::string::String' - ? string - : never; - // Convert a struct field non-struct Move type to a TypeScript type type ConvertStructFieldNonStructType< TABITable extends ABITable, TMoveType extends MoveNonStructTypes, > = TMoveType extends MovePrimitive - ? ConvertPrimitiveStructField - : TMoveType extends `vector<${infer TInner}>` + ? MovePrimitiveMap[TMoveType] + : TMoveType extends MoveVector // Custom Vector type ? ConvertStructFieldType[] - : TMoveType extends `0x1::object::Object<${string}>` + : TMoveType extends MoveObject ? { inner: `0x${string}` } : TMoveType extends `0x1::option::Option<${infer TInner}>` ? ConvertStructFieldOptionType diff --git a/src/types/moveTypes.ts b/src/types/moveTypes.ts index 95bdc74..3d963e0 100644 --- a/src/types/moveTypes.ts +++ b/src/types/moveTypes.ts @@ -2,25 +2,51 @@ * Types from Move language */ +import { AnyNumber } from './common.js'; + export type MoveNonStructTypes = | MovePrimitive | MoveVector | MoveObject | MoveOption; -export type MovePrimitive = - | 'bool' - | 'u8' - | 'u16' - | 'u32' - | 'u64' - | 'u128' - | 'u256' - | 'address' - | '0x1::string::String'; +/** + * All primitive simple types that are not complex, such as vector, object or struct + * + * @type {HighValue} - The type of high value number, in some cases can return just as string, or as AnyNumber, default as string + * @returns - A map of Move primitive types to their corresponding TypeScript types + */ +export type MovePrimitiveMap = { + bool: boolean; + + address: `0x${string}`; + '0x1::string::String': string; + + // Number + u8: number; + u16: number; + u32: number; + u64: HighValue; + u128: HighValue; + u256: HighValue; + + 'vector': boolean[]; + 'vector': string | number[] | Uint8Array; + 'vector': number[]; + 'vector': number[]; + + 'vector': HighValue[]; + 'vector': HighValue[]; + 'vector': HighValue[]; + 'vector
': `0x${string}`[]; + 'vector': string[]; + 'vector<0x1::string::String>': string[]; +}; + +export type MovePrimitive = keyof MovePrimitiveMap; -export type MoveVector = `vector<${string}>`; +export type MoveVector = `vector<${I}>`; -export type MoveObject = `0x1::object::Object<${string}>`; +export type MoveObject = `0x1::object::Object<${I}>`; -export type MoveOption = `0x1::option::Option<${string}>`; +export type MoveOption = `0x1::option::Option<${I}>`;