2017-08-08 03:45:08 +00:00
|
|
|
// @flow
|
2017-08-23 06:57:18 +00:00
|
|
|
import Big from 'bignumber.js';
|
|
|
|
import translate from 'translations';
|
2017-08-28 17:43:57 +00:00
|
|
|
import { padToEven, addHexPrefix, toChecksumAddress } from 'ethereumjs-util';
|
2017-08-23 06:57:18 +00:00
|
|
|
import { isValidETHAddress } from 'libs/validators';
|
|
|
|
import ERC20 from 'libs/erc20';
|
|
|
|
import { toTokenUnit } from 'libs/units';
|
2017-08-28 17:43:57 +00:00
|
|
|
import { stripHex } from 'libs/values';
|
2017-09-05 16:32:14 +00:00
|
|
|
import type { INode } from 'libs/nodes/INode';
|
2017-08-23 06:57:18 +00:00
|
|
|
import type { BaseWallet } from 'libs/wallet';
|
|
|
|
import type { Token } from 'config/data';
|
|
|
|
import type EthTx from 'ethereumjs-tx';
|
2017-08-31 04:00:31 +00:00
|
|
|
import { toUnit } from 'libs/units';
|
|
|
|
|
|
|
|
export type BroadcastStatusTransaction = {
|
|
|
|
isBroadcasting: boolean,
|
|
|
|
signedTx: string,
|
|
|
|
successfullyBroadcast: boolean
|
|
|
|
};
|
2017-08-08 03:45:08 +00:00
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
// TODO: Enforce more bigs, or find better way to avoid ether vs wei for value
|
2017-08-08 03:45:08 +00:00
|
|
|
export type TransactionWithoutGas = {|
|
2017-08-11 21:54:10 +00:00
|
|
|
from: string,
|
2017-08-08 03:45:08 +00:00
|
|
|
to: string,
|
2017-08-11 21:54:10 +00:00
|
|
|
gasLimit?: string | number,
|
|
|
|
value: string | number,
|
|
|
|
data?: string,
|
|
|
|
chainId?: number
|
2017-08-08 03:45:08 +00:00
|
|
|
|};
|
|
|
|
|
|
|
|
export type Transaction = {|
|
|
|
|
...TransactionWithoutGas,
|
2017-08-11 21:54:10 +00:00
|
|
|
gasPrice: string | number
|
|
|
|
|};
|
|
|
|
|
|
|
|
export type RawTransaction = {|
|
|
|
|
nonce: string,
|
|
|
|
gasPrice: string,
|
|
|
|
gasLimit: string,
|
|
|
|
to: string,
|
|
|
|
value: string,
|
|
|
|
data: string,
|
|
|
|
chainId: number
|
|
|
|
|};
|
|
|
|
|
|
|
|
export type BroadcastTransaction = {|
|
|
|
|
...RawTransaction,
|
|
|
|
rawTx: string,
|
|
|
|
signedTx: string
|
2017-08-08 03:45:08 +00:00
|
|
|
|};
|
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),
|
|
|
|
// Everything else is as-is
|
|
|
|
nonce,
|
|
|
|
gasPrice,
|
|
|
|
gasLimit,
|
|
|
|
v,
|
|
|
|
r,
|
|
|
|
s
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function generateTransaction(
|
2017-09-05 16:32:14 +00:00
|
|
|
node: INode,
|
2017-08-23 06:57:18 +00:00
|
|
|
tx: Transaction,
|
|
|
|
wallet: BaseWallet,
|
|
|
|
token: ?Token
|
|
|
|
): Promise<BroadcastTransaction> {
|
|
|
|
// Reject bad addresses
|
|
|
|
if (!isValidETHAddress(tx.to)) {
|
|
|
|
throw new Error(translate('ERROR_5'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject token transactions without data
|
|
|
|
if (token && !tx.data) {
|
|
|
|
throw new Error('Tokens must be sent with data');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject gas limit under 21000 (Minimum for transaction)
|
|
|
|
// Reject if limit over 5000000
|
|
|
|
// TODO: Make this dynamic, the limit shifts
|
|
|
|
const limitBig = new Big(tx.gasLimit);
|
|
|
|
if (limitBig.lessThan(21000)) {
|
|
|
|
throw new Error(
|
|
|
|
translate('Gas limit must be at least 21000 for transactions')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limitBig.greaterThan(5000000)) {
|
|
|
|
throw new Error(translate('GETH_GasLimit'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject gas over 1000gwei (1000000000000)
|
|
|
|
const gasPriceBig = new Big(tx.gasPrice);
|
|
|
|
if (gasPriceBig.greaterThan(new Big('1000000000000'))) {
|
|
|
|
throw new Error(
|
|
|
|
'Gas price too high. Please contact support if this was not a mistake.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure their balance exceeds the amount they're sending
|
|
|
|
// TODO: Include gas price too, tokens should probably check ETH too
|
|
|
|
let value;
|
|
|
|
let balance;
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
// $FlowFixMe - We reject above if tx has no data for token
|
|
|
|
value = new Big(ERC20.$transfer(tx.data).value);
|
|
|
|
balance = toTokenUnit(await node.getTokenBalance(tx.from, token), token);
|
|
|
|
} else {
|
|
|
|
value = new Big(tx.value);
|
|
|
|
balance = await node.getBalance(tx.from);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.gte(balance)) {
|
|
|
|
throw new Error(translate('GETH_Balance'));
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
const cleanHex = hex => addHexPrefix(padToEven(stripHex(hex)));
|
|
|
|
|
2017-08-23 06:57:18 +00:00
|
|
|
// Generate the raw transaction
|
|
|
|
const txCount = await node.getTransactionCount(tx.from);
|
|
|
|
const rawTx = {
|
2017-08-28 17:43:57 +00:00
|
|
|
nonce: cleanHex(txCount),
|
|
|
|
gasPrice: cleanHex(new Big(tx.gasPrice).toString(16)),
|
|
|
|
gasLimit: cleanHex(new Big(tx.gasLimit).toString(16)),
|
|
|
|
to: cleanHex(tx.to),
|
|
|
|
value: token ? '0x00' : cleanHex(value.toString(16)),
|
|
|
|
data: tx.data ? cleanHex(tx.data) : '',
|
2017-08-23 06:57:18 +00:00
|
|
|
chainId: tx.chainId || 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// Sign the transaction
|
|
|
|
const rawTxJson = JSON.stringify(rawTx);
|
|
|
|
const signedTx = await wallet.signRawTransaction(rawTx);
|
|
|
|
|
|
|
|
// Repeat all of this shit for Flow typechecking. Sealed objects don't
|
|
|
|
// like spreads, so we have to be explicit.
|
|
|
|
return {
|
|
|
|
nonce: rawTx.nonce,
|
|
|
|
gasPrice: rawTx.gasPrice,
|
|
|
|
gasLimit: rawTx.gasLimit,
|
|
|
|
to: rawTx.to,
|
|
|
|
value: rawTx.value,
|
|
|
|
data: rawTx.data,
|
|
|
|
chainId: rawTx.chainId,
|
|
|
|
rawTx: rawTxJson,
|
|
|
|
signedTx: signedTx
|
|
|
|
};
|
|
|
|
}
|
2017-08-31 04:00:31 +00:00
|
|
|
|
|
|
|
// TODO determine best place for helper function
|
|
|
|
export function getBalanceMinusGasCosts(
|
|
|
|
weiGasLimit: Big,
|
|
|
|
weiGasPrice: Big,
|
|
|
|
weiBalance: Big
|
|
|
|
): Big {
|
|
|
|
const weiGasCosts = weiGasPrice.times(weiGasLimit);
|
|
|
|
const weiBalanceMinusGasCosts = weiBalance.minus(weiGasCosts);
|
|
|
|
return toUnit(weiBalanceMinusGasCosts, 'wei', 'ether');
|
|
|
|
}
|