feat: added two methods for tickers

This commit is contained in:
Mikhail Gladchenko
2024-03-29 09:55:25 +00:00
parent 57474d2872
commit bfa4cc3d6f
5 changed files with 73 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.20.79-rc0",
"version": "0.20.79-rc1",
"description": "Orion Protocol SDK",
"main": "./lib/index.cjs",
"module": "./lib/index.js",

View File

@@ -1,5 +1,6 @@
import { fetchWithValidation } from 'simple-typed-fetch';
import { searchTickersSchema } from './schemas';
import { tickersSchema } from './schemas';
import type { tickersBaseSearchParams, tickersCategories } from '../../types';
export class Frontage {
private readonly apiUrl: string;
@@ -8,11 +9,56 @@ export class Frontage {
this.apiUrl = apiUrl;
this.searchTickers = this.searchTickers.bind(this);
this.getTickers = this.getTickers.bind(this);
}
searchTickers = () => {
return fetchWithValidation(`${this.apiUrl}/api/v1/tickers/search`,
searchTickersSchema
searchTickers = ({
searchValue,
currentNetwork,
targetNetwork,
sortBy,
sortType,
offset,
limit,
}: { searchValue: string } & tickersBaseSearchParams) => {
const queryParams = [
`searchValue=${encodeURIComponent(searchValue)}`,
currentNetwork !== undefined ? `&currentNetwork=${encodeURIComponent(currentNetwork)}` : '',
targetNetwork !== undefined ? `&targetNetwork=${encodeURIComponent(targetNetwork)}` : '',
sortBy !== undefined ? `&sortBy=${encodeURIComponent(sortBy)}` : '',
sortType !== undefined ? `&sortType=${encodeURIComponent(sortType)}` : '',
offset !== undefined ? `&offset=${offset}` : '',
limit !== undefined ? `&limit=${limit}` : '',
].filter(Boolean).join('&');
return fetchWithValidation(
`${this.apiUrl}/api/v1/tickers/search?${queryParams}`,
tickersSchema
);
};
getTickers = ({
category,
currentNetwork,
targetNetwork,
sortBy,
sortType,
offset,
limit,
}: { category: tickersCategories } & tickersBaseSearchParams) => {
const queryParams = [
`category=${encodeURIComponent(category)}`,
currentNetwork !== undefined ? `&currentNetwork=${encodeURIComponent(currentNetwork)}` : '',
targetNetwork !== undefined ? `&targetNetwork=${encodeURIComponent(targetNetwork)}` : '',
sortBy !== undefined ? `&sortBy=${encodeURIComponent(sortBy)}` : '',
sortType !== undefined ? `&sortType=${encodeURIComponent(sortType)}` : '',
offset !== undefined ? `&offset=${offset}` : '',
limit !== undefined ? `&limit=${limit}` : '',
].filter(Boolean).join('&');
return fetchWithValidation(
`/api/v1/tickers/get?${queryParams}`,
tickersSchema
);
};
}

View File

@@ -1 +1 @@
export * from './search-tickers-schema';
export * from './tickers-schema';

View File

@@ -1,6 +1,6 @@
import { z } from 'zod';
export const searchTickersSchema = z.array(z.object({
export const tickersSchema = z.array(z.object({
pair: z.string(),
volume24: z.number(),
change24: z.number(),

View File

@@ -3,8 +3,9 @@ import type factories from './constants/factories.js';
import type { BigNumber } from 'bignumber.js';
import type subOrderStatuses from './constants/subOrderStatuses.js';
import type positionStatuses from './constants/positionStatuses.js';
import type { knownEnvs } from './config/schemas/index.js';
import type { knownEnvs } from './config/schemas';
import type getHistory from './Orion/bridge/getHistory.js';
import type uppercasedNetworkCodes from './constants/uppercasedNetworkCodes';
export type DeepPartial<T> = T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>;
@@ -465,3 +466,21 @@ export type AtomicSwap = Partial<
}
export type OrderSource = 'TERMINAL_MARKET' | 'TERMINAL_LIMIT' | 'SWAP_UI' | 'WIDGET';
// Frontage
export type NetworkCode = typeof uppercasedNetworkCodes[number];
export type tickersCategories = 'USD' | 'ORN' | 'BNB' | 'ALTS';
export type tickersSortBy = 'PRICE' | 'CHANGE' | 'VOLUME';
export type tickersSortType = 'ASCENDING' | 'DESCENDING';
export type tickersBaseSearchParams = {
currentNetwork?: NetworkCode
targetNetwork?: NetworkCode
sortBy?: tickersSortBy
sortType?: tickersSortType
offset?: number
limit?: number
}