From 32e1ae2b186271ca5420b085b97772b317c31983 Mon Sep 17 00:00:00 2001 From: Daniel Pyrathon Date: Thu, 5 Mar 2020 16:07:49 -0800 Subject: [PATCH] added unit tests to avoid regression due to variable order --- .../asset-swapper/test/dex_sampler_test.ts | 30 +++++++++++++++++++ .../test/utils/mock_sampler_contract.ts | 24 +++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/packages/asset-swapper/test/dex_sampler_test.ts b/packages/asset-swapper/test/dex_sampler_test.ts index 4ba897c9ac..e13ec25e22 100644 --- a/packages/asset-swapper/test/dex_sampler_test.ts +++ b/packages/asset-swapper/test/dex_sampler_test.ts @@ -13,6 +13,7 @@ import { provider, randomAddress, txDefaults, + toBaseUnitAmount, } from '@0x/contracts-test-utils'; import { assetDataUtils, generatePseudoRandomSalt } from '@0x/order-utils'; import { SignedOrder } from '@0x/types'; @@ -149,6 +150,35 @@ describe('DexSampler tests', () => { expect(fillableAmounts).to.deep.eq(expectedMakerFillAmounts); }); + it('getLiquidityProviderSellQuotes()', async () => { + const expectedMakerToken = randomAddress(); + const expectedTakerToken = randomAddress(); + const registry = randomAddress(); + const sampler = new MockSamplerContract({ + sampleSellsFromLiquidityProviderRegistry: (registryAddress, takerToken, makerToken, fillAmounts) => { + expect(registryAddress).to.eq(registry); + expect(takerToken).to.eq(expectedTakerToken); + expect(makerToken).to.eq(expectedMakerToken); + return [toBaseUnitAmount(1001)]; + }, + }); + const dexOrderSampler = new DexOrderSampler(sampler); + const [result] = await dexOrderSampler.executeAsync( + DexOrderSampler.ops.getSellQuotes( + [ERC20BridgeSource.LiquidityProvider], + expectedMakerToken, + expectedTakerToken, + [toBaseUnitAmount(1000)], + registry, + ), + ); + expect(result).to.deep.equal([[{ + source: 'LiquidityProvider', + output: toBaseUnitAmount(1001), + input: toBaseUnitAmount(1000), + }]]); + }) + it('getEth2DaiSellQuotes()', async () => { const expectedTakerToken = randomAddress(); const expectedMakerToken = randomAddress(); diff --git a/packages/asset-swapper/test/utils/mock_sampler_contract.ts b/packages/asset-swapper/test/utils/mock_sampler_contract.ts index 0ddc580b8f..8eb7320afb 100644 --- a/packages/asset-swapper/test/utils/mock_sampler_contract.ts +++ b/packages/asset-swapper/test/utils/mock_sampler_contract.ts @@ -21,6 +21,12 @@ export type SampleBuysHandler = ( makerToken: string, makerTokenAmounts: BigNumber[], ) => SampleResults; +export type SampleSellsLPHandler = ( + registryAddress: string, + takerToken: string, + makerToken: string, + takerTokenAmounts: BigNumber[], +) => SampleResults; const DUMMY_PROVIDER = { sendAsync: (...args: any[]): any => { @@ -32,10 +38,12 @@ interface Handlers { getOrderFillableMakerAssetAmounts: GetOrderFillableAssetAmountHandler; getOrderFillableTakerAssetAmounts: GetOrderFillableAssetAmountHandler; sampleSellsFromKyberNetwork: SampleSellsHandler; + sampleSellsFromLiquidityProviderRegistry: SampleSellsLPHandler; sampleSellsFromEth2Dai: SampleSellsHandler; sampleSellsFromUniswap: SampleSellsHandler; sampleBuysFromEth2Dai: SampleBuysHandler; sampleBuysFromUniswap: SampleBuysHandler; + sampleBuysFromLiquidityProviderRegistry: SampleSellsLPHandler; } export class MockSamplerContract extends IERC20BridgeSamplerContract { @@ -119,6 +127,22 @@ export class MockSamplerContract extends IERC20BridgeSamplerContract { ); } + public sampleSellsFromLiquidityProviderRegistry( + registryAddress: string, + takerToken: string, + makerToken: string, + takerAssetAmounts: BigNumber[], + ): ContractFunctionObj { + return this._wrapCall( + super.sampleSellsFromLiquidityProviderRegistry, + this._handlers.sampleSellsFromLiquidityProviderRegistry, + registryAddress, + takerToken, + makerToken, + takerAssetAmounts, + ); + } + public sampleBuysFromEth2Dai( takerToken: string, makerToken: string,