Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exact output comparison #225

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading