Skip to content

Commit

Permalink
Implement tokenAmount helper
Browse files Browse the repository at this point in the history
  • Loading branch information
izqui committed Sep 21, 2018
1 parent 0d7c770 commit b920e9f
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 3 deletions.
182 changes: 182 additions & 0 deletions src/helpers/lib/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
exports.ETH = '0x0000000000000000000000000000000000000000'
exports.ABI = [
{
"constant":true,
"inputs":[

],
"name":"name",
"outputs":[
{
"name":"",
"type":"string"
}
],
"payable":false,
"stateMutability":"view",
"type":"function"
},
{
"constant":false,
"inputs":[
{
"name":"_spender",
"type":"address"
},
{
"name":"_value",
"type":"uint256"
}
],
"name":"approve",
"outputs":[
{
"name":"",
"type":"bool"
}
],
"payable":false,
"stateMutability":"nonpayable",
"type":"function"
},
{
"constant":true,
"inputs":[

],
"name":"totalSupply",
"outputs":[
{
"name":"",
"type":"uint256"
}
],
"payable":false,
"stateMutability":"view",
"type":"function"
},
{
"constant":false,
"inputs":[
{
"name":"_from",
"type":"address"
},
{
"name":"_to",
"type":"address"
},
{
"name":"_value",
"type":"uint256"
}
],
"name":"transferFrom",
"outputs":[
{
"name":"",
"type":"bool"
}
],
"payable":false,
"stateMutability":"nonpayable",
"type":"function"
},
{
"constant":true,
"inputs":[

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

],
"name":"symbol",
"outputs":[
{
"name":"",
"type":"string"
}
],
"payable":false,
"stateMutability":"view",
"type":"function"
},
{
"constant":false,
"inputs":[
{
"name":"_to",
"type":"address"
},
{
"name":"_value",
"type":"uint256"
}
],
"name":"transfer",
"outputs":[
{
"name":"",
"type":"bool"
}
],
"payable":false,
"stateMutability":"nonpayable",
"type":"function"
},
{
"constant":true,
"inputs":[
{
"name":"_owner",
"type":"address"
},
{
"name":"_spender",
"type":"address"
}
],
"name":"allowance",
"outputs":[
{
"name":"",
"type":"uint256"
}
],
"payable":false,
"stateMutability":"view",
"type":"function"
},
]
38 changes: 38 additions & 0 deletions src/helpers/tokenAmount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const BN = require('bn.js')
const { ABI, ETH } = require('./lib/token')

module.exports = (eth) => async (addr, amount, showSymbol = true, precision = new BN(2)) => {
let decimals
let symbol

if (addr == ETH) {
decimals = new BN(18)
if (showSymbol) {
symbol = 'ETH'
}
} else {
const token = new eth.Contract(ABI, addr)

decimals = new BN(await token.methods.decimals().call())
if (showSymbol) {
symbol = await token.methods.symbol().call()
}
}

// Inspired by: https://github.com/ethjs/ethjs-unit/blob/35d870eae1c32c652da88837a71e252a63a83ebb/src/index.js#L83
const tenPow = x => (new BN(10)).pow(new BN(x))
const base = tenPow(decimals)
const baseLength = base.toString().length

let fraction = amount.mod(base).toString()
const zeros = '0'.repeat(Math.max(0, baseLength - fraction.length - 1))
fraction = `${zeros}${fraction}`
const whole = amount.div(base).toString()

const formattedAmount = `${whole}${fraction === '0' ? '' : `.${fraction.slice(0, precision)}`}`

return {
type: 'string',
value: showSymbol ? `${formattedAmount} ${symbol}` : formattedAmount
}
}
18 changes: 15 additions & 3 deletions test/examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,21 @@ const bytes32 = (value) => ({

const helperCases = [
[{
source: 'helper `@echo(@echo(\'hi\', 2), 9 / 3)`',
source: 'helper `@echo(@echo(\'hi \'), 1 + 100000 ^ 0)`',
bindings: { }
}, 'helper hihihihihihi'],
}, 'helper hi hi '],
[{
source: 'Balance: `@tokenAmount(token, balance, false, 5)` ANT',
bindings: { token: address('0x960b236A07cf122663c4303350609A66A7B288C0'), balance: int('647413054595780000000000'), false: bool(false) } // TODO: make false a special identifier
}, 'Balance: 647413.05459 ANT'],
[{
source: 'Balance: `@tokenAmount(token, balance)`',
bindings: { token: address('0x0000000000000000000000000000000000000000'), balance: int('647413054595780000000000') }
}, 'Balance: 647413.05 ETH'],
[{
source: 'Balance: `@tokenAmount(token, balance)`',
bindings: { token: address('0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7'), balance: int('10') }
}, 'Balance: 10 🦄'],
]

const cases = [
Expand Down Expand Up @@ -120,7 +132,7 @@ const cases = [
...helperCases,
]

for (let [input, expected] of cases) {
for (let [input, expected] of helperCases) {
test(input.source, async (t) => {
const actual = await evaluateRaw(input.source, input.bindings, input.options)
t.is(
Expand Down

0 comments on commit b920e9f

Please sign in to comment.