mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Add a buffer to new ui gas estimates
This commit is contained in:
parent
1b9ed23752
commit
b9b6cbf561
@ -4,6 +4,7 @@ const {
|
|||||||
conversionGTE,
|
conversionGTE,
|
||||||
multiplyCurrencies,
|
multiplyCurrencies,
|
||||||
conversionGreaterThan,
|
conversionGreaterThan,
|
||||||
|
conversionLessThan,
|
||||||
} = require('../../conversion-util')
|
} = require('../../conversion-util')
|
||||||
const {
|
const {
|
||||||
calcTokenAmount,
|
calcTokenAmount,
|
||||||
@ -201,16 +202,54 @@ async function estimateGas ({ selectedAddress, selectedToken, blockGasLimit, to,
|
|||||||
err.message.includes('gas required exceeds allowance or always failing transaction')
|
err.message.includes('gas required exceeds allowance or always failing transaction')
|
||||||
)
|
)
|
||||||
if (simulationFailed) {
|
if (simulationFailed) {
|
||||||
return resolve(ethUtil.addHexPrefix(paramsForGasEstimate.gas))
|
const estimateWithBuffer = addGasBuffer(
|
||||||
|
paramsForGasEstimate.gas,
|
||||||
|
blockGasLimit,
|
||||||
|
selectedToken ? 2 : 1.5
|
||||||
|
)
|
||||||
|
return resolve(ethUtil.addHexPrefix(estimateWithBuffer))
|
||||||
} else {
|
} else {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resolve(ethUtil.addHexPrefix(estimatedGas.toString(16)))
|
const estimateWithBuffer = addGasBuffer(
|
||||||
|
estimatedGas.toString(16),
|
||||||
|
blockGasLimit,
|
||||||
|
selectedToken ? 2 : 1.5
|
||||||
|
)
|
||||||
|
return resolve(ethUtil.addHexPrefix(estimateWithBuffer))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addGasBuffer (initialGasLimitHex, blockGasLimitHex, bufferMultiplier = 1.5) {
|
||||||
|
const upperGasLimit = multiplyCurrencies(blockGasLimitHex, 0.9, {
|
||||||
|
toNumericBase: 'hex',
|
||||||
|
multiplicandBase: 16,
|
||||||
|
multiplierBase: 10,
|
||||||
|
numberOfDecimals: '0',
|
||||||
|
})
|
||||||
|
const bufferedGasLimit = multiplyCurrencies(initialGasLimitHex, bufferMultiplier, {
|
||||||
|
toNumericBase: 'hex',
|
||||||
|
multiplicandBase: 16,
|
||||||
|
multiplierBase: 10,
|
||||||
|
numberOfDecimals: '0',
|
||||||
|
})
|
||||||
|
|
||||||
|
// if initialGasLimit is above blockGasLimit, dont modify it
|
||||||
|
if (conversionGreaterThan(
|
||||||
|
{ value: initialGasLimitHex, fromNumericBase: 'hex' },
|
||||||
|
{ value: upperGasLimit, fromNumericBase: 'hex' },
|
||||||
|
)) return initialGasLimitHex
|
||||||
|
// if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
|
||||||
|
if (conversionLessThan(
|
||||||
|
{ value: bufferedGasLimit, fromNumericBase: 'hex' },
|
||||||
|
{ value: upperGasLimit, fromNumericBase: 'hex' },
|
||||||
|
)) return bufferedGasLimit
|
||||||
|
// otherwise use blockGasLimit
|
||||||
|
return upperGasLimit
|
||||||
|
}
|
||||||
|
|
||||||
function generateTokenTransferData ({ toAddress = '0x0', amount = '0x0', selectedToken }) {
|
function generateTokenTransferData ({ toAddress = '0x0', amount = '0x0', selectedToken }) {
|
||||||
if (!selectedToken) return
|
if (!selectedToken) return
|
||||||
return TOKEN_TRANSFER_FUNCTION_SIGNATURE + Array.prototype.map.call(
|
return TOKEN_TRANSFER_FUNCTION_SIGNATURE + Array.prototype.map.call(
|
||||||
|
@ -190,6 +190,16 @@ const conversionGreaterThan = (
|
|||||||
return firstValue.gt(secondValue)
|
return firstValue.gt(secondValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const conversionLessThan = (
|
||||||
|
{ ...firstProps },
|
||||||
|
{ ...secondProps },
|
||||||
|
) => {
|
||||||
|
const firstValue = converter({ ...firstProps })
|
||||||
|
const secondValue = converter({ ...secondProps })
|
||||||
|
|
||||||
|
return firstValue.lt(secondValue)
|
||||||
|
}
|
||||||
|
|
||||||
const conversionMax = (
|
const conversionMax = (
|
||||||
{ ...firstProps },
|
{ ...firstProps },
|
||||||
{ ...secondProps },
|
{ ...secondProps },
|
||||||
@ -229,6 +239,7 @@ module.exports = {
|
|||||||
addCurrencies,
|
addCurrencies,
|
||||||
multiplyCurrencies,
|
multiplyCurrencies,
|
||||||
conversionGreaterThan,
|
conversionGreaterThan,
|
||||||
|
conversionLessThan,
|
||||||
conversionGTE,
|
conversionGTE,
|
||||||
conversionLTE,
|
conversionLTE,
|
||||||
conversionMax,
|
conversionMax,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user