diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-22 06:50:14 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-24 05:13:36 +0800 |
commit | 63dc606a9c08dad5e0aae09b86178fcd3e78e867 (patch) | |
tree | 1b82eb0d3da14ab22d614d69338623086214c3db /packages/0x.js/src | |
parent | 131236305bff6cdf85a7edb7e3c61c50ffbb1956 (diff) | |
download | dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.gz dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.bz2 dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.lz dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.xz dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.zst dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.zip |
Make getZRXTokenAddress non async
Diffstat (limited to 'packages/0x.js/src')
-rw-r--r-- | packages/0x.js/src/artifacts.ts | 2 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/exchange_wrapper.ts | 32 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/token_wrapper.ts | 4 | ||||
-rw-r--r-- | packages/0x.js/src/order_watcher/order_state_watcher.ts | 4 | ||||
-rw-r--r-- | packages/0x.js/src/types.ts | 3 | ||||
-rw-r--r-- | packages/0x.js/src/utils/order_state_utils.ts | 2 |
6 files changed, 29 insertions, 18 deletions
diff --git a/packages/0x.js/src/artifacts.ts b/packages/0x.js/src/artifacts.ts index 447f9880a..93b11a959 100644 --- a/packages/0x.js/src/artifacts.ts +++ b/packages/0x.js/src/artifacts.ts @@ -1,4 +1,5 @@ import {Artifact} from './types'; +import * as ZRXArtifact from './artifacts/ZRX.json'; import * as TokenArtifact from './artifacts/Token.json'; import * as ExchangeArtifact from './artifacts/Exchange.json'; import * as EtherTokenArtifact from './artifacts/EtherToken.json'; @@ -6,6 +7,7 @@ import * as TokenRegistryArtifact from './artifacts/TokenRegistry.json'; import * as TokenTransferProxyArtifact from './artifacts/TokenTransferProxy.json'; export const artifacts = { + ZRXArtifact: ZRXArtifact as any as Artifact, TokenArtifact: TokenArtifact as any as Artifact, ExchangeArtifact: ExchangeArtifact as any as Artifact, EtherTokenArtifact: EtherTokenArtifact as any as Artifact, diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index 3e631b73e..96b642a90 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -63,6 +63,7 @@ export class ExchangeWrapper extends ContractWrapper { [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FillBalanceAllowanceError, }; private _contractAddressIfExists?: string; + private _zrxContractAddressIfExists?: string; private static _getOrderAddressesAndValues(order: Order): [OrderAddresses, OrderValues] { const orderAddresses: OrderAddresses = [ order.maker, @@ -177,7 +178,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync( exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); @@ -249,7 +250,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); for (const signedOrder of signedOrders) { await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync( @@ -339,7 +340,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); for (const orderFillRequest of orderFillRequests) { await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync( @@ -419,7 +420,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync( exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); @@ -483,7 +484,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); for (const orderFillRequest of orderFillRequests) { await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync( @@ -719,7 +720,7 @@ export class ExchangeWrapper extends ContractWrapper { signedOrder: SignedOrder, opts?: ValidateOrderFillableOpts, ): Promise<void> { assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const expectedFillTakerTokenAmount = !_.isUndefined(opts) ? opts.expectedFillTakerTokenAmount : undefined; const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateOrderFillableOrThrowAsync( @@ -740,7 +741,7 @@ export class ExchangeWrapper extends ContractWrapper { assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); assert.isValidBaseUnitAmount('fillTakerTokenAmount', fillTakerTokenAmount); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync( exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); @@ -774,7 +775,7 @@ export class ExchangeWrapper extends ContractWrapper { assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); assert.isValidBaseUnitAmount('fillTakerTokenAmount', fillTakerTokenAmount); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync( exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); @@ -819,10 +820,17 @@ export class ExchangeWrapper extends ContractWrapper { * Returns the ZRX token address used by the exchange contract. * @return Address of ZRX token */ - public async getZRXTokenAddressAsync(): Promise<string> { - const exchangeInstance = await this._getExchangeContractAsync(); - const ZRXtokenAddress = await exchangeInstance.ZRX_TOKEN_CONTRACT.callAsync(); - return ZRXtokenAddress; + public getZRXTokenAddress(): string { + const networkId = this._web3Wrapper.getNetworkId(); + if (_.isUndefined(this._zrxContractAddressIfExists)) { + const zrxTokenAddress = artifacts.ZRXArtifact.networks[networkId].address; + if (_.isUndefined(zrxTokenAddress)) { + throw new Error(ZeroExError.ZRXContractDoesNotExist); + } + return zrxTokenAddress; + } else { + return this._zrxContractAddressIfExists; + } } private async _invalidateContractInstancesAsync(): Promise<void> { this.unsubscribeAll(); diff --git a/packages/0x.js/src/contract_wrappers/token_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_wrapper.ts index 4b89d3cfc..5fd3a2a5d 100644 --- a/packages/0x.js/src/contract_wrappers/token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_wrapper.ts @@ -76,8 +76,8 @@ export class TokenWrapper extends ContractWrapper { // Hack: for some reason default estimated gas amount causes `base fee exceeds gas limit` exception // on testrpc. Probably related to https://github.com/ethereumjs/testrpc/issues/294 // TODO: Debug issue in testrpc and submit a PR, then remove this hack - const networkIdIfExists = await this._web3Wrapper.getNetworkIdIfExistsAsync(); - const gas = networkIdIfExists === constants.TESTRPC_NETWORK_ID ? ALLOWANCE_TO_ZERO_GAS_AMOUNT : undefined; + const networkId = this._web3Wrapper.getNetworkId(); + const gas = networkId === constants.TESTRPC_NETWORK_ID ? ALLOWANCE_TO_ZERO_GAS_AMOUNT : undefined; const txHash = await tokenContract.approve.sendTransactionAsync(spenderAddress, amountInBaseUnits, { from: ownerAddress, gas, diff --git a/packages/0x.js/src/order_watcher/order_state_watcher.ts b/packages/0x.js/src/order_watcher/order_state_watcher.ts index 44a41669d..870f42650 100644 --- a/packages/0x.js/src/order_watcher/order_state_watcher.ts +++ b/packages/0x.js/src/order_watcher/order_state_watcher.ts @@ -116,7 +116,7 @@ export class OrderStateWatcher { delete this._orderByOrderHash[orderHash]; delete this._orderStateByOrderHashCache[orderHash]; const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper; - const zrxTokenAddress = await exchange.getZRXTokenAddressAsync(); + const zrxTokenAddress = exchange.getZRXTokenAddress(); this.removeFromDependentOrderHashes(signedOrder.maker, zrxTokenAddress, orderHash); this.removeFromDependentOrderHashes(signedOrder.maker, signedOrder.makerTokenAddress, orderHash); this._expirationWatcher.removeOrder(orderHash); @@ -263,7 +263,7 @@ export class OrderStateWatcher { } this._dependentOrderHashes[signedOrder.maker][signedOrder.makerTokenAddress].add(orderHash); const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper; - const zrxTokenAddress = await exchange.getZRXTokenAddressAsync(); + const zrxTokenAddress = exchange.getZRXTokenAddress(); if (_.isUndefined(this._dependentOrderHashes[signedOrder.maker][zrxTokenAddress])) { this._dependentOrderHashes[signedOrder.maker][zrxTokenAddress] = new Set(); } diff --git a/packages/0x.js/src/types.ts b/packages/0x.js/src/types.ts index d34bb02b6..313540edb 100644 --- a/packages/0x.js/src/types.ts +++ b/packages/0x.js/src/types.ts @@ -4,6 +4,7 @@ import BigNumber from 'bignumber.js'; export enum ZeroExError { ContractDoesNotExist = 'CONTRACT_DOES_NOT_EXIST', ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', + ZRXContractDoesNotExist = 'ZRX_CONTRACT_DOES_NOT_EXIST', UnhandledError = 'UNHANDLED_ERROR', UserHasNoAssociatedAddress = 'USER_HAS_NO_ASSOCIATED_ADDRESSES', InvalidSignature = 'INVALID_SIGNATURE', @@ -441,7 +442,7 @@ export interface TransactionReceiptWithDecodedLogs extends TransactionReceipt { } export interface Artifact { - abi: any; + abi: Web3.ContractAbi; networks: {[networkId: number]: { address: string; }}; diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts index 1d8f02a18..891849424 100644 --- a/packages/0x.js/src/utils/order_state_utils.ts +++ b/packages/0x.js/src/utils/order_state_utils.ts @@ -55,7 +55,7 @@ export class OrderStateUtils { // because JS doesn't support async constructors. // Moreover - it's cached under the hood so it's equivalent to an async constructor. const exchange = (this.orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper; - const zrxTokenAddress = await exchange.getZRXTokenAddressAsync(); + const zrxTokenAddress = exchange.getZRXTokenAddress(); const orderHash = ZeroEx.getOrderHashHex(signedOrder); const makerBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync( signedOrder.makerTokenAddress, signedOrder.maker, |