0

Can someone please shed light on what i might be doing wrong here , im trying to query the token balance of an account using geth (v1.7.2) and im doing the following but i get invalid address even though the address is correct of both the address and the token.

The following are the steps im doing.

Thanks for your help.

Set default account

web3.eth.defaultAccount = web3.eth.accounts[0];

"0x41f32f70119e9deead9681d371207cae0b2c16f6"

Unlock Account

web3.personal.unlockAccount(web3.eth.accounts[0], "PASSWORD", 3600);

true

Set Token's ABI

tokenabi = web3.eth.contract([{"constant": "true","inputs": [{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name": "","type": "uint256"}],"payable":"false","stateMutability":"view","type":"function"}]);

{ abi: [{ constant: "true", inputs: [{...}], name: "balanceOf", outputs: [{...}], payable: "false", stateMutability: "view", type: "function" }], eth: { accounts: ["0x41f32f70119e9deead9681d371207cae0b2c16f6", "0x77ca2e7e9aa9f0494560ed0eacbe1b1b90f9fcb6"], blockNumber: 10887, coinbase: "0x41f32f70119e9deead9681d371207cae0b2c16f6", compile: { lll: function(), serpent: function(), solidity: function() }, defaultAccount: "0x41f32f70119e9deead9681d371207cae0b2c16f6", defaultBlock: "latest", gasPrice: 0, hashrate: 0, mining: false, pendingTransactions: [], protocolVersion: "0x3f", syncing: false, call: function(), contract: function(abi), estimateGas: function(), filter: function(options, callback, filterCreationErrorCallback), getAccounts: function(callback), getBalance: function(), getBlock: function(), getBlockNumber: function(callback), getBlockTransactionCount: function(), getBlockUncleCount: function(), getCode: function(), getCoinbase: function(callback), getCompilers: function(), getGasPrice: function(callback), getHashrate: function(callback), getMining: function(callback), getPendingTransactions: function(callback), getProtocolVersion: function(callback), getRawTransaction: function(), getRawTransactionFromBlock: function(), getStorageAt: function(), getSyncing: function(callback), getTransaction: function(), getTransactionCount: function(), getTransactionFromBlock: function(), getTransactionReceipt: function(), getUncle: function(), getWork: function(), iban: function(iban), icapNamereg: function(), isSyncing: function(callback), namereg: function(), resend: function(), sendIBANTransaction: function(), sendRawTransaction: function(), sendTransaction: function(), sign: function(), signTransaction: function(), submitTransaction: function(), submitWork: function() }, at: function(address, callback), getData: function(), new: function() }

Define Token Instance

tokeninst = tokenabi.at(0xd27716ebAdDaE1902C45878803A5c3582426ada3);

{ abi: [{ constant: "true", inputs: [{...}], name: "balanceOf", outputs: [{...}], payable: "false", stateMutability: "view", type: "function" }], address: 1.201543847219371e+48, transactionHash: null, allEvents: function(), balanceOf: function() }

Validate Address with Balance

web3.isAddress("0x41f32f70119e9deead9681d371207cae0b2c16f6");

true

Validate Token Contract Address

web3.isAddress("0xd27716ebAdDaE1902C45878803A5c3582426ada3");

true

Query Balance of Address

balance = tokeninst.balanceOf.call("0x41f32f70119e9deead9681d371207cae0b2c16f6");

Error: invalid address at web3.js:3930:15 at web3.js:3734:22 at web3.js:5025:28 at map () at web3.js:5024:12 at web3.js:5050:18 at web3.js:5075:23 at web3.js:4102:22 at :1:11

Have also tried it with

balance = tokeninst.balanceOf("0x41f32f70119e9deead9681d371207cae0b2c16f6");

Error: invalid address at web3.js:3930:15 at web3.js:3734:22 at web3.js:5025:28 at map () at web3.js:5024:12 at web3.js:5050:18 at web3.js:5075:23 at web3.js:4102:22 at apply () at web3.js:4227:12

What do you reckon is wrong / missing here with my steps ?

0

I think this line is missing quotes:

tokeninst = tokenabi.at(0xd27716ebAdDaE1902C45878803A5c3582426ada3);

Try this instead:

tokeninst = tokenabi.at("0xd27716ebAdDaE1902C45878803A5c3582426ada3");
  • I was just thinking the same, but why does it give the correct ABI if the address was malformed :( – Lauri Peltonen Apr 7 '18 at 5:13
  • The ABI was passed in correctly, so why wouldn't it be correct? Note that tokenabi is a misnomer, since it's a contract object. And note that the address field in the return value of tokenabi.at is a number. – smarx Apr 7 '18 at 5:30
  • great spotting , that was it , when passing token name without quotes it was still returning an output without an error however the difference is when passing it with quotes it returns the address of the token as shown in the output below. thanks heaps. { abi: [{ constant: "true", inputs: [{...}], name: "balanceOf", outputs: [{...}], payable: "false", stateMutability: "view", type: "function" }], address: "0xd27716ebAdDaE1902C45878803A5c3582426ada3", transactionHash: null, allEvents: function(), balanceOf: function() } – Tanveer Roowala Apr 8 '18 at 22:36

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.