You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.7 KiB
94 lines
2.7 KiB
7 years ago
|
const assert = require('assert')
|
||
7 years ago
|
const PendingBalanceCalculator = require('../../../app/scripts/lib/pending-balance-calculator')
|
||
|
const MockTxGen = require('../../lib/mock-tx-gen')
|
||
7 years ago
|
const BN = require('ethereumjs-util').BN
|
||
|
let providerResultStub = {}
|
||
|
|
||
7 years ago
|
const zeroBn = new BN(0)
|
||
7 years ago
|
const etherBn = new BN(String(1e18))
|
||
|
const ether = '0x' + etherBn.toString(16)
|
||
|
|
||
7 years ago
|
describe('PendingBalanceCalculator', function () {
|
||
7 years ago
|
let balanceCalculator, pendingTxs
|
||
7 years ago
|
|
||
7 years ago
|
describe('#calculateMaxCost(tx)', function () {
|
||
7 years ago
|
it('returns a BN for a given tx value', function () {
|
||
|
const txGen = new MockTxGen()
|
||
|
pendingTxs = txGen.generate({
|
||
|
status: 'submitted',
|
||
|
txParams: {
|
||
|
value: ether,
|
||
|
gasPrice: '0x0',
|
||
|
gas: '0x0',
|
||
|
}
|
||
|
}, { count: 1 })
|
||
|
|
||
7 years ago
|
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
|
||
7 years ago
|
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
|
||
7 years ago
|
assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
|
||
|
})
|
||
7 years ago
|
|
||
|
it('calculates gas costs as well', function () {
|
||
|
const txGen = new MockTxGen()
|
||
|
pendingTxs = txGen.generate({
|
||
|
status: 'submitted',
|
||
|
txParams: {
|
||
|
value: '0x0',
|
||
|
gasPrice: '0x2',
|
||
|
gas: '0x3',
|
||
|
}
|
||
|
}, { count: 1 })
|
||
|
|
||
|
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
|
||
7 years ago
|
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
|
||
|
assert.equal(result.toString(), '6', 'computes 6 wei of gas')
|
||
7 years ago
|
})
|
||
7 years ago
|
})
|
||
|
|
||
7 years ago
|
describe('if you have no pending txs and one ether', function () {
|
||
|
|
||
|
beforeEach(function () {
|
||
7 years ago
|
balanceCalculator = generateBalanceCalcWith([], etherBn)
|
||
7 years ago
|
})
|
||
|
|
||
7 years ago
|
it('returns the network balance', async function () {
|
||
7 years ago
|
const result = await balanceCalculator.getBalance()
|
||
7 years ago
|
assert.equal(result, ether, `gave ${result} needed ${ether}`)
|
||
7 years ago
|
})
|
||
|
})
|
||
7 years ago
|
|
||
|
describe('if you have a one ether pending tx and one ether', function () {
|
||
|
beforeEach(function () {
|
||
|
const txGen = new MockTxGen()
|
||
|
pendingTxs = txGen.generate({
|
||
|
status: 'submitted',
|
||
|
txParams: {
|
||
|
value: ether,
|
||
|
gasPrice: '0x0',
|
||
|
gas: '0x0',
|
||
|
}
|
||
|
}, { count: 1 })
|
||
|
|
||
7 years ago
|
balanceCalculator = generateBalanceCalcWith(pendingTxs, etherBn)
|
||
7 years ago
|
})
|
||
|
|
||
7 years ago
|
it('returns the subtracted result', async function () {
|
||
7 years ago
|
const result = await balanceCalculator.getBalance()
|
||
|
assert.equal(result, '0x0', `gave ${result} needed '0x0'`)
|
||
|
return true
|
||
|
})
|
||
|
|
||
|
})
|
||
7 years ago
|
})
|
||
|
|
||
7 years ago
|
function generateBalanceCalcWith (transactions, providerStub = zeroBn) {
|
||
7 years ago
|
const getPendingTransactions = async () => transactions
|
||
|
const getBalance = async () => providerStub
|
||
|
|
||
7 years ago
|
return new PendingBalanceCalculator({
|
||
7 years ago
|
getBalance,
|
||
7 years ago
|
getPendingTransactions,
|
||
|
})
|
||
|
}
|
||
7 years ago
|
|