Skip to content

Commit

Permalink
fix: deprecate v1 swap amountReference to
Browse files Browse the repository at this point in the history
  • Loading branch information
alessey committed Jan 22, 2025
1 parent bbaceed commit 37921d4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/core/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 type { SwapAPIResponse } from '../../swap/types';
import { getSwapErrorCode } from '../../swap/utils/getSwapErrorCode';
import { CDP_GET_SWAP_TRADE } from '../network/definitions/swap';
Expand All @@ -18,7 +20,7 @@ export async function buildSwapTransaction(
): Promise<BuildSwapTransactionResponse> {
// Default parameters
const defaultParams = {
amountReference: 'from',
amountReference: 'from' as BuildSwapTransactionParams['amountReference'],
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
12 changes: 11 additions & 1 deletion src/core/api/getSwapQuote.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SwapMessage, UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE } from '@/swap/constants';
import type { SwapQuote } from '../../swap/types';
import { getSwapErrorCode } from '../../swap/utils/getSwapErrorCode';
import { CDP_GET_SWAP_QUOTE } from '../network/definitions/swap';
Expand All @@ -17,7 +18,7 @@ export async function getSwapQuote(
): Promise<GetSwapQuoteResponse> {
// Default parameters
const defaultParams = {
amountReference: 'from',
amountReference: 'from' as GetSwapQuoteParams['amountReference'],
isAmountInDecimals: false,
};
let apiParams = getAPIParamsForToken({
Expand All @@ -28,6 +29,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/core/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,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 @@ -93,7 +93,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
2 changes: 2 additions & 0 deletions src/swap/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const UNCAUGHT_SWAP_ERROR_CODE = 'UNCAUGHT_SWAP_ERROR';
export const UNIVERSALROUTER_CONTRACT_ADDRESS =
'0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD';
export const USER_REJECTED_ERROR_CODE = 'USER_REJECTED';
export const UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE = 'UNSUPPORTED_AMOUNT_REFERENCE_ERROR';
export enum SwapMessage {
BALANCE_ERROR = 'Error fetching token balance',
CONFIRM_IN_WALLET = 'Confirm in wallet',
Expand All @@ -22,6 +23,7 @@ export enum SwapMessage {
SWAP_IN_PROGRESS = 'Swap in progress...',
TOO_MANY_REQUESTS = 'Too many requests. Please try again later.',
USER_REJECTED = 'User rejected the transaction',
UNSUPPORTED_AMOUNT_REFERENCE = 'useAggregator does not support amountReference: to, please use useAggregator: false',
}

export const ONRAMP_PAYMENT_METHODS = [
Expand Down

0 comments on commit 37921d4

Please sign in to comment.