diff --git a/src/types/convertor/argsConvertor.ts b/src/types/convertor/argsConvertor.ts index 36932ed..c4c639c 100644 --- a/src/types/convertor/argsConvertor.ts +++ b/src/types/convertor/argsConvertor.ts @@ -2,13 +2,9 @@ * Convert Move type to TypeScript type, * for input arguments of view function or entry function. */ + import { AnyNumber, UnknownStruct } from '../common.js'; -import { - MoveObject, - MoveOption, - MovePrimitive, - MovePrimitiveMap, -} from '../moveTypes.js'; +import { MoveNonStructTypes, MovePrimitive } from '../moveTypes.js'; /** * Convert an array of input arguments type. @@ -20,11 +16,46 @@ export type ConvertArgs = T extends readonly [ ? [ConvertArgType, ...ConvertArgs] : []; -// Internal -type ConvertArgType = TMoveType extends MovePrimitive - ? MovePrimitiveMap[TMoveType] - : TMoveType extends MoveObject - ? `0x${string}` - : TMoveType extends MoveOption - ? ConvertArgType | undefined - : UnknownStruct; +/** + * 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; diff --git a/src/types/convertor/returnConvertor.ts b/src/types/convertor/returnConvertor.ts index fff6ced..46e1620 100644 --- a/src/types/convertor/returnConvertor.ts +++ b/src/types/convertor/returnConvertor.ts @@ -5,12 +5,7 @@ import { UnknownStruct } from '../common.js'; import { DefaultABITable } from '../defaultABITable.js'; -import { - MoveObject, - MoveOption, - MovePrimitive, - MovePrimitiveMap, -} from '../moveTypes.js'; +import { MoveNonStructTypes, MovePrimitive } from '../moveTypes.js'; import { ConvertStructFieldOptionType } from './structConvertor.js'; /** @@ -27,10 +22,40 @@ 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 - ? MovePrimitiveMap[TMoveType] - : TMoveType extends MoveObject - ? { inner: `0x${string}` } - : TMoveType extends MoveOption - ? ConvertStructFieldOptionType - : UnknownStruct; + ? 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; diff --git a/src/types/convertor/structConvertor.ts b/src/types/convertor/structConvertor.ts index d4227ec..15b2bbd 100644 --- a/src/types/convertor/structConvertor.ts +++ b/src/types/convertor/structConvertor.ts @@ -7,13 +7,7 @@ import { ExtractStructType, ResourceStructName, } from '../extractor/structExtractor.js'; -import { - MoveNonStructTypes, - MoveObject, - MovePrimitive, - MovePrimitiveMap, - MoveVector, -} from '../moveTypes.js'; +import { MoveNonStructTypes, MovePrimitive } from '../moveTypes.js'; // Convert a struct field Move type to a TypeScript type export type ConvertStructFieldType< @@ -25,15 +19,38 @@ 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 - ? MovePrimitiveMap[TMoveType] - : TMoveType extends MoveVector // Custom Vector type + ? ConvertPrimitiveStructField + : TMoveType extends `vector<${infer TInner}>` ? ConvertStructFieldType[] - : TMoveType extends MoveObject + : TMoveType extends `0x1::object::Object<${string}>` ? { inner: `0x${string}` } : TMoveType extends `0x1::option::Option<${infer TInner}>` ? ConvertStructFieldOptionType diff --git a/src/types/moveTypes.ts b/src/types/moveTypes.ts index 3d963e0..95bdc74 100644 --- a/src/types/moveTypes.ts +++ b/src/types/moveTypes.ts @@ -2,51 +2,25 @@ * Types from Move language */ -import { AnyNumber } from './common.js'; - export type MoveNonStructTypes = | MovePrimitive | MoveVector | MoveObject | MoveOption; -/** - * 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 MovePrimitive = + | 'bool' + | 'u8' + | 'u16' + | 'u32' + | 'u64' + | 'u128' + | 'u256' + | 'address' + | '0x1::string::String'; -export type MoveVector = `vector<${I}>`; +export type MoveVector = `vector<${string}>`; -export type MoveObject = `0x1::object::Object<${I}>`; +export type MoveObject = `0x1::object::Object<${string}>`; -export type MoveOption = `0x1::option::Option<${I}>`; +export type MoveOption = `0x1::option::Option<${string}>`;