From 178be3dfd66998f5c38469d12eb267d031a441d7 Mon Sep 17 00:00:00 2001 From: Aleksandr Kraiz Date: Thu, 19 May 2022 11:04:36 +0400 Subject: [PATCH] Orion Aggregator subscription system improvements --- package.json | 2 +- .../OrionAggregator/ws/MessageType.ts | 20 ++-- .../OrionAggregator/ws/SubscriptionType.ts | 14 +-- .../OrionAggregator/ws/UnsubscriptionType.ts | 8 +- src/services/OrionAggregator/ws/index.ts | 102 +++++++----------- src/types.ts | 58 ++++++++++ 6 files changed, 117 insertions(+), 87 deletions(-) diff --git a/package.json b/package.json index a223a5a..c2239be 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/services/OrionAggregator/ws/MessageType.ts b/src/services/OrionAggregator/ws/MessageType.ts index 9177313..bf52367 100644 --- a/src/services/OrionAggregator/ws/MessageType.ts +++ b/src/services/OrionAggregator/ws/MessageType.ts @@ -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; diff --git a/src/services/OrionAggregator/ws/SubscriptionType.ts b/src/services/OrionAggregator/ws/SubscriptionType.ts index c95f946..0170123 100644 --- a/src/services/OrionAggregator/ws/SubscriptionType.ts +++ b/src/services/OrionAggregator/ws/SubscriptionType.ts @@ -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; diff --git a/src/services/OrionAggregator/ws/UnsubscriptionType.ts b/src/services/OrionAggregator/ws/UnsubscriptionType.ts index 8d597b4..3e71155 100644 --- a/src/services/OrionAggregator/ws/UnsubscriptionType.ts +++ b/src/services/OrionAggregator/ws/UnsubscriptionType.ts @@ -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; diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index 0e3d5bc..7682d5b 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -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): FullOrder => ({ kind: 'full', id: o.I, @@ -131,8 +84,8 @@ type PairConfigSubscription = { type AggregatedOrderbookSubscription = { payload: string, callback: ( - asks: z.infer['ob']['a'], - bids: z.infer['ob']['b'], + asks: OrderbookItem[], + bids: OrderbookItem[], pair: string ) => void, } @@ -163,11 +116,13 @@ type Subscription = { [SubscriptionType.SWAP_SUBSCRIBE]: SwapInfoSubscription } -type Subscriptions = { [K in T]: Subscription[K] } +type Subscriptions = { + [K in T]: Subscription[K] +} class OrionAggregatorWS { private ws: WebSocket | undefined; - private subscriptions: Partial> = {}; + private subscriptions: Partial> = {}; private onError?: (err: string) => void; @@ -198,7 +153,7 @@ class OrionAggregatorWS { } } - subscribe( + subscribe( 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((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[ diff --git a/src/types.ts b/src/types.ts index 48d7665..2bf243b 100644 --- a/src/types.ts +++ b/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