mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-03-14 06:02:36 +03:00
Orion Aggregator subscription system improvements
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@orionprotocol/sdk",
|
||||
"version": "0.7.0",
|
||||
"version": "0.8.0",
|
||||
"description": "Orion Protocol SDK",
|
||||
"main": "./lib/esm/index.js",
|
||||
"module": "./lib/esm/index.js",
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
enum MessageType {
|
||||
ERROR = 'e',
|
||||
PING_PONG = 'pp',
|
||||
SWAP_INFO = 'si',
|
||||
INITIALIZATION = 'i',
|
||||
AGGREGATED_ORDER_BOOK_UPDATE = 'aobu',
|
||||
ASSET_PAIRS_CONFIG_UPDATE = 'apcu',
|
||||
ADDRESS_UPDATE = 'au',
|
||||
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATE = 'btasabu'
|
||||
}
|
||||
const MessageType = {
|
||||
ERROR: 'e',
|
||||
PING_PONG: 'pp',
|
||||
SWAP_INFO: 'si',
|
||||
INITIALIZATION: 'i',
|
||||
AGGREGATED_ORDER_BOOK_UPDATE: 'aobu',
|
||||
ASSET_PAIRS_CONFIG_UPDATE: 'apcu',
|
||||
ADDRESS_UPDATE: 'au',
|
||||
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATE: 'btasabu',
|
||||
} as const;
|
||||
|
||||
export default MessageType;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
enum SubscriptionType {
|
||||
ASSET_PAIRS_CONFIG_UPDATES_SUBSCRIBE = 'apcus',
|
||||
AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE = 'aobus',
|
||||
ADDRESS_UPDATES_SUBSCRIBE = 'aus',
|
||||
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_SUBSCRIBE = 'btasabus',
|
||||
SWAP_SUBSCRIBE = 'ss',
|
||||
}
|
||||
const SubscriptionType = {
|
||||
ASSET_PAIRS_CONFIG_UPDATES_SUBSCRIBE: 'apcus',
|
||||
AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE: 'aobus',
|
||||
ADDRESS_UPDATES_SUBSCRIBE: 'aus',
|
||||
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_SUBSCRIBE: 'btasabus',
|
||||
SWAP_SUBSCRIBE: 'ss',
|
||||
} as const;
|
||||
|
||||
export default SubscriptionType;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
enum UnsubscriptionType {
|
||||
ASSET_PAIRS_CONFIG_UPDATES_UNSUBSCRIBE = 'apcu',
|
||||
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_UNSUBSCRIBE = 'btasabu',
|
||||
}
|
||||
const UnsubscriptionType = {
|
||||
ASSET_PAIRS_CONFIG_UPDATES_UNSUBSCRIBE: 'apcu',
|
||||
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_UNSUBSCRIBE: 'btasabu',
|
||||
} as const;
|
||||
export default UnsubscriptionType;
|
||||
|
||||
@@ -12,57 +12,10 @@ import {
|
||||
import UnsubscriptionType from './UnsubscriptionType';
|
||||
import {
|
||||
SwapInfoByAmountIn, SwapInfoByAmountOut, SwapInfoBase,
|
||||
FullOrder, OrderUpdate, AssetPairUpdate, OrderbookItem, Balance,
|
||||
} from '../../../types';
|
||||
import { orderStatuses, subOrderStatuses } from '../../../constants';
|
||||
// import errorSchema from './schemas/errorSchema';
|
||||
|
||||
type AssetPairUpdate = {
|
||||
minQty: number,
|
||||
pricePrecision: number,
|
||||
}
|
||||
type SubOrder = {
|
||||
pair: string,
|
||||
exchange: string,
|
||||
id: number,
|
||||
amount: number,
|
||||
settledAmount: number,
|
||||
price: number,
|
||||
status: typeof subOrderStatuses[number],
|
||||
side: 'BUY' | 'SELL',
|
||||
subOrdQty: number
|
||||
}
|
||||
type FullOrder = {
|
||||
kind: 'full',
|
||||
id: string,
|
||||
settledAmount: number,
|
||||
feeAsset: string,
|
||||
fee: number,
|
||||
status: typeof orderStatuses[number],
|
||||
date: number,
|
||||
clientOrdId: string,
|
||||
type: 'BUY' | 'SELL',
|
||||
pair: string,
|
||||
amount: number,
|
||||
price: number,
|
||||
subOrders: SubOrder[]
|
||||
}
|
||||
|
||||
type OrderUpdate = {
|
||||
kind: 'update',
|
||||
id: string,
|
||||
settledAmount: number,
|
||||
status: typeof orderStatuses[number],
|
||||
subOrders: SubOrder[]
|
||||
}
|
||||
|
||||
type Balance = {
|
||||
tradable: string,
|
||||
reserved: string,
|
||||
contract: string,
|
||||
wallet: string,
|
||||
allowance: string,
|
||||
}
|
||||
|
||||
const mapFullOrder = (o: z.infer<typeof fullOrderSchema>): FullOrder => ({
|
||||
kind: 'full',
|
||||
id: o.I,
|
||||
@@ -131,8 +84,8 @@ type PairConfigSubscription = {
|
||||
type AggregatedOrderbookSubscription = {
|
||||
payload: string,
|
||||
callback: (
|
||||
asks: z.infer<typeof orderBookSchema>['ob']['a'],
|
||||
bids: z.infer<typeof orderBookSchema>['ob']['b'],
|
||||
asks: OrderbookItem[],
|
||||
bids: OrderbookItem[],
|
||||
pair: string
|
||||
) => void,
|
||||
}
|
||||
@@ -163,11 +116,13 @@ type Subscription = {
|
||||
[SubscriptionType.SWAP_SUBSCRIBE]: SwapInfoSubscription
|
||||
}
|
||||
|
||||
type Subscriptions<T extends SubscriptionType> = { [K in T]: Subscription[K] }
|
||||
type Subscriptions<T extends typeof SubscriptionType[keyof typeof SubscriptionType]> = {
|
||||
[K in T]: Subscription[K]
|
||||
}
|
||||
class OrionAggregatorWS {
|
||||
private ws: WebSocket | undefined;
|
||||
|
||||
private subscriptions: Partial<Subscriptions<SubscriptionType>> = {};
|
||||
private subscriptions: Partial<Subscriptions<typeof SubscriptionType[keyof typeof SubscriptionType]>> = {};
|
||||
|
||||
private onError?: (err: string) => void;
|
||||
|
||||
@@ -198,7 +153,7 @@ class OrionAggregatorWS {
|
||||
}
|
||||
}
|
||||
|
||||
subscribe<T extends SubscriptionType>(
|
||||
subscribe<T extends typeof SubscriptionType[keyof typeof SubscriptionType]>(
|
||||
type: T,
|
||||
subscription: Subscription[T],
|
||||
) {
|
||||
@@ -213,7 +168,7 @@ class OrionAggregatorWS {
|
||||
this.subscriptions[type] = subscription;
|
||||
}
|
||||
|
||||
unsubscribe(subscription: UnsubscriptionType | string) {
|
||||
unsubscribe(subscription: keyof typeof UnsubscriptionType | string) {
|
||||
this.send({
|
||||
T: UNSUBSCRIBE,
|
||||
S: subscription,
|
||||
@@ -329,9 +284,33 @@ class OrionAggregatorWS {
|
||||
// break;
|
||||
case MessageType.AGGREGATED_ORDER_BOOK_UPDATE: {
|
||||
const { ob, S } = json;
|
||||
const mapOrderbookItems = (rawItems: typeof ob.a | typeof ob.b) => rawItems.reduce<OrderbookItem[]>((acc, item) => {
|
||||
const [
|
||||
price,
|
||||
amount,
|
||||
exchanges,
|
||||
vob,
|
||||
] = item;
|
||||
return [
|
||||
...acc,
|
||||
{
|
||||
price,
|
||||
amount,
|
||||
exchanges,
|
||||
vob: vob.map(([side, pairName]) => ({
|
||||
side,
|
||||
pairName,
|
||||
})),
|
||||
},
|
||||
];
|
||||
}, []);
|
||||
this.subscriptions[
|
||||
SubscriptionType.AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE
|
||||
]?.callback(ob.a, ob.b, S);
|
||||
]?.callback(
|
||||
mapOrderbookItems(ob.a),
|
||||
mapOrderbookItems(ob.b),
|
||||
S,
|
||||
);
|
||||
}
|
||||
break;
|
||||
case MessageType.ASSET_PAIRS_CONFIG_UPDATE: {
|
||||
@@ -386,16 +365,9 @@ class OrionAggregatorWS {
|
||||
let orderUpdate: OrderUpdate | FullOrder | undefined;
|
||||
if (json.o) {
|
||||
const firstOrder = json.o[0];
|
||||
switch (firstOrder.k) {
|
||||
case 'full':
|
||||
orderUpdate = mapFullOrder(firstOrder);
|
||||
break;
|
||||
case 'update':
|
||||
orderUpdate = mapOrderUpdate(firstOrder);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
orderUpdate = firstOrder.k === 'full'
|
||||
? mapFullOrder(firstOrder)
|
||||
: mapOrderUpdate(firstOrder);
|
||||
}
|
||||
|
||||
this.subscriptions[
|
||||
|
||||
58
src/types.ts
58
src/types.ts
@@ -1,5 +1,63 @@
|
||||
import BigNumber from 'bignumber.js';
|
||||
import orderStatuses from './constants/orderStatuses';
|
||||
import subOrderStatuses from './constants/subOrderStatuses';
|
||||
|
||||
export type OrderbookItem = {
|
||||
price: string,
|
||||
amount: string,
|
||||
exchanges: string[],
|
||||
vob: {
|
||||
side: 'BUY' | 'SELL',
|
||||
pairName: string
|
||||
}[]
|
||||
}
|
||||
|
||||
export type AssetPairUpdate = {
|
||||
minQty: number,
|
||||
pricePrecision: number,
|
||||
}
|
||||
export type SubOrder = {
|
||||
pair: string,
|
||||
exchange: string,
|
||||
id: number,
|
||||
amount: number,
|
||||
settledAmount: number,
|
||||
price: number,
|
||||
status: typeof subOrderStatuses[number],
|
||||
side: 'BUY' | 'SELL',
|
||||
subOrdQty: number
|
||||
}
|
||||
export type FullOrder = {
|
||||
kind: 'full',
|
||||
id: string,
|
||||
settledAmount: number,
|
||||
feeAsset: string,
|
||||
fee: number,
|
||||
status: typeof orderStatuses[number],
|
||||
date: number,
|
||||
clientOrdId: string,
|
||||
type: 'BUY' | 'SELL',
|
||||
pair: string,
|
||||
amount: number,
|
||||
price: number,
|
||||
subOrders: SubOrder[]
|
||||
}
|
||||
|
||||
export type OrderUpdate = {
|
||||
kind: 'update',
|
||||
id: string,
|
||||
settledAmount: number,
|
||||
status: typeof orderStatuses[number],
|
||||
subOrders: SubOrder[]
|
||||
}
|
||||
|
||||
export type Balance = {
|
||||
tradable: string,
|
||||
reserved: string,
|
||||
contract: string,
|
||||
wallet: string,
|
||||
allowance: string,
|
||||
}
|
||||
export interface Order {
|
||||
senderAddress: string; // address
|
||||
matcherAddress: string; // address
|
||||
|
||||
Reference in New Issue
Block a user