Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Commit

Permalink
review-fix: Refactor to use util functions isEtcChainId, chainIdToStr…
Browse files Browse the repository at this point in the history
…ing, isNotErc20TokenAddress
  • Loading branch information
Luke Schoen committed Apr 8, 2019
1 parent 3452680 commit b31e433
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 36 deletions.
14 changes: 9 additions & 5 deletions packages/fether-react/src/Send/TxForm/TxDetails/TxDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import React, { Component } from 'react';
import BigNumber from 'bignumber.js';
import { fromWei, toWei } from '@parity/api/lib/util/wei';

import { chainIdToString, isNotErc20TokenAddress } from '../../../utils/chain';

class TxDetails extends Component {
renderDetails = () => {
const { estimatedTxFee, token, values } = this.props;
Expand Down Expand Up @@ -51,33 +53,35 @@ ${this.renderTotalAmount()}`;

renderFee = () => {
const { estimatedTxFee, values } = this.props;
const currentChainIdBN = values.chainId.valueOf();

if (!estimatedTxFee) {
return;
}

return `Fee: ${fromWei(estimatedTxFee, 'ether')
.toFixed(9)
.toString()} ${
values.chainId.valueOf() === '61' ? 'ETC' : 'ETH'
} (gas limit * gas price)`;
.toString()} ${chainIdToString(
currentChainIdBN
)} (gas limit * gas price)`;
};

renderTotalAmount = () => {
const { estimatedTxFee, token, values } = this.props;
const currentChainIdBN = values.chainId.valueOf();

if (!estimatedTxFee || !values.amount || !token.address) {
return;
}

return `Total Amount: ${fromWei(
estimatedTxFee.plus(
token.address === 'ETH' || token.address === 'ETC'
isNotErc20TokenAddress(token.address)
? toWei(values.amount.toString())
: 0
),
'ether'
).toString()} ${values.chainId.valueOf() === '61' ? 'ETC' : 'ETH'}`;
).toString()} ${chainIdToString(currentChainIdBN)}`;
};

render () {
Expand Down
37 changes: 20 additions & 17 deletions packages/fether-react/src/Send/TxForm/TxForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import { estimateGas } from '../../utils/transaction';
import RequireHealthOverlay from '../../RequireHealthOverlay';
import TokenBalance from '../../Tokens/TokensList/TokenBalance';
import TxDetails from './TxDetails';
import {
chainIdToString,
isEtcChainId,
isNotErc20TokenAddress
} from '../../utils/chain';
import withAccount from '../../utils/withAccount';
import withBalance, { withEthBalance } from '../../utils/withBalance';
import withTokens from '../../utils/withTokens';
Expand Down Expand Up @@ -106,7 +111,7 @@ class TxForm extends Component {
const gasPriceBn = new BigNumber(gasPrice);
let output;

if (token.address === 'ETH' || token.address === 'ETC') {
if (isNotErc20TokenAddress(token.address)) {
output = fromWei(
toWei(balance).minus(gasBn.multipliedBy(toWei(gasPriceBn, 'shannon')))
);
Expand Down Expand Up @@ -397,16 +402,16 @@ class TxForm extends Component {
} else if (amountBn.isZero()) {
if (this.state.maxSelected) {
return {
amount: `${
currentChainIdBN.valueOf() === '61' ? 'ETC' : 'ETH'
} balance too low to pay for gas.`
amount: `${chainIdToString(
currentChainIdBN
)} balance too low to pay for gas.`
};
}
return { amount: 'Please enter a non-zero amount' };
} else if (amountBn.isNegative()) {
return { amount: 'Please enter a positive amount' };
} else if (
(token.address === 'ETH' || token.address === 'ETC') &&
isNotErc20TokenAddress(token.address) &&
toWei(values.amount).lt(1)
) {
return { amount: 'Please enter at least 1 Wei' };
Expand All @@ -420,12 +425,12 @@ class TxForm extends Component {
return { amount: `You don't have enough ${token.symbol} balance` };
} else if (
(!values.to || !isAddress(values.to)) &&
currentChainIdBN.valueOf() !== '61'
!isEtcChainId(currentChainIdBN)
) {
return { to: 'Please enter a valid Ethereum address' };
} else if (
(!values.to || isAddress(values.to)) &&
currentChainIdBN.valueOf() === '61'
isEtcChainId(currentChainIdBN)
) {
return { to: 'Please enter a valid Ethereum Classic address' };
} else if (values.to === '0x0000000000000000000000000000000000000000') {
Expand Down Expand Up @@ -491,22 +496,20 @@ class TxForm extends Component {
if (
this.estimatedTxFee(values)
.plus(
token.address === 'ETH' || token.address === 'ETC'
? toWei(values.amount)
: 0
isNotErc20TokenAddress(token.address) ? toWei(values.amount) : 0
)
.gt(toWei(values.ethBalance))
) {
return token.address !== 'ETH' && token.address === 'ETC'
return isNotErc20TokenAddress(token.address)
? {
amount: `${
currentChainIdBN.valueOf() === '61' ? 'ETC' : 'ETH'
} balance too low to pay for gas`
amount: `${chainIdToString(
currentChainIdBN
)} balance too low to pay for gas`
}
: {
amount: `You don't have enough ${
currentChainIdBN.valueOf() === '61' ? 'ETC' : 'ETH'
} balance`
amount: `You don't have enough ${chainIdToString(
currentChainIdBN
)} balance`
};
}

Expand Down
21 changes: 10 additions & 11 deletions packages/fether-react/src/stores/sendStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { action, computed, observable } from 'mobx';
import { blockNumber$, post$, postRaw$ } from '@parity/light.js';

import { isNotErc20TokenAddress } from '../utils/chain';
import {
contractForToken,
txForErc20,
Expand Down Expand Up @@ -55,17 +56,15 @@ export class SendStore {
send = password => {
const { token } = this.tx;

const tx =
token.address === 'ETH' || token.address === 'ETC'
? txForEth(this.tx)
: txForErc20(this.tx, token);
const send$ =
token.address === 'ETH' || token.address === 'ETC'
? post$(tx, { passphrase: password })
: contractForToken(token.address).transfer$(...tx.args, {
...tx.options,
passphrase: password
});
const tx = isNotErc20TokenAddress(token.address)
? txForEth(this.tx)
: txForErc20(this.tx, token);
const send$ = isNotErc20TokenAddress(token.address)
? post$(tx, { passphrase: password })
: contractForToken(token.address).transfer$(...tx.args, {
...tx.options,
passphrase: password
});

debug('Sending tx.', tx);

Expand Down
4 changes: 3 additions & 1 deletion packages/fether-react/src/utils/blockscout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import { isNotErc20TokenAddress } from './chain';

const baseUrlForChain = chainName => {
let baseUrl;

Expand Down Expand Up @@ -39,7 +41,7 @@ const tokenTxUrl = (chainName, transactionHash) =>
`${baseUrlForChain(chainName)}/tx/${transactionHash}/token_transfers`;

const blockscoutTxUrl = (chainName, transactionHash, tokenAddress) =>
tokenAddress === 'ETH' || tokenAddress === 'ETC'
isNotErc20TokenAddress(tokenAddress)
? ethTxUrl(chainName, transactionHash)
: tokenTxUrl(chainName, transactionHash);

Expand Down
18 changes: 18 additions & 0 deletions packages/fether-react/src/utils/chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause

const isEtcChainId = currentChainIdBN => {
return currentChainIdBN.valueOf() === '61';
};

const chainIdToString = currentChainIdBN => {
return isEtcChainId(currentChainIdBN) ? 'ETC' : 'ETH';
};

const isNotErc20TokenAddress = tokenAddress => {
return tokenAddress === 'ETH' || tokenAddress === 'ETC';
};

export { chainIdToString, isNotErc20TokenAddress, isEtcChainId };
5 changes: 3 additions & 2 deletions packages/fether-react/src/utils/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { makeContract } from '@parity/light.js';
import memoize from 'lodash/memoize';
import { toWei } from '@parity/api/lib/util/wei';

import { isNotErc20TokenAddress } from './chain';
import Debug from './debug';
import EthereumTx from 'ethereumjs-tx';

Expand All @@ -30,7 +31,7 @@ export const estimateGas = (tx, token, api) => {
return Promise.reject(new Error('Tx not set.'));
}

if (token.address === 'ETH' || token.address === 'ETC') {
if (isNotErc20TokenAddress(token.address)) {
return estimateGasForEth(txForEth(tx), api).then(estimatedGasForEth => {
// do not add any buffer in case of an account to account transaction
return estimatedGasForEth.eq(21000)
Expand Down Expand Up @@ -136,7 +137,7 @@ const getEthereumTx = tx => {
chainId
};

if (token.address === 'ETH' || token.address === 'ETC') {
if (isNotErc20TokenAddress(token.address)) {
txParams.to = to;
txParams.value = parseFloat(amount) * Math.pow(10, 18);
} else {
Expand Down

0 comments on commit b31e433

Please sign in to comment.