1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

Rework estimateGas logic

The tests still pass!
This commit is contained in:
Whymarrh Whitby 2018-09-27 12:22:12 -02:30
parent 918fb71df3
commit c9f22916dd

View File

@ -212,6 +212,16 @@ async function estimateGas ({
}) { }) {
const paramsForGasEstimate = { from: selectedAddress, value, gasPrice } const paramsForGasEstimate = { from: selectedAddress, value, gasPrice }
// if recipient has no code, gas is 21k max:
if (!selectedToken && !data) {
const code = Boolean(to) && await global.eth.getCode(to)
if (!code || code === '0x') {
return SIMPLE_GAS_COST
}
} else if (selectedToken && !to) {
return BASE_TOKEN_GAS_COST
}
if (selectedToken) { if (selectedToken) {
paramsForGasEstimate.value = '0x0' paramsForGasEstimate.value = '0x0'
paramsForGasEstimate.data = generateTokenTransferData({ toAddress: to, amount: value, selectedToken }) paramsForGasEstimate.data = generateTokenTransferData({ toAddress: to, amount: value, selectedToken })
@ -226,16 +236,6 @@ async function estimateGas ({
} }
} }
// if recipient has no code, gas is 21k max:
if (!selectedToken && !data) {
const code = Boolean(to) && await global.eth.getCode(to)
if (!code || code === '0x') {
return SIMPLE_GAS_COST
}
} else if (selectedToken && !to) {
return BASE_TOKEN_GAS_COST
}
// if not, fall back to block gasLimit // if not, fall back to block gasLimit
paramsForGasEstimate.gas = ethUtil.addHexPrefix(multiplyCurrencies(blockGasLimit, 0.95, { paramsForGasEstimate.gas = ethUtil.addHexPrefix(multiplyCurrencies(blockGasLimit, 0.95, {
multiplicandBase: 16, multiplicandBase: 16,