1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 03:20:23 +01:00
metamask-extension/test/unit/app/pending-balance-test.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-09-06 23:27:21 +02:00
const assert = require('assert')
const PendingBalanceCalculator = require('../../../app/scripts/lib/pending-balance-calculator')
const MockTxGen = require('../../lib/mock-tx-gen')
2017-09-06 23:27:21 +02:00
const BN = require('ethereumjs-util').BN
let providerResultStub = {}
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',
}
}, { 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',
}
}, { 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',
}
}, { 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