0

I am trying to call a function from my smart contract using web3 and react. Here is my code.

app.js const project=new web3.eth.Contract(Mycontractabi.abi,networkData.address); const retailer=await project.getretailer.call(); console.log(retailer); contract: address public retailer function getretailer() public view returns (address) { return retailer; } 

Kindly point out what is wrong as I am not able to get retailer address as desired by the code

2
  • maybe instead of this Mycontractabi.abi, just Mycontractabi. console,log(project) see what you are getting Commented Sep 24, 2022 at 16:47
  • nope, tried your solution, Mycontractabi.abi is correct Commented Sep 24, 2022 at 17:04

2 Answers 2

0

I found that to access a variable from a smart contract using web3.js there is simple method:

contract.methods.variable_name.call().call(); 

So in my case, the modified code will be :

const project=new web3.eth.Contract(Mycontractabi.abi,networkData.address); const retailer=await project.methods.retailer.call().call(); 

There is no need to call any function just to access a single variable. We can access any variable directly by using above method.

Sign up to request clarification or add additional context in comments.

1 Comment

small remark: the first parameter is address, abi is the second.
0

You can call a function like the following.

const retailer = await project.methods.retailer().call(); 

If a method has some parameters, you can use:

const response = await project.methods.myAnotherMethod(param1, param2).send({from: address}); 

address is the current connected wallet address.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.