diff --git a/src/entities/trades/OneInch/OneInch.ts b/src/entities/trades/OneInch/OneInch.ts index 8dd18ee5..b91d9bca 100644 --- a/src/entities/trades/OneInch/OneInch.ts +++ b/src/entities/trades/OneInch/OneInch.ts @@ -1,4 +1,3 @@ -import { BigNumber } from '@ethersproject/bignumber' import { BaseProvider } from '@ethersproject/providers' import { UnsignedTransaction } from '@ethersproject/transactions' import invariant from 'tiny-invariant' @@ -32,7 +31,6 @@ interface OneInchConstructorParams { tradeType: TradeType chainId: ChainId approveAddress: string - estimatedGas: BigNumber } /** @@ -46,7 +44,6 @@ export class OneInchTrade extends Trade { tradeType, chainId, approveAddress, - estimatedGas, }: OneInchConstructorParams) { super({ details: undefined, @@ -65,13 +62,12 @@ export class OneInchTrade extends Trade { priceImpact: new Percent('0', '100'), fee: new Percent('0', '10000'), approveAddress, - estimatedGas, }) } static async getQuote( { amount, quoteCurrency, tradeType, maximumSlippage = defaultMaximumSlippage }: OneInchQuoteTypes, - provider?: BaseProvider + provider?: BaseProvider, ): Promise { const chainId = tryGetChainId(amount, quoteCurrency) @@ -84,7 +80,7 @@ export class OneInchTrade extends Trade { // Ensure the provider's chainId matches the provided currencies invariant( (await provider.getNetwork()).chainId == chainId, - `OneInch.getQuote: currencies chainId does not match provider's chainId` + `OneInch.getQuote: currencies chainId does not match provider's chainId`, ) const currencyIn = amount.currency @@ -92,7 +88,6 @@ export class OneInchTrade extends Trade { // Ensure that the currencies are present invariant(currencyIn.address && currencyOut.address, `getQuote: Currency address is required`) - let gas try { //Fetch approve address @@ -105,32 +100,24 @@ export class OneInchTrade extends Trade { amount: amount.raw.toString(), } - // Fetch the quote from the API - const { fromTokenAmount, toTokenAmount, estimatedGas } = await ( + const { toAmount } = await ( await fetch(generateApiRequestUrl({ methodName: RequestType.QUOTE, queryParams, chainId })) ).json() - gas = estimatedGas - let fromTokenAmountApi = fromTokenAmount - let toTokenAmountApi = toTokenAmount - + let toTokenAmountApi = toAmount + let fromTokenAmountApi = amount.raw.toString() if (tradeType === TradeType.EXACT_OUTPUT) { // Prepare the query parameters for the API request - const queryParams = { fromTokenAddress: currencyOut.address, toTokenAddress: currencyIn.address, - amount: toTokenAmount.toString(), + amount: toAmount.toString(), } - const { - toTokenAmount: toTokenAmountOutput, - fromTokenAmount, - estimatedGas, - } = await (await fetch(generateApiRequestUrl({ methodName: RequestType.QUOTE, queryParams, chainId }))).json() + const { toAmount: toTokenAmountOutput } = await ( + await fetch(generateApiRequestUrl({ methodName: RequestType.QUOTE, queryParams, chainId })) + ).json() - fromTokenAmountApi = fromTokenAmount toTokenAmountApi = toTokenAmountOutput - gas = estimatedGas } const currencyInType = tradeType === TradeType.EXACT_INPUT ? currencyIn : currencyOut @@ -150,7 +137,6 @@ export class OneInchTrade extends Trade { tradeType, chainId, approveAddress, - estimatedGas: BigNumber.from(gas), }) } catch (error) { console.error('OneInch.getQuote: Error fetching the quote:', error.message) @@ -192,7 +178,7 @@ export class OneInchTrade extends Trade { public async swapTransaction(options: ExtentendedTradeOptions): Promise { invariant( this.inputAmount.currency.address && this.outputAmount.currency.address, - 'OneInchTrade: Currency address is required' + 'OneInchTrade: Currency address is required', ) const queryParams = { diff --git a/src/entities/trades/OneInch/api.ts b/src/entities/trades/OneInch/api.ts index 30f31c3e..bf61fe26 100644 --- a/src/entities/trades/OneInch/api.ts +++ b/src/entities/trades/OneInch/api.ts @@ -1,32 +1,66 @@ import { ChainId } from '../../../constants' import { REFFERER_ADDRESS_CHAIN_MAPPING } from '../constants' -const apiBaseUrl = (chainId: number) => 'https://api.1inch.io/v5.0/' + chainId - export enum RequestType { QUOTE = '/quote', SWAP = '/swap', } +enum ApiName { + FUSION = '/fusion', + SPOT_PRICE = '/price', + SWAP = '/swap', +} + +enum SubApiName { + ORDERS = '/orders', + QUOTER = '/quoter', + RELAYER = '/relayer', +} + +enum ApiVersion { + FUSION = '/v1.0', + SPOT_PRICE = '/v1.1', + SWAP = '/v5.2', +} + interface ApiRequestUrlParams { methodName: RequestType queryParams: Record chainId: ChainId } -//0.1% fee here is link to api https://docs.1inch.io/docs/aggregation-protocol/api/swap-params +interface ApiUrlConfig { + apiName: ApiName + apiVersion: ApiVersion + chainId: number + subApiName?: SubApiName | '' +} +/** + * @see https://portal.1inch.dev/documentation/swap/introduction + */ + +const API_BASE_URL = process.env.REACT_APP_ONEINCH_BASE_API_URL ?? 'https://api.1inch.dev/' const ONE_INCH_REFFERER_FEE = '0' //MIN-> 0 MAX-> 3 +const getApiUrl = ({ apiName, apiVersion, chainId, subApiName = '' }: ApiUrlConfig) => + `${API_BASE_URL}${apiName}${subApiName}${apiVersion}/${chainId}` + export function generateApiRequestUrl({ methodName, queryParams, chainId }: ApiRequestUrlParams) { if (REFFERER_ADDRESS_CHAIN_MAPPING[chainId]) { queryParams.referrerAddress = REFFERER_ADDRESS_CHAIN_MAPPING[chainId] ?? '' queryParams.fee = ONE_INCH_REFFERER_FEE } - return apiBaseUrl(chainId) + methodName + '?' + new URLSearchParams(queryParams).toString() + return ( + getApiUrl({ apiName: ApiName.SWAP, apiVersion: ApiVersion.SWAP, chainId }) + + methodName + + '?' + + new URLSearchParams(queryParams).toString() + ) } export function approveAddressUrl(chainId: ChainId) { - return apiBaseUrl(chainId) + '/approve/spender' + return getApiUrl({ apiName: ApiName.SWAP, apiVersion: ApiVersion.SWAP, chainId }) + '/approve/spender' }