Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
fix(Eth): higher precision arithmetic for sweep amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Thore3 committed Aug 31, 2017
1 parent c982cea commit b8db591
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/eth/eth-tx-builder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const EthereumTx = require('ethereumjs-tx');
const util = require('ethereumjs-util');
const API = require('../api');
const { toWei, fromWei } = require('../helpers');
const { toWei, fromWei, toBigNumber, bnMax, bnToBuffer } = require('../helpers');

const MAINNET = 1;

Expand Down Expand Up @@ -34,13 +34,13 @@ class EthTxBuilder {
}

setValue (amount) {
this._tx.value = parseInt(toWei(amount, 'ether'));
this._tx.value = bnToBuffer(toWei(toBigNumber(amount), 'ether'));
this.update();
return this;
}

setGasPrice (gasPrice) {
this._tx.gasPrice = parseInt(toWei(gasPrice, 'gwei'));
this._tx.gasPrice = bnToBuffer(toWei(toBigNumber(gasPrice), 'gwei'));
this.update();
return this;
}
Expand All @@ -54,7 +54,7 @@ class EthTxBuilder {
setSweep () {
this.setValue(0);
let balance = this._account.wei;
let amount = Math.max(balance.sub(this._tx.getUpfrontCost()), 0);
let amount = bnMax(balance.sub(this._tx.getUpfrontCost()), 0);
this.setValue(fromWei(amount, 'ether'));
return this;
}
Expand Down
10 changes: 10 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,16 @@ Helpers.toBigNumber = function (x) {
return Helpers.isBigNumber(x) ? x : new BigNumber((x || 0).toString());
};

Helpers.bnMax = function (a, b) {
return BigNumber.max(a, b);
};

Helpers.bnToBuffer = function (bn) {
let hex = bn.toString(16);
if (hex.length % 2 !== 0) hex = '0' + hex;
return Buffer.from(hex, 'hex');
};

Helpers.isEtherAddress = function (address) {
return (
ethUtil.isValidChecksumAddress(address) ||
Expand Down
9 changes: 9 additions & 0 deletions tests/eth/eth-tx-builder.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable semi */
const EthTxBuilder = require('../../src/eth/eth-tx-builder')
const EthAccount = require('../../src/eth/eth-account')
const util = require('ethereumjs-util');

describe('EthTxBuilder', () => {
const wallet = {
Expand Down Expand Up @@ -80,6 +81,14 @@ describe('EthTxBuilder', () => {
expect(payment.amount).toEqual(0.099559)
expect(payment.fee).toEqual(0.000441)
})

it('should sweep with a very large value', () => {
account.setData({ balance: '19991027158563527', nonce: 3 })
payment.setSweep()
expect(new util.BN(payment._tx.value).toString()).toEqual('19550027158563527')
expect(payment.amount).toEqual(0.019550027158563528)
expect(payment.fee).toEqual(0.000441)
})
})

describe('.sign()', () => {
Expand Down

0 comments on commit b8db591

Please sign in to comment.