Skip to content

Commit

Permalink
fix: deprecate v1 swap amountReference to (#1851)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessey authored Jan 23, 2025
1 parent b009e22 commit 2f48a95
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 70 deletions.
38 changes: 29 additions & 9 deletions src/api/buildSwapTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { CDP_GET_SWAP_TRADE } from '@/core/network/definitions/swap';
import { sendRequest } from '@/core/network/request';
import { SwapMessage } from '@/swap/constants';
import { UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE } from '@/swap/constants';
import { DEGEN_TOKEN, ETH_TOKEN } from '@/swap/mocks';
import type { Address } from 'viem';
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { buildSwapTransaction } from './buildSwapTransaction';
import type { BuildSwapTransaction } from './types';
Expand All @@ -12,9 +15,9 @@ import { getSwapTransaction } from './utils/getSwapTransaction';

vi.mock('@/core/network/request');

const testFromAddress = '0x6Cd01c0F55ce9E0Bf78f5E90f72b4345b16d515d';
const testFromAddress: Address = '0x6Cd01c0F55ce9E0Bf78f5E90f72b4345b16d515d';
const testAmount = '3305894409732200';
const testAmountReference = 'from';
const testAmountReference = 'from' as const;

describe('buildSwapTransaction', () => {
beforeEach(() => {
Expand All @@ -24,7 +27,7 @@ describe('buildSwapTransaction', () => {
it('should return a swap', async () => {
const mockParams = {
useAggregator: true,
fromAddress: testFromAddress as `0x${string}`,
fromAddress: testFromAddress,
amountReference: testAmountReference,
from: ETH_TOKEN,
to: DEGEN_TOKEN,
Expand Down Expand Up @@ -120,7 +123,7 @@ describe('buildSwapTransaction', () => {
it('should return a swap with useAggregator=false', async () => {
const mockParams = {
useAggregator: false,
fromAddress: testFromAddress as `0x${string}`,
fromAddress: testFromAddress,
amountReference: testAmountReference,
from: ETH_TOKEN,
to: DEGEN_TOKEN,
Expand Down Expand Up @@ -199,11 +202,28 @@ describe('buildSwapTransaction', () => {
]);
});

it('should return an error for an unsupported amount reference', async () => {
const mockParams = {
useAggregator: true,
fromAddress: testFromAddress,
amountReference: 'to' as const,
from: ETH_TOKEN,
to: DEGEN_TOKEN,
amount: testAmount,
};
const error = await buildSwapTransaction(mockParams);
expect(error).toEqual({
code: UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE,
error: SwapMessage.UNSUPPORTED_AMOUNT_REFERENCE,
message: '',
});
});

it('should return a swap with an approve transaction', async () => {
const mockParams = {
useAggregator: true,
maxSlippage: '3',
fromAddress: testFromAddress as `0x${string}`,
fromAddress: testFromAddress,
amountReference: testAmountReference,
from: DEGEN_TOKEN,
to: ETH_TOKEN,
Expand Down Expand Up @@ -292,7 +312,7 @@ describe('buildSwapTransaction', () => {
it('should return an error if sendRequest fails', async () => {
const mockParams = {
useAggregator: true,
fromAddress: testFromAddress as `0x${string}`,
fromAddress: testFromAddress,
amountReference: testAmountReference,
from: ETH_TOKEN,
to: DEGEN_TOKEN,
Expand All @@ -318,7 +338,7 @@ describe('buildSwapTransaction', () => {
it('should return an error object from buildSwapTransaction', async () => {
const mockParams = {
useAggregator: true,
fromAddress: testFromAddress as `0x${string}`,
fromAddress: testFromAddress,
amountReference: testAmountReference,
from: ETH_TOKEN,
to: DEGEN_TOKEN,
Expand Down Expand Up @@ -349,7 +369,7 @@ describe('buildSwapTransaction', () => {
it('should return an error object from buildSwapTransaction for invalid `amount` input', async () => {
const mockParams = {
useAggregator: true,
fromAddress: testFromAddress as `0x${string}`,
fromAddress: testFromAddress,
amountReference: testAmountReference,
from: ETH_TOKEN,
to: DEGEN_TOKEN,
Expand All @@ -368,7 +388,7 @@ describe('buildSwapTransaction', () => {
const mockParams = {
useAggregator: true,
maxSlippage: '3',
fromAddress: testFromAddress as `0x${string}`,
fromAddress: testFromAddress,
amountReference: testAmountReference,
from: ETH_TOKEN,
to: DEGEN_TOKEN,
Expand Down
13 changes: 12 additions & 1 deletion src/api/buildSwapTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SwapMessage } from '@/swap/constants';
import { UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE } from '@/swap/constants';
import { CDP_GET_SWAP_TRADE } from '../core/network/definitions/swap';
import { sendRequest } from '../core/network/request';
import type { SwapAPIResponse } from '../swap/types';
Expand All @@ -18,7 +20,7 @@ export async function buildSwapTransaction(
): Promise<BuildSwapTransactionResponse> {
// Default parameters
const defaultParams = {
amountReference: 'from',
amountReference: 'from' as const,
isAmountInDecimals: false,
};

Expand All @@ -30,6 +32,15 @@ export async function buildSwapTransaction(
return apiParams;
}

if (params.useAggregator && params.amountReference === 'to') {
console.error(SwapMessage.UNSUPPORTED_AMOUNT_REFERENCE);
return {
code: UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE,
error: SwapMessage.UNSUPPORTED_AMOUNT_REFERENCE,
message: '',
};
}

if (!params.useAggregator) {
apiParams = {
v2Enabled: true,
Expand Down
20 changes: 19 additions & 1 deletion src/api/getSwapQuote.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CDP_GET_SWAP_QUOTE } from '@/core/network/definitions/swap';
import { sendRequest } from '@/core/network/request';
import { SwapMessage } from '@/swap/constants';
import { UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE } from '@/swap/constants';
import { type Mock, afterEach, describe, expect, it, vi } from 'vitest';
import { DEGEN_TOKEN, ETH_TOKEN } from '../swap/mocks';
/**
Expand All @@ -11,7 +13,7 @@ import { getAPIParamsForToken } from './utils/getAPIParamsForToken';
vi.mock('@/core/network/request');

const testAmount = '3305894409732200';
const testAmountReference = 'from';
const testAmountReference = 'from' as const;

describe('getSwapQuote', () => {
afterEach(() => {
Expand Down Expand Up @@ -91,6 +93,22 @@ describe('getSwapQuote', () => {
]);
});

it('should return an error for an unsupported amount reference', async () => {
const mockParams = {
useAggregator: true,
amountReference: 'to' as const,
from: ETH_TOKEN,
to: DEGEN_TOKEN,
amount: testAmount,
};
const error = await getSwapQuote(mockParams);
expect(error).toEqual({
code: UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE,
error: SwapMessage.UNSUPPORTED_AMOUNT_REFERENCE,
message: '',
});
});

it('should return an error if sendRequest fails', async () => {
const mockParams = {
useAggregator: true,
Expand Down
13 changes: 12 additions & 1 deletion src/api/getSwapQuote.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SwapMessage } from '@/swap/constants';
import { UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE } from '@/swap/constants';
import { CDP_GET_SWAP_QUOTE } from '../core/network/definitions/swap';
import { sendRequest } from '../core/network/request';
import type { SwapQuote } from '../swap/types';
Expand All @@ -17,7 +19,7 @@ export async function getSwapQuote(
): Promise<GetSwapQuoteResponse> {
// Default parameters
const defaultParams = {
amountReference: 'from',
amountReference: 'from' as const,
isAmountInDecimals: false,
};
let apiParams = getAPIParamsForToken({
Expand All @@ -28,6 +30,15 @@ export async function getSwapQuote(
return apiParams;
}

if (params.useAggregator && params.amountReference === 'to') {
console.error(SwapMessage.UNSUPPORTED_AMOUNT_REFERENCE);
return {
code: UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE,
error: SwapMessage.UNSUPPORTED_AMOUNT_REFERENCE,
message: '',
};
}

if (!params.useAggregator) {
apiParams = {
v2Enabled: true,
Expand Down
4 changes: 2 additions & 2 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type GetAPIParamsForToken =

export type GetQuoteAPIParams = {
amount: string; // The amount to be swapped
amountReference?: string; // The reference amount for the swap
amountReference?: 'to' | 'from'; // The reference amount for the swap, 'to' is only supported with v2Enabled: false
from: AddressOrETH | ''; // The source address or 'ETH' for Ethereum
to: AddressOrETH | ''; // The destination address or 'ETH' for Ethereum
v2Enabled?: boolean; // Whether to use V2 of the API (default: false)
Expand All @@ -88,7 +88,7 @@ export type GetSwapAPIParams = GetQuoteAPIParams & {
*/
export type GetSwapQuoteParams = {
amount: string; // The amount to be swapped
amountReference?: string; // The reference amount for the swap
amountReference?: 'to' | 'from'; // The reference amount for the swap, 'to' is only supported with v2Enabled: false
from: Token; // The source token for the swap
isAmountInDecimals?: boolean; // Whether the amount is in decimals
maxSlippage?: string; // The slippage of the swap
Expand Down
1 change: 1 addition & 0 deletions src/buy/components/BuyAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function BuyAmountInput() {
)}
placeholder="0.0"
delayMs={1000}
inputMode="decimal"
value={formatAmount(to.amount)}
setValue={to.setAmount}
disabled={to.loading}
Expand Down
28 changes: 0 additions & 28 deletions src/buy/components/BuyProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,6 @@ describe('BuyProvider', () => {
expect.objectContaining({
maxSlippage: '10',
amount: '5',
amountReference: 'to',
from: ethToken,
to: degenToken,
useAggregator: true,
Expand All @@ -720,33 +719,6 @@ describe('BuyProvider', () => {
});
});

it('should pass the correct amountReference to get', async () => {
const TestComponent = () => {
const { handleAmountChange } = useBuyContext();
// biome-ignore lint: hello
React.useEffect(() => {
const initializeSwap = () => {
handleAmountChange('100');
};
initializeSwap();
}, []);
return null;
};
await act(async () => {
renderWithProviders({ Component: TestComponent });
});
expect(getBuyQuote).toHaveBeenCalledWith(
expect.objectContaining({
maxSlippage: '10',
amount: '100',
amountReference: 'to',
from: ethToken,
to: degenToken,
useAggregator: true,
}),
);
});

it('should handle undefined in input', async () => {
const TestComponent = () => {
const { handleAmountChange } = useBuyContext();
Expand Down
3 changes: 0 additions & 3 deletions src/buy/components/BuyProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ export function BuyProvider({
formattedFromAmount: formattedAmountETH,
} = await getBuyQuote({
amount,
amountReference: 'to',
from: fromETH.token,
maxSlippage: String(maxSlippage),
to: to.token,
Expand All @@ -269,7 +268,6 @@ export function BuyProvider({
formattedFromAmount: formattedAmountUSDC,
} = await getBuyQuote({
amount,
amountReference: 'to',
from: fromUSDC.token,
maxSlippage: String(maxSlippage),
to: to.token,
Expand All @@ -282,7 +280,6 @@ export function BuyProvider({
formattedFromAmount: formattedAmountFrom,
} = await getBuyQuote({
amount,
amountReference: 'to',
from: from?.token,
maxSlippage: String(maxSlippage),
to: to.token,
Expand Down
Loading

0 comments on commit 2f48a95

Please sign in to comment.