Refactor TokenState type

This commit is contained in:
Brandon Millman
2018-03-21 12:57:16 -07:00
parent 3cf2cb89bb
commit 4e5cd472c2
4 changed files with 40 additions and 55 deletions

View File

@@ -10,7 +10,14 @@ import ReactTooltip = require('react-tooltip');
import { Blockchain } from 'ts/blockchain'; import { Blockchain } from 'ts/blockchain';
import { EthWethConversionButton } from 'ts/components/eth_weth_conversion_button'; import { EthWethConversionButton } from 'ts/components/eth_weth_conversion_button';
import { Dispatcher } from 'ts/redux/dispatcher'; import { Dispatcher } from 'ts/redux/dispatcher';
import { OutdatedWrappedEtherByNetworkId, Side, Token, TokenByAddress, TokenState } from 'ts/types'; import {
OutdatedWrappedEtherByNetworkId,
Side,
Token,
TokenByAddress,
TokenState,
TokenStateByAddress,
} from 'ts/types';
import { configs } from 'ts/utils/configs'; import { configs } from 'ts/utils/configs';
import { constants } from 'ts/utils/constants'; import { constants } from 'ts/utils/constants';
import { utils } from 'ts/utils/utils'; import { utils } from 'ts/utils/utils';
@@ -20,13 +27,6 @@ const ICON_DIMENSION = 40;
const ETHER_ICON_PATH = '/images/ether.png'; const ETHER_ICON_PATH = '/images/ether.png';
const OUTDATED_WETH_ICON_PATH = '/images/wrapped_eth_gray.png'; const OUTDATED_WETH_ICON_PATH = '/images/wrapped_eth_gray.png';
interface OutdatedWETHAddressToIsStateLoaded {
[address: string]: boolean;
}
interface OutdatedWETHStateByAddress {
[address: string]: TokenState;
}
interface EthWrappersProps { interface EthWrappersProps {
networkId: number; networkId: number;
blockchain: Blockchain; blockchain: Blockchain;
@@ -39,9 +39,7 @@ interface EthWrappersProps {
interface EthWrappersState { interface EthWrappersState {
ethTokenState: TokenState; ethTokenState: TokenState;
isWethStateLoaded: boolean; outdatedWETHStateByAddress: TokenStateByAddress;
outdatedWETHAddressToIsStateLoaded: OutdatedWETHAddressToIsStateLoaded;
outdatedWETHStateByAddress: OutdatedWETHStateByAddress;
} }
export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersState> { export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersState> {
@@ -50,22 +48,20 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
super(props); super(props);
this._isUnmounted = false; this._isUnmounted = false;
const outdatedWETHAddresses = this._getOutdatedWETHAddresses(); const outdatedWETHAddresses = this._getOutdatedWETHAddresses();
const outdatedWETHAddressToIsStateLoaded: OutdatedWETHAddressToIsStateLoaded = {}; const outdatedWETHStateByAddress: TokenStateByAddress = {};
const outdatedWETHStateByAddress: OutdatedWETHStateByAddress = {};
_.each(outdatedWETHAddresses, outdatedWETHAddress => { _.each(outdatedWETHAddresses, outdatedWETHAddress => {
outdatedWETHAddressToIsStateLoaded[outdatedWETHAddress] = false;
outdatedWETHStateByAddress[outdatedWETHAddress] = { outdatedWETHStateByAddress[outdatedWETHAddress] = {
balance: new BigNumber(0), balance: new BigNumber(0),
allowance: new BigNumber(0), allowance: new BigNumber(0),
isLoaded: false,
}; };
}); });
this.state = { this.state = {
outdatedWETHAddressToIsStateLoaded,
outdatedWETHStateByAddress, outdatedWETHStateByAddress,
isWethStateLoaded: false,
ethTokenState: { ethTokenState: {
balance: new BigNumber(0), balance: new BigNumber(0),
allowance: new BigNumber(0), allowance: new BigNumber(0),
isLoaded: false,
}, },
}; };
} }
@@ -169,7 +165,7 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
{this._renderTokenLink(tokenLabel, etherscanUrl)} {this._renderTokenLink(tokenLabel, etherscanUrl)}
</TableRowColumn> </TableRowColumn>
<TableRowColumn> <TableRowColumn>
{this.state.isWethStateLoaded ? ( {this.state.ethTokenState.isLoaded ? (
`${wethBalance.toFixed(configs.AMOUNT_DISPLAY_PRECSION)} WETH` `${wethBalance.toFixed(configs.AMOUNT_DISPLAY_PRECSION)} WETH`
) : ( ) : (
<i className="zmdi zmdi-spinner zmdi-hc-spin" /> <i className="zmdi zmdi-spinner zmdi-hc-spin" />
@@ -183,7 +179,7 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
networkId={this.props.networkId} networkId={this.props.networkId}
isOutdatedWrappedEther={false} isOutdatedWrappedEther={false}
direction={Side.Receive} direction={Side.Receive}
isDisabled={!this.state.isWethStateLoaded} isDisabled={!this.state.ethTokenState.isLoaded}
ethToken={etherToken} ethToken={etherToken}
dispatcher={this.props.dispatcher} dispatcher={this.props.dispatcher}
blockchain={this.props.blockchain} blockchain={this.props.blockchain}
@@ -266,8 +262,8 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
...etherToken, ...etherToken,
address: outdatedWETHIfExists.address, address: outdatedWETHIfExists.address,
}; };
const isStateLoaded = this.state.outdatedWETHAddressToIsStateLoaded[outdatedWETHIfExists.address];
const outdatedEtherTokenState = this.state.outdatedWETHStateByAddress[outdatedWETHIfExists.address]; const outdatedEtherTokenState = this.state.outdatedWETHStateByAddress[outdatedWETHIfExists.address];
const isStateLoaded = outdatedEtherTokenState.isLoaded;
const balanceInEthIfExists = isStateLoaded const balanceInEthIfExists = isStateLoaded
? ZeroEx.toUnitAmount(outdatedEtherTokenState.balance, constants.DECIMAL_PLACES_ETH).toFixed( ? ZeroEx.toUnitAmount(outdatedEtherTokenState.balance, constants.DECIMAL_PLACES_ETH).toFixed(
configs.AMOUNT_DISPLAY_PRECSION, configs.AMOUNT_DISPLAY_PRECSION,
@@ -345,10 +341,15 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
); );
} }
private async _onOutdatedConversionSuccessfulAsync(outdatedWETHAddress: string) { private async _onOutdatedConversionSuccessfulAsync(outdatedWETHAddress: string) {
const currentOutdatedWETHState = this.state.outdatedWETHStateByAddress[outdatedWETHAddress];
this.setState({ this.setState({
outdatedWETHAddressToIsStateLoaded: { outdatedWETHStateByAddress: {
...this.state.outdatedWETHAddressToIsStateLoaded, ...this.state.outdatedWETHStateByAddress,
[outdatedWETHAddress]: false, [outdatedWETHAddress]: {
balance: currentOutdatedWETHState.balance,
allowance: currentOutdatedWETHState.allowance,
isLoaded: false,
},
}, },
}); });
const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress; const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress;
@@ -357,15 +358,12 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
outdatedWETHAddress, outdatedWETHAddress,
); );
this.setState({ this.setState({
outdatedWETHAddressToIsStateLoaded: {
...this.state.outdatedWETHAddressToIsStateLoaded,
[outdatedWETHAddress]: true,
},
outdatedWETHStateByAddress: { outdatedWETHStateByAddress: {
...this.state.outdatedWETHStateByAddress, ...this.state.outdatedWETHStateByAddress,
[outdatedWETHAddress]: { [outdatedWETHAddress]: {
balance, balance,
allowance, allowance,
isLoaded: true,
}, },
}, },
}); });
@@ -380,8 +378,7 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
); );
const outdatedWETHAddresses = this._getOutdatedWETHAddresses(); const outdatedWETHAddresses = this._getOutdatedWETHAddresses();
const outdatedWETHAddressToIsStateLoaded: OutdatedWETHAddressToIsStateLoaded = {}; const outdatedWETHStateByAddress: TokenStateByAddress = {};
const outdatedWETHStateByAddress: OutdatedWETHStateByAddress = {};
for (const address of outdatedWETHAddresses) { for (const address of outdatedWETHAddresses) {
const [balance, allowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync( const [balance, allowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
userAddressIfExists, userAddressIfExists,
@@ -390,18 +387,17 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
outdatedWETHStateByAddress[address] = { outdatedWETHStateByAddress[address] = {
balance, balance,
allowance, allowance,
isLoaded: true,
}; };
outdatedWETHAddressToIsStateLoaded[address] = true;
} }
if (!this._isUnmounted) { if (!this._isUnmounted) {
this.setState({ this.setState({
outdatedWETHStateByAddress, outdatedWETHStateByAddress,
outdatedWETHAddressToIsStateLoaded,
ethTokenState: { ethTokenState: {
balance: wethBalance, balance: wethBalance,
allowance: wethAllowance, allowance: wethAllowance,
isLoaded: true,
}, },
isWethStateLoaded: true,
}); });
} }
} }
@@ -434,6 +430,7 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
ethTokenState: { ethTokenState: {
balance, balance,
allowance, allowance,
isLoaded: true,
}, },
}); });
} }

View File

@@ -37,6 +37,7 @@ import {
ScreenWidths, ScreenWidths,
Token, Token,
TokenByAddress, TokenByAddress,
TokenStateByAddress,
TokenVisibility, TokenVisibility,
} from 'ts/types'; } from 'ts/types';
import { configs } from 'ts/utils/configs'; import { configs } from 'ts/utils/configs';
@@ -61,14 +62,6 @@ const styles: Styles = {
}, },
}; };
interface TokenStateByAddress {
[address: string]: {
balance: BigNumber;
allowance: BigNumber;
isLoaded: boolean;
};
}
interface TokenBalancesProps { interface TokenBalancesProps {
blockchain: Blockchain; blockchain: Blockchain;
blockchainErr: BlockchainErrs; blockchainErr: BlockchainErrs;

View File

@@ -20,7 +20,7 @@ import { AllowanceToggle } from 'ts/components/inputs/allowance_toggle';
import { Identicon } from 'ts/components/ui/identicon'; import { Identicon } from 'ts/components/ui/identicon';
import { TokenIcon } from 'ts/components/ui/token_icon'; import { TokenIcon } from 'ts/components/ui/token_icon';
import { Dispatcher } from 'ts/redux/dispatcher'; import { Dispatcher } from 'ts/redux/dispatcher';
import { BalanceErrs, BlockchainErrs, Token, TokenByAddress } from 'ts/types'; import { BalanceErrs, BlockchainErrs, Token, TokenByAddress, TokenState, TokenStateByAddress } from 'ts/types';
import { constants } from 'ts/utils/constants'; import { constants } from 'ts/utils/constants';
import { utils } from 'ts/utils/utils'; import { utils } from 'ts/utils/utils';
@@ -41,16 +41,6 @@ interface WalletState {
trackedTokenStateByAddress: TokenStateByAddress; trackedTokenStateByAddress: TokenStateByAddress;
} }
interface TokenStateByAddress {
[address: string]: TokenState;
}
interface TokenState {
balance: BigNumber;
allowance: BigNumber;
isLoaded: boolean;
}
enum WrappedEtherAction { enum WrappedEtherAction {
Wrap, Wrap,
Unwrap, Unwrap,

View File

@@ -21,11 +21,6 @@ export interface TokenByAddress {
[address: string]: Token; [address: string]: Token;
} }
export interface TokenState {
allowance: BigNumber;
balance: BigNumber;
}
export interface AssetToken { export interface AssetToken {
address?: string; address?: string;
amount?: BigNumber; amount?: BigNumber;
@@ -469,4 +464,14 @@ export enum Providers {
Metamask = 'METAMASK', Metamask = 'METAMASK',
Mist = 'MIST', Mist = 'MIST',
} }
export interface TokenStateByAddress {
[address: string]: TokenState;
}
export interface TokenState {
balance: BigNumber;
allowance: BigNumber;
isLoaded: boolean;
}
// tslint:disable:max-file-line-count // tslint:disable:max-file-line-count