@sega-so/sega-sdk
Version:
An SDK for building applications on top of SEGA.
209 lines (173 loc) • 5.87 kB
text/typescript
import axios, { AxiosInstance } from "axios";
import { createLogger, sleep } from "../common";
import { Cluster } from "../solana";
import {
ApiCpmmConfigInfo,
ApiV3Token,
AvailabilityCheckAPI3,
PoolKeys,
} from "./type";
import { API_URLS, API_URL_CONFIG } from "./url";
import { updateReqHistory } from "./utils";
import { PublicKey } from "@solana/web3.js";
const logger = createLogger("Sega_Api");
const poolKeysCache: Map<string, PoolKeys> = new Map();
export async function endlessRetry<T>(name: string, call: () => Promise<T>, interval = 1000): Promise<T> {
let result: T | undefined;
while (result == undefined) {
try {
logger.debug(`Request ${name} through endlessRetry`);
result = await call();
} catch (err) {
logger.error(`Request ${name} failed, retry after ${interval} ms`, err);
await sleep(interval);
}
}
return result;
}
export interface ApiProps {
cluster: Cluster;
timeout: number;
logRequests?: boolean;
logCount?: number;
urlConfigs?: API_URL_CONFIG;
}
export class Api {
public cluster: Cluster;
public api: AxiosInstance;
public logCount: number;
public urlConfigs: API_URL_CONFIG;
constructor({ cluster, timeout, logRequests, logCount, urlConfigs }: ApiProps) {
this.cluster = cluster;
this.urlConfigs = urlConfigs || {};
this.logCount = logCount || 1000;
this.api = axios.create({ baseURL: this.urlConfigs.BASE_HOST || API_URLS.BASE_HOST, timeout });
this.api.interceptors.request.use(
(config) => {
// before request
const { method, baseURL, url } = config;
logger.debug(`${method?.toUpperCase()} ${baseURL}${url}`);
return config;
},
(error) => {
// request error
logger.error(`Request failed`);
return Promise.reject(error);
},
);
this.api.interceptors.response.use(
(response) => {
// 2xx
const { config, data, status } = response;
const { method, baseURL, url } = config;
if (logRequests) {
updateReqHistory({
status,
url: `${baseURL}${url}`,
params: config.params,
data,
logCount: this.logCount,
});
}
logger.debug(`${method?.toUpperCase()} ${baseURL}${url} ${status}`);
return data;
},
(error) => {
// https://axios-http.com/docs/handling_errors
// not 2xx
const { config, response = {} } = error;
const { status } = response;
const { method, baseURL, url } = config;
if (logRequests) {
updateReqHistory({
status,
url: `${baseURL}${url}`,
params: config.params,
data: error.message,
logCount: this.logCount,
});
}
logger.error(`${method.toUpperCase()} ${baseURL}${url} ${status || error.message}`);
return Promise.reject(error);
},
);
}
async getCpmmConfigs(): Promise<ApiCpmmConfigInfo[]> {
const res = await this.api.get(this.urlConfigs.CPMM_CONFIG || API_URLS.CPMM_CONFIG);
return res.data;
}
async getBlockSlotCountForSecond(endpointUrl?: string): Promise<number> {
if (!endpointUrl) return 2;
const res: {
id: string;
jsonrpc: string;
result: { numSlots: number; numTransactions: number; samplePeriodSecs: number; slot: number }[];
} = await axios.post(endpointUrl, {
id: "getRecentPerformanceSamples",
jsonrpc: "2.0",
method: "getRecentPerformanceSamples",
params: [4],
});
const slotList = res.result.map((data) => data.numSlots);
return slotList.reduce((a, b) => a + b, 0) / slotList.length / 60;
}
async getChainTimeOffset(): Promise<{ offset: number }> {
const res = await this.api.get(this.urlConfigs.CHAIN_TIME || API_URLS.CHAIN_TIME);
return res.data;
}
async fetchPoolKeysById(props: { idList: string[] }): Promise<PoolKeys[]> {
const { idList } = props;
const cacheList: PoolKeys[] = [];
const readyList = idList.filter((poolId) => {
if (poolKeysCache.has(poolId)) {
cacheList.push(poolKeysCache.get(poolId)!);
return false;
}
return true;
});
let data: PoolKeys[] = [];
if (readyList.length) {
const res = await this.api.get<PoolKeys[]>(
(this.urlConfigs.POOL_KEY_BY_ID || API_URLS.POOL_KEY_BY_ID) + `?ids=${readyList.join(",")}`,
);
data = res.data.filter(Boolean);
data.forEach((poolKey) => {
poolKeysCache.set(poolKey.id, poolKey);
});
}
return cacheList.concat(data);
}
async getRpcs(): Promise<{
rpcs: { batch: boolean; name: string; url: string; weight: number }[];
strategy: string;
}> {
return this.api.get(this.urlConfigs.RPCS || API_URLS.RPCS);
}
async getTokenList(): Promise<{ mintList: ApiV3Token[]; blacklist: string[]; whiteList: string[] }> {
const res = await this.api.get(this.urlConfigs.TOKEN_LIST || API_URLS.TOKEN_LIST);
return res.data;
}
async getJupTokenList(): Promise<
(ApiV3Token & {
daily_volume: number;
freeze_authority: string | null;
mint_authority: string | null;
})[]
> {
return this.api.get("", {
baseURL: this.urlConfigs.JUP_TOKEN_LIST || API_URLS.JUP_TOKEN_LIST,
});
}
async getTokenInfo(mint: (string | PublicKey)[]): Promise<ApiV3Token[]> {
const res = await this.api.get(
(this.urlConfigs.MINT_INFO_ID || API_URLS.MINT_INFO_ID) + `?mints=${mint.map((m) => m.toString()).join(",")}`,
);
return res.data;
}
async fetchAvailabilityStatus(): Promise<AvailabilityCheckAPI3> {
const res = await this.api.get<AvailabilityCheckAPI3>(
this.urlConfigs.CHECK_AVAILABILITY || API_URLS.CHECK_AVAILABILITY,
);
return res.data;
}
}