aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan <danjm.com@gmail.com>2018-06-15 09:02:03 +0800
committerDan <danjm.com@gmail.com>2018-06-15 09:02:03 +0800
commitb9b6cbf561a2efc983680c30064ee3beb64402c4 (patch)
tree1e3706a19bfe3fab6e7d7da8a451539d40fb15f5
parent1b9ed237527b9970c3dfcbb0234cfdb2ffa8c807 (diff)
downloadtangerine-wallet-browser-b9b6cbf561a2efc983680c30064ee3beb64402c4.tar
tangerine-wallet-browser-b9b6cbf561a2efc983680c30064ee3beb64402c4.tar.gz
tangerine-wallet-browser-b9b6cbf561a2efc983680c30064ee3beb64402c4.tar.bz2
tangerine-wallet-browser-b9b6cbf561a2efc983680c30064ee3beb64402c4.tar.lz
tangerine-wallet-browser-b9b6cbf561a2efc983680c30064ee3beb64402c4.tar.xz
tangerine-wallet-browser-b9b6cbf561a2efc983680c30064ee3beb64402c4.tar.zst
tangerine-wallet-browser-b9b6cbf561a2efc983680c30064ee3beb64402c4.zip
Add a buffer to new ui gas estimates
-rw-r--r--ui/app/components/send_/send.utils.js43
-rw-r--r--ui/app/conversion-util.js11
2 files changed, 52 insertions, 2 deletions
diff --git a/ui/app/components/send_/send.utils.js b/ui/app/components/send_/send.utils.js
index 04a41456c..3df1506dc 100644
--- a/ui/app/components/send_/send.utils.js
+++ b/ui/app/components/send_/send.utils.js
@@ -4,6 +4,7 @@ const {
conversionGTE,
multiplyCurrencies,
conversionGreaterThan,
+ conversionLessThan,
} = require('../../conversion-util')
const {
calcTokenAmount,
@@ -201,16 +202,54 @@ async function estimateGas ({ selectedAddress, selectedToken, blockGasLimit, to,
err.message.includes('gas required exceeds allowance or always failing transaction')
)
if (simulationFailed) {
- return resolve(ethUtil.addHexPrefix(paramsForGasEstimate.gas))
+ const estimateWithBuffer = addGasBuffer(
+ paramsForGasEstimate.gas,
+ blockGasLimit,
+ selectedToken ? 2 : 1.5
+ )
+ return resolve(ethUtil.addHexPrefix(estimateWithBuffer))
} else {
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 }) {
if (!selectedToken) return
return TOKEN_TRANSFER_FUNCTION_SIGNATURE + Array.prototype.map.call(
diff --git a/ui/app/conversion-util.js b/ui/app/conversion-util.js
index 100402d95..337763067 100644
--- a/ui/app/conversion-util.js
+++ b/ui/app/conversion-util.js
@@ -190,6 +190,16 @@ const conversionGreaterThan = (
return firstValue.gt(secondValue)
}
+const conversionLessThan = (
+ { ...firstProps },
+ { ...secondProps },
+) => {
+ const firstValue = converter({ ...firstProps })
+ const secondValue = converter({ ...secondProps })
+
+ return firstValue.lt(secondValue)
+}
+
const conversionMax = (
{ ...firstProps },
{ ...secondProps },
@@ -229,6 +239,7 @@ module.exports = {
addCurrencies,
multiplyCurrencies,
conversionGreaterThan,
+ conversionLessThan,
conversionGTE,
conversionLTE,
conversionMax,