3

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?

MidnightLightning
  • 2,988
  • 16
  • 28
mididouze
  • 83
  • 1
  • 5

1 Answers1

3

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(), not eth.sendTransaction:

sudo geth --exec \
  "eth.call({to: "0xNNNNN", data: "0xNNNN"})" attach

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 the balanceOf(address) function (0x70a08231000000000000000000000000), and the second half is the address you want to look up. So, for that example address you gave:

sudo geth --exec \
  "eth.call({to: "0xa74476443119A942dE498590Fe1f2454d7D4aC0d", \
  data: "0x70a08231000000000000000000000000f6e5911696a3729a7740cbdcd64c05bc2eec60e7"})" attach
MidnightLightning
  • 2,988
  • 16
  • 28
  • Is it possible to get an example for sending token like you did for the balanceOf(address) above. How should I format the data string. for sending 1 ETH I would use sudo geth --exec "eth.sendTransaction({from: '0xf6e5911696a3729a7740cbdcd64c05bc2eec60e7', to: '0xDF1b0d8c6B3BCc4cdfc6E8A710C02a265bC5d25d',value: '100000000000000000', gas: '0x76D0', gasPrice: '0x4A817C800'})" attach – mididouze Jun 12 '17 at 12:17
  • To do the same thing for sending tokens, in the ERC20 standard, that's the transfer(address _to, address _from) method. Its function selector is 0xa9059cbb000000000000000000000000 and then needs two addresses after it, so you'll end up with a data element that's 48-bytes long. – MidnightLightning Jun 12 '17 at 14:39
  • How about the amount??? Is the call function read only, because this doesn't work sudo geth --exec "eth.call({to: '0xad408ef944da873fe9b65f4e78e21ac2d489db90', value: '100000000000000', gas: '0x76D0', gasPrice: '0x4A817C800', data: '0xa9059cbb000000000000000000000000280e5dbe0d36be7344743ce03‌​4b104b94d930e51df1b0‌​d8c6b3bcc4cdfc6e8a71‌​0c02a265bc5d25d'})" attach then I get "0x000000000000000000000000000000000000000000000000000000000‌​0000000" – mididouze Jun 15 '17 at 12:10
  • No, the transfer function is not read only (you're sending tokens to someone else), so needs to be eth.sendTransaction – MidnightLightning Jun 15 '17 at 14:37
  • How/where to input value, gas, gasPrice. . Example of sending string would be nice. – mididouze Jun 16 '17 at 09:57
  • You have an example of that in your original question; gas and gasPrice are additional properties in the JSON blob, alongside to and data. – MidnightLightning Jun 20 '17 at 03:27
  • is there any spec on how to assemble the data variable? where did you get those values from? – Nulik Apr 09 '18 at 23:37
  • @Nulik, that would be best submitted as a separate question; I can answer it, but it's more involved, so wouldn't be best in a comment. – MidnightLightning Apr 10 '18 at 15:20