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: sanitize quote response amount #266

Merged
merged 2 commits into from
Feb 13, 2024
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
11 changes: 9 additions & 2 deletions lib/entities/QuoteResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class QuoteResponse implements QuoteResponseData {
};
}

// ensure quoted tokens match
if (
request?.tokenIn?.toLowerCase() !== data?.tokenIn?.toLowerCase() ||
request?.tokenOut?.toLowerCase() !== data?.tokenOut?.toLowerCase()
Expand All @@ -71,14 +72,20 @@ export class QuoteResponse implements QuoteResponseData {
};
}

// take quoted amount from RFQ response
// but specified amount from request to avoid any inaccuracies from incorrect echoed response
const [amountIn, amountOut] =
request.type === TradeType.EXACT_INPUT
? [request.amount, BigNumber.from(data.amountOut ?? 0)]
: [BigNumber.from(data.amountIn ?? 0), request.amount];
return {
response: new QuoteResponse(
{
...data,
quoteId: data.quoteId ?? uuidv4(),
swapper: request.swapper,
amountIn: BigNumber.from(data.amountIn ?? 0),
amountOut: BigNumber.from(data.amountOut ?? 0),
amountIn,
amountOut,
},
type
),
Expand Down
4 changes: 2 additions & 2 deletions test/entities/QuoteResponse.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TradeType } from '@uniswap/sdk-core';
import { BigNumber } from 'ethers';
import { parseEther } from 'ethers/lib/utils';

import { QuoteResponse } from '../../lib/entities';
Expand Down Expand Up @@ -101,7 +100,8 @@ describe('QuoteRequest', () => {
quoteId: QUOTE_ID,
};
const response = QuoteResponse.fromRFQ(quoteRequest, invalidResponse, TradeType.EXACT_INPUT);
expect(response.response.amountIn).toEqual(BigNumber.from(100));
// ensure we overwrite amount with the request amount, dont just accept what the quoter returned
expect(response.response.amountIn).toEqual(quoteRequest.amount);
expect(response.validationError?.message).toBe('"amountIn" must be a string');
expect(response.validationError?.value).toBe(invalidResponse);
});
Expand Down
2 changes: 1 addition & 1 deletion test/providers/quoters/WebhookQuoter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ describe('WebhookQuoter tests', () => {
...quote,
quoteId: expect.any(String),
amountOut: BigNumber.from(quote.amountOut),
amountIn: BigNumber.from(0),
amountIn: BigNumber.from(request.amount),
},
type: 0,
},
Expand Down
Loading