Improve error handling for unknown network ids in contract-wrappers

This commit is contained in:
Alex Browne 2018-10-10 16:52:35 -07:00
parent 974ec23ecd
commit c83dec22c9
9 changed files with 31 additions and 18 deletions

View File

@ -27,6 +27,8 @@ import { contractWrappersPublicNetworkConfigSchema } from './schemas/contract_wr
import { ContractWrappersConfig } from './types'; import { ContractWrappersConfig } from './types';
import { assert } from './utils/assert'; import { assert } from './utils/assert';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { _getDefaultContractAddresses } from './utils/contract_addresses';
/** /**
* The ContractWrappers class contains smart contract wrappers helpful when building on 0x protocol. * The ContractWrappers class contains smart contract wrappers helpful when building on 0x protocol.
*/ */
@ -102,7 +104,7 @@ export class ContractWrappers {
? constants.DEFAULT_BLOCK_POLLING_INTERVAL ? constants.DEFAULT_BLOCK_POLLING_INTERVAL
: config.blockPollingIntervalMs; : config.blockPollingIntervalMs;
const contractAddresses = _.isUndefined(config.contractAddresses) const contractAddresses = _.isUndefined(config.contractAddresses)
? getContractAddressesForNetwork(config.networkId) ? _getDefaultContractAddresses(config.networkId)
: config.contractAddresses; : config.contractAddresses;
this.erc20Proxy = new ERC20ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc20Proxy); this.erc20Proxy = new ERC20ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc20Proxy);
this.erc721Proxy = new ERC721ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc721Proxy); this.erc721Proxy = new ERC721ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc721Proxy);

View File

@ -1,4 +1,4 @@
import { ContractAddresses, getContractAddressesForNetwork } from '@0xproject/contract-addresses'; import { ContractAddresses, getContractAddressesForNetwork, NetworkId } from '@0xproject/contract-addresses';
import { AbiDecoder, intervalUtils, logUtils } from '@0xproject/utils'; import { AbiDecoder, intervalUtils, logUtils } from '@0xproject/utils';
import { marshaller, Web3Wrapper } from '@0xproject/web3-wrapper'; import { marshaller, Web3Wrapper } from '@0xproject/web3-wrapper';
import { import {
@ -112,10 +112,6 @@ export abstract class ContractWrapper {
const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log); const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log);
return logWithDecodedArgs; return logWithDecodedArgs;
} }
protected _getDefaultContractAddresses(): ContractAddresses {
// TODO(albrow): Figure out better error handling here.
return getContractAddressesForNetwork(this._networkId);
}
private _onLogStateChanged<ArgsType extends ContractEventArgs>(isRemoved: boolean, rawLog: RawLogEntry): void { private _onLogStateChanged<ArgsType extends ContractEventArgs>(isRemoved: boolean, rawLog: RawLogEntry): void {
const log: LogEntry = marshaller.unmarshalLog(rawLog); const log: LogEntry = marshaller.unmarshalLog(rawLog);
_.forEach(this._filters, (filter: FilterObject, filterToken: string) => { _.forEach(this._filters, (filter: FilterObject, filterToken: string) => {

View File

@ -6,6 +6,7 @@ import { ContractAbi } from 'ethereum-types';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { assert } from '../utils/assert'; import { assert } from '../utils/assert';
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
import { ContractWrapper } from './contract_wrapper'; import { ContractWrapper } from './contract_wrapper';
@ -25,7 +26,7 @@ export class ERC20ProxyWrapper extends ContractWrapper {
*/ */
constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) {
super(web3Wrapper, networkId); super(web3Wrapper, networkId);
this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().erc20Proxy : address; this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).erc20Proxy : address;
} }
/** /**
* Get the 4 bytes ID of this asset proxy * Get the 4 bytes ID of this asset proxy

View File

@ -6,6 +6,7 @@ import { ContractAbi } from 'ethereum-types';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { assert } from '../utils/assert'; import { assert } from '../utils/assert';
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
import { ContractWrapper } from './contract_wrapper'; import { ContractWrapper } from './contract_wrapper';
@ -25,7 +26,7 @@ export class ERC721ProxyWrapper extends ContractWrapper {
*/ */
constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) {
super(web3Wrapper, networkId); super(web3Wrapper, networkId);
this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().erc721Proxy : address; this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).erc721Proxy : address;
} }
/** /**
* Get the 4 bytes ID of this asset proxy * Get the 4 bytes ID of this asset proxy

View File

@ -29,6 +29,7 @@ import {
ValidateOrderFillableOpts, ValidateOrderFillableOpts,
} from '../types'; } from '../types';
import { assert } from '../utils/assert'; import { assert } from '../utils/assert';
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
import { decorators } from '../utils/decorators'; import { decorators } from '../utils/decorators';
import { TransactionEncoder } from '../utils/transaction_encoder'; import { TransactionEncoder } from '../utils/transaction_encoder';
@ -72,9 +73,9 @@ export class ExchangeWrapper extends ContractWrapper {
super(web3Wrapper, networkId, blockPollingIntervalMs); super(web3Wrapper, networkId, blockPollingIntervalMs);
this._erc20TokenWrapper = erc20TokenWrapper; this._erc20TokenWrapper = erc20TokenWrapper;
this._erc721TokenWrapper = erc721TokenWrapper; this._erc721TokenWrapper = erc721TokenWrapper;
this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address; this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).exchange : address;
this.zrxTokenAddress = _.isUndefined(zrxTokenAddress) this.zrxTokenAddress = _.isUndefined(zrxTokenAddress)
? this._getDefaultContractAddresses().zrxToken ? _getDefaultContractAddresses(networkId).zrxToken
: zrxTokenAddress; : zrxTokenAddress;
} }
/** /**

View File

@ -7,12 +7,12 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper';
import { ContractAbi } from 'ethereum-types'; import { ContractAbi } from 'ethereum-types';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { orderTxOptsSchema } from '../schemas/order_tx_opts_schema';
import { txOptsSchema } from '../schemas/tx_opts_schema'; import { txOptsSchema } from '../schemas/tx_opts_schema';
import { OrderTransactionOpts } from '../types'; import { OrderTransactionOpts } from '../types';
import { assert } from '../utils/assert'; import { assert } from '../utils/assert';
import { calldataOptimizationUtils } from '../utils/calldata_optimization_utils'; import { calldataOptimizationUtils } from '../utils/calldata_optimization_utils';
import { constants } from '../utils/constants'; import { constants } from '../utils/constants';
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
import { decorators } from '../utils/decorators'; import { decorators } from '../utils/decorators';
import { utils } from '../utils/utils'; import { utils } from '../utils/utils';
@ -49,12 +49,12 @@ export class ForwarderWrapper extends ContractWrapper {
etherTokenAddress?: string, etherTokenAddress?: string,
) { ) {
super(web3Wrapper, networkId); super(web3Wrapper, networkId);
this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address; this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).exchange : address;
this.zrxTokenAddress = _.isUndefined(zrxTokenAddress) this.zrxTokenAddress = _.isUndefined(zrxTokenAddress)
? this._getDefaultContractAddresses().zrxToken ? _getDefaultContractAddresses(networkId).zrxToken
: zrxTokenAddress; : zrxTokenAddress;
this.etherTokenAddress = _.isUndefined(etherTokenAddress) this.etherTokenAddress = _.isUndefined(etherTokenAddress)
? this._getDefaultContractAddresses().etherToken ? _getDefaultContractAddresses(networkId).etherToken
: etherTokenAddress; : etherTokenAddress;
} }
/** /**

View File

@ -9,6 +9,7 @@ import * as _ from 'lodash';
import { BalanceAndAllowance, OrderAndTraderInfo, TraderInfo } from '../types'; import { BalanceAndAllowance, OrderAndTraderInfo, TraderInfo } from '../types';
import { assert } from '../utils/assert'; import { assert } from '../utils/assert';
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
import { ContractWrapper } from './contract_wrapper'; import { ContractWrapper } from './contract_wrapper';
@ -28,7 +29,7 @@ export class OrderValidatorWrapper extends ContractWrapper {
*/ */
constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) {
super(web3Wrapper, networkId); super(web3Wrapper, networkId);
this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address; this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).exchange : address;
} }
/** /**
* Get an object conforming to OrderAndTraderInfo containing on-chain information of the provided order and address * Get an object conforming to OrderAndTraderInfo containing on-chain information of the provided order and address

View File

@ -0,0 +1,13 @@
import { ContractAddresses, getContractAddressesForNetwork, NetworkId } from '@0xproject/contract-addresses';
import * as _ from 'lodash';
// Returns the default contract addresses for the given networkId or throws with
// a context-specific error message if the networkId is not recognized.
export function _getDefaultContractAddresses(networkId: number): ContractAddresses {
if (!(networkId in NetworkId)) {
throw new Error(
`No default contract addresses found for the given network id (${networkId}). If you want to use ContractWrappers on this network, you must manually pass in the contract address(es) to the constructor.`,
);
}
return getContractAddressesForNetwork(networkId);
}

View File

@ -28,13 +28,11 @@ describe('ExchangeWrapper', () => {
let exchangeContractAddress: string; let exchangeContractAddress: string;
let makerTokenAddress: string; let makerTokenAddress: string;
let takerTokenAddress: string; let takerTokenAddress: string;
let coinbase: string;
let makerAddress: string; let makerAddress: string;
let anotherMakerAddress: string; let anotherMakerAddress: string;
let takerAddress: string; let takerAddress: string;
let makerAssetData: string; let makerAssetData: string;
let takerAssetData: string; let takerAssetData: string;
let feeRecipient: string;
let txHash: string; let txHash: string;
const fillableAmount = new BigNumber(5); const fillableAmount = new BigNumber(5);
const takerTokenFillAmount = new BigNumber(5); const takerTokenFillAmount = new BigNumber(5);
@ -61,7 +59,7 @@ describe('ExchangeWrapper', () => {
contractWrappers.erc20Proxy.address, contractWrappers.erc20Proxy.address,
contractWrappers.erc721Proxy.address, contractWrappers.erc721Proxy.address,
); );
[coinbase, makerAddress, takerAddress, feeRecipient, anotherMakerAddress] = userAddresses; [, makerAddress, takerAddress, , anotherMakerAddress] = userAddresses;
[makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses(); [makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses();
[makerAssetData, takerAssetData] = [ [makerAssetData, takerAssetData] = [
assetDataUtils.encodeERC20AssetData(makerTokenAddress), assetDataUtils.encodeERC20AssetData(makerTokenAddress),