In possession of a dictionary
{
"function_name":"myFunc",
"arguments":["a1","a2"]
}
A desire to create a function where the name matches that in the dictionary above (i.e myFunc ) with respective arguments (i.e ["a1","a2"]).
The ultimate goal is to generate:
myFunc(a1,a2){
}
Actual Scenario: Intention to integrate this function into a class instance and utilize it
and expanding on this idea.
If the function is async, then it should be awaitable.
Illustration:
When a smart contract calls a function, the usual method involves (Using the greeter smart contract as reference )
contract.functions.greet().then(k => console.log(k))
The type of the contract function is:
readonly functions: { [ name: string ]: ContractFunction };
export type ContractFunction<T = any> = (...args: Array<any>) => Promise<T>;
using ethers library.
The objective is to dynamically generate the greet function using the contract ABI :
[
{
"inputs": [
{
"internalType": "string",
"name": "_greeting",
"type": "string"
},
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "greet",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_greeting",
"type": "string"
}
],
"name": "setGreeting",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
Parsing the JSON data to extract the function name and arguments was successful. As a final step, the aim is to bind this generated function to the contract and execute it.