0

I have a smart contract deployed with the following function:

function getInfo(address foo) public view returns (uint256[] memory){

  return to_raise[foo];

}

my question is: how do I call getInfo(myaddress) using web3 (and get the number it returns)?

1 Answer 1

0

I have found a working answer thanks to this question and answer: How to get result from contract function called through web3 using sendTransaction

and thanks to somedocumentation: https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html

The following code is working

async function doAnotherThing(){


  var address = "0xcee911a1bd2341a5ac878652f4505f14f3fdfc6c";// contract address

  const jsonInterface = [/* your abi here */]

  var MyContract = new web3.eth.Contract(jsonInterface, address);

  // set provider for all later instances to use
  MyContract.setProvider('wss://blissful-frequent-energy.bsc-testnet.discover.quiknode.pro/YOURAPIKEYHERE/');

  try{

    MyContract.methods.getInfo(accounts[0]).call({from: accounts[0]}, function(error, result){
    alert( result)
  });


  // MyContract.methods.getInfo.call({from:accounts[0]}).then( value => alert(value))
  
  }catch(error){
    alert(error)

  }


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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