0

I want to dynamically call contract functions. The only problem with this is, that all of the functions have a different amount of variables. As of now, the function looks like this:

async call(message: string, args, accountId: string){

    const provider =  new WsProvider(this.providerAddress)
    const api = await ApiPromise.create({ provider })
    const contract = new ContractPromise(api, this.abi, this.contractAddress)

    const gasLimit = this.gasLimit
    const storageDepositLimit = this.storageDepositLimit
            

    if(args.length == 1) {
         const { gasRequired, storageDeposit, result, output } = await   contract.query[message](
                    accountId,
                    {
                     gasLimit,
                     storageDepositLimit,
                    },
                    args[0]
                )
                    return{gasRequired, storageDeposit, result, output}
     }
}

'args' is an array of the arguments passed to the contract query. Is it possible to loop through all args in the contract call,instead of calling them all individually using their index?

1
  • You can use the JS spread operator, e.g. contract.query[message](accountId, <opts>, ...args)
    – Jaco
    Sep 27, 2022 at 12:08

1 Answer 1

1

An option to do this is using apply. The code would look like this:

const { gasRequired, storageDeposit, result, output } = await contract.query[message].apply(null, [
       accountId,
       {
           gasLimit,
           storageDepositLimit,
       },
       ...this.args
])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.