Added new Orion Aggregator methods

This commit is contained in:
Aleksandr Kraiz
2022-05-10 23:04:52 +04:00
parent af9168e3aa
commit 545caa77e9
3 changed files with 58 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.5.3",
"version": "0.5.4",
"description": "Orion Protocol SDK",
"main": "./lib/esm/index.js",
"module": "./lib/esm/index.js",

View File

@@ -11,6 +11,9 @@ import { OrionAggregatorWS } from './ws';
import { atomicSwapHistorySchema } from './schemas/atomicSwapHistorySchema';
import { SignedCancelOrderRequest, SignedOrder, SupportedChainId } from '../../types';
import { pairConfigSchema } from './schemas';
import {
aggregatedOrderbookSchema, exchangeOrderbookSchema,
} from './schemas/aggregatedOrderbookSchema';
class OrionAggregator {
private readonly apiUrl: string;
@@ -32,6 +35,8 @@ class OrionAggregator {
this.cancelOrder = this.cancelOrder.bind(this);
this.checkWhitelisted = this.checkWhitelisted.bind(this);
this.getLockedBalance = this.getLockedBalance.bind(this);
this.getAggregatedOrderbook = this.getAggregatedOrderbook.bind(this);
this.getExchangeOrderbook = this.getExchangeOrderbook.bind(this);
}
get aggregatorWSUrl() { return `wss://${this.apiUrl}/v1`; }
@@ -47,6 +52,33 @@ class OrionAggregator {
);
}
getAggregatedOrderbook(pair: string, depth = 20) {
const url = new URL(`${this.aggregatorUrl}/api/v1/orderbook`);
url.searchParams.append('pair', pair);
url.searchParams.append('depth', depth.toString());
return fetchWithValidation(
url.toString(),
aggregatedOrderbookSchema,
undefined,
errorSchema,
);
}
getExchangeOrderbook(pair: string, exchange: string, depth = 20, filterByBrokerBalances: boolean | null = null) {
const url = new URL(`${this.aggregatorUrl}/api/v1/orderbook/${exchange}/${pair}`);
url.searchParams.append('pair', pair);
url.searchParams.append('depth', depth.toString());
if (filterByBrokerBalances !== null) {
url.searchParams.append('filterByBrokerBalances', filterByBrokerBalances.toString());
}
return fetchWithValidation(
url.toString(),
exchangeOrderbookSchema,
undefined,
errorSchema,
);
}
getPairConfigs() {
return fetchWithValidation(
`${this.aggregatorUrl}/api/v1/pairs/exchangeInfo`,

View File

@@ -0,0 +1,25 @@
import { z } from 'zod';
const orderbookElementSchema = z.object({
price: z.number(),
amount: z.number(),
path: z.array(z.object({
assetPair: z.string(),
action: z.enum(['BUY', 'SELL']),
})),
});
const aggregatedOrderbookElementSchema = orderbookElementSchema
.extend({
exchanges: z.string().array(),
});
export const aggregatedOrderbookSchema = z.object({
asks: z.array(aggregatedOrderbookElementSchema),
bids: z.array(aggregatedOrderbookElementSchema),
});
export const exchangeOrderbookSchema = z.object({
asks: z.array(orderbookElementSchema),
bids: z.array(orderbookElementSchema),
});