Fix getTokenBalanceAndAllowanceAsync to take ownerAddressIfExists so that we don't sometimes pass an empty string and sometimes undefined

This commit is contained in:
Fabio Berger
2018-03-11 12:33:34 +01:00
parent 56a8e0a09a
commit caaa70f630
6 changed files with 25 additions and 13 deletions

View File

@@ -496,18 +496,21 @@ export class Blockchain {
);
return tokenBalanceAndAllowance;
}
public async getTokenBalanceAndAllowanceAsync(ownerAddress: string, tokenAddress: string): Promise<BigNumber[]> {
public async getTokenBalanceAndAllowanceAsync(
ownerAddressIfExists: string,
tokenAddress: string,
): Promise<BigNumber[]> {
utils.assert(!_.isUndefined(this._zeroEx), 'ZeroEx must be instantiated.');
if (_.isEmpty(ownerAddress)) {
if (_.isUndefined(ownerAddressIfExists)) {
const zero = new BigNumber(0);
return [zero, zero];
}
let balance = new BigNumber(0);
let allowance = new BigNumber(0);
if (this._doesUserAddressExist()) {
balance = await this._zeroEx.token.getBalanceAsync(tokenAddress, ownerAddress);
allowance = await this._zeroEx.token.getProxyAllowanceAsync(tokenAddress, ownerAddress);
balance = await this._zeroEx.token.getBalanceAsync(tokenAddress, ownerAddressIfExists);
allowance = await this._zeroEx.token.getProxyAllowanceAsync(tokenAddress, ownerAddressIfExists);
}
return [balance, allowance];
}

View File

@@ -1,6 +1,7 @@
import { ZeroEx } from '0x.js';
import { colors } from '@0xproject/react-shared';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
import Dialog from 'material-ui/Dialog';
import FlatButton from 'material-ui/FlatButton';
import * as React from 'react';
@@ -185,8 +186,9 @@ export class EthWethConversionDialog extends React.Component<
this.props.onCancelled();
}
private async _fetchEthTokenBalanceAsync() {
const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress;
const [balance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
this.props.userAddress,
userAddressIfExists,
this.props.token.address,
);
if (!this._isUnmounted) {

View File

@@ -351,8 +351,9 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
[outdatedWETHAddress]: false,
},
});
const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress;
const [balance, allowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
this.props.userAddress,
userAddressIfExists,
outdatedWETHAddress,
);
this.setState({
@@ -372,8 +373,9 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
private async _fetchWETHStateAsync() {
const tokens = _.values(this.props.tokenByAddress);
const wethToken = _.find(tokens, token => token.symbol === 'WETH');
const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress;
const [wethBalance, wethAllowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
this.props.userAddress,
userAddressIfExists,
wethToken.address,
);
@@ -382,7 +384,7 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
const outdatedWETHStateByAddress: OutdatedWETHStateByAddress = {};
for (const address of outdatedWETHAddresses) {
const [balance, allowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
this.props.userAddress,
userAddressIfExists,
address,
);
outdatedWETHStateByAddress[address] = {
@@ -423,8 +425,9 @@ export class EthWrappers extends React.Component<EthWrappersProps, EthWrappersSt
}
private async _refetchEthTokenStateAsync() {
const etherToken = this._getEthToken();
const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress;
const [balance, allowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
this.props.userAddress,
userAddressIfExists,
etherToken.address,
);
this.setState({

View File

@@ -237,8 +237,9 @@ export class GenerateOrderForm extends React.Component<GenerateOrderFormProps, G
// Check if all required inputs were supplied
const debitToken = this.props.sideToAssetToken[Side.Deposit];
const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress;
const [debitBalance, debitAllowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
this.props.userAddress,
userAddressIfExists,
debitToken.address,
);
const receiveAmount = this.props.sideToAssetToken[Side.Receive].amount;

View File

@@ -109,8 +109,9 @@ export class TokenAmountInput extends React.Component<TokenAmountInputProps, Tok
this.setState({
isBalanceAndAllowanceLoaded: false,
});
const userAddressIfExists = _.isEmpty(userAddress) ? undefined : userAddress;
const [balance, allowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
userAddress,
userAddressIfExists,
tokenAddress,
);
if (!this._isUnmounted) {

View File

@@ -687,9 +687,10 @@ export class TokenBalances extends React.Component<TokenBalancesProps, TokenBala
}
private async _fetchBalancesAndAllowancesAsync(tokenAddresses: string[]) {
const trackedTokenStateByAddress = this.state.trackedTokenStateByAddress;
const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress;
for (const tokenAddress of tokenAddresses) {
const [balance, allowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
this.props.userAddress,
userAddressIfExists,
tokenAddress,
);
trackedTokenStateByAddress[tokenAddress] = {
@@ -716,8 +717,9 @@ export class TokenBalances extends React.Component<TokenBalancesProps, TokenBala
return trackedTokenStateByAddress;
}
private async _refetchTokenStateAsync(tokenAddress: string) {
const userAddressIfExists = _.isEmpty(this.props.userAddress) ? undefined : this.props.userAddress;
const [balance, allowance] = await this.props.blockchain.getTokenBalanceAndAllowanceAsync(
this.props.userAddress,
userAddressIfExists,
tokenAddress,
);
this.setState({