@@ -11,8 +11,8 @@ import {
1111 ViewFunctionABI ,
1212 FunctionABI ,
1313} from "../types" ;
14- import { Bool , MoveOption , MoveString , MoveVector , U128 , U16 , U256 , U32 , U64 , U8 } from "../../bcs" ;
15- import { AccountAddress } from "../../core" ;
14+ import { Bool , Deserializer , MoveOption , MoveString , MoveVector , U128 , U16 , U256 , U32 , U64 , U8 } from "../../bcs" ;
15+ import { AccountAddress , Ed25519PublicKey } from "../../core" ;
1616import { getModule } from "../../internal/account" ;
1717import {
1818 findFirstNonSignerArg ,
@@ -31,9 +31,18 @@ import {
3131 isNull ,
3232 isNumber ,
3333 isString ,
34+ isTypeTagAddress ,
35+ isTypeTagBool ,
36+ isTypeTagGeneric ,
37+ isTypeTagU128 ,
38+ isTypeTagU16 ,
39+ isTypeTagU256 ,
40+ isTypeTagU32 ,
41+ isTypeTagU64 ,
42+ isTypeTagU8 ,
3443 throwTypeMismatch ,
3544} from "./helpers" ;
36- import { MoveFunction } from "../../types" ;
45+ import { MoveFunction , MoveStruct } from "../../types" ;
3746
3847const TEXT_ENCODER = new TextEncoder ( ) ;
3948
@@ -80,7 +89,7 @@ export async function fetchStructAbi(
8089 moduleName : string ,
8190 structName : string ,
8291 aptosConfig : AptosConfig ,
83- ) {
92+ ) : Promise < MoveStruct | undefined > {
8493 // This fetch from the API is currently cached
8594 const module = await getModule ( { aptosConfig, accountAddress : moduleAddress , moduleName } ) ;
8695
@@ -181,7 +190,7 @@ export async function fetchStructFieldsAbi(
181190 moduleName : string ,
182191 structName : string ,
183192 aptosConfig : AptosConfig ,
184- ) {
193+ ) : Promise < FunctionABI > {
185194 const structAbi = await fetchStructAbi ( moduleAddress , moduleName , structName , aptosConfig ) ;
186195
187196 // If there's no ABI, then the function is invalid
@@ -467,3 +476,107 @@ function checkType(param: TypeTag, arg: EntryFunctionArgumentTypes, position: nu
467476
468477 throw new Error ( `Type mismatch for argument ${ position } , expected '${ param . toString ( ) } '` ) ;
469478}
479+
480+ export function deserializeArgument (
481+ params : Array < TypeTag > ,
482+ deserializer : Deserializer ,
483+ ) : Array < SimpleEntryFunctionArgumentTypes > {
484+ return params . map ( ( param ) => deserializeArg ( deserializer , param ) ) ;
485+ }
486+
487+ export function deserializeArg ( deserializer : Deserializer , param : TypeTag ) : SimpleEntryFunctionArgumentTypes {
488+ if ( isTypeTagBool ( param ) ) {
489+ return Bool . deserialize ( deserializer ) . value ;
490+ }
491+ if ( isTypeTagAddress ( param ) ) {
492+ return AccountAddress . deserialize ( deserializer ) . toString ( ) ;
493+ }
494+ if ( isTypeTagU8 ( param ) ) {
495+ return U8 . deserialize ( deserializer ) . value ;
496+ }
497+ if ( isTypeTagU16 ( param ) ) {
498+ return U16 . deserialize ( deserializer ) . value ;
499+ }
500+ if ( isTypeTagU32 ( param ) ) {
501+ return U32 . deserialize ( deserializer ) . value ;
502+ }
503+ if ( isTypeTagU64 ( param ) ) {
504+ return U64 . deserialize ( deserializer ) . value ;
505+ }
506+ if ( isTypeTagU128 ( param ) ) {
507+ return U128 . deserialize ( deserializer ) . value ;
508+ }
509+ if ( isTypeTagU256 ( param ) ) {
510+ return U256 . deserialize ( deserializer ) . value ;
511+ }
512+ if ( isTypeTagGeneric ( param ) ) {
513+ // // Currently, TS SDK `deserialize` can only handle a single class, not a class with generics
514+ throw new Error ( "Generic type deserialization is not implemented" ) ;
515+ }
516+
517+ if ( param . isVector ( ) ) {
518+ if ( isTypeTagU8 ( param . value ) ) {
519+ // TODO handle Secp256k1PublicKey
520+ const { values } = MoveVector . deserialize ( deserializer , U8 ) ;
521+ const numbers = values . map ( ( value ) => value . value ) ;
522+ try {
523+ return new Ed25519PublicKey ( new Uint8Array ( numbers ) ) . toString ( ) ;
524+ } catch ( e : any ) {
525+ return numbers ;
526+ }
527+ }
528+ if ( isTypeTagU16 ( param . value ) ) {
529+ const { values } = MoveVector . deserialize ( deserializer , U16 ) ;
530+ return values . map ( ( value ) => value . value ) ;
531+ }
532+ if ( isTypeTagU32 ( param . value ) ) {
533+ const { values } = MoveVector . deserialize ( deserializer , U32 ) ;
534+ return values . map ( ( value ) => value . value ) ;
535+ }
536+ if ( isTypeTagU64 ( param . value ) ) {
537+ const { values } = MoveVector . deserialize ( deserializer , U64 ) ;
538+ return values . map ( ( value ) => value . value ) ;
539+ }
540+ if ( isTypeTagU128 ( param . value ) ) {
541+ const { values } = MoveVector . deserialize ( deserializer , U128 ) ;
542+ return values . map ( ( value ) => value . value ) ;
543+ }
544+ if ( isTypeTagU256 ( param . value ) ) {
545+ const { values } = MoveVector . deserialize ( deserializer , U256 ) ;
546+ return values . map ( ( value ) => value . value ) ;
547+ }
548+ if ( isTypeTagBool ( param . value ) ) {
549+ const { values } = MoveVector . deserialize ( deserializer , Bool ) ;
550+ return values . map ( ( value ) => value . value ) ;
551+ }
552+ if ( isTypeTagAddress ( param . value ) ) {
553+ const { values } = MoveVector . deserialize ( deserializer , AccountAddress ) ;
554+ return values . map ( ( value ) => value . toString ( ) ) ;
555+ }
556+ if ( param . value . isStruct ( ) ) {
557+ if ( param . value . isObject ( ) ) {
558+ const { values } = MoveVector . deserialize ( deserializer , AccountAddress ) ;
559+ return values . map ( ( value ) => value . toString ( ) ) ;
560+ }
561+ if ( param . value . isOption ( ) ) {
562+ // Currently, TS SDK `deserialize` can only handle a single class, not a class with generics
563+ throw new Error ( "Option type deserialization is not implemented" ) ;
564+ }
565+
566+ const { values } = MoveVector . deserialize ( deserializer , MoveString ) ;
567+ return values . map ( ( value ) => value . value ) ;
568+ }
569+ }
570+ if ( param . isStruct ( ) ) {
571+ if ( param . isObject ( ) ) {
572+ return AccountAddress . deserialize ( deserializer ) . toString ( ) ;
573+ }
574+ if ( param . isOption ( ) ) {
575+ // Currently, TS SDK `deserialize` can only handle a single class, not a class with generics
576+ throw new Error ( "Option type deserialization is not implemented" ) ;
577+ }
578+ return MoveString . deserialize ( deserializer ) . value ;
579+ }
580+
581+ throw new Error ( `Could not deserialize type '${ param . toString ( ) } '` ) ;
582+ }
0 commit comments