How to pass a function argument to a created instance inside the function javascript?

Viewed 29

I have this async function:

async function estimateGas() {
  const w3 = await new Web3(window.ethereum);
  window.contract = await new w3.eth.Contract(myContr.abi, myContr.addr);
  window.contract.methods.buy().estimateGas({from: ethereum.selectedAddress});
}

I want to change the function so that I can choose any of the methods my instance has at will like so:

async function estimateGas(argumentName) {
  const w3 = await new Web3(window.ethereum);
  window.contract = await new w3.eth.Contract(myContr.abi, myContr.addr);

  // I want to change here below what was the ".buy()" method with my argument as if it would have been ".argumentName()"

  window.contract.methods.argumentName().estimateGas({from: ethereum.selectedAddress});
}

How can I use the funciton argument to change a method name in an instance created inside the function with javascript?

2 Answers
async function estimateGas(argumentName) {
  const w3 = await new Web3(window.ethereum);
  window.contract = await new w3.eth.Contract(myContr.abi, myContr.addr);    
  window.contract.methods[argumentName]().estimateGas({from: ethereum.selectedAddress});
}

put the function as a parameter then when calling it use parentheses for example:

function main (function){
return function()
}

Related