2018-04-26 18:38:38 +02:00
|
|
|
const {
|
|
|
|
addCurrencies,
|
|
|
|
conversionUtil,
|
|
|
|
conversionGTE,
|
|
|
|
multiplyCurrencies,
|
2018-05-05 17:11:53 +02:00
|
|
|
conversionGreaterThan,
|
2018-06-15 03:02:03 +02:00
|
|
|
conversionLessThan,
|
2019-04-17 21:15:13 +02:00
|
|
|
} = require('../../helpers/utils/conversion-util')
|
2018-04-26 18:38:38 +02:00
|
|
|
const {
|
|
|
|
calcTokenAmount,
|
2019-04-17 21:15:13 +02:00
|
|
|
} = require('../../helpers/utils/token-util')
|
2018-04-30 18:30:01 +02:00
|
|
|
const {
|
2018-06-29 01:33:09 +02:00
|
|
|
BASE_TOKEN_GAS_COST,
|
2018-04-30 18:30:01 +02:00
|
|
|
INSUFFICIENT_FUNDS_ERROR,
|
|
|
|
INSUFFICIENT_TOKENS_ERROR,
|
2019-08-31 18:29:31 +02:00
|
|
|
MIN_GAS_LIMIT_HEX,
|
2018-04-30 18:30:01 +02:00
|
|
|
NEGATIVE_ETH_ERROR,
|
2018-05-22 10:10:06 +02:00
|
|
|
ONE_GWEI_IN_WEI_HEX,
|
2018-05-22 17:16:53 +02:00
|
|
|
SIMPLE_GAS_COST,
|
2018-06-02 06:23:01 +02:00
|
|
|
TOKEN_TRANSFER_FUNCTION_SIGNATURE,
|
2018-04-30 18:30:01 +02:00
|
|
|
} = require('./send.constants')
|
2018-05-05 17:11:53 +02:00
|
|
|
const abi = require('ethereumjs-abi')
|
2018-05-23 18:43:25 +02:00
|
|
|
const ethUtil = require('ethereumjs-util')
|
2018-04-27 21:03:00 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2018-06-15 04:24:48 +02:00
|
|
|
addGasBuffer,
|
2018-04-27 21:03:00 +02:00
|
|
|
calcGasTotal,
|
2018-05-22 17:16:53 +02:00
|
|
|
calcTokenBalance,
|
2018-04-27 21:03:00 +02:00
|
|
|
doesAmountErrorRequireUpdate,
|
2018-05-22 10:10:06 +02:00
|
|
|
estimateGas,
|
|
|
|
estimateGasPriceFromRecentBlocks,
|
2018-04-27 21:03:00 +02:00
|
|
|
generateTokenTransferData,
|
|
|
|
getAmountErrorObject,
|
2018-06-29 19:19:40 +02:00
|
|
|
getGasFeeErrorObject,
|
2018-06-18 18:37:01 +02:00
|
|
|
getToAddressForGasUpdate,
|
2018-04-27 21:03:00 +02:00
|
|
|
isBalanceSufficient,
|
|
|
|
isTokenBalanceSufficient,
|
2018-07-02 03:36:57 +02:00
|
|
|
removeLeadingZeroes,
|
2019-07-31 21:56:44 +02:00
|
|
|
ellipsify,
|
2018-04-27 21:03:00 +02:00
|
|
|
}
|
2018-04-26 18:38:38 +02:00
|
|
|
|
2018-06-23 08:52:45 +02:00
|
|
|
function calcGasTotal (gasLimit = '0', gasPrice = '0') {
|
2018-04-27 02:38:14 +02:00
|
|
|
return multiplyCurrencies(gasLimit, gasPrice, {
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
multiplicandBase: 16,
|
|
|
|
multiplierBase: 16,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-26 18:38:38 +02:00
|
|
|
function isBalanceSufficient ({
|
|
|
|
amount = '0x0',
|
2018-06-23 08:52:45 +02:00
|
|
|
amountConversionRate = 1,
|
|
|
|
balance = '0x0',
|
|
|
|
conversionRate = 1,
|
2018-04-27 02:38:14 +02:00
|
|
|
gasTotal = '0x0',
|
|
|
|
primaryCurrency,
|
2018-04-26 18:38:38 +02:00
|
|
|
}) {
|
|
|
|
const totalAmount = addCurrencies(amount, gasTotal, {
|
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
})
|
|
|
|
|
|
|
|
const balanceIsSufficient = conversionGTE(
|
|
|
|
{
|
|
|
|
value: balance,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
fromCurrency: primaryCurrency,
|
|
|
|
conversionRate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: totalAmount,
|
|
|
|
fromNumericBase: 'hex',
|
2018-05-30 18:40:30 +02:00
|
|
|
conversionRate: Number(amountConversionRate) || conversionRate,
|
2018-04-26 18:38:38 +02:00
|
|
|
fromCurrency: primaryCurrency,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return balanceIsSufficient
|
|
|
|
}
|
|
|
|
|
|
|
|
function isTokenBalanceSufficient ({
|
|
|
|
amount = '0x0',
|
|
|
|
tokenBalance,
|
|
|
|
decimals,
|
|
|
|
}) {
|
|
|
|
const amountInDec = conversionUtil(amount, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
})
|
|
|
|
|
|
|
|
const tokenBalanceIsSufficient = conversionGTE(
|
|
|
|
{
|
|
|
|
value: tokenBalance,
|
2018-11-20 01:06:34 +01:00
|
|
|
fromNumericBase: 'hex',
|
2018-04-26 18:38:38 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: calcTokenAmount(amountInDec, decimals),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return tokenBalanceIsSufficient
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:03:00 +02:00
|
|
|
function getAmountErrorObject ({
|
|
|
|
amount,
|
|
|
|
amountConversionRate,
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
|
|
|
gasTotal,
|
|
|
|
primaryCurrency,
|
|
|
|
selectedToken,
|
|
|
|
tokenBalance,
|
|
|
|
}) {
|
|
|
|
let insufficientFunds = false
|
2018-06-29 19:19:40 +02:00
|
|
|
if (gasTotal && conversionRate && !selectedToken) {
|
2018-04-27 21:03:00 +02:00
|
|
|
insufficientFunds = !isBalanceSufficient({
|
2018-06-29 19:19:40 +02:00
|
|
|
amount,
|
2018-04-27 21:03:00 +02:00
|
|
|
amountConversionRate,
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
|
|
|
gasTotal,
|
|
|
|
primaryCurrency,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let inSufficientTokens = false
|
|
|
|
if (selectedToken && tokenBalance !== null) {
|
|
|
|
const { decimals } = selectedToken
|
|
|
|
inSufficientTokens = !isTokenBalanceSufficient({
|
|
|
|
tokenBalance,
|
|
|
|
amount,
|
|
|
|
decimals,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const amountLessThanZero = conversionGreaterThan(
|
|
|
|
{ value: 0, fromNumericBase: 'dec' },
|
|
|
|
{ value: amount, fromNumericBase: 'hex' },
|
|
|
|
)
|
|
|
|
|
|
|
|
let amountError = null
|
|
|
|
|
|
|
|
if (insufficientFunds) {
|
2018-04-30 18:30:01 +02:00
|
|
|
amountError = INSUFFICIENT_FUNDS_ERROR
|
2018-04-27 21:03:00 +02:00
|
|
|
} else if (inSufficientTokens) {
|
2018-04-30 18:30:01 +02:00
|
|
|
amountError = INSUFFICIENT_TOKENS_ERROR
|
2018-04-27 21:03:00 +02:00
|
|
|
} else if (amountLessThanZero) {
|
2018-04-30 18:30:01 +02:00
|
|
|
amountError = NEGATIVE_ETH_ERROR
|
2018-04-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return { amount: amountError }
|
|
|
|
}
|
|
|
|
|
2018-06-29 19:19:40 +02:00
|
|
|
function getGasFeeErrorObject ({
|
|
|
|
amountConversionRate,
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
|
|
|
gasTotal,
|
|
|
|
primaryCurrency,
|
|
|
|
}) {
|
|
|
|
let gasFeeError = null
|
|
|
|
|
|
|
|
if (gasTotal && conversionRate) {
|
|
|
|
const insufficientFunds = !isBalanceSufficient({
|
|
|
|
amount: '0x0',
|
|
|
|
amountConversionRate,
|
|
|
|
balance,
|
|
|
|
conversionRate,
|
|
|
|
gasTotal,
|
|
|
|
primaryCurrency,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (insufficientFunds) {
|
|
|
|
gasFeeError = INSUFFICIENT_FUNDS_ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { gasFee: gasFeeError }
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:03:00 +02:00
|
|
|
function calcTokenBalance ({ selectedToken, usersToken }) {
|
|
|
|
const { decimals } = selectedToken || {}
|
2018-11-20 01:06:34 +01:00
|
|
|
return calcTokenAmount(usersToken.balance.toString(), decimals).toString(16)
|
2018-04-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function doesAmountErrorRequireUpdate ({
|
|
|
|
balance,
|
|
|
|
gasTotal,
|
|
|
|
prevBalance,
|
|
|
|
prevGasTotal,
|
|
|
|
prevTokenBalance,
|
|
|
|
selectedToken,
|
|
|
|
tokenBalance,
|
|
|
|
}) {
|
|
|
|
const balanceHasChanged = balance !== prevBalance
|
|
|
|
const gasTotalHasChange = gasTotal !== prevGasTotal
|
|
|
|
const tokenBalanceHasChanged = selectedToken && tokenBalance !== prevTokenBalance
|
|
|
|
const amountErrorRequiresUpdate = balanceHasChanged || gasTotalHasChange || tokenBalanceHasChanged
|
|
|
|
|
|
|
|
return amountErrorRequiresUpdate
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:46:24 +02:00
|
|
|
async function estimateGas ({
|
|
|
|
selectedAddress,
|
|
|
|
selectedToken,
|
2019-08-31 18:29:31 +02:00
|
|
|
blockGasLimit = MIN_GAS_LIMIT_HEX,
|
2018-09-27 15:46:24 +02:00
|
|
|
to,
|
|
|
|
value,
|
|
|
|
data,
|
|
|
|
gasPrice,
|
|
|
|
estimateGasMethod,
|
|
|
|
}) {
|
2018-05-23 18:43:25 +02:00
|
|
|
const paramsForGasEstimate = { from: selectedAddress, value, gasPrice }
|
2018-05-22 17:16:53 +02:00
|
|
|
|
2018-09-27 16:52:12 +02:00
|
|
|
// if recipient has no code, gas is 21k max:
|
|
|
|
if (!selectedToken && !data) {
|
|
|
|
const code = Boolean(to) && await global.eth.getCode(to)
|
2018-10-21 06:52:41 +02:00
|
|
|
// Geth will return '0x', and ganache-core v2.2.1 will return '0x0'
|
|
|
|
const codeIsEmpty = !code || code === '0x' || code === '0x0'
|
|
|
|
if (codeIsEmpty) {
|
2018-09-27 16:52:12 +02:00
|
|
|
return SIMPLE_GAS_COST
|
|
|
|
}
|
|
|
|
} else if (selectedToken && !to) {
|
|
|
|
return BASE_TOKEN_GAS_COST
|
|
|
|
}
|
|
|
|
|
2018-06-02 06:23:01 +02:00
|
|
|
if (selectedToken) {
|
|
|
|
paramsForGasEstimate.value = '0x0'
|
|
|
|
paramsForGasEstimate.data = generateTokenTransferData({ toAddress: to, amount: value, selectedToken })
|
2018-09-27 15:46:24 +02:00
|
|
|
paramsForGasEstimate.to = selectedToken.address
|
|
|
|
} else {
|
|
|
|
if (data) {
|
|
|
|
paramsForGasEstimate.data = data
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to) {
|
|
|
|
paramsForGasEstimate.to = to
|
|
|
|
}
|
2019-02-25 19:16:23 +01:00
|
|
|
|
|
|
|
if (!value || value === '0') {
|
|
|
|
paramsForGasEstimate.value = '0xff'
|
|
|
|
}
|
2018-05-22 17:16:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if not, fall back to block gasLimit
|
2019-10-07 21:29:37 +02:00
|
|
|
if (!blockGasLimit) {
|
2019-10-08 19:14:20 +02:00
|
|
|
blockGasLimit = MIN_GAS_LIMIT_HEX
|
2019-10-07 21:29:37 +02:00
|
|
|
}
|
|
|
|
|
2019-08-31 18:29:31 +02:00
|
|
|
paramsForGasEstimate.gas = ethUtil.addHexPrefix(multiplyCurrencies(blockGasLimit, 0.95, {
|
2018-05-22 17:16:53 +02:00
|
|
|
multiplicandBase: 16,
|
|
|
|
multiplierBase: 10,
|
|
|
|
roundDown: '0',
|
|
|
|
toNumericBase: 'hex',
|
2018-05-23 18:43:25 +02:00
|
|
|
}))
|
2019-10-07 21:29:37 +02:00
|
|
|
|
2018-05-22 17:16:53 +02:00
|
|
|
// run tx
|
2018-05-23 18:43:25 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-25 15:37:16 +02:00
|
|
|
return estimateGasMethod(paramsForGasEstimate, (err, estimatedGas) => {
|
2018-05-23 18:43:25 +02:00
|
|
|
if (err) {
|
2018-05-25 15:37:16 +02:00
|
|
|
const simulationFailed = (
|
|
|
|
err.message.includes('Transaction execution error.') ||
|
|
|
|
err.message.includes('gas required exceeds allowance or always failing transaction')
|
|
|
|
)
|
|
|
|
if (simulationFailed) {
|
2018-06-15 23:36:52 +02:00
|
|
|
const estimateWithBuffer = addGasBuffer(paramsForGasEstimate.gas, blockGasLimit, 1.5)
|
2018-06-15 03:02:03 +02:00
|
|
|
return resolve(ethUtil.addHexPrefix(estimateWithBuffer))
|
2018-05-25 15:37:16 +02:00
|
|
|
} else {
|
|
|
|
return reject(err)
|
|
|
|
}
|
2018-05-23 18:43:25 +02:00
|
|
|
}
|
2018-06-15 23:36:52 +02:00
|
|
|
const estimateWithBuffer = addGasBuffer(estimatedGas.toString(16), blockGasLimit, 1.5)
|
2018-06-15 03:02:03 +02:00
|
|
|
return resolve(ethUtil.addHexPrefix(estimateWithBuffer))
|
2018-05-23 18:43:25 +02:00
|
|
|
})
|
|
|
|
})
|
2018-05-22 10:10:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-15 03:02:03 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-02 06:23:01 +02:00
|
|
|
function generateTokenTransferData ({ toAddress = '0x0', amount = '0x0', selectedToken }) {
|
2018-04-27 21:03:00 +02:00
|
|
|
if (!selectedToken) return
|
2018-06-02 06:23:01 +02:00
|
|
|
return TOKEN_TRANSFER_FUNCTION_SIGNATURE + Array.prototype.map.call(
|
|
|
|
abi.rawEncode(['address', 'uint256'], [toAddress, ethUtil.addHexPrefix(amount)]),
|
2018-04-27 21:03:00 +02:00
|
|
|
x => ('00' + x.toString(16)).slice(-2)
|
|
|
|
).join('')
|
2018-04-27 02:38:14 +02:00
|
|
|
}
|
2018-05-22 10:10:06 +02:00
|
|
|
|
|
|
|
function estimateGasPriceFromRecentBlocks (recentBlocks) {
|
|
|
|
// Return 1 gwei if no blocks have been observed:
|
|
|
|
if (!recentBlocks || recentBlocks.length === 0) {
|
|
|
|
return ONE_GWEI_IN_WEI_HEX
|
|
|
|
}
|
2018-05-30 17:26:42 +02:00
|
|
|
|
2018-05-22 10:10:06 +02:00
|
|
|
const lowestPrices = recentBlocks.map((block) => {
|
|
|
|
if (!block.gasPrices || block.gasPrices.length < 1) {
|
|
|
|
return ONE_GWEI_IN_WEI_HEX
|
|
|
|
}
|
2018-05-30 17:26:42 +02:00
|
|
|
return block.gasPrices.reduce((currentLowest, next) => {
|
|
|
|
return parseInt(next, 16) < parseInt(currentLowest, 16) ? next : currentLowest
|
|
|
|
})
|
2018-05-22 10:10:06 +02:00
|
|
|
})
|
2019-07-31 22:17:11 +02:00
|
|
|
.sort((a, b) => parseInt(a, 16) > parseInt(b, 16) ? 1 : -1)
|
2018-05-22 17:16:53 +02:00
|
|
|
|
2018-05-22 10:10:06 +02:00
|
|
|
return lowestPrices[Math.floor(lowestPrices.length / 2)]
|
|
|
|
}
|
2018-06-18 18:37:01 +02:00
|
|
|
|
|
|
|
function getToAddressForGasUpdate (...addresses) {
|
|
|
|
return [...addresses, ''].find(str => str !== undefined && str !== null).toLowerCase()
|
|
|
|
}
|
2018-07-02 03:36:57 +02:00
|
|
|
|
|
|
|
function removeLeadingZeroes (str) {
|
|
|
|
return str.replace(/^0*(?=\d)/, '')
|
|
|
|
}
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
function ellipsify (text, first = 6, last = 4) {
|
|
|
|
return `${text.slice(0, first)}...${text.slice(-last)}`
|
|
|
|
}
|