diff options
author | Dan Finlay <dan@danfinlay.com> | 2016-04-20 09:56:22 +0800 |
---|---|---|
committer | Dan Finlay <dan@danfinlay.com> | 2016-04-20 09:56:22 +0800 |
commit | d6ab6bb4fa506c5fb9479b6e534ab74632c1b819 (patch) | |
tree | 752bba009f4a31636d74621431cda51495741693 /ui/app | |
parent | f79601ee58a07ec6275d4588845578795f550d84 (diff) | |
download | tangerine-wallet-browser-d6ab6bb4fa506c5fb9479b6e534ab74632c1b819.tar tangerine-wallet-browser-d6ab6bb4fa506c5fb9479b6e534ab74632c1b819.tar.gz tangerine-wallet-browser-d6ab6bb4fa506c5fb9479b6e534ab74632c1b819.tar.bz2 tangerine-wallet-browser-d6ab6bb4fa506c5fb9479b6e534ab74632c1b819.tar.lz tangerine-wallet-browser-d6ab6bb4fa506c5fb9479b6e534ab74632c1b819.tar.xz tangerine-wallet-browser-d6ab6bb4fa506c5fb9479b6e534ab74632c1b819.tar.zst tangerine-wallet-browser-d6ab6bb4fa506c5fb9479b6e534ab74632c1b819.zip |
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.
Diffstat (limited to 'ui/app')
-rw-r--r-- | ui/app/send.js | 8 | ||||
-rw-r--r-- | ui/app/util.js | 14 |
2 files changed, 16 insertions, 6 deletions
diff --git a/ui/app/send.js b/ui/app/send.js index ad4a27604..d34accddc 100644 --- a/ui/app/send.js +++ b/ui/app/send.js @@ -108,11 +108,11 @@ SendTransactionScreen.prototype.back = function() { SendTransactionScreen.prototype.onSubmit = function(event) { var recipient = document.querySelector('input.address').value - var amount = new ethUtil.BN(document.querySelector('input.ether').value, 10) + + var inputAmount = parseFloat(document.querySelector('input.ether').value) var currency = document.querySelector('select.currency').value - var txData = document.querySelector('textarea.txData').value + var value = util.normalizeNumberToWei(inputAmount, currency) - var value = util.normalizeToWei(amount, currency) var balance = this.props.balance if (value.gt(balance)) { @@ -132,6 +132,8 @@ SendTransactionScreen.prototype.onSubmit = function(event) { from: this.props.address, value: '0x' + value.toString(16), } + + var txData = document.querySelector('textarea.txData').value if (txData) txParams.data = txData this.props.dispatch(actions.signTx(txParams)) diff --git a/ui/app/util.js b/ui/app/util.js index 18862fade..bacf00c66 100644 --- a/ui/app/util.js +++ b/ui/app/util.js @@ -28,6 +28,7 @@ module.exports = { ethToWei: ethToWei, weiToEth: weiToEth, normalizeToWei: normalizeToWei, + normalizeNumberToWei: normalizeNumberToWei, valueTable: valueTable, bnTable: bnTable, } @@ -85,13 +86,20 @@ function dataSize(data) { // returns a BN in wei function normalizeToWei(amount, currency) { try { - var ether = amount.div(bnTable[currency]) - var wei = ether.mul(bnTable.wei) - return wei + return amount.mul(bnTable.wei).div(bnTable[currency]) } catch (e) {} 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) { var date = new Date(ms) var month = date.getMonth() |