Fix valueFor test

feature/default_network_editable
Dan Finlay 7 years ago
parent 4058574436
commit 7b92268428
  1. 15
      app/scripts/lib/pending-balance-calculator.js
  2. 9
      test/unit/pending-balance-test.js

@ -15,32 +15,33 @@ class PendingBalanceCalculator {
this.getNetworkBalance(), this.getNetworkBalance(),
this.getPendingTransactions(), this.getPendingTransactions(),
]) ])
console.dir(results)
const balance = results[0] const balance = results[0]
const pending = results[1] const pending = results[1]
console.dir({ balance, pending })
console.dir(pending) console.dir(pending)
const pendingValue = pending.reduce(function (total, tx) { const pendingValue = pending.reduce((total, tx) => {
return total.add(this.valueFor(tx)) return total.add(this.valueFor(tx))
}, new BN(0)) }, new BN(0))
const balanceBn = new BN(normalize(balance)) console.log(`subtracting ${pendingValue.toString()} from ${balance.toString()}`)
console.log(`subtracting ${pendingValue.toString()} from ${balanceBn.toString()}`)
return `0x${ balanceBn.sub(pendingValue).toString(16) }` return `0x${ balance.sub(pendingValue).toString(16) }`
} }
valueFor (tx) { valueFor (tx) {
const txValue = tx.txParams.value const txValue = tx.txParams.value
const normalized = normalize(txValue).substring(2) const normalized = normalize(txValue).substring(2)
console.log({ txValue, normalized }) console.log({ txValue, normalized })
const value = new BN(normalize(txValue).substring(2), 16) const value = this.hexToBn(txValue)
return value return value
} }
hexToBn (hex) {
return new BN(normalize(hex).substring(2), 16)
}
} }
module.exports = PendingBalanceCalculator module.exports = PendingBalanceCalculator

@ -4,6 +4,7 @@ const MockTxGen = require('../lib/mock-tx-gen')
const BN = require('ethereumjs-util').BN const BN = require('ethereumjs-util').BN
let providerResultStub = {} let providerResultStub = {}
const zeroBn = new BN(0)
const etherBn = new BN(String(1e18)) const etherBn = new BN(String(1e18))
const ether = '0x' + etherBn.toString(16) const ether = '0x' + etherBn.toString(16)
@ -22,7 +23,7 @@ describe('PendingBalanceCalculator', function () {
} }
}, { count: 1 }) }, { count: 1 })
const balanceCalculator = generateBalaneCalcWith([], '0x0') const balanceCalculator = generateBalanceCalcWith([], zeroBn)
const result = balanceCalculator.valueFor(pendingTxs[0]) const result = balanceCalculator.valueFor(pendingTxs[0])
assert.equal(result.toString(), etherBn.toString(), 'computes one ether') assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
}) })
@ -31,7 +32,7 @@ describe('PendingBalanceCalculator', function () {
describe('if you have no pending txs and one ether', function () { describe('if you have no pending txs and one ether', function () {
beforeEach(function () { beforeEach(function () {
balanceCalculator = generateBalaneCalcWith([], ether) balanceCalculator = generateBalanceCalcWith([], zeroBn)
}) })
it('returns the network balance', async function () { it('returns the network balance', async function () {
@ -52,7 +53,7 @@ describe('PendingBalanceCalculator', function () {
} }
}, { count: 1 }) }, { count: 1 })
balanceCalculator = generateBalaneCalcWith(pendingTxs, ether) balanceCalculator = generateBalanceCalcWith(pendingTxs, etherBn)
}) })
it('returns the network balance', async function () { it('returns the network balance', async function () {
@ -69,7 +70,7 @@ describe('PendingBalanceCalculator', function () {
}) })
}) })
function generateBalaneCalcWith (transactions, providerStub = '0x0') { function generateBalanceCalcWith (transactions, providerStub = zeroBn) {
const getPendingTransactions = () => Promise.resolve(transactions) const getPendingTransactions = () => Promise.resolve(transactions)
const getBalance = () => Promise.resolve(providerStub) const getBalance = () => Promise.resolve(providerStub)
providerResultStub.result = providerStub providerResultStub.result = providerStub

Loading…
Cancel
Save