Skip to content
Permalink
Browse files

token relay

  • Loading branch information...
elaineo committed Mar 20, 2017
1 parent d9ba5a4 commit 27cbcd9a2fdd3d147f64faa47d20563ade29df16
Showing with 55 additions and 0 deletions.
  1. +55 −0 routes/token.js
@@ -0,0 +1,55 @@
#!/usr/bin/env node

/*
Endpoint for client interface with ERC-20 tokens
*/

var eth = require('./web3relay').eth;

var BigNumber = require('bignumber.js');
var etherUnits = require(__lib + "etherUnits.js")

const ABI = [{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"}];

const Contract = eth.contract(ABI);


module.exports = function(req, res){
console.log(req.body)

var contractAddress = req.body.address;

var Token = Contract.at(contractAddress);

if (!("action" in req.body))
res.status(400).send();
else if (req.body.action=="info") {
try {
var actualBalance = eth.getBalance(contractAddress);
actualBalance = etherUnits.toEther(actualBalance, 'wei');
var totalSupply = Token.totalSupply();
totalSupply = etherUnits.toEther(totalSupply, 'wei')*100;
var tokenData = {
"balance": actualBalance,
"total_supply": totalSupply
}
res.write(JSON.stringify(tokenData));
res.end();
} catch (e) {
console.error(e);
}
} else if (req.body.action=="balanceOf") {
var addr = req.body.addr.toLowerCase();
try {
var tokens = Token.balanceOf(addr);
tokens = etherUnits.toEther(tokens, 'wei')*100;
res.write(JSON.stringify({"tokens": tokens}));
res.end();
} catch (e) {
console.error(e);
}
}

};

const MAX_ENTRIES = 50;

0 comments on commit 27cbcd9

Please sign in to comment.
You can’t perform that action at this time.