diff options
Diffstat (limited to 'test')
-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 |
4 files changed, 66 insertions, 36 deletions
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(); + }); + }); +}); |