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

chore: Update Swap Slippage v1 API for getting the Swap Quote #1248

Merged
merged 4 commits into from
Sep 12, 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
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,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we need this logic in a 3rd place, we should extract to a utility

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

}
Expand Down