From 95acc712144fd7793c8f81566dc315d8fbda5958 Mon Sep 17 00:00:00 2001 From: Paul Cramer Date: Thu, 12 Sep 2024 13:26:47 -0700 Subject: [PATCH] chore: Update Swap Slippage v1 API for getting the Swap Quote (#1248) --- .changeset/shy-foxes-jog.md | 2 +- src/api/getSwapQuote.test.ts | 73 ++++++++++++++++++++++++++++++++++++ src/api/getSwapQuote.ts | 8 +++- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/.changeset/shy-foxes-jog.md b/.changeset/shy-foxes-jog.md index 5a3e97d56a..7e4d3c34ca 100644 --- a/.changeset/shy-foxes-jog.md +++ b/.changeset/shy-foxes-jog.md @@ -4,4 +4,4 @@ - **feat**: introduced `config` for the `Swap` component, with the first option for `maxSlippage`. By @zizzamia & @cpcramer #1242 - **fix**: added spacing between swap input and token select. By @alessey #1229 -- **feat**: added slippage support in `Swap` component with settings UI. This combined feat incorporates slippage functionality into the Swap component, including a dedicated settings section with a title, description, and input field for configuring slippage tolerance. By @cpcramer #1187 #1192 #1191 #1189 #1195 #1196 #1206 +- **feat**: added slippage support in `Swap` component with settings UI. This combined feat incorporates slippage functionality into the Swap component, including a dedicated settings section with a title, description, and input field for configuring slippage tolerance. By @cpcramer #1187 #1192 #1191 #1189 #1195 #1196 #1206 #1248 diff --git a/src/api/getSwapQuote.test.ts b/src/api/getSwapQuote.test.ts index a3052a9ac1..007d8b2361 100644 --- a/src/api/getSwapQuote.test.ts +++ b/src/api/getSwapQuote.test.ts @@ -160,4 +160,77 @@ describe('getSwapQuote', () => { message: '', }); }); + + it('should adjust slippage for V1 API when useAggregator is true', async () => { + const mockParams = { + useAggregator: true, + maxSlippage: '3', + amountReference: testAmountReference, + from: ETH_TOKEN, + to: DEGEN_TOKEN, + amount: testAmount, + }; + const mockApiParams = getAPIParamsForToken(mockParams); + const mockResponse = { + id: 1, + jsonrpc: '2.0', + result: { + from: ETH_TOKEN, + to: DEGEN_TOKEN, + fromAmount: '100000000000000000', + toAmount: '16732157880511600003860', + amountReference: 'from', + priceImpact: '0.07', + chainId: 8453, + hasHighPriceImpact: false, + slippage: '30', + }, + }; + (sendRequest as vi.Mock).mockResolvedValue(mockResponse); + await getSwapQuote(mockParams); + expect(sendRequest).toHaveBeenCalledTimes(1); + expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_QUOTE, [ + { + ...mockApiParams, + slippagePercentage: '30', + }, + ]); + }); + + it('should not adjust slippage when useAggregator is false', async () => { + const mockParams = { + useAggregator: false, + maxSlippage: '3', + amountReference: testAmountReference, + from: ETH_TOKEN, + to: DEGEN_TOKEN, + amount: testAmount, + }; + const mockApiParams = getAPIParamsForToken(mockParams); + const mockResponse = { + id: 1, + jsonrpc: '2.0', + result: { + from: ETH_TOKEN, + to: DEGEN_TOKEN, + fromAmount: '100000000000000000', + toAmount: '16732157880511600003860', + amountReference: 'from', + priceImpact: '0.07', + chainId: 8453, + hasHighPriceImpact: false, + slippage: '3', + }, + }; + (sendRequest as vi.Mock).mockResolvedValue(mockResponse); + await getSwapQuote(mockParams); + expect(sendRequest).toHaveBeenCalledTimes(1); + expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_QUOTE, [ + { + ...mockApiParams, + v2Enabled: true, + slippagePercentage: '3', + }, + ]); + }); }); diff --git a/src/api/getSwapQuote.ts b/src/api/getSwapQuote.ts index a708c2ee48..98c6b5a06b 100644 --- a/src/api/getSwapQuote.ts +++ b/src/api/getSwapQuote.ts @@ -37,8 +37,14 @@ export async function getSwapQuote( }; } if (params.maxSlippage) { + let slippagePercentage = params.maxSlippage; + // Adjust slippage for V1 API (aggregator) + // V1 expects slippage in tenths of a percent (e.g., 30 = 3%) + if (params.useAggregator) { + slippagePercentage = (Number(params.maxSlippage) * 10).toString(); + } apiParams = { - slippagePercentage: params.maxSlippage, + slippagePercentage: slippagePercentage, ...apiParams, }; }