0

I use latest version stable geth: [root@localhost ~]# geth version

WARN [05-30|06:27:22.750] Sanitizing cache to Go's GC limits       provided=1024 updated=612
Geth
Version: 1.8.27-stable
Git Commit: 4bcc0a37ab70cb79b16893556cffdaad6974e7d8
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.11.9
Operating System: linux
GOPATH=
GOROOT=/home/travis/.gimme/versions/go1.11.9.linux.amd64

The web3 version info:

> web3.version
{
  api: "0.20.1",
  ethereum: "0x3f",
  network: "123456",
  node: "Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.11.9",
  whisper: undefined,
  getEthereum: function(callback),
  getNetwork: function(callback),
  getNode: function(callback),
  getWhisper: function(callback)
}

I use node 10.15.3 and installed web3, its version is:

> web3.version
'1.0.0-beta.55'

When I set up a ERC20 Token contract and call the function balanceOf in node env, I got the result below:

contract_instance.methods.balanceOf('0x3c46b43e683b5addd7e66b3d1d958854ffb58361').call().then(console.log);
    Promise {
      <pending>,
      domain:
       Domain {
         domain: null,
         _events:
          [Object: null prototype] {
            removeListener: [Function: updateExceptionCapture],
            newListener: [Function: updateExceptionCapture],
            error: [Function: debugDomainError] },
         _eventsCount: 3,
         _maxListeners: undefined,
         members: [] } }
     BigNumber { _hex: '0x3635c9adc5dea00000' }

I check the bigNumber, it's correct number I issue token. From my expection, the result should be balance number, why I got these returns?

Another question is: when I call contract function to get the balance of token like below:

> var balance = contract.methods.balanceOf('0x3c46b43e683b5addd7e66b3d1d958854ffb58361').call();
undefined
> console.log(`balance before send: ${balance}`);
balance before send: [object Promise]

the return result is not a number, why and how to get correct number? Thanks!

  • This is probably one of the most unclear questions that I've read here in a while. What on earth does From my expection, the result should be balance number even mean????? I understand that you probably meant to say "expectation", but what on earth is "balance number"??? And why would we have anything to say about the balance of any sender, as given in your contract??? – goodvibration May 30 at 13:46
  • I mean the balance from function call is expected to be a number. but the console log show ${balance} is "[object Promise]".thanks for you comments! – Ntydrm May 30 at 14:05
  • You have to resolve this promise, or await for it to complete. That's the nature of promises in Javascript. And a function which makes a remote-procedure-call (i.e., request from another process, as the the case here) would typically run asynchronously and return a promise which then must be resolved. – goodvibration May 30 at 14:08
1

You need to wait for the result returned by function. This will work for you,

async function check(){
  var balance = await contract.methods.balanceOf('0x3c46b43e683b5addd7e66b3d1d958854ffb58361').call();
  console.log(balance);
}

check();
  • 1
    I really works, I can get correct return value now.thanks. – Ntydrm Jun 5 at 3:55

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.