@@ -71,12 +71,22 @@ import {
7171 InputViewFunctionDataWithRemoteABI ,
7272 InputViewFunctionDataWithABI ,
7373 FunctionABI ,
74+ SimpleEntryFunctionArgumentTypes ,
7475} from "../types" ;
75- import { convertArgument , fetchEntryFunctionAbi , fetchViewFunctionAbi , standardizeTypeTags } from "./remoteAbi" ;
76+ import {
77+ convertArgument ,
78+ fetchEntryFunctionAbi ,
79+ fetchStructFieldsAbi ,
80+ fetchViewFunctionAbi ,
81+ standardizeTypeTags ,
82+ } from "./remoteAbi" ;
7683import { memoizeAsync } from "../../utils/memoize" ;
84+ import { AnyNumber , MoveFunctionId } from "../../types" ;
7785import { getFunctionParts , isScriptDataInput } from "./helpers" ;
7886import { SimpleTransaction } from "../instances/simpleTransaction" ;
7987import { MultiAgentTransaction } from "../instances/multiAgentTransaction" ;
88+ import { ProofChallenge } from "../instances/proofChallenge" ;
89+ import { MoveString } from "../../bcs" ;
8090
8191/**
8292 * We are defining function signatures, each with its specific input and output.
@@ -649,3 +659,34 @@ async function fetchAbi<T extends FunctionABI>({
649659 1000 * 60 * 5 , // 5 minutes
650660 ) ( ) ;
651661}
662+
663+ export async function generateProofChallenge ( args : {
664+ config : AptosConfig ;
665+ struct : MoveFunctionId ;
666+ data : Array < EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes > ;
667+ } ) {
668+ const { config, struct, data } = args ;
669+ const { moduleAddress, moduleName, functionName } = getFunctionParts ( struct ) ;
670+ const structFieldsAbi = await fetchStructFieldsAbi ( moduleAddress , moduleName , functionName , config ) ;
671+
672+ // Check all BCS types, and convert any non-BCS types
673+ // TODO repeated code, move to a central place
674+ const functionArguments : Array < EntryFunctionArgumentTypes > =
675+ data . map ( ( arg , i ) => convertArgument ( functionName , structFieldsAbi , arg , i , structFieldsAbi . parameters ) ) ?? [ ] ;
676+
677+ // Check that all arguments are accounted for
678+ if ( functionArguments . length !== structFieldsAbi . parameters . length ) {
679+ throw new Error (
680+ // eslint-disable-next-line max-len
681+ `Too few arguments for '${ moduleAddress } ::${ moduleName } ::${ functionName } ', expected ${ structFieldsAbi . parameters . length } but got ${ functionArguments . length } ` ,
682+ ) ;
683+ }
684+
685+ const challenge = new ProofChallenge ( [
686+ AccountAddress . from ( moduleAddress ) ,
687+ new MoveString ( moduleName ) ,
688+ new MoveString ( functionName ) ,
689+ ...functionArguments ,
690+ ] ) ;
691+ return challenge ;
692+ }
0 commit comments