diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-07-13 23:45:25 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-07-13 23:45:25 +0800 |
commit | 95e9f33f6aa5fa7849279062b008afa763a465d8 (patch) | |
tree | 085b1303c0576a1bafe471191253a416cbd939d2 /packages/order-watcher/test | |
parent | c599a20b34331c6481318d3cd3b01855444f339d (diff) | |
download | dexon-sol-tools-95e9f33f6aa5fa7849279062b008afa763a465d8.tar dexon-sol-tools-95e9f33f6aa5fa7849279062b008afa763a465d8.tar.gz dexon-sol-tools-95e9f33f6aa5fa7849279062b008afa763a465d8.tar.bz2 dexon-sol-tools-95e9f33f6aa5fa7849279062b008afa763a465d8.tar.lz dexon-sol-tools-95e9f33f6aa5fa7849279062b008afa763a465d8.tar.xz dexon-sol-tools-95e9f33f6aa5fa7849279062b008afa763a465d8.tar.zst dexon-sol-tools-95e9f33f6aa5fa7849279062b008afa763a465d8.zip |
Migrate order-watcher to v2
Diffstat (limited to 'packages/order-watcher/test')
-rw-r--r-- | packages/order-watcher/test/expiration_watcher_test.ts | 110 | ||||
-rw-r--r-- | packages/order-watcher/test/global_hooks.ts | 6 | ||||
-rw-r--r-- | packages/order-watcher/test/order_watcher_test.ts | 401 | ||||
-rw-r--r-- | packages/order-watcher/test/utils/token_utils.ts | 34 | ||||
-rw-r--r-- | packages/order-watcher/test/utils/web3_wrapper.ts | 2 |
5 files changed, 238 insertions, 315 deletions
diff --git a/packages/order-watcher/test/expiration_watcher_test.ts b/packages/order-watcher/test/expiration_watcher_test.ts index dfd3556bc..3c92ddb63 100644 --- a/packages/order-watcher/test/expiration_watcher_test.ts +++ b/packages/order-watcher/test/expiration_watcher_test.ts @@ -1,8 +1,9 @@ import { ContractWrappers } from '@0xproject/contract-wrappers'; +import { tokenUtils } from '@0xproject/contract-wrappers/lib/test/utils/token_utils'; import { BlockchainLifecycle, callbackErrorReporter } from '@0xproject/dev-utils'; import { FillScenarios } from '@0xproject/fill-scenarios'; -import { getOrderHashHex } from '@0xproject/order-utils'; -import { DoneCallback, Token } from '@0xproject/types'; +import { assetProxyUtils, orderHashUtils } from '@0xproject/order-utils'; +import { DoneCallback } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import * as chai from 'chai'; import * as _ from 'lodash'; @@ -14,7 +15,6 @@ import { utils } from '../src/utils/utils'; import { chaiSetup } from './utils/chai_setup'; import { constants } from './utils/constants'; -import { TokenUtils } from './utils/token_utils'; import { provider, web3Wrapper } from './utils/web3_wrapper'; chaiSetup.configure(); @@ -23,15 +23,16 @@ const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); const MILISECONDS_IN_SECOND = 1000; describe('ExpirationWatcher', () => { - let contractWrappers: ContractWrappers; - let tokenUtils: TokenUtils; - let tokens: Token[]; + const config = { + networkId: constants.TESTRPC_NETWORK_ID, + }; + const contractWrappers = new ContractWrappers(provider, config); let userAddresses: string[]; let zrxTokenAddress: string; let fillScenarios: FillScenarios; - let exchangeContractAddress: string; - let makerTokenAddress: string; - let takerTokenAddress: string; + const exchangeContractAddress = contractWrappers.exchange.getContractAddress(); + let makerAssetData: string; + let takerAssetData: string; let coinbase: string; let makerAddress: string; let takerAddress: string; @@ -41,21 +42,26 @@ describe('ExpirationWatcher', () => { let timer: Sinon.SinonFakeTimers; let expirationWatcher: ExpirationWatcher; before(async () => { - const config = { - networkId: constants.TESTRPC_NETWORK_ID, - }; - contractWrappers = new ContractWrappers(provider, config); - exchangeContractAddress = contractWrappers.exchange.getContractAddress(); + await blockchainLifecycle.startAsync(); + const erc20ProxyAddress = contractWrappers.erc20Proxy.getContractAddress(); userAddresses = await web3Wrapper.getAvailableAddressesAsync(); - tokens = await contractWrappers.tokenRegistry.getTokensAsync(); - tokenUtils = new TokenUtils(tokens); - zrxTokenAddress = tokenUtils.getProtocolTokenOrThrow().address; - fillScenarios = new FillScenarios(provider, userAddresses, tokens, zrxTokenAddress, exchangeContractAddress); + zrxTokenAddress = tokenUtils.getProtocolTokenAddress(); + fillScenarios = new FillScenarios( + provider, + userAddresses, + zrxTokenAddress, + exchangeContractAddress, + erc20ProxyAddress, + ); [coinbase, makerAddress, takerAddress, feeRecipient] = userAddresses; - tokens = await contractWrappers.tokenRegistry.getTokensAsync(); - const [makerToken, takerToken] = tokenUtils.getDummyTokens(); - makerTokenAddress = makerToken.address; - takerTokenAddress = takerToken.address; + const [makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses(); + [makerAssetData, takerAssetData] = [ + assetProxyUtils.encodeERC20AssetData(makerTokenAddress), + assetProxyUtils.encodeERC20AssetData(takerTokenAddress), + ]; + }); + after(async () => { + await blockchainLifecycle.revertAsync(); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -75,15 +81,15 @@ describe('ExpirationWatcher', () => { const orderLifetimeSec = 60; const expirationUnixTimestampSec = currentUnixTimestampSec.plus(orderLifetimeSec); const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, - takerTokenAddress, + makerAssetData, + takerAssetData, makerAddress, takerAddress, fillableAmount, expirationUnixTimestampSec, ); - const orderHash = getOrderHashHex(signedOrder); - expirationWatcher.addOrder(orderHash, signedOrder.expirationUnixTimestampSec.times(MILISECONDS_IN_SECOND)); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); + expirationWatcher.addOrder(orderHash, signedOrder.expirationTimeSeconds.times(MILISECONDS_IN_SECOND)); const callbackAsync = callbackErrorReporter.reportNoErrorCallbackErrors(done)((hash: string) => { expect(hash).to.be.equal(orderHash); expect(utils.getCurrentUnixTimestampSec()).to.be.bignumber.gte(expirationUnixTimestampSec); @@ -97,15 +103,15 @@ describe('ExpirationWatcher', () => { const orderLifetimeSec = 60; const expirationUnixTimestampSec = currentUnixTimestampSec.plus(orderLifetimeSec); const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, - takerTokenAddress, + makerAssetData, + takerAssetData, makerAddress, takerAddress, fillableAmount, expirationUnixTimestampSec, ); - const orderHash = getOrderHashHex(signedOrder); - expirationWatcher.addOrder(orderHash, signedOrder.expirationUnixTimestampSec.times(MILISECONDS_IN_SECOND)); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); + expirationWatcher.addOrder(orderHash, signedOrder.expirationTimeSeconds.times(MILISECONDS_IN_SECOND)); const callbackAsync = callbackErrorReporter.reportNoErrorCallbackErrors(done)(async (_hash: string) => { done(new Error('Emitted expiration went before the order actually expired')); }); @@ -122,31 +128,25 @@ describe('ExpirationWatcher', () => { const order1ExpirationUnixTimestampSec = currentUnixTimestampSec.plus(order1Lifetime); const order2ExpirationUnixTimestampSec = currentUnixTimestampSec.plus(order2Lifetime); const signedOrder1 = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, - takerTokenAddress, + makerAssetData, + takerAssetData, makerAddress, takerAddress, fillableAmount, order1ExpirationUnixTimestampSec, ); const signedOrder2 = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, - takerTokenAddress, + makerAssetData, + takerAssetData, makerAddress, takerAddress, fillableAmount, order2ExpirationUnixTimestampSec, ); - const orderHash1 = getOrderHashHex(signedOrder1); - const orderHash2 = getOrderHashHex(signedOrder2); - expirationWatcher.addOrder( - orderHash2, - signedOrder2.expirationUnixTimestampSec.times(MILISECONDS_IN_SECOND), - ); - expirationWatcher.addOrder( - orderHash1, - signedOrder1.expirationUnixTimestampSec.times(MILISECONDS_IN_SECOND), - ); + const orderHash1 = orderHashUtils.getOrderHashHex(signedOrder1); + const orderHash2 = orderHashUtils.getOrderHashHex(signedOrder2); + expirationWatcher.addOrder(orderHash2, signedOrder2.expirationTimeSeconds.times(MILISECONDS_IN_SECOND)); + expirationWatcher.addOrder(orderHash1, signedOrder1.expirationTimeSeconds.times(MILISECONDS_IN_SECOND)); const expirationOrder = [orderHash1, orderHash2]; const expectToBeCalledOnce = false; const callbackAsync = callbackErrorReporter.reportNoErrorCallbackErrors(done, expectToBeCalledOnce)( @@ -169,31 +169,25 @@ describe('ExpirationWatcher', () => { const order1ExpirationUnixTimestampSec = currentUnixTimestampSec.plus(order1Lifetime); const order2ExpirationUnixTimestampSec = currentUnixTimestampSec.plus(order2Lifetime); const signedOrder1 = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, - takerTokenAddress, + makerAssetData, + takerAssetData, makerAddress, takerAddress, fillableAmount, order1ExpirationUnixTimestampSec, ); const signedOrder2 = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, - takerTokenAddress, + makerAssetData, + takerAssetData, makerAddress, takerAddress, fillableAmount, order2ExpirationUnixTimestampSec, ); - const orderHash1 = getOrderHashHex(signedOrder1); - const orderHash2 = getOrderHashHex(signedOrder2); - expirationWatcher.addOrder( - orderHash1, - signedOrder1.expirationUnixTimestampSec.times(MILISECONDS_IN_SECOND), - ); - expirationWatcher.addOrder( - orderHash2, - signedOrder2.expirationUnixTimestampSec.times(MILISECONDS_IN_SECOND), - ); + const orderHash1 = orderHashUtils.getOrderHashHex(signedOrder1); + const orderHash2 = orderHashUtils.getOrderHashHex(signedOrder2); + expirationWatcher.addOrder(orderHash1, signedOrder1.expirationTimeSeconds.times(MILISECONDS_IN_SECOND)); + expirationWatcher.addOrder(orderHash2, signedOrder2.expirationTimeSeconds.times(MILISECONDS_IN_SECOND)); const expirationOrder = orderHash1 < orderHash2 ? [orderHash1, orderHash2] : [orderHash2, orderHash1]; const expectToBeCalledOnce = false; const callbackAsync = callbackErrorReporter.reportNoErrorCallbackErrors(done, expectToBeCalledOnce)( diff --git a/packages/order-watcher/test/global_hooks.ts b/packages/order-watcher/test/global_hooks.ts index 30b0cd697..4552e01b0 100644 --- a/packages/order-watcher/test/global_hooks.ts +++ b/packages/order-watcher/test/global_hooks.ts @@ -1,5 +1,5 @@ import { devConstants } from '@0xproject/dev-utils'; -import { runV1MigrationsAsync } from '@0xproject/migrations'; +import { runV2MigrationsAsync } from '@0xproject/migrations'; import { provider } from './utils/web3_wrapper'; @@ -12,6 +12,6 @@ before('migrate contracts', async function(): Promise<void> { gas: devConstants.GAS_LIMIT, from: devConstants.TESTRPC_FIRST_ADDRESS, }; - const artifactsDir = `../migrations/artifacts/1.0.0`; - await runV1MigrationsAsync(provider, artifactsDir, txDefaults); + const artifactsDir = `../migrations/artifacts/2.0.0`; + await runV2MigrationsAsync(provider, artifactsDir, txDefaults); }); diff --git a/packages/order-watcher/test/order_watcher_test.ts b/packages/order-watcher/test/order_watcher_test.ts index 2889051bc..6339505ce 100644 --- a/packages/order-watcher/test/order_watcher_test.ts +++ b/packages/order-watcher/test/order_watcher_test.ts @@ -1,8 +1,9 @@ // tslint:disable:no-unnecessary-type-assertion -import { ContractWrappers } from '@0xproject/contract-wrappers'; +import { BlockParamLiteral, ContractWrappers } from '@0xproject/contract-wrappers'; +import { tokenUtils } from '@0xproject/contract-wrappers/lib/test/utils/token_utils'; import { BlockchainLifecycle, callbackErrorReporter } from '@0xproject/dev-utils'; import { FillScenarios } from '@0xproject/fill-scenarios'; -import { getOrderHashHex } from '@0xproject/order-utils'; +import { assetProxyUtils, orderHashUtils } from '@0xproject/order-utils'; import { DoneCallback, ExchangeContractErrs, @@ -10,7 +11,6 @@ import { OrderStateInvalid, OrderStateValid, SignedOrder, - Token, } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -18,12 +18,15 @@ import * as chai from 'chai'; import * as _ from 'lodash'; import 'mocha'; +import { + DependentOrderHashesTracker, + OrderHashesByERC20ByMakerAddress, +} from '../src/order_watcher/dependent_order_hashes_tracker'; import { OrderWatcher } from '../src/order_watcher/order_watcher'; import { OrderWatcherError } from '../src/types'; import { chaiSetup } from './utils/chai_setup'; import { constants } from './utils/constants'; -import { TokenUtils } from './utils/token_utils'; import { provider, web3Wrapper } from './utils/web3_wrapper'; const TIMEOUT_MS = 150; @@ -33,37 +36,49 @@ const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); describe('OrderWatcher', () => { - let contractWrappers: ContractWrappers; - let tokens: Token[]; - let tokenUtils: TokenUtils; + const networkId = constants.TESTRPC_NETWORK_ID; + const config = { networkId }; + const contractWrappers = new ContractWrappers(provider, config); let fillScenarios: FillScenarios; let userAddresses: string[]; let zrxTokenAddress: string; let exchangeContractAddress: string; - let makerToken: Token; - let takerToken: Token; - let maker: string; - let taker: string; + let makerAssetData: string; + let takerAssetData: string; + let makerTokenAddress: string; + let takerTokenAddress: string; + let makerAddress: string; + let takerAddress: string; + let coinbase: string; + let feeRecipient: string; let signedOrder: SignedOrder; let orderWatcher: OrderWatcher; const decimals = constants.ZRX_DECIMALS; const fillableAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), decimals); before(async () => { - const networkId = await web3Wrapper.getNetworkIdAsync(); - const config = { - networkId, - }; - contractWrappers = new ContractWrappers(provider, config); - orderWatcher = new OrderWatcher(provider, networkId); - exchangeContractAddress = contractWrappers.exchange.getContractAddress(); + await blockchainLifecycle.startAsync(); + const erc20ProxyAddress = contractWrappers.erc20Proxy.getContractAddress(); userAddresses = await web3Wrapper.getAvailableAddressesAsync(); - [, maker, taker] = userAddresses; - tokens = await contractWrappers.tokenRegistry.getTokensAsync(); - tokenUtils = new TokenUtils(tokens); - zrxTokenAddress = tokenUtils.getProtocolTokenOrThrow().address; - fillScenarios = new FillScenarios(provider, userAddresses, tokens, zrxTokenAddress, exchangeContractAddress); - await fillScenarios.initTokenBalancesAsync(); - [makerToken, takerToken] = tokenUtils.getDummyTokens(); + zrxTokenAddress = tokenUtils.getProtocolTokenAddress(); + exchangeContractAddress = contractWrappers.exchange.getContractAddress(); + fillScenarios = new FillScenarios( + provider, + userAddresses, + zrxTokenAddress, + exchangeContractAddress, + erc20ProxyAddress, + ); + [coinbase, makerAddress, takerAddress, feeRecipient] = userAddresses; + [makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses(); + [makerAssetData, takerAssetData] = [ + assetProxyUtils.encodeERC20AssetData(makerTokenAddress), + assetProxyUtils.encodeERC20AssetData(takerTokenAddress), + ]; + const orderWatcherConfig = { stateLayer: BlockParamLiteral.Latest }; + orderWatcher = new OrderWatcher(provider, networkId, orderWatcherConfig); + }); + after(async () => { + await blockchainLifecycle.revertAsync(); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -74,35 +89,40 @@ describe('OrderWatcher', () => { describe('#removeOrder', async () => { it('should successfully remove existing order', async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); orderWatcher.addOrder(signedOrder); expect((orderWatcher as any)._orderByOrderHash).to.include({ [orderHash]: signedOrder, }); - let dependentOrderHashes = (orderWatcher as any)._dependentOrderHashes; - expect(dependentOrderHashes[signedOrder.maker][signedOrder.makerTokenAddress]).to.have.keys(orderHash); + const dependentOrderHashesTracker = (orderWatcher as any) + ._dependentOrderHashesTracker as DependentOrderHashesTracker; + let orderHashesByERC20ByMakerAddress: OrderHashesByERC20ByMakerAddress = (dependentOrderHashesTracker as any) + ._orderHashesByERC20ByMakerAddress; + expect(orderHashesByERC20ByMakerAddress[signedOrder.makerAddress][makerTokenAddress]).to.have.keys( + orderHash, + ); orderWatcher.removeOrder(orderHash); expect((orderWatcher as any)._orderByOrderHash).to.not.include({ [orderHash]: signedOrder, }); - dependentOrderHashes = (orderWatcher as any)._dependentOrderHashes; - expect(dependentOrderHashes[signedOrder.maker]).to.be.undefined(); + orderHashesByERC20ByMakerAddress = (dependentOrderHashesTracker as any)._orderHashesByERC20ByMakerAddress; + expect(orderHashesByERC20ByMakerAddress[signedOrder.makerAddress]).to.be.undefined(); }); it('should no-op when removing a non-existing order', async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); const nonExistentOrderHash = `0x${orderHash .substr(2) .split('') @@ -123,19 +143,19 @@ describe('OrderWatcher', () => { describe('tests with cleanup', async () => { afterEach(async () => { orderWatcher.unsubscribe(); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); orderWatcher.removeOrder(orderHash); }); - it('should emit orderStateInvalid when maker allowance set to 0 for watched order', (done: DoneCallback) => { + it('should emit orderStateInvalid when makerAddress allowance set to 0 for watched order', (done: DoneCallback) => { (async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); orderWatcher.addOrder(signedOrder); const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { expect(orderState.isValid).to.be.false(); @@ -144,16 +164,20 @@ describe('OrderWatcher', () => { expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerAllowance); }); orderWatcher.subscribe(callback); - await contractWrappers.token.setProxyAllowanceAsync(makerToken.address, maker, new BigNumber(0)); + await contractWrappers.erc20Token.setProxyAllowanceAsync( + makerTokenAddress, + makerAddress, + new BigNumber(0), + ); })().catch(done); }); it('should not emit an orderState event when irrelevant Transfer event received', (done: DoneCallback) => { (async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); orderWatcher.addOrder(signedOrder); @@ -162,10 +186,10 @@ describe('OrderWatcher', () => { }); orderWatcher.subscribe(callback); const notTheMaker = userAddresses[0]; - const anyRecipient = taker; + const anyRecipient = takerAddress; const transferAmount = new BigNumber(2); - await contractWrappers.token.transferAsync( - makerToken.address, + await contractWrappers.erc20Token.transferAsync( + makerTokenAddress, notTheMaker, anyRecipient, transferAmount, @@ -175,16 +199,16 @@ describe('OrderWatcher', () => { }, TIMEOUT_MS); })().catch(done); }); - it('should emit orderStateInvalid when maker moves balance backing watched order', (done: DoneCallback) => { + it('should emit orderStateInvalid when makerAddress moves balance backing watched order', (done: DoneCallback) => { (async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); orderWatcher.addOrder(signedOrder); const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { expect(orderState.isValid).to.be.false(); @@ -193,21 +217,26 @@ describe('OrderWatcher', () => { expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerBalance); }); orderWatcher.subscribe(callback); - const anyRecipient = taker; - const makerBalance = await contractWrappers.token.getBalanceAsync(makerToken.address, maker); - await contractWrappers.token.transferAsync(makerToken.address, maker, anyRecipient, makerBalance); + const anyRecipient = takerAddress; + const makerBalance = await contractWrappers.erc20Token.getBalanceAsync(makerTokenAddress, makerAddress); + await contractWrappers.erc20Token.transferAsync( + makerTokenAddress, + makerAddress, + anyRecipient, + makerBalance, + ); })().catch(done); }); it('should emit orderStateInvalid when watched order fully filled', (done: DoneCallback) => { (async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); orderWatcher.addOrder(signedOrder); const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { @@ -218,28 +247,22 @@ describe('OrderWatcher', () => { }); orderWatcher.subscribe(callback); - const shouldThrowOnInsufficientBalanceOrAllowance = true; - await contractWrappers.exchange.fillOrderAsync( - signedOrder, - fillableAmount, - shouldThrowOnInsufficientBalanceOrAllowance, - taker, - ); + await contractWrappers.exchange.fillOrderAsync(signedOrder, fillableAmount, takerAddress); })().catch(done); }); it('should emit orderStateValid when watched order partially filled', (done: DoneCallback) => { (async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); - const makerBalance = await contractWrappers.token.getBalanceAsync(makerToken.address, maker); + const makerBalance = await contractWrappers.erc20Token.getBalanceAsync(makerTokenAddress, makerAddress); const fillAmountInBaseUnits = new BigNumber(2); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); orderWatcher.addOrder(signedOrder); const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { @@ -249,22 +272,16 @@ describe('OrderWatcher', () => { const orderRelevantState = validOrderState.orderRelevantState; const remainingMakerBalance = makerBalance.sub(fillAmountInBaseUnits); const remainingFillable = fillableAmount.minus(fillAmountInBaseUnits); - expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableMakerAssetAmount).to.be.bignumber.equal( remainingFillable, ); - expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableTakerAssetAmount).to.be.bignumber.equal( remainingFillable, ); expect(orderRelevantState.makerBalance).to.be.bignumber.equal(remainingMakerBalance); }); orderWatcher.subscribe(callback); - const shouldThrowOnInsufficientBalanceOrAllowance = true; - await contractWrappers.exchange.fillOrderAsync( - signedOrder, - fillAmountInBaseUnits, - shouldThrowOnInsufficientBalanceOrAllowance, - taker, - ); + await contractWrappers.exchange.fillOrderAsync(signedOrder, fillAmountInBaseUnits, takerAddress); })().catch(done); }); it('should trigger the callback when orders backing ZRX allowance changes', (done: DoneCallback) => { @@ -272,19 +289,23 @@ describe('OrderWatcher', () => { const makerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(2), decimals); const takerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(0), decimals); signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( - makerToken.address, - takerToken.address, + makerAssetData, + takerAssetData, makerFee, takerFee, - maker, - taker, + makerAddress, + takerAddress, fillableAmount, - taker, + takerAddress, ); const callback = callbackErrorReporter.reportNodeCallbackErrors(done)(); orderWatcher.addOrder(signedOrder); orderWatcher.subscribe(callback); - await contractWrappers.token.setProxyAllowanceAsync(zrxTokenAddress, maker, new BigNumber(0)); + await contractWrappers.erc20Token.setProxyAllowanceAsync( + zrxTokenAddress, + makerAddress, + new BigNumber(0), + ); })().catch(done); }); describe('remainingFillable(M|T)akerTokenAmount', () => { @@ -293,45 +314,39 @@ describe('OrderWatcher', () => { const takerFillableAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(10), decimals); const makerFillableAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(20), decimals); signedOrder = await fillScenarios.createAsymmetricFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, makerFillableAmount, takerFillableAmount, ); const fillAmountInBaseUnits = Web3Wrapper.toBaseUnitAmount(new BigNumber(2), decimals); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); orderWatcher.addOrder(signedOrder); const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { expect(orderState.isValid).to.be.true(); const validOrderState = orderState as OrderStateValid; expect(validOrderState.orderHash).to.be.equal(orderHash); const orderRelevantState = validOrderState.orderRelevantState; - expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableMakerAssetAmount).to.be.bignumber.equal( Web3Wrapper.toBaseUnitAmount(new BigNumber(16), decimals), ); - expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableTakerAssetAmount).to.be.bignumber.equal( Web3Wrapper.toBaseUnitAmount(new BigNumber(8), decimals), ); }); orderWatcher.subscribe(callback); - const shouldThrowOnInsufficientBalanceOrAllowance = true; - await contractWrappers.exchange.fillOrderAsync( - signedOrder, - fillAmountInBaseUnits, - shouldThrowOnInsufficientBalanceOrAllowance, - taker, - ); + await contractWrappers.exchange.fillOrderAsync(signedOrder, fillAmountInBaseUnits, takerAddress); })().catch(done); }); it('should equal approved amount when approved amount is lowest', (done: DoneCallback) => { (async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); @@ -341,17 +356,17 @@ describe('OrderWatcher', () => { const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { const validOrderState = orderState as OrderStateValid; const orderRelevantState = validOrderState.orderRelevantState; - expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableMakerAssetAmount).to.be.bignumber.equal( changedMakerApprovalAmount, ); - expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableTakerAssetAmount).to.be.bignumber.equal( changedMakerApprovalAmount, ); }); orderWatcher.subscribe(callback); - await contractWrappers.token.setProxyAllowanceAsync( - makerToken.address, - maker, + await contractWrappers.erc20Token.setProxyAllowanceAsync( + makerTokenAddress, + makerAddress, changedMakerApprovalAmount, ); })().catch(done); @@ -359,14 +374,17 @@ describe('OrderWatcher', () => { it('should equal balance amount when balance amount is lowest', (done: DoneCallback) => { (async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); - const makerBalance = await contractWrappers.token.getBalanceAsync(makerToken.address, maker); + const makerBalance = await contractWrappers.erc20Token.getBalanceAsync( + makerTokenAddress, + makerAddress, + ); const remainingAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(1), decimals); const transferAmount = makerBalance.sub(remainingAmount); @@ -376,66 +394,33 @@ describe('OrderWatcher', () => { expect(orderState.isValid).to.be.true(); const validOrderState = orderState as OrderStateValid; const orderRelevantState = validOrderState.orderRelevantState; - expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableMakerAssetAmount).to.be.bignumber.equal( remainingAmount, ); - expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableTakerAssetAmount).to.be.bignumber.equal( remainingAmount, ); }); orderWatcher.subscribe(callback); - await contractWrappers.token.transferAsync( - makerToken.address, - maker, + await contractWrappers.erc20Token.transferAsync( + makerTokenAddress, + makerAddress, constants.NULL_ADDRESS, transferAmount, ); })().catch(done); }); - it('should equal remaining amount when partially cancelled and order has fees', (done: DoneCallback) => { - (async () => { - const takerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(0), decimals); - const makerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), decimals); - const feeRecipient = taker; - signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( - makerToken.address, - takerToken.address, - makerFee, - takerFee, - maker, - taker, - fillableAmount, - feeRecipient, - ); - - const remainingTokenAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(4), decimals); - const transferTokenAmount = makerFee.sub(remainingTokenAmount); - orderWatcher.addOrder(signedOrder); - - const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { - expect(orderState.isValid).to.be.true(); - const validOrderState = orderState as OrderStateValid; - const orderRelevantState = validOrderState.orderRelevantState; - expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( - remainingTokenAmount, - ); - }); - orderWatcher.subscribe(callback); - await contractWrappers.exchange.cancelOrderAsync(signedOrder, transferTokenAmount); - })().catch(done); - }); it('should equal ratio amount when fee balance is lowered', (done: DoneCallback) => { (async () => { const takerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(0), decimals); const makerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), decimals); - const feeRecipient = taker; signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( - makerToken.address, - takerToken.address, + makerAssetData, + takerAssetData, makerFee, takerFee, - maker, - taker, + makerAddress, + takerAddress, fillableAmount, feeRecipient, ); @@ -449,15 +434,19 @@ describe('OrderWatcher', () => { const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { const validOrderState = orderState as OrderStateValid; const orderRelevantState = validOrderState.orderRelevantState; - expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableMakerAssetAmount).to.be.bignumber.equal( remainingFeeAmount, ); }); orderWatcher.subscribe(callback); - await contractWrappers.token.setProxyAllowanceAsync(zrxTokenAddress, maker, remainingFeeAmount); - await contractWrappers.token.transferAsync( - makerToken.address, - maker, + await contractWrappers.erc20Token.setProxyAllowanceAsync( + zrxTokenAddress, + makerAddress, + remainingFeeAmount, + ); + await contractWrappers.erc20Token.transferAsync( + makerTokenAddress, + makerAddress, constants.NULL_ADDRESS, transferTokenAmount, ); @@ -467,14 +456,13 @@ describe('OrderWatcher', () => { (async () => { const takerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(0), decimals); const makerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(2), decimals); - const feeRecipient = taker; signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( - makerToken.address, - takerToken.address, + makerAssetData, + takerAssetData, makerFee, takerFee, - maker, - taker, + makerAddress, + takerAddress, fillableAmount, feeRecipient, ); @@ -484,14 +472,14 @@ describe('OrderWatcher', () => { const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { const validOrderState = orderState as OrderStateValid; const orderRelevantState = validOrderState.orderRelevantState; - expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( + expect(orderRelevantState.remainingFillableMakerAssetAmount).to.be.bignumber.equal( fillableAmount, ); }); orderWatcher.subscribe(callback); - await contractWrappers.token.setProxyAllowanceAsync( - makerToken.address, - maker, + await contractWrappers.erc20Token.setProxyAllowanceAsync( + makerTokenAddress, + makerAddress, Web3Wrapper.toBaseUnitAmount(new BigNumber(100), decimals), ); })().catch(done); @@ -500,37 +488,36 @@ describe('OrderWatcher', () => { it('should emit orderStateInvalid when watched order cancelled', (done: DoneCallback) => { (async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); orderWatcher.addOrder(signedOrder); const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { expect(orderState.isValid).to.be.false(); const invalidOrderState = orderState as OrderStateInvalid; expect(invalidOrderState.orderHash).to.be.equal(orderHash); - expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderRemainingFillAmountZero); + expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderFillRoundingError); }); orderWatcher.subscribe(callback); - - await contractWrappers.exchange.cancelOrderAsync(signedOrder, fillableAmount); + await contractWrappers.exchange.cancelOrderAsync(signedOrder); })().catch(done); }); it('should emit orderStateInvalid when within rounding error range', (done: DoneCallback) => { (async () => { const remainingFillableAmountInBaseUnits = new BigNumber(100); signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, + makerAssetData, + takerAssetData, + makerAddress, + takerAddress, fillableAmount, ); - const orderHash = getOrderHashHex(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); orderWatcher.addOrder(signedOrder); const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { @@ -540,36 +527,12 @@ describe('OrderWatcher', () => { expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderFillRoundingError); }); orderWatcher.subscribe(callback); - await contractWrappers.exchange.cancelOrderAsync( + await contractWrappers.exchange.fillOrderAsync( signedOrder, fillableAmount.minus(remainingFillableAmountInBaseUnits), + takerAddress, ); })().catch(done); }); - it('should emit orderStateValid when watched order partially cancelled', (done: DoneCallback) => { - (async () => { - signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerToken.address, - takerToken.address, - maker, - taker, - fillableAmount, - ); - - const cancelAmountInBaseUnits = new BigNumber(2); - const orderHash = getOrderHashHex(signedOrder); - orderWatcher.addOrder(signedOrder); - - const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => { - expect(orderState.isValid).to.be.true(); - const validOrderState = orderState as OrderStateValid; - expect(validOrderState.orderHash).to.be.equal(orderHash); - const orderRelevantState = validOrderState.orderRelevantState; - expect(orderRelevantState.cancelledTakerTokenAmount).to.be.bignumber.equal(cancelAmountInBaseUnits); - }); - orderWatcher.subscribe(callback); - await contractWrappers.exchange.cancelOrderAsync(signedOrder, cancelAmountInBaseUnits); - })().catch(done); - }); }); }); // tslint:disable:max-file-line-count diff --git a/packages/order-watcher/test/utils/token_utils.ts b/packages/order-watcher/test/utils/token_utils.ts deleted file mode 100644 index e1191b5bb..000000000 --- a/packages/order-watcher/test/utils/token_utils.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { Token } from '@0xproject/types'; -import * as _ from 'lodash'; - -import { InternalOrderWatcherError } from '../../src/types'; - -const PROTOCOL_TOKEN_SYMBOL = 'ZRX'; -const WETH_TOKEN_SYMBOL = 'WETH'; - -export class TokenUtils { - private _tokens: Token[]; - constructor(tokens: Token[]) { - this._tokens = tokens; - } - public getProtocolTokenOrThrow(): Token { - const zrxToken = _.find(this._tokens, { symbol: PROTOCOL_TOKEN_SYMBOL }); - if (_.isUndefined(zrxToken)) { - throw new Error(InternalOrderWatcherError.ZrxNotInTokenRegistry); - } - return zrxToken; - } - public getWethTokenOrThrow(): Token { - const wethToken = _.find(this._tokens, { symbol: WETH_TOKEN_SYMBOL }); - if (_.isUndefined(wethToken)) { - throw new Error(InternalOrderWatcherError.WethNotInTokenRegistry); - } - return wethToken; - } - public getDummyTokens(): Token[] { - const dummyTokens = _.filter(this._tokens, token => { - return !_.includes([PROTOCOL_TOKEN_SYMBOL, WETH_TOKEN_SYMBOL], token.symbol); - }); - return dummyTokens; - } -} diff --git a/packages/order-watcher/test/utils/web3_wrapper.ts b/packages/order-watcher/test/utils/web3_wrapper.ts index f7d11f138..ab801fa7f 100644 --- a/packages/order-watcher/test/utils/web3_wrapper.ts +++ b/packages/order-watcher/test/utils/web3_wrapper.ts @@ -1,6 +1,6 @@ import { web3Factory } from '@0xproject/dev-utils'; -import { Provider } from '@0xproject/types'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { Provider } from 'ethereum-types'; const provider: Provider = web3Factory.getRpcProvider({ shouldUseInProcessGanache: true }); const web3Wrapper = new Web3Wrapper(provider); |