mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Add basic test for valueFor
This commit is contained in:
parent
b6e8791bc2
commit
4058574436
@ -6,13 +6,13 @@ class PendingBalanceCalculator {
|
||||
|
||||
constructor ({ getBalance, getPendingTransactions }) {
|
||||
this.getPendingTransactions = getPendingTransactions
|
||||
this.getBalance = getBalance
|
||||
this.getNetworkBalance = getBalance
|
||||
}
|
||||
|
||||
async getBalance() {
|
||||
console.log('getting balance')
|
||||
const results = await Promise.all([
|
||||
this.getBalance(),
|
||||
this.getNetworkBalance(),
|
||||
this.getPendingTransactions(),
|
||||
])
|
||||
console.dir(results)
|
||||
@ -21,18 +21,23 @@ class PendingBalanceCalculator {
|
||||
const pending = results[1]
|
||||
|
||||
console.dir({ balance, pending })
|
||||
console.dir(pending)
|
||||
|
||||
const pendingValue = pending.reduce(function (total, tx) {
|
||||
return total.sub(this.valueFor(tx))
|
||||
return total.add(this.valueFor(tx))
|
||||
}, new BN(0))
|
||||
|
||||
const balanceBn = new BN(normalize(balance))
|
||||
console.log(`subtracting ${pendingValue.toString()} from ${balanceBn.toString()}`)
|
||||
|
||||
return `0x${ balanceBn.sub(pendingValue).toString(16) }`
|
||||
}
|
||||
|
||||
valueFor (tx) {
|
||||
const value = new BN(normalize(tx.txParams.value))
|
||||
const txValue = tx.txParams.value
|
||||
const normalized = normalize(txValue).substring(2)
|
||||
console.log({ txValue, normalized })
|
||||
const value = new BN(normalize(txValue).substring(2), 16)
|
||||
return value
|
||||
}
|
||||
|
||||
|
@ -4,11 +4,31 @@ const MockTxGen = require('../lib/mock-tx-gen')
|
||||
const BN = require('ethereumjs-util').BN
|
||||
let providerResultStub = {}
|
||||
|
||||
const etherBn = new BN(String(1e18))
|
||||
const ether = '0x' + etherBn.toString(16)
|
||||
|
||||
describe('PendingBalanceCalculator', function () {
|
||||
let balanceCalculator
|
||||
|
||||
describe('#valueFor(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 = generateBalaneCalcWith([], '0x0')
|
||||
const result = balanceCalculator.valueFor(pendingTxs[0])
|
||||
assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
|
||||
})
|
||||
})
|
||||
|
||||
describe('if you have no pending txs and one ether', function () {
|
||||
const ether = '0x' + (new BN(String(1e18))).toString(16)
|
||||
|
||||
beforeEach(function () {
|
||||
balanceCalculator = generateBalaneCalcWith([], ether)
|
||||
@ -21,8 +41,6 @@ describe('PendingBalanceCalculator', function () {
|
||||
})
|
||||
|
||||
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({
|
||||
@ -40,6 +58,7 @@ describe('PendingBalanceCalculator', function () {
|
||||
it('returns the network balance', async function () {
|
||||
console.log('one')
|
||||
console.dir(balanceCalculator)
|
||||
console.dir(balanceCalculator.getBalance.toString())
|
||||
const result = await balanceCalculator.getBalance()
|
||||
console.log('two')
|
||||
console.dir(result)
|
||||
|
Loading…
Reference in New Issue
Block a user