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 config defaultMaxSlippage naming #1305

Closed
wants to merge 4 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/heavy-carrots-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@coinbase/onchainkit': patch
---

-**chore**: Update `Swap` config parameter `maxSlippage` to `defaultMaxSlippage`. By @cpcramer #1305
7 changes: 5 additions & 2 deletions playground/nextjs-app-router/components/demo/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { TransactionReceipt } from 'viem';
import { base } from 'viem/chains';
import { AppContext } from '../AppProvider';

const DEFAULT_MAX_SLIPPAGE = 3;
const FALLBACK_DEFAULT_MAX_SLIPPAGE = 3;

function SwapComponent() {
const { chainId, defaultMaxSlippage, paymasters } = useContext(AppContext);
Expand Down Expand Up @@ -108,7 +108,10 @@ function SwapComponent() {
onStatus={handleOnStatus}
onSuccess={handleOnSuccess}
onError={handleOnError}
config={{ maxSlippage: defaultMaxSlippage || DEFAULT_MAX_SLIPPAGE }}
config={{
defaultMaxSlippage:
defaultMaxSlippage || FALLBACK_DEFAULT_MAX_SLIPPAGE,
}}
isSponsored={paymasters != null}
>
<SwapSettings>
Expand Down
4 changes: 2 additions & 2 deletions src/swap/components/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Children, useMemo } from 'react';
import { findComponent } from '../../internal/utils/findComponent';
import { background, cn, text } from '../../styles/theme';
import { useIsMounted } from '../../useIsMounted';
import { DEFAULT_MAX_SLIPPAGE } from '../constants';
import { FALLBACK_DEFAULT_MAX_SLIPPAGE } from '../constants';
import type { SwapReact } from '../types';
import { SwapAmountInput } from './SwapAmountInput';
import { SwapButton } from './SwapButton';
Expand All @@ -15,7 +15,7 @@ import { SwapToggleButton } from './SwapToggleButton';
export function Swap({
children,
config = {
maxSlippage: DEFAULT_MAX_SLIPPAGE,
defaultMaxSlippage: FALLBACK_DEFAULT_MAX_SLIPPAGE,
},
className,
experimental = { useAggregator: false },
Expand Down
8 changes: 4 additions & 4 deletions src/swap/components/SwapProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const wrapper = ({ children }) => (
<WagmiProvider config={accountConfig}>
<QueryClientProvider client={queryClient}>
<SwapProvider
config={{ maxSlippage: 5 }}
config={{ defaultMaxSlippage: 5 }}
experimental={{ useAggregator: true }}
>
{children}
Expand All @@ -114,7 +114,7 @@ const renderWithProviders = ({
onStatus = vi.fn(),
onSuccess = vi.fn(),
}) => {
const config = { maxSlippage: 10 };
const config = { defaultMaxSlippage: 10 };
const mockExperimental = { useAggregator: true };
return render(
<WagmiProvider config={accountConfig}>
Expand Down Expand Up @@ -813,12 +813,12 @@ describe('SwapProvider', () => {
});
});

it('should use default maxSlippage when not provided in experimental', () => {
it('should use default defaultMaxSlippage when not provided in experimental', () => {
const useTestHook = () => {
const { lifecycleStatus } = useSwapContext();
return lifecycleStatus;
};
const config = { maxSlippage: 3 };
const config = { defaultMaxSlippage: 3 };
const wrapper = ({ children }) => (
<WagmiProvider config={accountConfig}>
<QueryClientProvider client={queryClient}>
Expand Down
10 changes: 5 additions & 5 deletions src/swap/components/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { formatTokenAmount } from '../../internal/utils/formatTokenAmount';
import type { Token } from '../../token';
import { GENERIC_ERROR_MESSAGE } from '../../transaction/constants';
import { isUserRejectedRequestError } from '../../transaction/utils/isUserRejectedRequestError';
import { DEFAULT_MAX_SLIPPAGE } from '../constants';
import { FALLBACK_DEFAULT_MAX_SLIPPAGE } from '../constants';
import { useAwaitCalls } from '../hooks/useAwaitCalls';
import { useFromTo } from '../hooks/useFromTo';
import { useLifecycleStatus } from '../hooks/useLifecycleStatus';
Expand All @@ -41,7 +41,7 @@ export function useSwapContext() {
export function SwapProvider({
children,
config = {
maxSlippage: DEFAULT_MAX_SLIPPAGE,
defaultMaxSlippage: FALLBACK_DEFAULT_MAX_SLIPPAGE,
},
experimental,
isSponsored,
Expand All @@ -63,7 +63,7 @@ export function SwapProvider({
statusName: 'init',
statusData: {
isMissingRequiredField: true,
maxSlippage: config.maxSlippage,
maxSlippage: config.defaultMaxSlippage,
},
}); // Component lifecycle

Expand Down Expand Up @@ -143,12 +143,12 @@ export function SwapProvider({
statusName: 'init',
statusData: {
isMissingRequiredField: true,
maxSlippage: config.maxSlippage,
maxSlippage: config.defaultMaxSlippage,
},
});
}
}, [
config.maxSlippage,
config.defaultMaxSlippage,
hasHandledSuccess,
lifecycleStatus.statusName,
updateLifecycleStatus,
Expand Down
30 changes: 15 additions & 15 deletions src/swap/components/SwapSettingsSlippageInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { DEFAULT_MAX_SLIPPAGE } from '../constants';
import { FALLBACK_DEFAULT_MAX_SLIPPAGE } from '../constants';
import { SwapSettingsSlippageInput } from './SwapSettingsSlippageInput';

const mockSetLifecycleStatus = vi.fn();
let mockLifecycleStatus = {
statusName: 'init',
statusData: {
isMissingRequiredField: false,
maxSlippage: DEFAULT_MAX_SLIPPAGE,
maxSlippage: FALLBACK_DEFAULT_MAX_SLIPPAGE,
},
};

vi.mock('./SwapProvider', () => ({
useSwapContext: () => ({
updateLifecycleStatus: mockSetLifecycleStatus,
lifecycleStatus: mockLifecycleStatus,
config: { maxSlippage: DEFAULT_MAX_SLIPPAGE },
config: { defaultMaxSlippage: FALLBACK_DEFAULT_MAX_SLIPPAGE },
}),
}));

Expand All @@ -31,15 +31,15 @@ describe('SwapSettingsSlippageInput', () => {
statusName: 'init',
statusData: {
isMissingRequiredField: false,
maxSlippage: DEFAULT_MAX_SLIPPAGE,
maxSlippage: FALLBACK_DEFAULT_MAX_SLIPPAGE,
},
};
});

it('renders with default props', () => {
render(<SwapSettingsSlippageInput />);
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
expect(screen.getByText('%')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Auto' })).toBeInTheDocument();
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('SwapSettingsSlippageInput', () => {
fireEvent.click(screen.getByRole('button', { name: 'Custom' }));
fireEvent.change(screen.getByRole('textbox'), { target: { value: '2.5' } });
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
expect(mockSetLifecycleStatus).toHaveBeenCalledWith({
statusName: 'slippageChange',
Expand All @@ -87,7 +87,7 @@ describe('SwapSettingsSlippageInput', () => {
fireEvent.click(screen.getByRole('button', { name: 'Custom' }));
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'abc' } });
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
expect(mockSetLifecycleStatus).toHaveBeenCalledWith({
statusName: 'slippageChange',
Expand All @@ -104,7 +104,7 @@ describe('SwapSettingsSlippageInput', () => {
target: { value: '2.75' },
});
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
expect(mockSetLifecycleStatus).toHaveBeenCalledWith({
statusName: 'slippageChange',
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('SwapSettingsSlippageInput', () => {
fireEvent.click(screen.getByRole('button', { name: 'Custom' }));
fireEvent.change(screen.getByRole('textbox'), { target: { value: '' } });
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
expect(mockSetLifecycleStatus).toHaveBeenCalledWith({
statusName: 'slippageChange',
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('SwapSettingsSlippageInput', () => {
fireEvent.click(screen.getByRole('button', { name: 'Custom' }));
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'abc' } });
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
expect(mockSetLifecycleStatus).toHaveBeenCalledWith({
statusName: 'slippageChange',
Expand All @@ -202,7 +202,7 @@ describe('SwapSettingsSlippageInput', () => {
fireEvent.change(screen.getByRole('textbox'), { target: { value: '5' } });
fireEvent.click(screen.getByRole('button', { name: 'Auto' }));
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
expect(mockSetLifecycleStatus).toHaveBeenLastCalledWith({
statusName: 'slippageChange',
Expand All @@ -215,19 +215,19 @@ describe('SwapSettingsSlippageInput', () => {
it('maintains default value when switching between Auto and Custom mode', () => {
render(<SwapSettingsSlippageInput />);
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
fireEvent.click(screen.getByRole('button', { name: 'Custom' }));
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
fireEvent.change(screen.getByRole('textbox'), { target: { value: '5' } });
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
fireEvent.click(screen.getByRole('button', { name: 'Auto' }));
expect(screen.getByRole('textbox')).toHaveValue(
DEFAULT_MAX_SLIPPAGE.toString(),
FALLBACK_DEFAULT_MAX_SLIPPAGE.toString(),
);
});
});
2 changes: 1 addition & 1 deletion src/swap/components/SwapSettingsSlippageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function SwapSettingsSlippageInput({
className,
}: SwapSettingsSlippageInputReact) {
const {
config: { maxSlippage: defaultMaxSlippage },
config: { defaultMaxSlippage },
updateLifecycleStatus,
lifecycleStatus,
} = useSwapContext();
Expand Down
2 changes: 1 addition & 1 deletion src/swap/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const DEFAULT_MAX_SLIPPAGE = 3;
export const FALLBACK_DEFAULT_MAX_SLIPPAGE = 3;
export const GENERAL_SWAP_ERROR_CODE = 'SWAP_ERROR';
export const GENERAL_SWAP_QUOTE_ERROR_CODE = 'SWAP_QUOTE_ERROR';
export const GENERAL_SWAP_BALANCE_ERROR_CODE = 'SWAP_BALANCE_ERROR';
Expand Down
4 changes: 2 additions & 2 deletions src/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export type SwapButtonReact = {
};

type SwapConfig = {
maxSlippage: number; // Maximum acceptable slippage for a swap. (default: 10) This is as a percent, not basis points;
defaultMaxSlippage: number; // Maximum acceptable slippage for a swap. (default: 10) This is as a percent, not basis points;
};

export type SwapContextType = {
Expand Down Expand Up @@ -268,7 +268,7 @@ export type SwapParams = {
export type SwapProviderReact = {
children: React.ReactNode;
config?: {
maxSlippage: number; // Maximum acceptable slippage for a swap. (default: 10) This is as a percent, not basis points
defaultMaxSlippage: number; // Maximum acceptable slippage for a swap. (default: 10) This is as a percent, not basis points
};
experimental: {
useAggregator: boolean; // Whether to use a DEX aggregator. (default: true)
Expand Down