0

I'm still learning, so apologies if this is a very dumb question. . . I am calling a smart contract to retrieve an allowance value, but I'm not sure how to get to the integer value in the response. My code is:

const contract = new web3.eth.Contract(ContractAbi, CONTRACT_ADDRESS);
const allowance = await contract.methods.allowance(userAddress, STAKING_CONTRACT_ADDRESS);
alert(allowance);

The alert that is displayed in the browser is 'undefined'. I think I need it to be allowance.<something? but can't figure out what?

Thank you!

4
  • There are many reasons why your alert would display "undefined". Off the top of my head: (1) check that the contract address is correct (2) check that the network you're interacting is the same as the one you intend to interact with (3) use console.log instead of alert to make sure that the problem is not a type issue. Commented Jun 29, 2021 at 10:21
  • Also, I recommend using ethers instead of web3. Related: What is the difference between web3.js and ethers.js?. Commented Jun 29, 2021 at 10:22
  • It's hard for me to test without knowing web3 versions, contract address/abi, etc., but try just changing the middle line to await contract.methods.allowance(userAddress, STAKING_CONTRACT_ADDRESS).call();
    – natewelch_
    Commented Jun 29, 2021 at 17:06
  • Thank you @natewelch_ and @PaulRazvanBerg! The answer was I hadn't included .call(), thank you for spotting that nate. This is my first question here, is there some way I can mark your comment as correct, or give it points or something? Thank you!
    – Enrique
    Commented Jun 29, 2021 at 21:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.