Improve error handling for unknown network ids in contract-wrappers
This commit is contained in:
parent
974ec23ecd
commit
c83dec22c9
@ -27,6 +27,8 @@ import { contractWrappersPublicNetworkConfigSchema } from './schemas/contract_wr
|
||||
import { ContractWrappersConfig } from './types';
|
||||
import { assert } from './utils/assert';
|
||||
import { constants } from './utils/constants';
|
||||
import { _getDefaultContractAddresses } from './utils/contract_addresses';
|
||||
|
||||
/**
|
||||
* 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
|
||||
: config.blockPollingIntervalMs;
|
||||
const contractAddresses = _.isUndefined(config.contractAddresses)
|
||||
? getContractAddressesForNetwork(config.networkId)
|
||||
? _getDefaultContractAddresses(config.networkId)
|
||||
: config.contractAddresses;
|
||||
this.erc20Proxy = new ERC20ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc20Proxy);
|
||||
this.erc721Proxy = new ERC721ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc721Proxy);
|
||||
|
@ -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 { marshaller, Web3Wrapper } from '@0xproject/web3-wrapper';
|
||||
import {
|
||||
@ -112,10 +112,6 @@ export abstract class ContractWrapper {
|
||||
const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log);
|
||||
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 {
|
||||
const log: LogEntry = marshaller.unmarshalLog(rawLog);
|
||||
_.forEach(this._filters, (filter: FilterObject, filterToken: string) => {
|
||||
|
@ -6,6 +6,7 @@ import { ContractAbi } from 'ethereum-types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { assert } from '../utils/assert';
|
||||
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
|
||||
|
||||
import { ContractWrapper } from './contract_wrapper';
|
||||
|
||||
@ -25,7 +26,7 @@ export class ERC20ProxyWrapper extends ContractWrapper {
|
||||
*/
|
||||
constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) {
|
||||
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
|
||||
|
@ -6,6 +6,7 @@ import { ContractAbi } from 'ethereum-types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { assert } from '../utils/assert';
|
||||
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
|
||||
|
||||
import { ContractWrapper } from './contract_wrapper';
|
||||
|
||||
@ -25,7 +26,7 @@ export class ERC721ProxyWrapper extends ContractWrapper {
|
||||
*/
|
||||
constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) {
|
||||
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
|
||||
|
@ -29,6 +29,7 @@ import {
|
||||
ValidateOrderFillableOpts,
|
||||
} from '../types';
|
||||
import { assert } from '../utils/assert';
|
||||
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
|
||||
import { decorators } from '../utils/decorators';
|
||||
import { TransactionEncoder } from '../utils/transaction_encoder';
|
||||
|
||||
@ -72,9 +73,9 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
super(web3Wrapper, networkId, blockPollingIntervalMs);
|
||||
this._erc20TokenWrapper = erc20TokenWrapper;
|
||||
this._erc721TokenWrapper = erc721TokenWrapper;
|
||||
this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address;
|
||||
this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).exchange : address;
|
||||
this.zrxTokenAddress = _.isUndefined(zrxTokenAddress)
|
||||
? this._getDefaultContractAddresses().zrxToken
|
||||
? _getDefaultContractAddresses(networkId).zrxToken
|
||||
: zrxTokenAddress;
|
||||
}
|
||||
/**
|
||||
|
@ -7,12 +7,12 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper';
|
||||
import { ContractAbi } from 'ethereum-types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { orderTxOptsSchema } from '../schemas/order_tx_opts_schema';
|
||||
import { txOptsSchema } from '../schemas/tx_opts_schema';
|
||||
import { OrderTransactionOpts } from '../types';
|
||||
import { assert } from '../utils/assert';
|
||||
import { calldataOptimizationUtils } from '../utils/calldata_optimization_utils';
|
||||
import { constants } from '../utils/constants';
|
||||
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
|
||||
import { decorators } from '../utils/decorators';
|
||||
import { utils } from '../utils/utils';
|
||||
|
||||
@ -49,12 +49,12 @@ export class ForwarderWrapper extends ContractWrapper {
|
||||
etherTokenAddress?: string,
|
||||
) {
|
||||
super(web3Wrapper, networkId);
|
||||
this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address;
|
||||
this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).exchange : address;
|
||||
this.zrxTokenAddress = _.isUndefined(zrxTokenAddress)
|
||||
? this._getDefaultContractAddresses().zrxToken
|
||||
? _getDefaultContractAddresses(networkId).zrxToken
|
||||
: zrxTokenAddress;
|
||||
this.etherTokenAddress = _.isUndefined(etherTokenAddress)
|
||||
? this._getDefaultContractAddresses().etherToken
|
||||
? _getDefaultContractAddresses(networkId).etherToken
|
||||
: etherTokenAddress;
|
||||
}
|
||||
/**
|
||||
|
@ -9,6 +9,7 @@ import * as _ from 'lodash';
|
||||
|
||||
import { BalanceAndAllowance, OrderAndTraderInfo, TraderInfo } from '../types';
|
||||
import { assert } from '../utils/assert';
|
||||
import { _getDefaultContractAddresses } from '../utils/contract_addresses';
|
||||
|
||||
import { ContractWrapper } from './contract_wrapper';
|
||||
|
||||
@ -28,7 +29,7 @@ export class OrderValidatorWrapper extends ContractWrapper {
|
||||
*/
|
||||
constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) {
|
||||
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
|
||||
|
13
packages/contract-wrappers/src/utils/contract_addresses.ts
Normal file
13
packages/contract-wrappers/src/utils/contract_addresses.ts
Normal 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);
|
||||
}
|
@ -28,13 +28,11 @@ describe('ExchangeWrapper', () => {
|
||||
let exchangeContractAddress: string;
|
||||
let makerTokenAddress: string;
|
||||
let takerTokenAddress: string;
|
||||
let coinbase: string;
|
||||
let makerAddress: string;
|
||||
let anotherMakerAddress: string;
|
||||
let takerAddress: string;
|
||||
let makerAssetData: string;
|
||||
let takerAssetData: string;
|
||||
let feeRecipient: string;
|
||||
let txHash: string;
|
||||
const fillableAmount = new BigNumber(5);
|
||||
const takerTokenFillAmount = new BigNumber(5);
|
||||
@ -61,7 +59,7 @@ describe('ExchangeWrapper', () => {
|
||||
contractWrappers.erc20Proxy.address,
|
||||
contractWrappers.erc721Proxy.address,
|
||||
);
|
||||
[coinbase, makerAddress, takerAddress, feeRecipient, anotherMakerAddress] = userAddresses;
|
||||
[, makerAddress, takerAddress, , anotherMakerAddress] = userAddresses;
|
||||
[makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses();
|
||||
[makerAssetData, takerAssetData] = [
|
||||
assetDataUtils.encodeERC20AssetData(makerTokenAddress),
|
||||
|
Loading…
x
Reference in New Issue
Block a user