Home > Blockchain >  How to construct a function from dictionary that contains function name and arguments typescript
How to construct a function from dictionary that contains function name and arguments typescript

Time:07-03

I am having a dictionary

{
   "function_name":"myFunc",
   "arguments":["a1","a2"]
}

I want to construct a function where the function name is the function name that is present in the above dictionary (i.e myFunc ) with respective arguments (i.e ["a1","a2"]).

The final output of the generator should be:

myFunc(a1,a2){

}

Actual Usage: I want to add this function to a class instance and invoke it

and extending this.

If this function is async then I should be able to await it.

Example:

for a smart contract to call a function we generally do (Referring to greeter smart contract )

contract.functions.greet().then(k => console.log(k))

using ethers library.

I wanted to generate the greet function using the contract ABI dynamically :

[
    {
      "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"
    }
  ]

I am able to parse the above JSON and get the function name and arguments. Now as a final step I want to attach this constructed function to the contract and call it.

CodePudding user response:

I guess you're trying to do something like the following:

class SomeClass {
  obj = {
    functionName: "add",
    args: [10, 20],
  };

  add(a, b) {
    return a   b;
  }

  invokeFunc() {
    const { functionName, args } = this.obj;
    return this[functionName](...args);
  }
}

const instance = new SomeClass();
console.log(instance.invokeFunc());

Since the function to be invoked is accessible from the this object, you can invoke it and spread (...) all the arguments.

For async functions it's not clear how it's indicated that it's an async function. You can have a type field in the object and conditionally await the function.

CodePudding user response:

Here is an example on how to do this.

read here https://www.c-sharpcorner.com/article/creating-functions-dynamically-in-javascript/ as this could help you

var strFunc = [{
  "isAsync":false,
  "function_name": "myFunc",
  "arguments": ["a1", "a2"],
  content: "return a1*a2"
},
{
  "isAsync":true,
  "function_name": "asyncMyFunc",
  "arguments": ["a1", "a2"],
  content: "return a1 a2"
}]

class Funcs {
constructor(f){
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
  f.forEach(x=> {
    let func = null;
    if (x.isAsync)
     func = new AsyncFunction(...x.arguments, x.content);
    else func = new Function(...x.arguments, x.content);
    
    this[x.function_name] = func;
  });

}

}

var mycl = new Funcs(strFunc);
mycl.asyncMyFunc(1,2).then(x=> console.log("asyncFunc", x))
console.log("syncFunc", mycl.myFunc(1,2));

  • Related