To get the balance of ether in an address, I am using the code below. (1 line)
sudo geth --exec \
"web3.eth.getBalance('0xf6e5911696a3729a7740cbdcd64c05bc2eec60e7')" attach
I get: 165060000000000000
I need to get balance of token (e.g. Golem) in one line like above. Can not interact with console. Return can be anything, 1 line or JSON.
Also looking for 1 line to send token like below:
sudo geth --exec \
"eth.sendTransaction({from: '0xxxxxxxxxxxxxxxxxxxxxxxxxxx', \
to: '0xxxxxxxxxxxxxxxxxxxxxxxxxx', value: '10000000000000000', \
gas: '0x76D0', gasPrice: '0x4A817C800'})" attach
Is this possible?
Best Answer
For tokens that adhere to the ERC20 standard (which the Golem token is), to get the token balance for a given address, you need to execute the method on the token contract called
balanceOf(address)
.You don't need to send this as a real transaction into the Ethereum blockchain, since your local node (if it's fully-synced) already has this data, and that function is a read-only method (doesn't change the state of the smart contract). So the command you want is
eth.call()
, noteth.sendTransaction
:Specifically, for the Golem contract, the "to" is
0xa74476443119A942dE498590Fe1f2454d7D4aC0d
, and to generate the "data" variable, for this specific contract interaction, it's going to be a 32-byte hex string, in two 16-byte pieces. The first piece is the function selector for thebalanceOf(address)
function (0x70a08231000000000000000000000000
), and the second half is the address you want to look up. So, for that example address you gave: