From 299e94716fbd4c296c858579e52f78447db897ea Mon Sep 17 00:00:00 2001 From: Aleksandr Kraiz Date: Fri, 27 May 2022 09:47:15 +0400 Subject: [PATCH] OP-2205 --- .github/workflows/prerelease-package.yml | 2 +- package-lock.json | 18 ++--- package.json | 4 +- src/config/envs.json | 10 +++ .../OrionAggregator/ws/MessageType.ts | 1 + src/services/OrionAggregator/ws/index.ts | 70 +++++++++++++------ .../ws/schemas/assetPairsConfigSchema.ts | 1 + .../ws/schemas/baseMessageSchema.ts | 1 + .../ws/schemas/unsubscriptionDoneSchema.ts | 9 +++ 9 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 src/services/OrionAggregator/ws/schemas/unsubscriptionDoneSchema.ts diff --git a/.github/workflows/prerelease-package.yml b/.github/workflows/prerelease-package.yml index df8a708..d9fb5d6 100644 --- a/.github/workflows/prerelease-package.yml +++ b/.github/workflows/prerelease-package.yml @@ -3,7 +3,7 @@ name: Prerelease on: push: branches: - - 'main' + - 'develop' jobs: build-and-publish: diff --git a/package-lock.json b/package-lock.json index de0aab9..57d541e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.5.8", + "version": "0.10.4-rc.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.5.8", + "version": "0.10.4-rc.0", "license": "ISC", "dependencies": { "@ethersproject/abstract-signer": "^5.6.0", @@ -25,7 +25,7 @@ "tiny-invariant": "^1.2.0", "uuid": "^8.3.2", "ws": "^8.5.0", - "zod": "^3.16.0" + "zod": "^3.17.3" }, "devDependencies": { "@typechain/ethers-v5": "^10.0.0", @@ -11099,9 +11099,9 @@ "integrity": "sha1-AI4G2AlDIMNy28L47XagymyKxBk=" }, "node_modules/zod": { - "version": "3.16.0", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.16.0.tgz", - "integrity": "sha512-szrIkryADbTM+xBt2a1KoS2CJQXec4f9xG78bj5MJeEH/XqmmHpnO+fG3IE115AKBJak+2HrbxLZkc9mhdbDKA==", + "version": "3.17.3", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.17.3.tgz", + "integrity": "sha512-4oKP5zvG6GGbMlqBkI5FESOAweldEhSOZ6LI6cG+JzUT7ofj1ZOC0PJudpQOpT1iqOFpYYtX5Pw0+o403y4bcg==", "funding": { "url": "https://github.com/sponsors/colinhacks" } @@ -18842,9 +18842,9 @@ "integrity": "sha1-AI4G2AlDIMNy28L47XagymyKxBk=" }, "zod": { - "version": "3.16.0", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.16.0.tgz", - "integrity": "sha512-szrIkryADbTM+xBt2a1KoS2CJQXec4f9xG78bj5MJeEH/XqmmHpnO+fG3IE115AKBJak+2HrbxLZkc9mhdbDKA==" + "version": "3.17.3", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.17.3.tgz", + "integrity": "sha512-4oKP5zvG6GGbMlqBkI5FESOAweldEhSOZ6LI6cG+JzUT7ofj1ZOC0PJudpQOpT1iqOFpYYtX5Pw0+o403y4bcg==" } } } diff --git a/package.json b/package.json index 9cf8d92..7154210 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.10.4-rc.0", + "version": "0.10.4-rc.1", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", @@ -76,7 +76,7 @@ "tiny-invariant": "^1.2.0", "uuid": "^8.3.2", "ws": "^8.5.0", - "zod": "^3.16.0" + "zod": "^3.17.3" }, "homepage": "https://github.com/orionprotocol/sdk#readme", "files": [ diff --git a/src/config/envs.json b/src/config/envs.json index b202a8d..2b1805f 100644 --- a/src/config/envs.json +++ b/src/config/envs.json @@ -44,6 +44,16 @@ } } }, + "experimental": { + "networks": { + "97": { + "api": "https://dn-dev.orionprotocol.io/bsc-testnet" + }, + "3": { + "api": "https://dn-dev.orionprotocol.io/eth-ropsten" + } + } + }, "broken": { "networks": { "0": { diff --git a/src/services/OrionAggregator/ws/MessageType.ts b/src/services/OrionAggregator/ws/MessageType.ts index bf52367..cb51d25 100644 --- a/src/services/OrionAggregator/ws/MessageType.ts +++ b/src/services/OrionAggregator/ws/MessageType.ts @@ -7,6 +7,7 @@ const MessageType = { ASSET_PAIRS_CONFIG_UPDATE: 'apcu', ADDRESS_UPDATE: 'au', BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATE: 'btasabu', + UNSUBSCRIPTION_DONE: 'ud', } as const; export default MessageType; diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index 7682d5b..ef979ad 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -1,6 +1,6 @@ import { z } from 'zod'; import WebSocket from 'isomorphic-ws'; -import { validate as uuidValidate } from 'uuid'; +import { validate as uuidValidate, v4 as uuidv4 } from 'uuid'; import { fullOrderSchema, orderUpdateSchema } from './schemas/addressUpdateSchema'; import MessageType from './MessageType'; import SubscriptionType from './SubscriptionType'; @@ -14,6 +14,7 @@ import { SwapInfoByAmountIn, SwapInfoByAmountOut, SwapInfoBase, FullOrder, OrderUpdate, AssetPairUpdate, OrderbookItem, Balance, } from '../../../types'; +import unsubscriptionDoneSchema from './schemas/unsubscriptionDoneSchema'; // import errorSchema from './schemas/errorSchema'; const mapFullOrder = (o: z.infer): FullOrder => ({ @@ -76,9 +77,10 @@ type BrokerTradableAtomicSwapBalanceSubscription = { } type PairConfigSubscription = { - callback: ( + callback: ({ kind, data }: { + kind: 'initial' | 'update', data: Partial>, - ) => void, + }) => void, } type AggregatedOrderbookSubscription = { @@ -94,18 +96,32 @@ type SwapInfoSubscription = { payload: SwapSubscriptionRequest, callback: (swapInfo: SwapInfoByAmountIn | SwapInfoByAmountOut) => void, } -type AddressUpdateSubscription = { - payload: string, - callback: ({ fullOrders, orderUpdate, balances } : { - fullOrders?: FullOrder[], - orderUpdate?: OrderUpdate | FullOrder, - balances?: Partial< + +type AddressUpdateUpdate = { + kind: 'update', + balances: Partial< Record< string, Balance > - >, - }) => void, + >, + order?: OrderUpdate | FullOrder +} + +type AddressUpdateInitial = { + kind: 'initial', + balances: Partial< + Record< + string, + Balance + > + >, + orders?: FullOrder[] // The field is not defined if the user has no orders +} + +type AddressUpdateSubscription = { + payload: string, + callback: (data: AddressUpdateUpdate | AddressUpdateInitial) => void, } type Subscription = { @@ -115,21 +131,22 @@ type Subscription = { [SubscriptionType.BROKER_TRADABLE_ATOMIC_SWAP_ASSETS_BALANCE_UPDATES_SUBSCRIBE]: BrokerTradableAtomicSwapBalanceSubscription, [SubscriptionType.SWAP_SUBSCRIBE]: SwapInfoSubscription } - -type Subscriptions = { - [K in T]: Subscription[K] -} class OrionAggregatorWS { private ws: WebSocket | undefined; - private subscriptions: Partial> = {}; + private subscriptions: Partial<{ + [K in keyof Subscription]: Subscription[K] + }> = {}; + + private onInit?: () => void; private onError?: (err: string) => void; private readonly wsUrl: string; - constructor(wsUrl: string, onError?: (err: string) => void) { + constructor(wsUrl: string, onInit?: () => void, onError?: (err: string) => void) { this.wsUrl = wsUrl; + this.onInit = onInit; this.onError = onError; } @@ -160,6 +177,7 @@ class OrionAggregatorWS { if (!this.ws) this.init(); this.send({ T: type, + id: uuidv4(), ...('payload' in subscription) && { S: subscription.payload, }, @@ -170,6 +188,7 @@ class OrionAggregatorWS { unsubscribe(subscription: keyof typeof UnsubscriptionType | string) { this.send({ + id: uuidv4(), T: UNSUBSCRIBE, S: subscription, }); @@ -221,6 +240,7 @@ class OrionAggregatorWS { orderBookSchema, swapInfoSchema, errorSchema, + unsubscriptionDoneSchema, ]); const json = messageSchema.parse(rawJson); @@ -280,8 +300,9 @@ class OrionAggregatorWS { } } break; - // case MessageType.INITIALIZATION: - // break; + case MessageType.INITIALIZATION: + this.onInit?.(); + break; case MessageType.AGGREGATED_ORDER_BOOK_UPDATE: { const { ob, S } = json; const mapOrderbookItems = (rawItems: typeof ob.a | typeof ob.b) => rawItems.reduce((acc, item) => { @@ -324,7 +345,10 @@ class OrionAggregatorWS { }), {}); this.subscriptions[ SubscriptionType.ASSET_PAIRS_CONFIG_UPDATES_SUBSCRIBE - ]?.callback(priceUpdates); + ]?.callback({ + kind: json.k === 'i' ? 'initial' : 'update', + data: priceUpdates, + }); } break; case MessageType.ADDRESS_UPDATE: { @@ -356,7 +380,8 @@ class OrionAggregatorWS { this.subscriptions[ SubscriptionType.ADDRESS_UPDATES_SUBSCRIBE ]?.callback({ - fullOrders, + kind: 'initial', + orders: fullOrders, balances, }); } @@ -373,7 +398,8 @@ class OrionAggregatorWS { this.subscriptions[ SubscriptionType.ADDRESS_UPDATES_SUBSCRIBE ]?.callback({ - orderUpdate, + kind: 'update', + order: orderUpdate, balances, }); } diff --git a/src/services/OrionAggregator/ws/schemas/assetPairsConfigSchema.ts b/src/services/OrionAggregator/ws/schemas/assetPairsConfigSchema.ts index 1cf160f..48e563f 100644 --- a/src/services/OrionAggregator/ws/schemas/assetPairsConfigSchema.ts +++ b/src/services/OrionAggregator/ws/schemas/assetPairsConfigSchema.ts @@ -4,6 +4,7 @@ import baseMessageSchema from './baseMessageSchema'; const assetPairsConfigSchema = baseMessageSchema.extend({ T: z.literal(MessageType.ASSET_PAIRS_CONFIG_UPDATE), + k: z.enum(['i', 'u']), u: z.array( z.tuple([ z.string(), // pairName diff --git a/src/services/OrionAggregator/ws/schemas/baseMessageSchema.ts b/src/services/OrionAggregator/ws/schemas/baseMessageSchema.ts index ec6c749..afec88d 100644 --- a/src/services/OrionAggregator/ws/schemas/baseMessageSchema.ts +++ b/src/services/OrionAggregator/ws/schemas/baseMessageSchema.ts @@ -3,6 +3,7 @@ import MessageType from '../MessageType'; const baseMessageSchema = z.object({ T: z.nativeEnum(MessageType), + id: z.string().optional(), _: z.number(), }); diff --git a/src/services/OrionAggregator/ws/schemas/unsubscriptionDoneSchema.ts b/src/services/OrionAggregator/ws/schemas/unsubscriptionDoneSchema.ts new file mode 100644 index 0000000..4dbe81d --- /dev/null +++ b/src/services/OrionAggregator/ws/schemas/unsubscriptionDoneSchema.ts @@ -0,0 +1,9 @@ +import { z } from 'zod'; +import MessageType from '../MessageType'; +import baseMessageSchema from './baseMessageSchema'; + +const unsubscriptionDoneSchema = baseMessageSchema.extend({ + T: z.literal(MessageType.UNSUBSCRIPTION_DONE), +}); + +export default unsubscriptionDoneSchema;