From a1bd22ca91013a77c30d22ed99abfc41c11f25be Mon Sep 17 00:00:00 2001 From: Riley Campbell Date: Fri, 3 Nov 2023 13:23:50 -0400 Subject: [PATCH] fix: exact output comparison --- lib/handlers/quote/handler.ts | 6 +++++- test/handlers/quote/handler.test.ts | 33 +++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/handlers/quote/handler.ts b/lib/handlers/quote/handler.ts index dc2f6e48..3680250b 100644 --- a/lib/handlers/quote/handler.ts +++ b/lib/handlers/quote/handler.ts @@ -111,7 +111,11 @@ async function getBestQuote( body: { ...quote.toLog(), offerer: quote.swapper }, }); - if (!bestQuote || quote.amountOut.gt(bestQuote.amountOut)) { + if ( + !bestQuote || + (quoteRequest.type == TradeType.EXACT_INPUT && quote.amountOut.gt(bestQuote.amountOut)) || + (quoteRequest.type == TradeType.EXACT_OUTPUT && quote.amountIn.lt(bestQuote.amountIn)) + ) { return quote; } return bestQuote; diff --git a/test/handlers/quote/handler.test.ts b/test/handlers/quote/handler.test.ts index 85f09c8f..49c3923b 100644 --- a/test/handlers/quote/handler.test.ts +++ b/test/handlers/quote/handler.test.ts @@ -63,15 +63,15 @@ describe('Quote handler', () => { body: JSON.stringify(request), } as APIGatewayProxyEvent); - const getRequest = (amountIn: string): PostQuoteRequestBody => ({ + const getRequest = (amount: string, type = 'EXACT_INPUT'): PostQuoteRequestBody => ({ requestId: REQUEST_ID, tokenInChainId: CHAIN_ID, tokenOutChainId: CHAIN_ID, swapper: SWAPPER, tokenIn: TOKEN_IN, - amount: amountIn, + amount, tokenOut: TOKEN_OUT, - type: 'EXACT_INPUT', + type, numOutputs: 1, }); @@ -130,7 +130,7 @@ describe('Quote handler', () => { ).toMatchObject({ ...quoteResponse, quoteId: expect.any(String) }); }); - it('Pick the greater of two quotes', async () => { + it('Pick the greater of two quotes - EXACT_IN', async () => { const quoters = [new MockQuoter(logger, 1, 1), new MockQuoter(logger, 2, 1)]; const amountIn = ethers.utils.parseEther('1'); const request = getRequest(amountIn.toString()); @@ -141,7 +141,28 @@ describe('Quote handler', () => { ); const quoteResponse: PostQuoteResponse = JSON.parse(response.body); // random quoteId expect(response.statusCode).toEqual(200); - expect(responseFromRequest(request, { amountOut: amountIn.mul(2).toString() })).toMatchObject({ + expect( + responseFromRequest(request, { amountOut: amountIn.mul(2).toString(), amountIn: amountIn.mul(1).toString() }) + ).toMatchObject({ + ...quoteResponse, + quoteId: expect.any(String), + }); + }); + + it('Pick the lesser of two quotes - EXACT_OUT', async () => { + const quoters = [new MockQuoter(logger, 1, 1), new MockQuoter(logger, 2, 1)]; + const amountOut = ethers.utils.parseEther('1'); + const request = getRequest(amountOut.toString(), 'EXACT_OUTPUT'); + + const response: APIGatewayProxyResult = await getQuoteHandler(quoters).handler( + getEvent(request), + {} as unknown as Context + ); + const quoteResponse: PostQuoteResponse = JSON.parse(response.body); // random quoteId + expect(response.statusCode).toEqual(200); + expect( + responseFromRequest(request, { amountOut: amountOut.mul(1).toString(), amountIn: amountOut.mul(1).toString() }) + ).toMatchObject({ ...quoteResponse, quoteId: expect.any(String), }); @@ -236,7 +257,7 @@ describe('Quote handler', () => { headers: { 'X-Authentication': '1234', }, - hash: '0xuni' + hash: '0xuni', }, ]); const circuitBreakerProvider = new MockCircuitBreakerConfigurationProvider([