You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
360 lines
8.0 KiB
360 lines
8.0 KiB
4 years ago
|
import abi from 'ethereumjs-abi';
|
||
5 years ago
|
import {
|
||
7 years ago
|
addCurrencies,
|
||
|
conversionUtil,
|
||
|
conversionGTE,
|
||
|
multiplyCurrencies,
|
||
7 years ago
|
conversionGreaterThan,
|
||
7 years ago
|
conversionLessThan,
|
||
4 years ago
|
} from '../../helpers/utils/conversion-util';
|
||
5 years ago
|
|
||
4 years ago
|
import { calcTokenAmount } from '../../helpers/utils/token-util';
|
||
4 years ago
|
import { addHexPrefix } from '../../../app/scripts/lib/util';
|
||
5 years ago
|
|
||
|
import {
|
||
7 years ago
|
BASE_TOKEN_GAS_COST,
|
||
7 years ago
|
INSUFFICIENT_FUNDS_ERROR,
|
||
|
INSUFFICIENT_TOKENS_ERROR,
|
||
5 years ago
|
MIN_GAS_LIMIT_HEX,
|
||
7 years ago
|
NEGATIVE_ETH_ERROR,
|
||
7 years ago
|
SIMPLE_GAS_COST,
|
||
7 years ago
|
TOKEN_TRANSFER_FUNCTION_SIGNATURE,
|
||
4 years ago
|
} from './send.constants';
|
||
5 years ago
|
|
||
|
export {
|
||
7 years ago
|
addGasBuffer,
|
||
7 years ago
|
calcGasTotal,
|
||
7 years ago
|
calcTokenBalance,
|
||
7 years ago
|
doesAmountErrorRequireUpdate,
|
||
4 years ago
|
estimateGasForSend,
|
||
7 years ago
|
generateTokenTransferData,
|
||
|
getAmountErrorObject,
|
||
7 years ago
|
getGasFeeErrorObject,
|
||
7 years ago
|
getToAddressForGasUpdate,
|
||
7 years ago
|
isBalanceSufficient,
|
||
|
isTokenBalanceSufficient,
|
||
7 years ago
|
removeLeadingZeroes,
|
||
5 years ago
|
ellipsify,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
4 years ago
|
function calcGasTotal(gasLimit = '0', gasPrice = '0') {
|
||
7 years ago
|
return multiplyCurrencies(gasLimit, gasPrice, {
|
||
|
toNumericBase: 'hex',
|
||
|
multiplicandBase: 16,
|
||
|
multiplierBase: 16,
|
||
4 years ago
|
});
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function isBalanceSufficient({
|
||
7 years ago
|
amount = '0x0',
|
||
7 years ago
|
balance = '0x0',
|
||
|
conversionRate = 1,
|
||
7 years ago
|
gasTotal = '0x0',
|
||
|
primaryCurrency,
|
||
7 years ago
|
}) {
|
||
|
const totalAmount = addCurrencies(amount, gasTotal, {
|
||
|
aBase: 16,
|
||
|
bBase: 16,
|
||
|
toNumericBase: 'hex',
|
||
4 years ago
|
});
|
||
7 years ago
|
|
||
|
const balanceIsSufficient = conversionGTE(
|
||
|
{
|
||
|
value: balance,
|
||
|
fromNumericBase: 'hex',
|
||
|
fromCurrency: primaryCurrency,
|
||
|
conversionRate,
|
||
|
},
|
||
|
{
|
||
|
value: totalAmount,
|
||
|
fromNumericBase: 'hex',
|
||
4 years ago
|
conversionRate,
|
||
7 years ago
|
fromCurrency: primaryCurrency,
|
||
|
},
|
||
4 years ago
|
);
|
||
7 years ago
|
|
||
4 years ago
|
return balanceIsSufficient;
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function isTokenBalanceSufficient({ amount = '0x0', tokenBalance, decimals }) {
|
||
7 years ago
|
const amountInDec = conversionUtil(amount, {
|
||
|
fromNumericBase: 'hex',
|
||
4 years ago
|
});
|
||
7 years ago
|
|
||
|
const tokenBalanceIsSufficient = conversionGTE(
|
||
|
{
|
||
|
value: tokenBalance,
|
||
6 years ago
|
fromNumericBase: 'hex',
|
||
7 years ago
|
},
|
||
|
{
|
||
|
value: calcTokenAmount(amountInDec, decimals),
|
||
|
},
|
||
4 years ago
|
);
|
||
7 years ago
|
|
||
4 years ago
|
return tokenBalanceIsSufficient;
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function getAmountErrorObject({
|
||
7 years ago
|
amount,
|
||
|
balance,
|
||
|
conversionRate,
|
||
|
gasTotal,
|
||
|
primaryCurrency,
|
||
5 years ago
|
sendToken,
|
||
7 years ago
|
tokenBalance,
|
||
|
}) {
|
||
4 years ago
|
let insufficientFunds = false;
|
||
5 years ago
|
if (gasTotal && conversionRate && !sendToken) {
|
||
7 years ago
|
insufficientFunds = !isBalanceSufficient({
|
||
7 years ago
|
amount,
|
||
7 years ago
|
balance,
|
||
|
conversionRate,
|
||
|
gasTotal,
|
||
|
primaryCurrency,
|
||
4 years ago
|
});
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
let inSufficientTokens = false;
|
||
5 years ago
|
if (sendToken && tokenBalance !== null) {
|
||
4 years ago
|
const { decimals } = sendToken;
|
||
7 years ago
|
inSufficientTokens = !isTokenBalanceSufficient({
|
||
|
tokenBalance,
|
||
|
amount,
|
||
|
decimals,
|
||
4 years ago
|
});
|
||
7 years ago
|
}
|
||
|
|
||
|
const amountLessThanZero = conversionGreaterThan(
|
||
|
{ value: 0, fromNumericBase: 'dec' },
|
||
|
{ value: amount, fromNumericBase: 'hex' },
|
||
4 years ago
|
);
|
||
7 years ago
|
|
||
4 years ago
|
let amountError = null;
|
||
7 years ago
|
|
||
|
if (insufficientFunds) {
|
||
4 years ago
|
amountError = INSUFFICIENT_FUNDS_ERROR;
|
||
7 years ago
|
} else if (inSufficientTokens) {
|
||
4 years ago
|
amountError = INSUFFICIENT_TOKENS_ERROR;
|
||
7 years ago
|
} else if (amountLessThanZero) {
|
||
4 years ago
|
amountError = NEGATIVE_ETH_ERROR;
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
return { amount: amountError };
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function getGasFeeErrorObject({
|
||
7 years ago
|
balance,
|
||
|
conversionRate,
|
||
|
gasTotal,
|
||
|
primaryCurrency,
|
||
|
}) {
|
||
4 years ago
|
let gasFeeError = null;
|
||
7 years ago
|
|
||
|
if (gasTotal && conversionRate) {
|
||
|
const insufficientFunds = !isBalanceSufficient({
|
||
|
amount: '0x0',
|
||
|
balance,
|
||
|
conversionRate,
|
||
|
gasTotal,
|
||
|
primaryCurrency,
|
||
4 years ago
|
});
|
||
7 years ago
|
|
||
|
if (insufficientFunds) {
|
||
4 years ago
|
gasFeeError = INSUFFICIENT_FUNDS_ERROR;
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
return { gasFee: gasFeeError };
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function calcTokenBalance({ sendToken, usersToken }) {
|
||
4 years ago
|
const { decimals } = sendToken || {};
|
||
|
return calcTokenAmount(usersToken.balance.toString(), decimals).toString(16);
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function doesAmountErrorRequireUpdate({
|
||
7 years ago
|
balance,
|
||
|
gasTotal,
|
||
|
prevBalance,
|
||
|
prevGasTotal,
|
||
|
prevTokenBalance,
|
||
5 years ago
|
sendToken,
|
||
7 years ago
|
tokenBalance,
|
||
|
}) {
|
||
4 years ago
|
const balanceHasChanged = balance !== prevBalance;
|
||
|
const gasTotalHasChange = gasTotal !== prevGasTotal;
|
||
|
const tokenBalanceHasChanged = sendToken && tokenBalance !== prevTokenBalance;
|
||
4 years ago
|
const amountErrorRequiresUpdate =
|
||
4 years ago
|
balanceHasChanged || gasTotalHasChange || tokenBalanceHasChanged;
|
||
7 years ago
|
|
||
4 years ago
|
return amountErrorRequiresUpdate;
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
async function estimateGasForSend({
|
||
6 years ago
|
selectedAddress,
|
||
5 years ago
|
sendToken,
|
||
5 years ago
|
blockGasLimit = MIN_GAS_LIMIT_HEX,
|
||
6 years ago
|
to,
|
||
|
value,
|
||
|
data,
|
||
|
gasPrice,
|
||
|
estimateGasMethod,
|
||
|
}) {
|
||
4 years ago
|
const paramsForGasEstimate = { from: selectedAddress, value, gasPrice };
|
||
7 years ago
|
|
||
6 years ago
|
// if recipient has no code, gas is 21k max:
|
||
5 years ago
|
if (!sendToken && !data) {
|
||
4 years ago
|
const code = Boolean(to) && (await global.eth.getCode(to));
|
||
6 years ago
|
// Geth will return '0x', and ganache-core v2.2.1 will return '0x0'
|
||
4 years ago
|
const codeIsEmpty = !code || code === '0x' || code === '0x0';
|
||
6 years ago
|
if (codeIsEmpty) {
|
||
4 years ago
|
return SIMPLE_GAS_COST;
|
||
6 years ago
|
}
|
||
5 years ago
|
} else if (sendToken && !to) {
|
||
4 years ago
|
return BASE_TOKEN_GAS_COST;
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
if (sendToken) {
|
||
4 years ago
|
paramsForGasEstimate.value = '0x0';
|
||
4 years ago
|
paramsForGasEstimate.data = generateTokenTransferData({
|
||
|
toAddress: to,
|
||
|
amount: value,
|
||
|
sendToken,
|
||
4 years ago
|
});
|
||
|
paramsForGasEstimate.to = sendToken.address;
|
||
6 years ago
|
} else {
|
||
|
if (data) {
|
||
4 years ago
|
paramsForGasEstimate.data = data;
|
||
6 years ago
|
}
|
||
|
|
||
|
if (to) {
|
||
4 years ago
|
paramsForGasEstimate.to = to;
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
if (!value || value === '0') {
|
||
4 years ago
|
paramsForGasEstimate.value = '0xff';
|
||
6 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
// if not, fall back to block gasLimit
|
||
5 years ago
|
if (!blockGasLimit) {
|
||
4 years ago
|
// eslint-disable-next-line no-param-reassign
|
||
4 years ago
|
blockGasLimit = MIN_GAS_LIMIT_HEX;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
paramsForGasEstimate.gas = addHexPrefix(
|
||
4 years ago
|
multiplyCurrencies(blockGasLimit, 0.95, {
|
||
|
multiplicandBase: 16,
|
||
|
multiplierBase: 10,
|
||
|
roundDown: '0',
|
||
|
toNumericBase: 'hex',
|
||
|
}),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
7 years ago
|
// run tx
|
||
5 years ago
|
try {
|
||
4 years ago
|
const estimatedGas = await estimateGasMethod(paramsForGasEstimate);
|
||
4 years ago
|
const estimateWithBuffer = addGasBuffer(
|
||
|
estimatedGas.toString(16),
|
||
|
blockGasLimit,
|
||
|
1.5,
|
||
4 years ago
|
);
|
||
|
return addHexPrefix(estimateWithBuffer);
|
||
5 years ago
|
} catch (error) {
|
||
4 years ago
|
const simulationFailed =
|
||
5 years ago
|
error.message.includes('Transaction execution error.') ||
|
||
4 years ago
|
error.message.includes(
|
||
|
'gas required exceeds allowance or always failing transaction',
|
||
4 years ago
|
);
|
||
5 years ago
|
if (simulationFailed) {
|
||
4 years ago
|
const estimateWithBuffer = addGasBuffer(
|
||
|
paramsForGasEstimate.gas,
|
||
|
blockGasLimit,
|
||
|
1.5,
|
||
4 years ago
|
);
|
||
|
return addHexPrefix(estimateWithBuffer);
|
||
5 years ago
|
}
|
||
4 years ago
|
throw error;
|
||
5 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function addGasBuffer(
|
||
|
initialGasLimitHex,
|
||
|
blockGasLimitHex,
|
||
|
bufferMultiplier = 1.5,
|
||
|
) {
|
||
7 years ago
|
const upperGasLimit = multiplyCurrencies(blockGasLimitHex, 0.9, {
|
||
|
toNumericBase: 'hex',
|
||
|
multiplicandBase: 16,
|
||
|
multiplierBase: 10,
|
||
|
numberOfDecimals: '0',
|
||
4 years ago
|
});
|
||
4 years ago
|
const bufferedGasLimit = multiplyCurrencies(
|
||
|
initialGasLimitHex,
|
||
|
bufferMultiplier,
|
||
|
{
|
||
|
toNumericBase: 'hex',
|
||
|
multiplicandBase: 16,
|
||
|
multiplierBase: 10,
|
||
|
numberOfDecimals: '0',
|
||
|
},
|
||
4 years ago
|
);
|
||
7 years ago
|
|
||
|
// if initialGasLimit is above blockGasLimit, dont modify it
|
||
4 years ago
|
if (
|
||
|
conversionGreaterThan(
|
||
|
{ value: initialGasLimitHex, fromNumericBase: 'hex' },
|
||
|
{ value: upperGasLimit, fromNumericBase: 'hex' },
|
||
|
)
|
||
|
) {
|
||
4 years ago
|
return initialGasLimitHex;
|
||
5 years ago
|
}
|
||
7 years ago
|
// if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
|
||
4 years ago
|
if (
|
||
|
conversionLessThan(
|
||
|
{ value: bufferedGasLimit, fromNumericBase: 'hex' },
|
||
|
{ value: upperGasLimit, fromNumericBase: 'hex' },
|
||
|
)
|
||
|
) {
|
||
4 years ago
|
return bufferedGasLimit;
|
||
5 years ago
|
}
|
||
7 years ago
|
// otherwise use blockGasLimit
|
||
4 years ago
|
return upperGasLimit;
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function generateTokenTransferData({
|
||
|
toAddress = '0x0',
|
||
|
amount = '0x0',
|
||
|
sendToken,
|
||
|
}) {
|
||
5 years ago
|
if (!sendToken) {
|
||
4 years ago
|
return undefined;
|
||
5 years ago
|
}
|
||
4 years ago
|
return (
|
||
|
TOKEN_TRANSFER_FUNCTION_SIGNATURE +
|
||
|
Array.prototype.map
|
||
|
.call(
|
||
|
abi.rawEncode(
|
||
|
['address', 'uint256'],
|
||
4 years ago
|
[toAddress, addHexPrefix(amount)],
|
||
4 years ago
|
),
|
||
|
(x) => `00${x.toString(16)}`.slice(-2),
|
||
|
)
|
||
|
.join('')
|
||
4 years ago
|
);
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
4 years ago
|
function getToAddressForGasUpdate(...addresses) {
|
||
|
return [...addresses, '']
|
||
|
.find((str) => str !== undefined && str !== null)
|
||
4 years ago
|
.toLowerCase();
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
4 years ago
|
function removeLeadingZeroes(str) {
|
||
4 years ago
|
return str.replace(/^0*(?=\d)/u, '');
|
||
7 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
function ellipsify(text, first = 6, last = 4) {
|
||
4 years ago
|
return `${text.slice(0, first)}...${text.slice(-last)}`;
|
||
5 years ago
|
}
|