2017-09-25 02:06:28 +00:00
|
|
|
import { Token } from 'config/data';
|
|
|
|
import EthTx from 'ethereumjs-tx';
|
|
|
|
import { addHexPrefix, padToEven, toChecksumAddress } from 'ethereumjs-util';
|
2017-08-23 06:57:18 +00:00
|
|
|
import ERC20 from 'libs/erc20';
|
2017-09-08 19:01:31 +00:00
|
|
|
import { TransactionWithoutGas } from 'libs/messages';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { RPCNode } from 'libs/nodes';
|
|
|
|
import { INode } from 'libs/nodes/INode';
|
2017-11-12 19:45:52 +00:00
|
|
|
import { UnitKey, Wei, TokenValue, toTokenBase } from 'libs/units';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { isValidETHAddress } from 'libs/validators';
|
2017-11-12 19:45:52 +00:00
|
|
|
import { stripHexPrefixAndLower, sanitizeHex, toHexWei } from 'libs/values';
|
2017-11-29 23:14:57 +00:00
|
|
|
import { IFullWallet, Web3Wallet } from 'libs/wallet';
|
2017-10-19 02:29:49 +00:00
|
|
|
import { translateRaw } from 'translations';
|
2017-09-05 21:28:19 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface TransactionInput {
|
|
|
|
token?: Token | null;
|
|
|
|
unit: UnitKey;
|
|
|
|
value: string;
|
|
|
|
to: string;
|
|
|
|
data: string;
|
|
|
|
}
|
2017-08-31 04:00:31 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface BroadcastTransactionStatus {
|
|
|
|
isBroadcasting: boolean;
|
|
|
|
signedTx: string;
|
|
|
|
successfullyBroadcast: boolean;
|
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface BaseTransaction {
|
|
|
|
to: string;
|
|
|
|
value: string;
|
|
|
|
data: string;
|
2017-11-12 19:45:52 +00:00
|
|
|
gasLimit: Wei | string;
|
2017-09-25 02:06:28 +00:00
|
|
|
gasPrice: Wei | string;
|
|
|
|
chainId: number;
|
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface RawTransaction extends BaseTransaction {
|
|
|
|
nonce: string;
|
|
|
|
}
|
2017-08-11 21:54:10 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface ExtendedRawTransaction extends RawTransaction {
|
2017-09-05 21:28:19 +00:00
|
|
|
// non-standard, legacy
|
2017-09-25 02:06:28 +00:00
|
|
|
from: string;
|
|
|
|
}
|
2017-08-11 21:54:10 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface CompleteTransaction extends RawTransaction {
|
|
|
|
rawTx: string;
|
|
|
|
signedTx: string;
|
|
|
|
}
|
2017-08-23 06:57:18 +00:00
|
|
|
|
|
|
|
// Get useable fields from an EthTx object.
|
|
|
|
export function getTransactionFields(tx: EthTx) {
|
|
|
|
// For some crazy reason, toJSON spits out an array, not keyed values.
|
|
|
|
const [nonce, gasPrice, gasLimit, to, value, data, v, r, s] = tx.toJSON();
|
|
|
|
|
|
|
|
return {
|
2017-08-28 17:43:57 +00:00
|
|
|
// No value comes back as '0x', but most things expect '0x00'
|
|
|
|
value: value === '0x' ? '0x00' : value,
|
2017-08-23 06:57:18 +00:00
|
|
|
// If data is 0x, it might as well not be there
|
|
|
|
data: data === '0x' ? null : data,
|
|
|
|
// To address is unchecksummed, which could cause mismatches in comparisons
|
|
|
|
to: toChecksumAddress(to),
|
2017-10-23 20:48:55 +00:00
|
|
|
from: sanitizeHex(tx.getSenderAddress().toString('hex')),
|
2017-08-23 06:57:18 +00:00
|
|
|
// Everything else is as-is
|
|
|
|
nonce,
|
|
|
|
gasPrice,
|
|
|
|
gasLimit,
|
|
|
|
v,
|
|
|
|
r,
|
|
|
|
s
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-11 05:04:49 +00:00
|
|
|
function getValue(
|
|
|
|
token: Token | null | undefined,
|
|
|
|
tx: ExtendedRawTransaction
|
2017-11-12 19:45:52 +00:00
|
|
|
): Wei {
|
2017-10-11 05:04:49 +00:00
|
|
|
let value;
|
|
|
|
if (token) {
|
2017-11-12 19:45:52 +00:00
|
|
|
value = Wei(ERC20.$transfer(tx.data).value);
|
2017-10-11 05:04:49 +00:00
|
|
|
} else {
|
2017-11-12 19:45:52 +00:00
|
|
|
value = Wei(tx.value);
|
2017-10-11 05:04:49 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getBalance(
|
2017-09-05 16:32:14 +00:00
|
|
|
node: INode,
|
2017-09-05 21:28:19 +00:00
|
|
|
tx: ExtendedRawTransaction,
|
2017-09-25 02:06:28 +00:00
|
|
|
token: Token | null | undefined
|
2017-10-11 05:04:49 +00:00
|
|
|
) {
|
|
|
|
const { from } = tx;
|
|
|
|
const ETHBalance = await node.getBalance(from);
|
2017-11-12 19:45:52 +00:00
|
|
|
let balance: Wei;
|
2017-10-11 05:04:49 +00:00
|
|
|
if (token) {
|
2017-11-12 19:45:52 +00:00
|
|
|
balance = toTokenBase(
|
|
|
|
await node.getTokenBalance(tx.from, token).toString(),
|
|
|
|
token.decimal
|
|
|
|
);
|
2017-10-11 05:04:49 +00:00
|
|
|
} else {
|
2017-11-12 19:45:52 +00:00
|
|
|
balance = ETHBalance;
|
2017-10-11 05:04:49 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
balance,
|
|
|
|
ETHBalance
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function balanceCheck(
|
|
|
|
node: INode,
|
|
|
|
tx: ExtendedRawTransaction,
|
|
|
|
token: Token | null | undefined,
|
2017-11-12 19:45:52 +00:00
|
|
|
value: Wei,
|
2017-10-11 05:04:49 +00:00
|
|
|
gasCost: Wei
|
|
|
|
) {
|
|
|
|
// Ensure their balance exceeds the amount they're sending
|
|
|
|
const { balance, ETHBalance } = await getBalance(node, tx, token);
|
|
|
|
if (value.gt(balance)) {
|
|
|
|
throw new Error(translateRaw('GETH_Balance'));
|
|
|
|
}
|
|
|
|
// ensure gas cost is not greaterThan current eth balance
|
|
|
|
// TODO check that eth balance is not lesser than txAmount + gasCost
|
2017-11-12 19:45:52 +00:00
|
|
|
if (gasCost.gt(ETHBalance)) {
|
2017-10-11 05:04:49 +00:00
|
|
|
throw new Error(
|
2017-11-12 19:45:52 +00:00
|
|
|
`gasCost: ${gasCost.toString()} greaterThan ETHBalance: ${ETHBalance.toString()}`
|
2017-10-11 05:04:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateTxValidation(
|
|
|
|
to: string,
|
|
|
|
token: Token | null | undefined,
|
|
|
|
data: string,
|
2017-11-12 19:45:52 +00:00
|
|
|
gasLimit: Wei | string,
|
2017-10-17 04:01:28 +00:00
|
|
|
gasPrice: Wei | string,
|
|
|
|
skipEthAddressValidation: boolean
|
2017-10-11 05:04:49 +00:00
|
|
|
) {
|
2017-08-23 06:57:18 +00:00
|
|
|
// Reject bad addresses
|
2017-10-17 04:01:28 +00:00
|
|
|
if (!isValidETHAddress(to) && !skipEthAddressValidation) {
|
2017-09-25 02:06:28 +00:00
|
|
|
throw new Error(translateRaw('ERROR_5'));
|
2017-08-23 06:57:18 +00:00
|
|
|
}
|
|
|
|
// Reject token transactions without data
|
2017-09-08 19:01:31 +00:00
|
|
|
if (token && !data) {
|
2017-08-23 06:57:18 +00:00
|
|
|
throw new Error('Tokens must be sent with data');
|
|
|
|
}
|
2017-09-25 02:06:28 +00:00
|
|
|
if (typeof gasLimit === 'string' || typeof gasPrice === 'string') {
|
|
|
|
throw Error('Gas Limit and Gas Price should be of type bignumber');
|
|
|
|
}
|
2017-08-23 06:57:18 +00:00
|
|
|
// Reject gas limit under 21000 (Minimum for transaction)
|
|
|
|
// Reject if limit over 5000000
|
|
|
|
// TODO: Make this dynamic, the limit shifts
|
2017-11-12 19:45:52 +00:00
|
|
|
if (gasLimit.ltn(21000)) {
|
2017-09-08 19:01:31 +00:00
|
|
|
throw new Error('Gas limit must be at least 21000 for transactions');
|
2017-08-23 06:57:18 +00:00
|
|
|
}
|
2017-09-08 19:01:31 +00:00
|
|
|
// Reject gasLimit over 5000000gwei
|
2017-11-12 19:45:52 +00:00
|
|
|
if (gasLimit.gtn(5000000)) {
|
2017-09-25 02:06:28 +00:00
|
|
|
throw new Error(translateRaw('GETH_GasLimit'));
|
2017-08-23 06:57:18 +00:00
|
|
|
}
|
2017-09-08 19:01:31 +00:00
|
|
|
// Reject gasPrice over 1000gwei (1000000000000)
|
2017-11-12 19:45:52 +00:00
|
|
|
const gwei = Wei('1000000000000');
|
|
|
|
if (gasPrice.gt(gwei)) {
|
2017-08-23 06:57:18 +00:00
|
|
|
throw new Error(
|
|
|
|
'Gas price too high. Please contact support if this was not a mistake.'
|
|
|
|
);
|
|
|
|
}
|
2017-10-11 05:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function generateCompleteTransactionFromRawTransaction(
|
|
|
|
node: INode,
|
|
|
|
tx: ExtendedRawTransaction,
|
2017-11-29 23:14:57 +00:00
|
|
|
wallet: IFullWallet,
|
2017-10-11 05:04:49 +00:00
|
|
|
token: Token | null | undefined,
|
2017-10-17 04:01:28 +00:00
|
|
|
skipValidation: boolean,
|
2017-10-11 05:04:49 +00:00
|
|
|
offline?: boolean
|
|
|
|
): Promise<CompleteTransaction> {
|
|
|
|
const { to, data, gasLimit, gasPrice, chainId, nonce } = tx;
|
|
|
|
// validation
|
2017-10-17 04:01:28 +00:00
|
|
|
generateTxValidation(to, token, data, gasLimit, gasPrice, skipValidation);
|
2017-10-11 05:04:49 +00:00
|
|
|
// duplicated from generateTxValidation -- typescript bug
|
|
|
|
if (typeof gasLimit === 'string' || typeof gasPrice === 'string') {
|
|
|
|
throw Error('Gas Limit and Gas Price should be of type bignumber');
|
2017-09-08 19:01:31 +00:00
|
|
|
}
|
2017-10-11 05:04:49 +00:00
|
|
|
// computed gas cost (gasprice * gaslimit)
|
2017-11-12 19:45:52 +00:00
|
|
|
const gasCost: Wei = Wei(gasPrice.mul(gasLimit));
|
2017-10-11 05:04:49 +00:00
|
|
|
// get amount value (either in ETH or in Token)
|
|
|
|
const value = getValue(token, tx);
|
|
|
|
// if not offline, ensure that balance exceeds costs
|
|
|
|
if (!offline) {
|
|
|
|
await balanceCheck(node, tx, token, value, gasCost);
|
2017-08-23 06:57:18 +00:00
|
|
|
}
|
2017-08-28 17:43:57 +00:00
|
|
|
// Taken from v3's `sanitizeHex`, ensures that the value is a %2 === 0
|
|
|
|
// prefix'd hex value.
|
2017-09-15 19:29:38 +00:00
|
|
|
const cleanHex = hex => addHexPrefix(padToEven(stripHexPrefixAndLower(hex)));
|
2017-09-05 21:28:19 +00:00
|
|
|
const cleanedRawTx = {
|
2017-09-08 19:01:31 +00:00
|
|
|
nonce: cleanHex(nonce),
|
|
|
|
gasPrice: cleanHex(gasPrice.toString(16)),
|
|
|
|
gasLimit: cleanHex(gasLimit.toString(16)),
|
2017-09-15 19:29:38 +00:00
|
|
|
to: toChecksumAddress(cleanHex(to)),
|
2017-08-28 17:43:57 +00:00
|
|
|
value: token ? '0x00' : cleanHex(value.toString(16)),
|
2017-09-08 19:01:31 +00:00
|
|
|
data: data ? cleanHex(data) : '',
|
2017-12-01 16:09:51 +00:00
|
|
|
chainId: chainId || 0
|
2017-08-23 06:57:18 +00:00
|
|
|
};
|
2017-10-11 05:04:49 +00:00
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
// Sign the transaction
|
2017-09-05 21:28:19 +00:00
|
|
|
const rawTxJson = JSON.stringify(cleanedRawTx);
|
|
|
|
const signedTx = await wallet.signRawTransaction(cleanedRawTx);
|
2017-10-11 05:04:49 +00:00
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
return {
|
2017-10-11 05:04:49 +00:00
|
|
|
...cleanedRawTx,
|
2017-08-23 06:57:18 +00:00
|
|
|
rawTx: rawTxJson,
|
2017-09-25 02:06:28 +00:00
|
|
|
signedTx
|
2017-08-23 06:57:18 +00:00
|
|
|
};
|
|
|
|
}
|
2017-08-31 04:00:31 +00:00
|
|
|
|
2017-09-05 21:28:19 +00:00
|
|
|
export async function formatTxInput(
|
2017-11-29 23:14:57 +00:00
|
|
|
wallet: IFullWallet,
|
2017-09-05 21:28:19 +00:00
|
|
|
{ token, unit, value, to, data }: TransactionInput
|
|
|
|
): Promise<TransactionWithoutGas> {
|
|
|
|
if (unit === 'ether') {
|
|
|
|
return {
|
|
|
|
to,
|
2017-11-08 18:16:43 +00:00
|
|
|
from: await wallet.getAddressString(),
|
2017-11-12 19:45:52 +00:00
|
|
|
value: toHexWei(value), //turn users ether to wei
|
2017-09-05 21:28:19 +00:00
|
|
|
data
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
if (!token) {
|
|
|
|
throw new Error('No matching token');
|
|
|
|
}
|
2017-11-12 19:45:52 +00:00
|
|
|
const bigAmount = TokenValue(value);
|
2017-09-08 19:01:31 +00:00
|
|
|
const ERC20Data = ERC20.transfer(to, bigAmount);
|
2017-09-05 21:28:19 +00:00
|
|
|
return {
|
|
|
|
to: token.address,
|
2017-11-08 18:16:43 +00:00
|
|
|
from: await wallet.getAddressString(),
|
2017-09-05 21:28:19 +00:00
|
|
|
value: '0x0',
|
2017-09-08 19:01:31 +00:00
|
|
|
data: ERC20Data
|
2017-09-05 21:28:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:30:20 +00:00
|
|
|
export async function confirmAndSendWeb3Transaction(
|
|
|
|
wallet: Web3Wallet,
|
|
|
|
nodeLib: RPCNode,
|
|
|
|
gasPrice: Wei,
|
2017-11-12 19:45:52 +00:00
|
|
|
gasLimit: Wei,
|
2017-11-10 03:30:20 +00:00
|
|
|
chainId: number,
|
|
|
|
transactionInput: TransactionInput
|
|
|
|
): Promise<string> {
|
|
|
|
const { from, to, value, data } = await formatTxInput(
|
|
|
|
wallet,
|
|
|
|
transactionInput
|
|
|
|
);
|
|
|
|
const transaction: ExtendedRawTransaction = {
|
|
|
|
nonce: await nodeLib.getTransactionCount(from),
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
gasLimit,
|
|
|
|
value,
|
|
|
|
data,
|
|
|
|
chainId,
|
|
|
|
gasPrice
|
|
|
|
};
|
|
|
|
|
|
|
|
return wallet.sendTransaction(transaction);
|
|
|
|
}
|
|
|
|
|
2017-09-05 21:28:19 +00:00
|
|
|
export async function generateCompleteTransaction(
|
2017-11-29 23:14:57 +00:00
|
|
|
wallet: IFullWallet,
|
2017-09-05 21:28:19 +00:00
|
|
|
nodeLib: RPCNode,
|
2017-09-08 19:01:31 +00:00
|
|
|
gasPrice: Wei,
|
2017-11-12 19:45:52 +00:00
|
|
|
gasLimit: Wei,
|
2017-09-05 21:28:19 +00:00
|
|
|
chainId: number,
|
2017-10-11 05:04:49 +00:00
|
|
|
transactionInput: TransactionInput,
|
2017-10-17 04:01:28 +00:00
|
|
|
skipValidation: boolean,
|
2017-10-11 05:04:49 +00:00
|
|
|
nonce?: number | null,
|
|
|
|
offline?: boolean
|
2017-09-05 21:28:19 +00:00
|
|
|
): Promise<CompleteTransaction> {
|
|
|
|
const { token } = transactionInput;
|
2017-09-08 19:01:31 +00:00
|
|
|
const { from, to, value, data } = await formatTxInput(
|
|
|
|
wallet,
|
|
|
|
transactionInput
|
|
|
|
);
|
2017-09-05 21:28:19 +00:00
|
|
|
const transaction: ExtendedRawTransaction = {
|
2017-10-11 05:04:49 +00:00
|
|
|
nonce: nonce ? `0x${nonce}` : await nodeLib.getTransactionCount(from),
|
2017-09-05 21:28:19 +00:00
|
|
|
from,
|
2017-09-08 19:01:31 +00:00
|
|
|
to,
|
2017-09-05 21:28:19 +00:00
|
|
|
gasLimit,
|
2017-09-08 19:01:31 +00:00
|
|
|
value,
|
|
|
|
data,
|
2017-09-05 21:28:19 +00:00
|
|
|
chainId,
|
|
|
|
gasPrice
|
|
|
|
};
|
|
|
|
return await generateCompleteTransactionFromRawTransaction(
|
|
|
|
nodeLib,
|
|
|
|
transaction,
|
|
|
|
wallet,
|
2017-10-11 05:04:49 +00:00
|
|
|
token,
|
2017-10-17 04:01:28 +00:00
|
|
|
skipValidation,
|
2017-10-11 05:04:49 +00:00
|
|
|
offline
|
2017-09-05 21:28:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:00:31 +00:00
|
|
|
// TODO determine best place for helper function
|
|
|
|
export function getBalanceMinusGasCosts(
|
2017-11-12 19:45:52 +00:00
|
|
|
gasLimit: Wei,
|
2017-09-08 19:01:31 +00:00
|
|
|
gasPrice: Wei,
|
|
|
|
balance: Wei
|
2017-11-12 19:45:52 +00:00
|
|
|
): Wei {
|
|
|
|
const weiGasCosts = gasPrice.mul(gasLimit);
|
|
|
|
const weiBalanceMinusGasCosts = balance.sub(weiGasCosts);
|
|
|
|
return Wei(weiBalanceMinusGasCosts);
|
2017-08-31 04:00:31 +00:00
|
|
|
}
|
2017-10-23 20:48:55 +00:00
|
|
|
|
|
|
|
export function decodeTransaction(transaction: EthTx, token: Token | false) {
|
|
|
|
const { to, value, data, gasPrice, nonce, from } = getTransactionFields(
|
|
|
|
transaction
|
|
|
|
);
|
2017-11-12 19:45:52 +00:00
|
|
|
let fixedValue: TokenValue;
|
2017-10-23 20:48:55 +00:00
|
|
|
let toAddress;
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
const tokenData = ERC20.$transfer(data);
|
2017-11-12 19:45:52 +00:00
|
|
|
fixedValue = tokenData.value;
|
2017-10-23 20:48:55 +00:00
|
|
|
toAddress = tokenData.to;
|
|
|
|
} else {
|
2017-11-12 19:45:52 +00:00
|
|
|
fixedValue = Wei(value);
|
2017-10-23 20:48:55 +00:00
|
|
|
toAddress = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
value: fixedValue,
|
2017-11-12 19:45:52 +00:00
|
|
|
gasPrice: Wei(gasPrice),
|
2017-10-23 20:48:55 +00:00
|
|
|
data,
|
|
|
|
toAddress,
|
|
|
|
nonce,
|
|
|
|
from
|
|
|
|
};
|
|
|
|
}
|