• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

132
Views
How to construct a function from dictionary that contains function name and arguments typescript

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))

The contract function type is:

 readonly functions: { [ name: string ]: ContractFunction };
 export type ContractFunction<T = any> = (...args: Array<any>) => Promise<T>;

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.

about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 3 years ago · Juan Pablo Isaza Report

0

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));

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error