2021-02-04 19:15:23 +01:00
|
|
|
import abi from 'ethereumjs-abi';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2021-04-28 21:53:59 +02:00
|
|
|
import { addHexPrefix } from '../../../app/scripts/lib/util';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { TokenStandard } from '../../../shared/constants/transaction';
|
2023-01-24 15:44:49 +01:00
|
|
|
import { Numeric } from '../../../shared/modules/Numeric';
|
2022-01-10 17:23:53 +01:00
|
|
|
import {
|
|
|
|
TOKEN_TRANSFER_FUNCTION_SIGNATURE,
|
|
|
|
COLLECTIBLE_TRANSFER_FROM_FUNCTION_SIGNATURE,
|
|
|
|
} from './send.constants';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
|
|
|
export {
|
2018-06-15 04:24:48 +02:00
|
|
|
addGasBuffer,
|
2022-01-10 17:23:53 +01:00
|
|
|
getAssetTransferData,
|
|
|
|
generateERC20TransferData,
|
|
|
|
generateERC721TransferData,
|
2018-04-27 21:03:00 +02:00
|
|
|
isBalanceSufficient,
|
|
|
|
isTokenBalanceSufficient,
|
2019-07-31 21:56:44 +02:00
|
|
|
ellipsify,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-04-26 18:38:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function isBalanceSufficient({
|
2018-04-26 18:38:38 +02:00
|
|
|
amount = '0x0',
|
2018-06-23 08:52:45 +02:00
|
|
|
balance = '0x0',
|
|
|
|
conversionRate = 1,
|
2018-04-27 02:38:14 +02:00
|
|
|
gasTotal = '0x0',
|
|
|
|
primaryCurrency,
|
2018-04-26 18:38:38 +02:00
|
|
|
}) {
|
2023-01-24 15:44:49 +01:00
|
|
|
let totalAmount = new Numeric(amount, 16).add(new Numeric(gasTotal, 16));
|
|
|
|
let balanceNumeric = new Numeric(balance, 16);
|
2018-04-26 18:38:38 +02:00
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
if (typeof primaryCurrency !== 'undefined' && primaryCurrency !== null) {
|
|
|
|
totalAmount = totalAmount.applyConversionRate(conversionRate);
|
|
|
|
balanceNumeric = balanceNumeric.applyConversionRate(conversionRate);
|
|
|
|
}
|
2018-04-26 18:38:38 +02:00
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
return balanceNumeric.greaterThanOrEqualTo(totalAmount);
|
2018-04-26 18:38:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function isTokenBalanceSufficient({ amount = '0x0', tokenBalance, decimals }) {
|
2023-01-24 15:44:49 +01:00
|
|
|
const amountNumeric = new Numeric(amount, 16).shiftedBy(decimals);
|
|
|
|
const tokenBalanceNumeric = new Numeric(tokenBalance, 16);
|
2018-04-26 18:38:38 +02:00
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
return tokenBalanceNumeric.greaterThanOrEqualTo(amountNumeric);
|
2018-04-26 18:38:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function addGasBuffer(
|
|
|
|
initialGasLimitHex,
|
|
|
|
blockGasLimitHex,
|
|
|
|
bufferMultiplier = 1.5,
|
|
|
|
) {
|
2023-01-24 15:44:49 +01:00
|
|
|
const initialGasLimit = new Numeric(initialGasLimitHex, 16);
|
|
|
|
const upperGasLimit = new Numeric(blockGasLimitHex, 16)
|
|
|
|
.times(new Numeric(0.9, 10))
|
|
|
|
.round(0);
|
|
|
|
|
|
|
|
const bufferedGasLimit = initialGasLimit
|
|
|
|
.times(new Numeric(bufferMultiplier, 10))
|
|
|
|
.round(0);
|
2018-06-15 03:02:03 +02:00
|
|
|
|
|
|
|
// if initialGasLimit is above blockGasLimit, dont modify it
|
2023-01-24 15:44:49 +01:00
|
|
|
if (initialGasLimit.greaterThanOrEqualTo(upperGasLimit)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return initialGasLimitHex;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-06-15 03:02:03 +02:00
|
|
|
// if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
|
2023-01-24 15:44:49 +01:00
|
|
|
if (bufferedGasLimit.lessThan(upperGasLimit)) {
|
|
|
|
return bufferedGasLimit.toString();
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-06-15 03:02:03 +02:00
|
|
|
// otherwise use blockGasLimit
|
2023-01-24 15:44:49 +01:00
|
|
|
return upperGasLimit.toString();
|
2018-06-15 03:02:03 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 17:23:53 +01:00
|
|
|
function generateERC20TransferData({
|
2020-11-03 00:41:28 +01:00
|
|
|
toAddress = '0x0',
|
|
|
|
amount = '0x0',
|
|
|
|
sendToken,
|
|
|
|
}) {
|
2020-05-29 19:46:10 +02:00
|
|
|
if (!sendToken) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
|
|
|
TOKEN_TRANSFER_FUNCTION_SIGNATURE +
|
|
|
|
Array.prototype.map
|
|
|
|
.call(
|
|
|
|
abi.rawEncode(
|
|
|
|
['address', 'uint256'],
|
2022-06-28 18:51:09 +02:00
|
|
|
[addHexPrefix(toAddress), addHexPrefix(amount)],
|
2020-11-03 00:41:28 +01:00
|
|
|
),
|
|
|
|
(x) => `00${x.toString(16)}`.slice(-2),
|
|
|
|
)
|
|
|
|
.join('')
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-04-27 02:38:14 +02:00
|
|
|
}
|
2018-05-22 10:10:06 +02:00
|
|
|
|
2022-01-10 17:23:53 +01:00
|
|
|
function generateERC721TransferData({
|
|
|
|
toAddress = '0x0',
|
|
|
|
fromAddress = '0x0',
|
|
|
|
tokenId,
|
|
|
|
}) {
|
|
|
|
if (!tokenId) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
COLLECTIBLE_TRANSFER_FROM_FUNCTION_SIGNATURE +
|
|
|
|
Array.prototype.map
|
|
|
|
.call(
|
|
|
|
abi.rawEncode(
|
|
|
|
['address', 'address', 'uint256'],
|
2022-06-28 18:51:09 +02:00
|
|
|
[addHexPrefix(fromAddress), addHexPrefix(toAddress), tokenId],
|
2022-01-10 17:23:53 +01:00
|
|
|
),
|
|
|
|
(x) => `00${x.toString(16)}`.slice(-2),
|
|
|
|
)
|
|
|
|
.join('')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAssetTransferData({ sendToken, fromAddress, toAddress, amount }) {
|
|
|
|
switch (sendToken.standard) {
|
2023-01-18 15:47:29 +01:00
|
|
|
case TokenStandard.ERC721:
|
2022-01-10 17:23:53 +01:00
|
|
|
return generateERC721TransferData({
|
|
|
|
toAddress,
|
|
|
|
fromAddress,
|
|
|
|
tokenId: sendToken.tokenId,
|
|
|
|
});
|
2023-01-18 15:47:29 +01:00
|
|
|
case TokenStandard.ERC20:
|
2022-01-10 17:23:53 +01:00
|
|
|
default:
|
|
|
|
return generateERC20TransferData({
|
|
|
|
toAddress,
|
|
|
|
amount,
|
|
|
|
sendToken,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function ellipsify(text, first = 6, last = 4) {
|
2023-01-13 20:28:52 +01:00
|
|
|
if (!text) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return `${text.slice(0, first)}...${text.slice(-last)}`;
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|