diff --git a/src/api/zaps/one-inch/OneInchSwapApi.ts b/src/api/zaps/one-inch/OneInchSwapApi.ts index 6e9773023..66f62b31e 100644 --- a/src/api/zaps/one-inch/OneInchSwapApi.ts +++ b/src/api/zaps/one-inch/OneInchSwapApi.ts @@ -10,11 +10,16 @@ import { } from './types'; export class OneInchSwapApi implements IOneInchSwapApi { - constructor(protected readonly baseUrl: string) {} + constructor(protected readonly baseUrl: string, protected readonly apiKey: string) {} protected buildUrl(path: string, request?: T) { - const params = request ? new URLSearchParams(request).toString() : ''; - return params ? `${this.baseUrl}${path}?${params}` : `${this.baseUrl}${path}`; + let queryString: string | undefined; + if (request) { + const params = new URLSearchParams(request); + queryString = params.toString(); + } + + return queryString ? `${this.baseUrl}${path}?${queryString}` : `${this.baseUrl}${path}`; } protected async get( @@ -25,6 +30,7 @@ export class OneInchSwapApi implements IOneInchSwapApi { const response = await fetch(url, { headers: { Accept: 'application/json', + Authorization: `Bearer ${this.apiKey}`, }, }); @@ -43,6 +49,7 @@ export class OneInchSwapApi implements IOneInchSwapApi { const response = await fetch(url, { headers: { Accept: 'application/json', + Authorization: `Bearer ${this.apiKey}`, }, }); @@ -58,20 +65,27 @@ export class OneInchSwapApi implements IOneInchSwapApi { return proxyResponse; } + protected addRequiredParams(request: T): T & { includeTokensInfo: boolean } { + return { + ...request, + includeTokensInfo: true, + }; + } + async getProxiedQuote(request: QuoteRequest): Promise { - return await this.transparentGet('/quote', request); + return await this.transparentGet('/quote', this.addRequiredParams(request)); } async getProxiedSwap(request: SwapRequest): Promise { - return await this.transparentGet('/swap', request); + return await this.transparentGet('/swap', this.addRequiredParams(request)); } async getQuote(request: QuoteRequest): Promise { - return await this.get('/quote', request); + return await this.get('/quote', this.addRequiredParams(request)); } async getSwap(request: SwapRequest): Promise { - return await this.get('/swap', request); + return await this.get('/swap', this.addRequiredParams(request)); } async isHealthy(): Promise { diff --git a/src/api/zaps/one-inch/RateLimitedOneInchSwapApi.ts b/src/api/zaps/one-inch/RateLimitedOneInchSwapApi.ts index 0c1579726..8188fbe75 100644 --- a/src/api/zaps/one-inch/RateLimitedOneInchSwapApi.ts +++ b/src/api/zaps/one-inch/RateLimitedOneInchSwapApi.ts @@ -1,9 +1,10 @@ import { OneInchSwapApi } from './OneInchSwapApi'; import PQueue from 'p-queue'; +import { ProxiedResponse } from './types.js'; export class RateLimitedOneInchSwapApi extends OneInchSwapApi { - constructor(baseUrl: string, protected readonly queue: PQueue) { - super(baseUrl); + constructor(baseUrl: string, apiKey: string, protected readonly queue: PQueue) { + super(baseUrl, apiKey); } protected async get( diff --git a/src/api/zaps/one-inch/index.ts b/src/api/zaps/one-inch/index.ts index da2d7e926..51daf58c7 100644 --- a/src/api/zaps/one-inch/index.ts +++ b/src/api/zaps/one-inch/index.ts @@ -4,24 +4,15 @@ import { AnyChain, ApiChain, toApiChain, toChainId } from '../../../utils/chain' import { OneInchPriceApi } from './OneInchPriceApi'; import { IOneInchPriceApi, IOneInchSwapApi } from './types'; -const DEFAULT_API_URL = 'https://api.1inch.io'; -const API_URL = process.env.ONE_INCH_API || DEFAULT_API_URL; -// With custom api endpoint we can do more requests per second -const API_QUEUE_CONFIG_CUSTOM = { - concurrency: 10, - intervalCap: 10, // 10 to 20 RPS as per 1inch for our private API - interval: 1000, -}; -// Default config is really slow at 1 per 2 seconds +// Configure to just under RPS allowed by our account const API_QUEUE_CONFIG = { - concurrency: 1, - intervalCap: 1, - interval: 2000, + concurrency: 1, // TODO change once we have enterprise account + intervalCap: 1, // TODO change once we have enterprise account + interval: 2000, // TODO change once we have enterprise account carryoverConcurrencyCount: true, autoStart: true, - timeout: 60 * 1000, + timeout: 30 * 1000, throwOnTimeout: true, - ...(API_URL === DEFAULT_API_URL ? {} : API_QUEUE_CONFIG_CUSTOM), }; const swapApiByChain: Partial> = {}; @@ -37,8 +28,12 @@ export function getOneInchSwapApi(chain: AnyChain): IOneInchSwapApi { } const chainId = toChainId(apiChain); - const baseUrl = `${API_URL}/v5.0/${chainId}`; - swapApiByChain[apiChain] = new RateLimitedOneInchSwapApi(baseUrl, swapApiQueue); + const baseUrl = `https://api.1inch.dev/swap/v5.2/${chainId}`; + const apiKey = process.env.ONE_INCH_API_KEY; + if (!apiKey) { + throw new Error(`ONE_INCH_API_KEY env variable is not set`); + } + swapApiByChain[apiChain] = new RateLimitedOneInchSwapApi(baseUrl, apiKey, swapApiQueue); } return swapApiByChain[apiChain]; diff --git a/src/api/zaps/one-inch/types.ts b/src/api/zaps/one-inch/types.ts index 7044ead7d..67af8667e 100644 --- a/src/api/zaps/one-inch/types.ts +++ b/src/api/zaps/one-inch/types.ts @@ -6,45 +6,35 @@ export type HealthCheckResponse = { }; export type QuoteRequest = { - fromTokenAddress: string; - toTokenAddress: string; + src: string; + dst: string; amount: string; fee?: string; }; export type QuoteToken = { address: string; + symbol: string; + name: string; decimals: number; logoURI: string; - name: string; - symbol: string; tags: string[]; }; -export type QuoteRoutePart = { - fromTokenAddress: string; - name: string; - part: number; - toTokenAddress: string; -}; - export type QuoteResponse = { - estimatedGas: number; fromToken: QuoteToken; - fromTokenAmount: string; toToken: QuoteToken; - toTokenAmount: string; - protocols: QuoteRoutePart[][][]; + toAmount: string; }; export type SwapRequest = { - fromTokenAddress: string; - toTokenAddress: string; + src: string; + dst: string; amount: string; - fromAddress: string; + from: string; slippage: number; fee?: string; - referrerAddress?: string; + referrer?: string; disableEstimate?: boolean; }; @@ -59,10 +49,8 @@ export type SwapTx = { export type SwapResponse = { fromToken: QuoteToken; - fromTokenAmount: string; toToken: QuoteToken; - toTokenAmount: string; - protocols: string[]; + toAmount: string; tx: SwapTx; }; diff --git a/src/api/zaps/zaps.ts b/src/api/zaps/zaps.ts index 4c84d92d2..7ec2c64c6 100644 --- a/src/api/zaps/zaps.ts +++ b/src/api/zaps/zaps.ts @@ -494,8 +494,8 @@ async function getCanSwapOnOneInch( // Don't quote wnative:wnative const calls = tokensExcludingWnative.map(token => api.getQuote({ - fromTokenAddress: token.address, - toTokenAddress: wnative.address, + src: token.address, + dst: wnative.address, amount: amountInWei, }) );