Derive available tx metadata (#1257)
* Derive "from" and "unit" parameters from current transaction when possible * Fix tsc errors * Address changes
This commit is contained in:
parent
3bce82ba86
commit
16e6677c0f
|
@ -8,6 +8,7 @@ import { connect } from 'react-redux';
|
|||
import { SerializedTransaction } from 'components/renderCbs';
|
||||
import { AppState } from 'reducers';
|
||||
import { getFrom, getUnit, isEtherTransaction } from 'selectors/transaction';
|
||||
import { toChecksumAddress } from 'ethereumjs-util';
|
||||
|
||||
interface StateProps {
|
||||
from: AppState['transaction']['meta']['from'];
|
||||
|
@ -24,7 +25,9 @@ class AddressesClass extends Component<StateProps> {
|
|||
return (
|
||||
<SerializedTransaction
|
||||
withSerializedTransaction={(_, { to, data }) => {
|
||||
const toFormatted = isToken ? ERC20.transfer.decodeInput(data)._to : to;
|
||||
const toFormatted = toChecksumAddress(
|
||||
isToken ? ERC20.transfer.decodeInput(data)._to : to
|
||||
);
|
||||
return (
|
||||
<div className="tx-modal-address">
|
||||
<div className="tx-modal-address-from">
|
||||
|
|
|
@ -3,13 +3,60 @@ import { getTransactionState } from './transaction';
|
|||
import { getToken } from 'selectors/wallet';
|
||||
import { isNetworkUnit } from 'selectors/config/wallet';
|
||||
import { getDecimalFromEtherUnit } from 'libs/units';
|
||||
import { getSerializedTransaction } from 'selectors/transaction';
|
||||
import EthTx from 'ethereumjs-tx';
|
||||
import { getCustomTokens } from 'selectors/customTokens';
|
||||
import { getNetworkConfig } from 'selectors/config';
|
||||
import { Token } from '../../../shared/types/network';
|
||||
import { stripHexPrefixAndLower } from 'libs/values';
|
||||
import { toChecksumAddress } from 'ethereumjs-util';
|
||||
|
||||
const getMetaState = (state: AppState) => getTransactionState(state).meta;
|
||||
const getFrom = (state: AppState) => getMetaState(state).from;
|
||||
const getFrom = (state: AppState) => {
|
||||
const serializedTransaction = getSerializedTransaction(state);
|
||||
// attempt to get the from address from the transaction
|
||||
if (serializedTransaction) {
|
||||
const transactionInstance = new EthTx(serializedTransaction);
|
||||
try {
|
||||
const from = transactionInstance.from;
|
||||
if (from) {
|
||||
return toChecksumAddress(from.toString('hex'));
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
}
|
||||
}
|
||||
return getMetaState(state).from;
|
||||
};
|
||||
const getDecimal = (state: AppState) => getMetaState(state).decimal;
|
||||
|
||||
const getTokenTo = (state: AppState) => getMetaState(state).tokenTo;
|
||||
const getTokenValue = (state: AppState) => getMetaState(state).tokenValue;
|
||||
const getUnit = (state: AppState) => getMetaState(state).unit;
|
||||
const getUnit = (state: AppState) => {
|
||||
const serializedTransaction = getSerializedTransaction(state);
|
||||
// attempt to get the to address from the transaction
|
||||
if (serializedTransaction) {
|
||||
const transactionInstance = new EthTx(serializedTransaction);
|
||||
const { to } = transactionInstance;
|
||||
if (to) {
|
||||
// see if any tokens match
|
||||
let networkTokens: null | Token[] = null;
|
||||
const customTokens = getCustomTokens(state);
|
||||
const networkConfig = getNetworkConfig(state);
|
||||
if (!networkConfig.isCustom) {
|
||||
networkTokens = networkConfig.tokens;
|
||||
}
|
||||
const mergedTokens = networkTokens ? [...networkTokens, ...customTokens] : customTokens;
|
||||
const stringTo = toChecksumAddress(stripHexPrefixAndLower(to.toString('hex')));
|
||||
const result = mergedTokens.find(t => t.address === stringTo);
|
||||
if (result) {
|
||||
return result.symbol;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getMetaState(state).unit;
|
||||
};
|
||||
const getPreviousUnit = (state: AppState) => getMetaState(state).previousUnit;
|
||||
const getDecimalFromUnit = (state: AppState, unit: string) => {
|
||||
if (isNetworkUnit(state, unit)) {
|
||||
|
|
Loading…
Reference in New Issue