mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Implemented feedback
This commit is contained in:
parent
40f1d08684
commit
8cd7329c91
@ -20,7 +20,7 @@ class BalanceController {
|
|||||||
getPendingTransactions: this._getPendingTransactions.bind(this),
|
getPendingTransactions: this._getPendingTransactions.bind(this),
|
||||||
})
|
})
|
||||||
|
|
||||||
this.registerUpdates()
|
this._registerUpdates()
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateBalance () {
|
async updateBalance () {
|
||||||
@ -30,7 +30,7 @@ class BalanceController {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
registerUpdates () {
|
_registerUpdates () {
|
||||||
const update = this.updateBalance.bind(this)
|
const update = this.updateBalance.bind(this)
|
||||||
this.txController.on('submitted', update)
|
this.txController.on('submitted', update)
|
||||||
this.txController.on('confirmed', update)
|
this.txController.on('confirmed', update)
|
||||||
|
@ -19,19 +19,17 @@ class PendingBalanceCalculator {
|
|||||||
this.getPendingTransactions(),
|
this.getPendingTransactions(),
|
||||||
])
|
])
|
||||||
|
|
||||||
const balance = results[0]
|
const [ balance, pending ] = results
|
||||||
const pending = results[1]
|
|
||||||
|
|
||||||
if (!balance) return undefined
|
if (!balance) return undefined
|
||||||
|
|
||||||
const pendingValue = pending.reduce((total, tx) => {
|
const pendingValue = pending.reduce((total, tx) => {
|
||||||
return total.add(this.valueFor(tx))
|
return total.add(this.calculateMaxCost(tx))
|
||||||
}, new BN(0))
|
}, new BN(0))
|
||||||
|
|
||||||
return `0x${balance.sub(pendingValue).toString(16)}`
|
return `0x${balance.sub(pendingValue).toString(16)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
valueFor (tx) {
|
calculateMaxCost (tx) {
|
||||||
const txValue = tx.txParams.value
|
const txValue = tx.txParams.value
|
||||||
const value = this.hexToBn(txValue)
|
const value = this.hexToBn(txValue)
|
||||||
const gasPrice = this.hexToBn(tx.txParams.gasPrice)
|
const gasPrice = this.hexToBn(tx.txParams.gasPrice)
|
||||||
|
@ -11,7 +11,7 @@ const ether = '0x' + etherBn.toString(16)
|
|||||||
describe('PendingBalanceCalculator', function () {
|
describe('PendingBalanceCalculator', function () {
|
||||||
let balanceCalculator
|
let balanceCalculator
|
||||||
|
|
||||||
describe('#valueFor(tx)', function () {
|
describe('#calculateMaxCost(tx)', function () {
|
||||||
it('returns a BN for a given tx value', function () {
|
it('returns a BN for a given tx value', function () {
|
||||||
const txGen = new MockTxGen()
|
const txGen = new MockTxGen()
|
||||||
pendingTxs = txGen.generate({
|
pendingTxs = txGen.generate({
|
||||||
@ -24,7 +24,7 @@ describe('PendingBalanceCalculator', function () {
|
|||||||
}, { count: 1 })
|
}, { count: 1 })
|
||||||
|
|
||||||
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
|
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
|
||||||
const result = balanceCalculator.valueFor(pendingTxs[0])
|
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
|
||||||
assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
|
assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -40,8 +40,8 @@ describe('PendingBalanceCalculator', function () {
|
|||||||
}, { count: 1 })
|
}, { count: 1 })
|
||||||
|
|
||||||
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
|
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
|
||||||
const result = balanceCalculator.valueFor(pendingTxs[0])
|
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
|
||||||
assert.equal(result.toString(), '6', 'computes one ether')
|
assert.equal(result.toString(), '6', 'computes 6 wei of gas')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -82,15 +82,9 @@ describe('PendingBalanceCalculator', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
function generateBalanceCalcWith (transactions, providerStub = zeroBn) {
|
function generateBalanceCalcWith (transactions, providerStub = zeroBn) {
|
||||||
const getPendingTransactions = () => Promise.resolve(transactions)
|
const getPendingTransactions = async () => transactions
|
||||||
const getBalance = () => Promise.resolve(providerStub)
|
const getBalance = async () => providerStub
|
||||||
providerResultStub.result = providerStub
|
|
||||||
const provider = {
|
|
||||||
sendAsync: (_, cb) => { cb(undefined, providerResultStub) },
|
|
||||||
_blockTracker: {
|
|
||||||
getCurrentBlock: () => '0x11b568',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return new PendingBalanceCalculator({
|
return new PendingBalanceCalculator({
|
||||||
getBalance,
|
getBalance,
|
||||||
getPendingTransactions,
|
getPendingTransactions,
|
||||||
|
Loading…
Reference in New Issue
Block a user