OP-2363: подписка на конфиг отдельной пары (#18)

* feat: add pair config subscription

* Small fix

* feat: add kind to update pair callback

* chore: version bump

Co-authored-by: Aleksandr Kraiz <selfsurfer@gmail.com>
This commit is contained in:
Oleg Nechiporenko
2022-07-26 08:50:25 +03:00
committed by GitHub
parent 4626cd7279
commit cc45da8e86
6 changed files with 52 additions and 5 deletions

View File

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

View File

@@ -5,6 +5,7 @@ const MessageType = {
INITIALIZATION: 'i',
AGGREGATED_ORDER_BOOK_UPDATE: 'aobu',
ASSET_PAIRS_CONFIG_UPDATE: 'apcu',
ASSET_PAIR_CONFIG_UPDATE: 'apiu',
ADDRESS_UPDATE: 'au',
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATE: 'btasabu',
UNSUBSCRIPTION_DONE: 'ud',

View File

@@ -1,5 +1,6 @@
const SubscriptionType = {
ASSET_PAIRS_CONFIG_UPDATES_SUBSCRIBE: 'apcus',
ASSET_PAIR_CONFIG_UPDATES_SUBSCRIBE: 'apius',
AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE: 'aobus',
ADDRESS_UPDATES_SUBSCRIBE: 'aus',
BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_SUBSCRIBE: 'btasabus',

View File

@@ -15,6 +15,7 @@ import {
FullOrder, OrderUpdate, AssetPairUpdate, OrderbookItem, Balance, Exchange,
} from '../../../types';
import unsubscriptionDoneSchema from './schemas/unsubscriptionDoneSchema';
import assetPairConfigSchema from './schemas/assetPairConfigSchema';
// import errorSchema from './schemas/errorSchema';
const mapFullOrder = (o: z.infer<typeof fullOrderSchema>): FullOrder => ({
@@ -77,13 +78,21 @@ type BrokerTradableAtomicSwapBalanceSubscription = {
callback: (balances: Partial<Record<string, number>>) => void,
}
type PairConfigSubscription = {
type PairsConfigSubscription = {
callback: ({ kind, data }: {
kind: 'initial' | 'update',
data: Partial<Record<string, AssetPairUpdate>>,
}) => void,
}
type PairConfigSubscription = {
payload: string,
callback: ({ kind, data }: {
kind: 'initial' | 'update',
data: AssetPairUpdate,
}) => void,
}
type AggregatedOrderbookSubscription = {
payload: string,
callback: (
@@ -128,7 +137,8 @@ type AddressUpdateSubscription = {
type Subscription = {
[SubscriptionType.ADDRESS_UPDATES_SUBSCRIBE]: AddressUpdateSubscription,
[SubscriptionType.AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE]: AggregatedOrderbookSubscription,
[SubscriptionType.ASSET_PAIRS_CONFIG_UPDATES_SUBSCRIBE]: PairConfigSubscription,
[SubscriptionType.ASSET_PAIRS_CONFIG_UPDATES_SUBSCRIBE]: PairsConfigSubscription,
[SubscriptionType.ASSET_PAIR_CONFIG_UPDATES_SUBSCRIBE]: PairConfigSubscription,
[SubscriptionType.BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_SUBSCRIBE]: BrokerTradableAtomicSwapBalanceSubscription,
[SubscriptionType.SWAP_SUBSCRIBE]: SwapInfoSubscription
}
@@ -243,8 +253,10 @@ class OrionAggregatorWS {
delete this.subscriptions[SubscriptionType.ADDRESS_UPDATES_SUBSCRIBE]?.[key];
}
}
} else if (uuidValidate(subscription)) { // is swap info subscription (contains hyphen)
} else if (uuidValidate(subscription)) {
// is swap info subscription (contains hyphen)
delete this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[subscription];
delete this.subscriptions[SubscriptionType.ASSET_PAIR_CONFIG_UPDATES_SUBSCRIBE]?.[subscription];
// !!! swap info subscription is uuid that contains hyphen
} else if (subscription.includes('-') && subscription.split('-').length === 2) { // is pair name(AGGREGATED_ORDER_BOOK_UPDATE)
const aobSubscriptions = this.subscriptions[SubscriptionType.AGGREGATED_ORDER_BOOK_UPDATES_SUBSCRIBE];
@@ -305,6 +317,7 @@ class OrionAggregatorWS {
pingPongMessageSchema,
addressUpdateSchema,
assetPairsConfigSchema,
assetPairConfigSchema,
brokerMessageSchema,
orderBookSchema,
swapInfoSchema,
@@ -407,6 +420,22 @@ class OrionAggregatorWS {
);
}
break;
case MessageType.ASSET_PAIR_CONFIG_UPDATE: {
const pair = json.u;
const [, minQty, pricePrecision] = pair;
this.subscriptions[
SubscriptionType.ASSET_PAIR_CONFIG_UPDATES_SUBSCRIBE
]?.[json.id]?.callback({
data: {
minQty,
pricePrecision,
},
kind: json.k === 'i' ? 'initial' : 'update',
});
break;
}
case MessageType.ASSET_PAIRS_CONFIG_UPDATE: {
const pairs = json;
const priceUpdates: Partial<Record<string, AssetPairUpdate>> = {};

View File

@@ -0,0 +1,16 @@
import { z } from 'zod';
import MessageType from '../MessageType';
import baseMessageSchema from './baseMessageSchema';
const assetPairConfigSchema = baseMessageSchema.extend({
id: z.string(),
T: z.literal(MessageType.ASSET_PAIR_CONFIG_UPDATE),
k: z.enum(['i', 'u']),
u: z.tuple([
z.string(), // pairName
z.number(), // minQty
z.number().int(), // pricePrecision
]),
});
export default assetPairConfigSchema;

View File

@@ -10,7 +10,7 @@ const assetPairsConfigSchema = baseMessageSchema.extend({
z.tuple([
z.string(), // pairName
z.number(), // minQty
z.number(), // pricePrecision
z.number().int(), // pricePrecision
]),
),
});