1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00

Merge pull request #299 from MetaMask/balances-bug-fix

added a failing case and fixed it by refactoring everything to strings
This commit is contained in:
Dan Finlay 2016-06-21 09:19:37 -07:00 committed by GitHub
commit cfc056e34b
2 changed files with 22 additions and 11 deletions

View File

@ -25,12 +25,26 @@ describe('util', function() {
})
})
describe('parseBalance', function() {
it('should render 0.01 eth correctly', function() {
it('should render 12.023 eth correctly', function() {
const input = 'A6DA46CCA6858000'
const output = util.parseBalance(input)
assert.deepEqual(output, ['12', '023'])
})
})
describe('parseBalance', function() {
it('should render 0.0000000342422 eth correctly', function() {
const input = '0x7F8FE81C0'
const output = util.parseBalance(input)
assert.deepEqual(output, ['0', '0000000342422'])
})
})
describe('parseBalance', function() {
it('should render 0 eth correctly', function() {
const input = '0x0'
const output = util.parseBalance(input)
assert.deepEqual(output, ['0', '0'])
})
})
describe('addressSummary', function() {
it('should add case-sensitive checksum', function() {

View File

@ -85,16 +85,13 @@ function weiToEth(bn) {
// Takes hex, returns [beforeDecimal, afterDecimal]
function parseBalance(balance) {
if (!balance || balance === '0x0') return ['0', '0']
var wei = numericBalance(balance).toString(10)
var eth = String(wei/valueTable['wei'])
var beforeDecimal = String(Math.floor(eth))
var afterDecimal
if(eth.indexOf('.') > -1){
afterDecimal = eth.slice(eth.indexOf('.') + 1)
}else{
afterDecimal = '0'
}
let beforeDecimal, afterDecimal
let wei = numericBalance(balance).toString()
let trailingZeros = /0+$/
beforeDecimal = wei.length > 18 ? wei.slice(0, wei.length - 18) : '0'
afterDecimal = ("000000000000000000" + wei).slice(-18).replace(trailingZeros, "")
if(afterDecimal == ""){afterDecimal = "0" }
return [beforeDecimal, afterDecimal]
}