2020-01-09 04:34:58 +01:00
|
|
|
import assert from 'assert'
|
|
|
|
import PendingBalanceCalculator from '../../../app/scripts/lib/pending-balance-calculator'
|
|
|
|
import MockTxGen from '../../lib/mock-tx-gen'
|
|
|
|
import { BN } from 'ethereumjs-util'
|
2017-09-06 23:27:21 +02:00
|
|
|
|
2017-09-07 21:43:10 +02:00
|
|
|
const zeroBn = new BN(0)
|
2017-09-07 21:30:25 +02:00
|
|
|
const etherBn = new BN(String(1e18))
|
|
|
|
const ether = '0x' + etherBn.toString(16)
|
|
|
|
|
2017-09-06 23:27:21 +02:00
|
|
|
describe('PendingBalanceCalculator', function () {
|
2017-10-02 20:11:54 +02:00
|
|
|
let balanceCalculator, pendingTxs
|
2017-09-06 23:27:21 +02:00
|
|
|
|
2017-09-25 23:36:49 +02:00
|
|
|
describe('#calculateMaxCost(tx)', function () {
|
2017-09-07 21:30:25 +02:00
|
|
|
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',
|
2018-07-03 00:49:33 +02:00
|
|
|
},
|
2017-09-07 21:30:25 +02:00
|
|
|
}, { count: 1 })
|
|
|
|
|
2017-09-07 21:43:10 +02:00
|
|
|
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
|
2017-09-25 23:36:49 +02:00
|
|
|
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
|
2017-09-07 21:30:25 +02:00
|
|
|
assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
|
|
|
|
})
|
2017-09-07 21:52:25 +02:00
|
|
|
|
|
|
|
it('calculates gas costs as well', function () {
|
|
|
|
const txGen = new MockTxGen()
|
|
|
|
pendingTxs = txGen.generate({
|
|
|
|
status: 'submitted',
|
|
|
|
txParams: {
|
|
|
|
value: '0x0',
|
|
|
|
gasPrice: '0x2',
|
|
|
|
gas: '0x3',
|
2018-07-03 00:49:33 +02:00
|
|
|
},
|
2017-09-07 21:52:25 +02:00
|
|
|
}, { count: 1 })
|
|
|
|
|
|
|
|
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
|
2017-09-25 23:36:49 +02:00
|
|
|
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
|
|
|
|
assert.equal(result.toString(), '6', 'computes 6 wei of gas')
|
2017-09-07 21:52:25 +02:00
|
|
|
})
|
2017-09-07 21:30:25 +02:00
|
|
|
})
|
|
|
|
|
2017-09-06 23:27:21 +02:00
|
|
|
describe('if you have no pending txs and one ether', function () {
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-09-07 21:47:27 +02:00
|
|
|
balanceCalculator = generateBalanceCalcWith([], etherBn)
|
2017-09-06 23:27:21 +02:00
|
|
|
})
|
|
|
|
|
2017-09-06 23:36:15 +02:00
|
|
|
it('returns the network balance', async function () {
|
2017-09-07 20:59:15 +02:00
|
|
|
const result = await balanceCalculator.getBalance()
|
2017-09-06 23:36:15 +02:00
|
|
|
assert.equal(result, ether, `gave ${result} needed ${ether}`)
|
2017-09-06 23:27:21 +02:00
|
|
|
})
|
|
|
|
})
|
2017-09-07 20:59:15 +02:00
|
|
|
|
|
|
|
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',
|
2018-07-03 00:49:33 +02:00
|
|
|
},
|
2017-09-07 20:59:15 +02:00
|
|
|
}, { count: 1 })
|
|
|
|
|
2017-09-07 21:43:10 +02:00
|
|
|
balanceCalculator = generateBalanceCalcWith(pendingTxs, etherBn)
|
2017-09-07 20:59:15 +02:00
|
|
|
})
|
|
|
|
|
2017-09-07 21:53:30 +02:00
|
|
|
it('returns the subtracted result', async function () {
|
2017-09-07 20:59:15 +02:00
|
|
|
const result = await balanceCalculator.getBalance()
|
|
|
|
assert.equal(result, '0x0', `gave ${result} needed '0x0'`)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
2017-09-06 23:27:21 +02:00
|
|
|
})
|
|
|
|
|
2017-09-07 21:43:10 +02:00
|
|
|
function generateBalanceCalcWith (transactions, providerStub = zeroBn) {
|
2017-09-25 23:36:49 +02:00
|
|
|
const getPendingTransactions = async () => transactions
|
|
|
|
const getBalance = async () => providerStub
|
|
|
|
|
2017-09-06 23:27:21 +02:00
|
|
|
return new PendingBalanceCalculator({
|
2017-09-06 23:36:15 +02:00
|
|
|
getBalance,
|
2017-09-06 23:27:21 +02:00
|
|
|
getPendingTransactions,
|
|
|
|
})
|
|
|
|
}
|
2017-09-13 00:06:19 +02:00
|
|
|
|