1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

test not passing

This commit is contained in:
Dan Finlay 2017-09-07 11:59:15 -07:00
parent 74f7fc4613
commit b6e8791bc2
2 changed files with 49 additions and 4 deletions

View File

@ -1,5 +1,6 @@
const BN = require('ethereumjs-util').BN const BN = require('ethereumjs-util').BN
const EthQuery = require('ethjs-query') const EthQuery = require('ethjs-query')
const normalize = require('eth-sig-util').normalize
class PendingBalanceCalculator { class PendingBalanceCalculator {
@ -9,15 +10,30 @@ class PendingBalanceCalculator {
} }
async getBalance() { async getBalance() {
console.log('getting balance')
const results = await Promise.all([ const results = await Promise.all([
this.getBalance(), this.getBalance(),
this.getPendingTransactions(), this.getPendingTransactions(),
]) ])
console.dir(results)
const balance = results[0] const balance = results[0]
const pending = results[1] const pending = results[1]
return balance console.dir({ balance, pending })
const pendingValue = pending.reduce(function (total, tx) {
return total.sub(this.valueFor(tx))
}, new BN(0))
const balanceBn = new BN(normalize(balance))
return `0x${ balanceBn.sub(pendingValue).toString(16) }`
}
valueFor (tx) {
const value = new BN(normalize(tx.txParams.value))
return value
} }
} }

View File

@ -5,20 +5,49 @@ const BN = require('ethereumjs-util').BN
let providerResultStub = {} let providerResultStub = {}
describe('PendingBalanceCalculator', function () { describe('PendingBalanceCalculator', function () {
let nonceTracker let balanceCalculator
describe('if you have no pending txs and one ether', function () { describe('if you have no pending txs and one ether', function () {
const ether = '0x' + (new BN(String(1e18))).toString(16) const ether = '0x' + (new BN(String(1e18))).toString(16)
beforeEach(function () { beforeEach(function () {
nonceTracker = generateBalaneCalcWith([], ether) balanceCalculator = generateBalaneCalcWith([], ether)
}) })
it('returns the network balance', async function () { it('returns the network balance', async function () {
const result = await nonceTracker.getBalance() const result = await balanceCalculator.getBalance()
assert.equal(result, ether, `gave ${result} needed ${ether}`) assert.equal(result, ether, `gave ${result} needed ${ether}`)
}) })
}) })
describe('if you have a one ether pending tx and one ether', function () {
const ether = '0x' + (new BN(String(1e18))).toString(16)
beforeEach(function () {
const txGen = new MockTxGen()
pendingTxs = txGen.generate({
status: 'submitted',
txParams: {
value: ether,
gasPrice: '0x0',
gas: '0x0',
}
}, { count: 1 })
balanceCalculator = generateBalaneCalcWith(pendingTxs, ether)
})
it('returns the network balance', async function () {
console.log('one')
console.dir(balanceCalculator)
const result = await balanceCalculator.getBalance()
console.log('two')
console.dir(result)
assert.equal(result, '0x0', `gave ${result} needed '0x0'`)
return true
})
})
}) })
function generateBalaneCalcWith (transactions, providerStub = '0x0') { function generateBalaneCalcWith (transactions, providerStub = '0x0') {