diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | package.json | 4 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 6 | ||||
-rw-r--r-- | src/web3_wrapper.ts | 9 | ||||
-rw-r--r-- | test/exchange_wrapper_test.ts | 12 | ||||
-rw-r--r-- | test/utils/constants.ts | 1 | ||||
-rw-r--r-- | test/utils/fill_scenarios.ts | 60 | ||||
-rw-r--r-- | test/web3_wrapper_test.ts | 29 |
8 files changed, 82 insertions, 42 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fdabfd92..413203e4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ v0.9.0 - TBD ------------------------ * Move `zeroEx.exchange.getAvailableContractAddressesAsync` to `zeroEx.getAvailableExchangeContractAddressesAsync` (#94) * Move `zeroEx.exchange.getProxyAuthorizedContractAddressesAsync` to `zeroEx.getProxyAuthorizedExchangeContractAddressesAsync` (#94) + * Cache `net_version` requests and invalidate the cache on calls to `setProvider` (#95) + * Rename `zeroEx.exchange.batchCancelOrderAsync` to `zeroEx.exchange.batchCancelOrdersAsync` + * Rename `zeroEx.exchange.batchFillOrderAsync` to `zeroEx.exchange.batchFillOrdersAsync` v0.8.0 - _Jul. 4, 2017_ ------------------------ diff --git a/package.json b/package.json index e8106b748..ecb5e28f0 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "pretest:umd": "run-s clean build:*:dev", "substitute_umd_bundle": "npm run remove_src_files_not_used_by_tests; shx mv _bundles/* lib/src", "remove_src_files_not_used_by_tests": "find ./lib/src \\( -path ./lib/src/utils -o -path ./lib/src/schemas -o -path \"./lib/src/types.*\" \\) -prune -o -type f -print | xargs rm", - "run_mocha": "mocha lib/test/**/*_test.js --timeout 3000" + "run_mocha": "mocha lib/test/**/*_test.js --timeout 3000 --bail" }, "config": { "artifacts": "Proxy Exchange TokenRegistry Token Mintable EtherToken", @@ -85,7 +85,7 @@ "typedoc": "^0.7.1", "typescript": "^2.3.3", "web3-provider-engine": "^13.0.1", - "web3-typescript-typings": "^0.0.10", + "web3-typescript-typings": "^0.0.11", "webpack": "^3.1.0" }, "dependencies": { diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 2353c826a..fa4b5904b 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -297,8 +297,8 @@ export class ExchangeWrapper extends ContractWrapper { * Must be available via the supplied Web3.Provider passed to 0x.js. */ @decorators.contractCallErrorHandler - public async batchFillOrderAsync(orderFillRequests: OrderFillRequest[], - shouldCheckTransfer: boolean, takerAddress: string): Promise<void> { + public async batchFillOrdersAsync(orderFillRequests: OrderFillRequest[], + shouldCheckTransfer: boolean, takerAddress: string): Promise<void> { assert.doesConformToSchema('orderFillRequests', orderFillRequests, orderFillRequestsSchema); const exchangeContractAddresses = _.map( orderFillRequests, @@ -520,7 +520,7 @@ export class ExchangeWrapper extends ContractWrapper { * interface. */ @decorators.contractCallErrorHandler - public async batchCancelOrderAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise<void> { + public async batchCancelOrdersAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise<void> { assert.doesConformToSchema('orderCancellationRequests', orderCancellationRequests, orderCancellationRequestsSchema); const exchangeContractAddresses = _.map( diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts index 630f0bef3..61bac45c9 100644 --- a/src/web3_wrapper.ts +++ b/src/web3_wrapper.ts @@ -5,11 +5,13 @@ import promisify = require('es6-promisify'); export class Web3Wrapper { private web3: Web3; + private networkIdIfExists?: number; constructor(provider: Web3.Provider) { this.web3 = new Web3(); this.web3.setProvider(provider); } public setProvider(provider: Web3.Provider) { + delete this.networkIdIfExists; this.web3.setProvider(provider); } public isAddress(address: string): boolean { @@ -27,9 +29,14 @@ export class Web3Wrapper { return this.web3.currentProvider; } public async getNetworkIdIfExistsAsync(): Promise<number|undefined> { + if (!_.isUndefined(this.networkIdIfExists)) { + return this.networkIdIfExists; + } + try { const networkId = await this.getNetworkAsync(); - return Number(networkId); + this.networkIdIfExists = Number(networkId); + return this.networkIdIfExists; } catch (err) { return undefined; } diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 51ebd485d..53032efd6 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -386,7 +386,7 @@ describe('ExchangeWrapper', () => { }); }); }); - describe('#batchFillOrderAsync', () => { + describe('#batchFillOrdersAsync', () => { let signedOrder: SignedOrder; let signedOrderHashHex: string; let anotherSignedOrder: SignedOrder; @@ -414,10 +414,10 @@ describe('ExchangeWrapper', () => { }); describe('successful batch fills', () => { it('should no-op for an empty batch', async () => { - await zeroEx.exchange.batchFillOrderAsync([], shouldCheckTransfer, takerAddress); + await zeroEx.exchange.batchFillOrdersAsync([], shouldCheckTransfer, takerAddress); }); it('should successfully fill multiple orders', async () => { - await zeroEx.exchange.batchFillOrderAsync(orderFillBatch, shouldCheckTransfer, takerAddress); + await zeroEx.exchange.batchFillOrdersAsync(orderFillBatch, shouldCheckTransfer, takerAddress); const filledAmount = await zeroEx.exchange.getFilledTakerAmountAsync( signedOrderHashHex, exchangeContractAddress, ); @@ -531,7 +531,7 @@ describe('ExchangeWrapper', () => { }); }); }); - describe('#batchCancelOrderAsync', () => { + describe('#batchCancelOrdersAsync', () => { let anotherSignedOrder: SignedOrder; let anotherOrderHashHex: string; let cancelBatch: OrderCancellationRequest[]; @@ -556,7 +556,7 @@ describe('ExchangeWrapper', () => { const signedOrderWithDifferentMaker = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, takerAddress, takerAddress, fillableAmount, ); - return expect(zeroEx.exchange.batchCancelOrderAsync([ + return expect(zeroEx.exchange.batchCancelOrdersAsync([ cancelBatch[0], { order: signedOrderWithDifferentMaker, @@ -567,7 +567,7 @@ describe('ExchangeWrapper', () => { }); describe('successful batch cancels', () => { it('should cancel a batch of orders', async () => { - await zeroEx.exchange.batchCancelOrderAsync(cancelBatch); + await zeroEx.exchange.batchCancelOrdersAsync(cancelBatch); const cancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync( orderHashHex, exchangeContractAddress, ); diff --git a/test/utils/constants.ts b/test/utils/constants.ts index 9b150b5c1..b677d7361 100644 --- a/test/utils/constants.ts +++ b/test/utils/constants.ts @@ -2,5 +2,6 @@ export const constants = { NULL_ADDRESS: '0x0000000000000000000000000000000000000000', RPC_HOST: 'localhost', RPC_PORT: 8545, + TESTRPC_NETWORK_ID: 50, KOVAN_RPC_URL: 'https://kovan.0xproject.com', }; diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index 65a912955..bebf82fd8 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -71,37 +71,15 @@ export class FillScenarios { makerAddress: string, takerAddress: string, makerFillableAmount: BigNumber.BigNumber, takerFillableAmount: BigNumber.BigNumber, feeRecepient: string, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise<SignedOrder> { - await this.zeroEx.token.transferAsync(makerTokenAddress, this.coinbase, makerAddress, makerFillableAmount); - const oldMakerAllowance = await this.zeroEx.token.getProxyAllowanceAsync(makerTokenAddress, makerAddress); - const newMakerAllowance = oldMakerAllowance.plus(makerFillableAmount); - await this.zeroEx.token.setProxyAllowanceAsync( - makerTokenAddress, makerAddress, newMakerAllowance, - ); - await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinbase, takerAddress, takerFillableAmount); - const oldTakerAllowance = await this.zeroEx.token.getProxyAllowanceAsync(takerTokenAddress, takerAddress); - const newTakerAllowance = oldTakerAllowance.plus(takerFillableAmount); - await this.zeroEx.token.setProxyAllowanceAsync( - takerTokenAddress, takerAddress, newTakerAllowance, - ); - if (!makerFee.isZero()) { - await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinbase, makerAddress, makerFee); - const oldMakerFeeAllowance = - await this.zeroEx.token.getProxyAllowanceAsync(this.zrxTokenAddress, makerAddress); - const newMakerFeeAllowance = oldMakerFeeAllowance.plus(makerFee); - await this.zeroEx.token.setProxyAllowanceAsync( - this.zrxTokenAddress, makerAddress, newMakerFeeAllowance, - ); - } - if (!takerFee.isZero()) { - await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinbase, takerAddress, takerFee); - const oldTakerFeeAllowance = - await this.zeroEx.token.getProxyAllowanceAsync(this.zrxTokenAddress, takerAddress); - const newTakerFeeAllowance = oldTakerFeeAllowance.plus(takerFee); - await this.zeroEx.token.setProxyAllowanceAsync( - this.zrxTokenAddress, takerAddress, newTakerFeeAllowance, - ); - } + await Promise.all([ + this.increaseBalanceAndAllowanceAsync(makerTokenAddress, makerAddress, makerFillableAmount), + this.increaseBalanceAndAllowanceAsync(takerTokenAddress, takerAddress, takerFillableAmount), + ]); + await Promise.all([ + this.increaseBalanceAndAllowanceAsync(this.zrxTokenAddress, makerAddress, makerFee), + this.increaseBalanceAndAllowanceAsync(this.zrxTokenAddress, takerAddress, takerFee), + ]); const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, takerAddress, makerFee, takerFee, @@ -109,4 +87,26 @@ export class FillScenarios { this.exchangeContractAddress, feeRecepient, expirationUnixTimestampSec); return signedOrder; } + private async increaseBalanceAndAllowanceAsync( + tokenAddress: string, address: string, amount: BigNumber.BigNumber): Promise<void> { + if (amount.isZero()) { + return; // noop + } + await Promise.all([ + this.increaseBalanceAsync(tokenAddress, address, amount), + this.increaseAllowanceAsync(tokenAddress, address, amount), + ]); + } + private async increaseBalanceAsync( + tokenAddress: string, address: string, amount: BigNumber.BigNumber): Promise<void> { + await this.zeroEx.token.transferAsync(tokenAddress, this.coinbase, address, amount); + } + private async increaseAllowanceAsync( + tokenAddress: string, address: string, amount: BigNumber.BigNumber): Promise<void> { + const oldMakerAllowance = await this.zeroEx.token.getProxyAllowanceAsync(tokenAddress, address); + const newMakerAllowance = oldMakerAllowance.plus(amount); + await this.zeroEx.token.setProxyAllowanceAsync( + tokenAddress, address, newMakerAllowance, + ); + } } diff --git a/test/web3_wrapper_test.ts b/test/web3_wrapper_test.ts new file mode 100644 index 000000000..d1c2e8e89 --- /dev/null +++ b/test/web3_wrapper_test.ts @@ -0,0 +1,29 @@ +import * as chai from 'chai'; +import {web3Factory} from './utils/web3_factory'; +import {ZeroEx} from '../src/'; +import {Web3Wrapper} from '../src/web3_wrapper'; +import {constants} from './utils/constants'; + +chai.config.includeStack = true; +const expect = chai.expect; + +describe('Web3Wrapper', () => { + const web3Provider = web3Factory.create().currentProvider; + describe('#getNetworkIdIfExistsAsync', () => { + it('caches network id requests', async () => { + const web3Wrapper = (new ZeroEx(web3Provider) as any)._web3Wrapper as Web3Wrapper; + expect((web3Wrapper as any).networkIdIfExists).to.be.undefined(); + const networkIdIfExists = await web3Wrapper.getNetworkIdIfExistsAsync(); + expect((web3Wrapper as any).networkIdIfExists).to.be.equal(constants.TESTRPC_NETWORK_ID); + }); + it('invalidates network id cache on setProvider call', async () => { + const web3Wrapper = (new ZeroEx(web3Provider) as any)._web3Wrapper as Web3Wrapper; + expect((web3Wrapper as any).networkIdIfExists).to.be.undefined(); + const networkIdIfExists = await web3Wrapper.getNetworkIdIfExistsAsync(); + expect((web3Wrapper as any).networkIdIfExists).to.be.equal(constants.TESTRPC_NETWORK_ID); + const newProvider = web3Factory.create().currentProvider; + web3Wrapper.setProvider(newProvider); + expect((web3Wrapper as any).networkIdIfExists).to.be.undefined(); + }); + }); +}); |