How to Send and Check Balance of ERC20 Tokens Using geth

So, you got some standard ERC20 token from wherever, be it a popular one like Golem (GNT) or a more obscure one like the infamous yet entirely unknown Kekel, and you want to check your balance. How do you do that? Mist offers a simple UI for that, but when you are a real crack, you are of course using only geth console. Here’s what you need.

Checking Token Balance – Basic Way

You’re going to need three things:

  1. The token contract address
  2. Your account address
  3. The token contract’s ABI

You need to figure out the first two yourself. You can use a generic ABI to check token balance. We instantiate the contract ABI as follows:

> var tokenContract = eth.contract([{
     "type":"function",
     "name":"balanceOf",
     "constant":true,
     "payable":false,
     "inputs":[{"name":"","type":"address"}],
     "outputs":[{"name":"","type":"uint256","value":"0"}]
}]);

Now we have the contract’s interface. Next we use it to check our balance. The following example will show my account’s Golem balance:

> var golemContractAddress = "0xa74476443119A942dE498590Fe1f2454d7D4aC0d";
> var account = "0x27f8a692b3c8279fce29f2629b8d87ac717300f8";
> tokenContract.at(golemContractAddress).balanceOf(account);
678633655750000000000

This outputs the token balance in plain tokens, i.e. without showing a decimal point.



 

Human Standard Token ABI

For a more thorough solution, I found the Human Standard Token ABI by Dan Finley on github. It is a node.js module which provides an API to standard tokens. I changed it a little so I can run it from the geth console. Install token.js in a local folder on the machine where you run geth, such as /home/user/geth/token.js, and run the geth console with

geth console --preload /home/user/geth/token.js

Now you have the full functionality of a token contracts in the variable “tokenContract”:

> var golemContractAddress = "0xa74476443119A942dE498590Fe1f2454d7D4aC0d";
> var account = "0x27f8a692b3c8279fce29f2629b8d87ac717300f8";
> var golem = tokenContract.at(golemContractAddress);
> golem.symbol();
"GNT"
> golem.name();
"Golem Network Token"
> golem.balanceOf(account);
678633655750000000000
> golem.decimals()
18

The ABI also supports sending tokens as follows:

var value = '100' // Base 10, accounts for decimals.
golem.transfer(toAddress, value, { from: addr }, function (err, txHash) {
  if (err) console.error(err)

  if (txHash) {
    console.log('Transaction sent')
    console.dir(txHash)
  }
})

Please refer to the readme on github.

I hope this was helpful and wish you a nice day 🙂



Paper Wallets and Cold Storage for Bitcoin and Ethereum

An Ethereum Paper Wallet

You made a fortune trading crypto currencies, but don’t like the general idea of actual money being stored on the hard disk of your worm-infested PC? Then cold storage and paper wallets are for you.

In this post, I am going to explain what cold storage and paper wallets are, and how you can create one, the really safe way.

Offline Accounts

Due to the nature of how blockchain-based currencies such as Bitcoin, Litecoin and Ethereum work, it is possible to generate a wallet or an account and send money to it, without the account ever having seen the network – you can generate and store it entirely offline, so no one could possibly steal it over the network.

When coins are being sent to an address, the blockchain merely stores the information “address X owns N coins”. Only in order to spend those coins again, one needs the private key associated with the address; the private key is used to sign a transaction which moves coins from the address to another one.

So all we need to do is use a software to generate our address and private key pairs.

Generating Wallets

We are going to use the tools provided by MyEtherWallet, BitAddress.org and LiteAddress.org for the respective currencies. You can just go there and do the generating on those websites – all generation happens client side, in the browser, and not on their servers.

However, we want to be really safe and sure that our keys never touch the net, so we go one step further. We are going to create our wallets on an air-gapped computer. An air-gapped computer is a computer which is not connected to the network.

Prerequisites

You need mainly two things – a computer which is not connected to the internet, and a means of safely and securely storing your generated keys. If you don’t have an extra computer around (which you totally should have, you can never have enough), you can use the one you are using right now, you just have to go offline for a while.

I will set up the key generation software on an extra RaspberryPi 3 I have lying around, and store my new wallet on a USB drive, and print it as a paper wallet. The USB drive will also be used to transfer the required software to the air-gapped computer.

Download the Software

For Ethereum, go to https://github.com/kvhnuke/etherwallet/releases/latest and download the distribution zip file (dist-vx.x.x.zip).

For Bitcoin, go to https://github.com/pointbiz/bitaddress.org/releases/latest and download the latest release source code archive (zip or tar.gz). (It is the same software for Bitcoin and Litecoin).

Store the archives you downloaded on a USB stick if you are going to transfer it to your air-gapped computer.

Working Offline

Setup an offline computer. All you need is a working browser, such as Chrome or Firefox. Transfer the software from the USB stick to your computer and unpack it.

For Ethereum, go to the unpacked archive and open index.html in your browser (by double-clicking it). You now have the same environment as on the websites mentioned before. Create your wallet as instructed.

Safe the encrypted JSON “UTC” key file. Then print your paper wallet – either generate a PDF or print it directly (obviously that requires a printer).

For Bitcoin, open bitcoinaddress.org.html in your browser. You need to generate some randomness by moving your mouse around. When done (counters go to 100%) you now have multiple options – generate a single wallet, a number of fancy looking paper walled, or just bulk wallets as a text file. Download the result and store or print it as needed.

You can now start to send money to your wallets.

Checking your wallet’s balance

You can check your wallet balance online. For Ethereum, you can enter your public address at etherscan.io. For Bitcoin, enter it at blockchain.info. No need to have your wallet online, as all the information needed is on the chain.

Secure Storage

You now have PDFs or pieces of paper with your *unencrypted* private key. No password is needed to spend money from your wallet with this key, so you have to keep it safe. And be sure to make a backup and store it in multiple locations (what happens if your house burns down?).

I hope you found this post helpful. If so, you can throw me a couple Satoshis via my donation address. Thank you 🙂