mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
added a failing case and fixed it by refactoring everything to strings
This commit is contained in:
parent
d19c286ee8
commit
9132f08507
@ -25,12 +25,26 @@ describe('util', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
describe('parseBalance', function() {
|
describe('parseBalance', function() {
|
||||||
it('should render 0.01 eth correctly', function() {
|
it('should render 12.023 eth correctly', function() {
|
||||||
const input = 'A6DA46CCA6858000'
|
const input = 'A6DA46CCA6858000'
|
||||||
const output = util.parseBalance(input)
|
const output = util.parseBalance(input)
|
||||||
assert.deepEqual(output, ['12', '023'])
|
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() {
|
describe('addressSummary', function() {
|
||||||
it('should add case-sensitive checksum', function() {
|
it('should add case-sensitive checksum', function() {
|
||||||
|
@ -85,22 +85,20 @@ function weiToEth(bn) {
|
|||||||
|
|
||||||
// Takes hex, returns [beforeDecimal, afterDecimal]
|
// Takes hex, returns [beforeDecimal, afterDecimal]
|
||||||
function parseBalance(balance) {
|
function parseBalance(balance) {
|
||||||
if (!balance || balance === '0x0') return ['0', '0']
|
let beforeDecimal, afterDecimal
|
||||||
var wei = numericBalance(balance).toString(10)
|
let wei = numericBalance(balance).toString()
|
||||||
var eth = String(wei/valueTable['wei'])
|
let trailingZeros = /0+$/
|
||||||
var beforeDecimal = String(Math.floor(eth))
|
|
||||||
var afterDecimal
|
beforeDecimal = wei.length > 18 ? wei.slice(0, wei.length - 18) : '0'
|
||||||
if(eth.indexOf('.') > -1){
|
afterDecimal = ("000000000000000000" + wei).slice(-18).replace(trailingZeros, "")
|
||||||
afterDecimal = eth.slice(eth.indexOf('.') + 1)
|
if(afterDecimal == ""){afterDecimal = "0" }
|
||||||
}else{
|
|
||||||
afterDecimal = '0'
|
|
||||||
}
|
|
||||||
return [beforeDecimal, afterDecimal]
|
return [beforeDecimal, afterDecimal]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes wei hex, returns "None" or "${formattedAmount} ETH"
|
// Takes wei hex, returns "None" or "${formattedAmount} ETH"
|
||||||
function formatBalance(balance, decimalsToKeep) {
|
function formatBalance(balance, decimalsToKeep) {
|
||||||
var parsed = parseBalance(balance)
|
var parsed = parseBalance(balance)
|
||||||
|
console.log(parsed)
|
||||||
var beforeDecimal = parsed[0]
|
var beforeDecimal = parsed[0]
|
||||||
var afterDecimal = parsed[1]
|
var afterDecimal = parsed[1]
|
||||||
var formatted = "None"
|
var formatted = "None"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user