20

I am just starting to dive into Web3. I am trying to display the various ERC20 token values for a given Ethereum Address.

The pieces I need to do this are:

  • Token Contract Address
  • Token Contract ABI

Are there any programmatic ways to get the Contract ABI given a Contract Address from the blockchain?

If not, it seems fair to assume the contract ABI is constant for any Token Contract... has anyone centralized a database that can be used to interact with these ERC20 tokens programmatically?

Are you able to interact with a Token Contract at all without the full ABI? For example, based on the ERC20 standard, every contract must have balanceOf(), can we call this function given an address and no ABI?

0

2 Answers 2

35

Thank you K.Fichter for the help, I just want to fully clarify the answer for anyone else who may run into this issue.

Let's say you want to get the balance of a token at an Ethereum Address. Using Web3 you would do the following:

var tokenContract = web3.eth.contract(contractABI).at(contractAddress)
var decimal = tokenContract.decimals()
var balance = tokenContract.balanceOf(address)
var adjustedBalance = balance / Math.pow(10, decimal)
var tokenName = tokenContract.name()
var tokenSymbol = tokenContract.symbol()

Then your final output could be something like this:

output = adjustedBalance + " " +  tokenSymbol + " (" + tokenName + ")";

I thought that I would need to load a new contractABI for each contractAddress because each token may have their own functions even if they follow the ERC20 standard. However, you only need to define the ABI for the functions you are using.

So in my example, you only need to define:

  1. balanceOf()
  2. decimals()
  3. name()
  4. symbol()

Assuming the token is following the ERC20 standard, these functions will always be the same, and can be represented with this ABI:

[
  {
    "constant": true,
    "inputs": [],
    "name": "name",
    "outputs": [
      {
        "name": "",
        "type": "string"
      }
    ],
    "payable": false,
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "decimals",
    "outputs": [
      {
        "name": "",
        "type": "uint8"
      }
    ],
    "payable": false,
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "_owner",
        "type": "address"
      }
    ],
    "name": "balanceOf",
    "outputs": [
      {
        "name": "balance",
        "type": "uint256"
      }
    ],
    "payable": false,
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "symbol",
    "outputs": [
      {
        "name": "",
        "type": "string"
      }
    ],
    "payable": false,
    "type": "function"
  }
]

Using this, you can connect to any contractAddress and be able to get the balance information, assuming they follow the ERC20 standard.

EDIT: I wrote a blog post about this here. I hope it helps others!

1
  • 3
    Thank you Shawn, I didn't know entire abi is not necessary to connect to the contract!!
    – Karan
    Commented Jul 4, 2018 at 14:23
9

If you're interacting with a standard ERC20 token, all of the contracts will share the same base ABI. This ABI is available online but can also be retrieved by implementing your own standard ERC20 and compiling the ABI.

ABI is really just an interface that tells Web3 how to create the "contract" object for you to interact with. It basically includes each field/function name and some meta data attached to them. Because the functions for standard ERC20 tokens should all match the standard as described here, these fields/functions and meta data will always match.

Basically, because ERC20 tokens will share the same ABI, you can just hard-code the standard ERC20 ABI into your app and use it for each token. However, you cannot get contract ABI without the contract's source code.

4
  • 1
    Thank you, this answered my question. To say it in a way that makes more sense to me: (1) The full ABI for a token is only available if you have the solidity source code, which is not on the blockchain decompiled. (2) You can use Etherscan.io as a basic api for contract ABIs. (3) You do NOT need the full ABI for a contract to interact with it. You only need to define the functions in the ABI which you want to interact with, and ERC20 has standardized this, so you can always have a baseline ABI when interacting with these tokens. Commented Nov 5, 2017 at 4:59
  • 1
    Yep! Sorry, I was focusing more on your use case but this is definitely a good question. You may want to consider submitting your own answer as I think it's a much clearer solution to your original question.
    – K. Fichter
    Commented Nov 5, 2017 at 5:03
  • Thanks for your help again. Let me know if anything in my answer looks wrong. Commented Nov 5, 2017 at 5:15
  • @ShawnTabrizi yep, it looks good!
    – K. Fichter
    Commented Nov 5, 2017 at 5:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.