0

I want to retrieve and assign the value of my contract state variable to my javascript variable.I am doing as per guidlines provided on this community, but failed to assigned. my javascript code is here I am using Node js.

const contractAddress1 = '0xc1847e91e386e11e4df447b3e333f1b18abd1aca'
const contractABI = [{"constant":true,"inputs":[],"................];
var contract1  = new web3.eth.Contract(contractABI, contractAddress1)
var ot="raw";
contract1.methods.getTotalSupply().call().then(function(result){

  console.log(result) 
  ot = result;
});
console.log("ot : ", ot);

and its output is :

ot :  raw
BigNumber { _hex: '0x2704' }

Why 'ot' has still value 'raw' , why not BigNumber { _hex: '0x2704' } ???

0

This is more of a javascript question rather than related to ethereum. The reason why your variable ot hasn't changed is because method contract1.methods.getTotalSupply() needs some time to query the blockchain for the result. This works on the same principle like Ajax request. What you could do is use async/ await or just place the variable inside the query method. Example:

const contractAddress1 = '0xc1847e91e386e11e4df447b3e333f1b18abd1aca'
const contractABI = [{"constant":true,"inputs":[],"................];
var contract1 = new web3.eth.Contract(contractABI, contractAddress1)
var ot = "raw";
contract1.methods.getTotalSupply().call().then(function(result){
    console.log(result) 
    ot = result;
    console.log("ot : ", ot);
});

OR

const contractAddress1 = '0xc1847e91e386e11e4df447b3e333f1b18abd1aca'
const contractABI = [{"constant":true,"inputs":[],"................];
var contract1 = new web3.eth.Contract(contractABI, contractAddress1)
var ot = "raw";

async function init() {
    ot = await contract1.methods.getTotalSupply().call();
    console.log("ot : ", ot);
}
init();
| improve this answer | |
  • thanks a lot , you save my mind from a blow up... – Amir Ali Nov 6 '19 at 8:52
  • sorry, it 'ot' is only available in init function , but when i try to use 'ot' outside of init , it become undefined ... Please help me now in this regard... thanks in advance – Amir Ali Nov 6 '19 at 8:56
0

You are printing ot before call result was stored to it. Try this code:

var ot="raw";
contract1.methods.getTotalSupply().call().then(function(result){
  console.log(result) 
  ot = result;
  console.log("ot : ", ot);
});

Explanation:

You are calling contract asynchronously, which means that result is not available immediately, but rather after some time. That's why you specify a callback function to be called when result is available.

However, you are trying to print ot immediately, when result is not available yet. Understanding asynchronous execution is essential for JS developer nowadays, and this is not directly related to Ethereum or blockchain. Check this documentation for details.

| improve this answer | |
  • thanks . but i want to assign result value in a js variable and then use to other functions in js, For my case, how could i proceed. BTW. your documentation link is not provided.. – Amir Ali Nov 6 '19 at 8:48
  • Surely, you may assign returned value to a variable and use it in another functions. You just cannot do this immediately, but have to wait a bit for result to become available. This is very common pattern for JS development. See the link (I've just fixed it). – Mikhail Vladimirov Nov 6 '19 at 8:50
0

The result of the contract call will be available only inside the init function. In order to make it available to other functions, you can return the result.

const contractAddress1 = '0xc1847e91e386e11e4df447b3e333f1b18abd1aca'
const contractABI = [{"constant":true,"inputs":[],"................];
var contract1 = new web3.eth.Contract(contractABI, contractAddress1)
var ot = "raw";

async function init() {
    const result = await contract1.methods.getTotalSupply().call();
    return result;
}

now in another function, you can access the result like this:

async function anotherFunc(){
  ot = await init();
  console.log(ot);
}

Hope it will help.

| improve this answer | |

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.