mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Improve insufficient balance checking in retry loop
This commit is contained in:
parent
d97c6533b8
commit
c7b9e3fb18
@ -3,6 +3,7 @@
|
|||||||
## Current Master
|
## Current Master
|
||||||
|
|
||||||
- No longer validate nonce client-side in retry loop.
|
- No longer validate nonce client-side in retry loop.
|
||||||
|
- Fix bug where insufficient balance error was sometimes shown on successful transactions.
|
||||||
|
|
||||||
## 3.8.4 2017-7-7
|
## 3.8.4 2017-7-7
|
||||||
|
|
||||||
|
@ -445,13 +445,10 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
_resubmitTx (txMeta, cb) {
|
_resubmitTx (txMeta, cb) {
|
||||||
const address = txMeta.txParams.from
|
const address = txMeta.txParams.from
|
||||||
const balance = this.ethStore.getState().accounts[address].balance
|
const balance = this.ethStore.getState().accounts[address].balance
|
||||||
const nonce = Number.parseInt(this.ethStore.getState().accounts[address].nonce)
|
|
||||||
const txNonce = Number.parseInt(txMeta.txParams.nonce)
|
|
||||||
const gtBalance = Number.parseInt(txMeta.txParams.value) > Number.parseInt(balance)
|
|
||||||
if (!('retryCount' in txMeta)) txMeta.retryCount = 0
|
if (!('retryCount' in txMeta)) txMeta.retryCount = 0
|
||||||
|
|
||||||
// if the value of the transaction is greater then the balance, fail.
|
// if the value of the transaction is greater then the balance, fail.
|
||||||
if (gtBalance) {
|
if (!this.txProviderUtils.sufficientBalance(txMeta.txParams, balance)) {
|
||||||
const message = 'Insufficient balance.'
|
const message = 'Insufficient balance.'
|
||||||
this.setTxStatusFailed(txMeta.id, { message })
|
this.setTxStatusFailed(txMeta.id, { message })
|
||||||
cb()
|
cb()
|
||||||
|
@ -118,6 +118,15 @@ module.exports = class txProviderUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sufficientBalance (tx, hexBalance) {
|
||||||
|
const balance = hexToBn(hexBalance)
|
||||||
|
const value = hexToBn(tx.value)
|
||||||
|
const gasLimit = hexToBn(tx.gas)
|
||||||
|
const gasPrice = hexToBn(tx.gasPrice)
|
||||||
|
|
||||||
|
const maxCost = value.add(gasLimit.mul(gasPrice))
|
||||||
|
return balance.gte(maxCost)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,44 @@ describe('txUtils', function () {
|
|||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#sufficientBalance', function () {
|
||||||
|
it('returns true if max tx cost is equal to balance.', function () {
|
||||||
|
const tx = {
|
||||||
|
'value': '0x1',
|
||||||
|
'gas': '0x2',
|
||||||
|
'gasPrice': '0x3',
|
||||||
|
}
|
||||||
|
const balance = '0x8'
|
||||||
|
|
||||||
|
const result = txUtils.sufficientBalance(tx, balance)
|
||||||
|
assert.ok(result, 'sufficient balance found.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns true if max tx cost is less than balance.', function () {
|
||||||
|
const tx = {
|
||||||
|
'value': '0x1',
|
||||||
|
'gas': '0x2',
|
||||||
|
'gasPrice': '0x3',
|
||||||
|
}
|
||||||
|
const balance = '0x9'
|
||||||
|
|
||||||
|
const result = txUtils.sufficientBalance(tx, balance)
|
||||||
|
assert.ok(result, 'sufficient balance found.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns false if max tx cost is more than balance.', function () {
|
||||||
|
const tx = {
|
||||||
|
'value': '0x1',
|
||||||
|
'gas': '0x2',
|
||||||
|
'gasPrice': '0x3',
|
||||||
|
}
|
||||||
|
const balance = '0x6'
|
||||||
|
|
||||||
|
const result = txUtils.sufficientBalance(tx, balance)
|
||||||
|
assert.ok(!result, 'insufficient balance found.')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('chain Id', function () {
|
describe('chain Id', function () {
|
||||||
it('prepares a transaction with the provided chainId', function () {
|
it('prepares a transaction with the provided chainId', function () {
|
||||||
const txParams = {
|
const txParams = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user