Orion Aggregator subscription system improvements

This commit is contained in:
Aleksandr Kraiz
2022-05-19 11:04:36 +04:00
parent 9205f4cd63
commit 178be3dfd6
6 changed files with 117 additions and 87 deletions

View File

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

View File

@@ -1,12 +1,12 @@
enum MessageType { const MessageType = {
ERROR = 'e', ERROR: 'e',
PING_PONG = 'pp', PING_PONG: 'pp',
SWAP_INFO = 'si', SWAP_INFO: 'si',
INITIALIZATION = 'i', INITIALIZATION: 'i',
AGGREGATED_ORDER_BOOK_UPDATE = 'aobu', AGGREGATED_ORDER_BOOK_UPDATE: 'aobu',
ASSET_PAIRS_CONFIG_UPDATE = 'apcu', ASSET_PAIRS_CONFIG_UPDATE: 'apcu',
ADDRESS_UPDATE = 'au', ADDRESS_UPDATE: 'au',
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATE = 'btasabu' BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATE: 'btasabu',
} } as const;
export default MessageType; export default MessageType;

View File

@@ -1,9 +1,9 @@
enum SubscriptionType { const SubscriptionType = {
ASSET_PAIRS_CONFIG_UPDATES_SUBSCRIBE = 'apcus', ASSET_PAIRS_CONFIG_UPDATES_SUBSCRIBE: 'apcus',
AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE = 'aobus', AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE: 'aobus',
ADDRESS_UPDATES_SUBSCRIBE = 'aus', ADDRESS_UPDATES_SUBSCRIBE: 'aus',
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_SUBSCRIBE = 'btasabus', BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_SUBSCRIBE: 'btasabus',
SWAP_SUBSCRIBE = 'ss', SWAP_SUBSCRIBE: 'ss',
} } as const;
export default SubscriptionType; export default SubscriptionType;

View File

@@ -1,5 +1,5 @@
enum UnsubscriptionType { const UnsubscriptionType = {
ASSET_PAIRS_CONFIG_UPDATES_UNSUBSCRIBE = 'apcu', ASSET_PAIRS_CONFIG_UPDATES_UNSUBSCRIBE: 'apcu',
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_UNSUBSCRIBE = 'btasabu', BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_UNSUBSCRIBE: 'btasabu',
} } as const;
export default UnsubscriptionType; export default UnsubscriptionType;

View File

@@ -12,57 +12,10 @@ import {
import UnsubscriptionType from './UnsubscriptionType'; import UnsubscriptionType from './UnsubscriptionType';
import { import {
SwapInfoByAmountIn, SwapInfoByAmountOut, SwapInfoBase, SwapInfoByAmountIn, SwapInfoByAmountOut, SwapInfoBase,
FullOrder, OrderUpdate, AssetPairUpdate, OrderbookItem, Balance,
} from '../../../types'; } from '../../../types';
import { orderStatuses, subOrderStatuses } from '../../../constants';
// import errorSchema from './schemas/errorSchema'; // 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 => ({ const mapFullOrder = (o: z.infer<typeof fullOrderSchema>): FullOrder => ({
kind: 'full', kind: 'full',
id: o.I, id: o.I,
@@ -131,8 +84,8 @@ type PairConfigSubscription = {
type AggregatedOrderbookSubscription = { type AggregatedOrderbookSubscription = {
payload: string, payload: string,
callback: ( callback: (
asks: z.infer<typeof orderBookSchema>['ob']['a'], asks: OrderbookItem[],
bids: z.infer<typeof orderBookSchema>['ob']['b'], bids: OrderbookItem[],
pair: string pair: string
) => void, ) => void,
} }
@@ -163,11 +116,13 @@ type Subscription = {
[SubscriptionType.SWAP_SUBSCRIBE]: SwapInfoSubscription [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 { class OrionAggregatorWS {
private ws: WebSocket | undefined; private ws: WebSocket | undefined;
private subscriptions: Partial<Subscriptions<SubscriptionType>> = {}; private subscriptions: Partial<Subscriptions<typeof SubscriptionType[keyof typeof SubscriptionType]>> = {};
private onError?: (err: string) => void; 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, type: T,
subscription: Subscription[T], subscription: Subscription[T],
) { ) {
@@ -213,7 +168,7 @@ class OrionAggregatorWS {
this.subscriptions[type] = subscription; this.subscriptions[type] = subscription;
} }
unsubscribe(subscription: UnsubscriptionType | string) { unsubscribe(subscription: keyof typeof UnsubscriptionType | string) {
this.send({ this.send({
T: UNSUBSCRIBE, T: UNSUBSCRIBE,
S: subscription, S: subscription,
@@ -329,9 +284,33 @@ class OrionAggregatorWS {
// break; // break;
case MessageType.AGGREGATED_ORDER_BOOK_UPDATE: { case MessageType.AGGREGATED_ORDER_BOOK_UPDATE: {
const { ob, S } = json; 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[ this.subscriptions[
SubscriptionType.AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE SubscriptionType.AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE
]?.callback(ob.a, ob.b, S); ]?.callback(
mapOrderbookItems(ob.a),
mapOrderbookItems(ob.b),
S,
);
} }
break; break;
case MessageType.ASSET_PAIRS_CONFIG_UPDATE: { case MessageType.ASSET_PAIRS_CONFIG_UPDATE: {
@@ -386,16 +365,9 @@ class OrionAggregatorWS {
let orderUpdate: OrderUpdate | FullOrder | undefined; let orderUpdate: OrderUpdate | FullOrder | undefined;
if (json.o) { if (json.o) {
const firstOrder = json.o[0]; const firstOrder = json.o[0];
switch (firstOrder.k) { orderUpdate = firstOrder.k === 'full'
case 'full': ? mapFullOrder(firstOrder)
orderUpdate = mapFullOrder(firstOrder); : mapOrderUpdate(firstOrder);
break;
case 'update':
orderUpdate = mapOrderUpdate(firstOrder);
break;
default:
break;
}
} }
this.subscriptions[ this.subscriptions[

View File

@@ -1,5 +1,63 @@
import BigNumber from 'bignumber.js'; 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 { export interface Order {
senderAddress: string; // address senderAddress: string; // address
matcherAddress: string; // address matcherAddress: string; // address