mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-26 20:39:08 +01:00
4f3fc95d50
* Use @metamask/eslint-config@1.1.0 * Use eslint-plugin-mocha@6.2.2 * Mark root ESLint config as root * Update Mocha ESLint rules with shared ESLint config
86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
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'
|
|
|
|
const zeroBn = new BN(0)
|
|
const etherBn = new BN(String(1e18))
|
|
const ether = '0x' + etherBn.toString(16)
|
|
|
|
describe('PendingBalanceCalculator', function () {
|
|
let balanceCalculator, pendingTxs
|
|
|
|
describe('#calculateMaxCost(tx)', function () {
|
|
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 })
|
|
|
|
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
|
|
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
|
|
assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
|
|
})
|
|
|
|
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)
|
|
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
|
|
assert.equal(result.toString(), '6', 'computes 6 wei of gas')
|
|
})
|
|
})
|
|
|
|
describe('if you have no pending txs and one ether', function () {
|
|
it('returns the network balance', async function () {
|
|
balanceCalculator = generateBalanceCalcWith([], etherBn)
|
|
const result = await balanceCalculator.getBalance()
|
|
assert.equal(result, ether, `gave ${result} needed ${ether}`)
|
|
})
|
|
})
|
|
|
|
describe('if you have a one ether pending tx and one ether', function () {
|
|
it('returns the subtracted result', async function () {
|
|
const txGen = new MockTxGen()
|
|
pendingTxs = txGen.generate({
|
|
status: 'submitted',
|
|
txParams: {
|
|
value: ether,
|
|
gasPrice: '0x0',
|
|
gas: '0x0',
|
|
},
|
|
}, { count: 1 })
|
|
|
|
balanceCalculator = generateBalanceCalcWith(pendingTxs, etherBn)
|
|
|
|
const result = await balanceCalculator.getBalance()
|
|
assert.equal(result, '0x0', `gave ${result} needed '0x0'`)
|
|
return true
|
|
})
|
|
})
|
|
})
|
|
|
|
function generateBalanceCalcWith (transactions, providerStub = zeroBn) {
|
|
const getPendingTransactions = async () => transactions
|
|
const getBalance = async () => providerStub
|
|
|
|
return new PendingBalanceCalculator({
|
|
getBalance,
|
|
getPendingTransactions,
|
|
})
|
|
}
|
|
|