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:
balanceOf()
decimals()
name()
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!