mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 09:57:02 +01:00
Fix floating point input bug
When sending a transaction, we were converting to BN before handling decimals, which meant we were losing any precision past a decimal point, since BN does not handle decimals! Put this numeric normalization into a utility function with a test around it and got it working.
This commit is contained in:
parent
f79601ee58
commit
d6ab6bb4fa
@ -82,33 +82,47 @@ describe('util', function() {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('#normalizeToWei', function() {
|
describe('normalizing values', function() {
|
||||||
it('should convert an eth to the appropriate equivalent values', function() {
|
|
||||||
var valueTable = {
|
|
||||||
wei: '1000000000000000000',
|
|
||||||
kwei: '1000000000000000',
|
|
||||||
mwei: '1000000000000',
|
|
||||||
gwei: '1000000000',
|
|
||||||
szabo: '1000000',
|
|
||||||
finney:'1000',
|
|
||||||
ether: '1',
|
|
||||||
kether:'0.001',
|
|
||||||
mether:'0.000001',
|
|
||||||
// AUDIT: We're getting BN numbers on these ones.
|
|
||||||
// I think they're big enough to ignore for now.
|
|
||||||
// gether:'0.000000001',
|
|
||||||
// tether:'0.000000000001',
|
|
||||||
}
|
|
||||||
var oneEthBn = new ethUtil.BN(ethInWei, 10)
|
|
||||||
|
|
||||||
for(var currency in valueTable) {
|
describe('#normalizeToWei', function() {
|
||||||
|
it('should convert an eth to the appropriate equivalent values', function() {
|
||||||
|
var valueTable = {
|
||||||
|
wei: '1000000000000000000',
|
||||||
|
kwei: '1000000000000000',
|
||||||
|
mwei: '1000000000000',
|
||||||
|
gwei: '1000000000',
|
||||||
|
szabo: '1000000',
|
||||||
|
finney:'1000',
|
||||||
|
ether: '1',
|
||||||
|
// kether:'0.001',
|
||||||
|
// mether:'0.000001',
|
||||||
|
// AUDIT: We're getting BN numbers on these ones.
|
||||||
|
// I think they're big enough to ignore for now.
|
||||||
|
// gether:'0.000000001',
|
||||||
|
// tether:'0.000000000001',
|
||||||
|
}
|
||||||
|
var oneEthBn = new ethUtil.BN(ethInWei, 10)
|
||||||
|
|
||||||
var value = new ethUtil.BN(valueTable[currency], 10)
|
for(var currency in valueTable) {
|
||||||
var output = util.normalizeToWei(value, currency)
|
|
||||||
assert.equal(output.toString(10), valueTable.wei, `value of ${output.toString(10)} ${currency} should convert to ${oneEthBn}`)
|
|
||||||
|
|
||||||
}
|
var value = new ethUtil.BN(valueTable[currency], 10)
|
||||||
|
var output = util.normalizeToWei(value, currency)
|
||||||
|
assert.equal(output.toString(10), valueTable.wei, `value of ${output.toString(10)} ${currency} should convert to ${oneEthBn}`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#normalizeNumberToWei', function() {
|
||||||
|
|
||||||
|
it('should convert a kwei number to the appropriate equivalent wei', function() {
|
||||||
|
var result = util.normalizeNumberToWei(1.111, 'kwei')
|
||||||
|
assert.equal(result.toString(10), '1111', 'accepts decimals')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should convert a ether number to the appropriate equivalent wei', function() {
|
||||||
|
var result = util.normalizeNumberToWei(1.111, 'ether')
|
||||||
|
assert.equal(result.toString(10), '1111000000000000000', 'accepts decimals')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
@ -108,11 +108,11 @@ SendTransactionScreen.prototype.back = function() {
|
|||||||
|
|
||||||
SendTransactionScreen.prototype.onSubmit = function(event) {
|
SendTransactionScreen.prototype.onSubmit = function(event) {
|
||||||
var recipient = document.querySelector('input.address').value
|
var recipient = document.querySelector('input.address').value
|
||||||
var amount = new ethUtil.BN(document.querySelector('input.ether').value, 10)
|
|
||||||
var currency = document.querySelector('select.currency').value
|
|
||||||
var txData = document.querySelector('textarea.txData').value
|
|
||||||
|
|
||||||
var value = util.normalizeToWei(amount, currency)
|
var inputAmount = parseFloat(document.querySelector('input.ether').value)
|
||||||
|
var currency = document.querySelector('select.currency').value
|
||||||
|
var value = util.normalizeNumberToWei(inputAmount, currency)
|
||||||
|
|
||||||
var balance = this.props.balance
|
var balance = this.props.balance
|
||||||
|
|
||||||
if (value.gt(balance)) {
|
if (value.gt(balance)) {
|
||||||
@ -132,6 +132,8 @@ SendTransactionScreen.prototype.onSubmit = function(event) {
|
|||||||
from: this.props.address,
|
from: this.props.address,
|
||||||
value: '0x' + value.toString(16),
|
value: '0x' + value.toString(16),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var txData = document.querySelector('textarea.txData').value
|
||||||
if (txData) txParams.data = txData
|
if (txData) txParams.data = txData
|
||||||
|
|
||||||
this.props.dispatch(actions.signTx(txParams))
|
this.props.dispatch(actions.signTx(txParams))
|
||||||
|
@ -28,6 +28,7 @@ module.exports = {
|
|||||||
ethToWei: ethToWei,
|
ethToWei: ethToWei,
|
||||||
weiToEth: weiToEth,
|
weiToEth: weiToEth,
|
||||||
normalizeToWei: normalizeToWei,
|
normalizeToWei: normalizeToWei,
|
||||||
|
normalizeNumberToWei: normalizeNumberToWei,
|
||||||
valueTable: valueTable,
|
valueTable: valueTable,
|
||||||
bnTable: bnTable,
|
bnTable: bnTable,
|
||||||
}
|
}
|
||||||
@ -85,13 +86,20 @@ function dataSize(data) {
|
|||||||
// returns a BN in wei
|
// returns a BN in wei
|
||||||
function normalizeToWei(amount, currency) {
|
function normalizeToWei(amount, currency) {
|
||||||
try {
|
try {
|
||||||
var ether = amount.div(bnTable[currency])
|
return amount.mul(bnTable.wei).div(bnTable[currency])
|
||||||
var wei = ether.mul(bnTable.wei)
|
|
||||||
return wei
|
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
return amount
|
return amount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var multiple = new ethUtil.BN('1000', 10)
|
||||||
|
function normalizeNumberToWei(n, currency) {
|
||||||
|
var enlarged = n * 1000
|
||||||
|
console.log(`Enlarged, we have ${enlarged}`)
|
||||||
|
var amount = new ethUtil.BN(String(enlarged), 10)
|
||||||
|
console.log("Amount inside is "+ amount.toString(10))
|
||||||
|
return normalizeToWei(amount, currency).div(multiple)
|
||||||
|
}
|
||||||
|
|
||||||
function readableDate(ms) {
|
function readableDate(ms) {
|
||||||
var date = new Date(ms)
|
var date = new Date(ms)
|
||||||
var month = date.getMonth()
|
var month = date.getMonth()
|
||||||
|
Loading…
Reference in New Issue
Block a user