Skip to content

Commit

Permalink
chore: Update Swap Slippage v1 API for getting the Swap Quote (#1248)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcramer authored Sep 12, 2024
1 parent eeba141 commit 95acc71
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .changeset/shy-foxes-jog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
73 changes: 73 additions & 0 deletions src/api/getSwapQuote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
]);
});
});
8 changes: 7 additions & 1 deletion src/api/getSwapQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down

0 comments on commit 95acc71

Please sign in to comment.