diff --git a/cdk.context.json b/cdk.context.json index df9b9011..d4db75dc 100644 --- a/cdk.context.json +++ b/cdk.context.json @@ -30,29 +30,10 @@ } ] }, - "availability-zones:account=797568089812:region=us-east-2": [ - "us-east-2a", - "us-east-2b", - "us-east-2c" - ], - "availability-zones:account=644039819003:region=us-east-2": [ - "us-east-2a", - "us-east-2b", - "us-east-2c" - ], - "availability-zones:account=801328487475:region=us-east-2": [ - "us-east-2a", - "us-east-2b", - "us-east-2c" - ], - "availability-zones:account=830217277613:region=us-east-2": [ - "us-east-2a", - "us-east-2b", - "us-east-2c" - ], - "availability-zones:account=032111050613:region=us-east-2": [ - "us-east-2a", - "us-east-2b", - "us-east-2c" - ] + "availability-zones:account=797568089812:region=us-east-2": ["us-east-2a", "us-east-2b", "us-east-2c"], + "availability-zones:account=644039819003:region=us-east-2": ["us-east-2a", "us-east-2b", "us-east-2c"], + "availability-zones:account=801328487475:region=us-east-2": ["us-east-2a", "us-east-2b", "us-east-2c"], + "availability-zones:account=830217277613:region=us-east-2": ["us-east-2a", "us-east-2b", "us-east-2c"], + "availability-zones:account=032111050613:region=us-east-2": ["us-east-2a", "us-east-2b", "us-east-2c"], + "availability-zones:account=206819120522:region=us-west-2": ["us-west-2a", "us-west-2b", "us-west-2c", "us-west-2d"] } 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..16753672 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 amountIn = ethers.utils.parseEther('1'); + const request = getRequest(amountIn.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: amountIn.mul(1).toString(), amountIn: amountIn.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([