Skip to content

Commit

Permalink
Merge pull request #266 from Uniswap/sanitize-amount
Browse files Browse the repository at this point in the history
fix: sanitize quote response amount
  • Loading branch information
marktoda authored Feb 13, 2024
2 parents 9c9db01 + 0271de1 commit 8ea8735
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
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

0 comments on commit 8ea8735

Please sign in to comment.