Skip to content

Commit

Permalink
fix: exact output comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
rileydcampbell committed Nov 3, 2023
1 parent 19d8120 commit a1bd22c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/handlers/quote/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 27 additions & 6 deletions test/handlers/quote/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down Expand Up @@ -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());
Expand All @@ -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),
});
Expand Down Expand Up @@ -236,7 +257,7 @@ describe('Quote handler', () => {
headers: {
'X-Authentication': '1234',
},
hash: '0xuni'
hash: '0xuni',
},
]);
const circuitBreakerProvider = new MockCircuitBreakerConfigurationProvider([
Expand Down

0 comments on commit a1bd22c

Please sign in to comment.