mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-03-14 06:02:36 +03:00
OP-2205
This commit is contained in:
2
.github/workflows/prerelease-package.yml
vendored
2
.github/workflows/prerelease-package.yml
vendored
@@ -3,7 +3,7 @@ name: Prerelease
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- 'main'
|
||||
- 'develop'
|
||||
|
||||
jobs:
|
||||
build-and-publish:
|
||||
|
||||
18
package-lock.json
generated
18
package-lock.json
generated
@@ -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=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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": [
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<typeof fullOrderSchema>): FullOrder => ({
|
||||
@@ -76,9 +77,10 @@ type BrokerTradableAtomicSwapBalanceSubscription = {
|
||||
}
|
||||
|
||||
type PairConfigSubscription = {
|
||||
callback: (
|
||||
callback: ({ kind, data }: {
|
||||
kind: 'initial' | 'update',
|
||||
data: Partial<Record<string, AssetPairUpdate>>,
|
||||
) => 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<T extends typeof SubscriptionType[keyof typeof SubscriptionType]> = {
|
||||
[K in T]: Subscription[K]
|
||||
}
|
||||
class OrionAggregatorWS {
|
||||
private ws: WebSocket | undefined;
|
||||
|
||||
private subscriptions: Partial<Subscriptions<typeof SubscriptionType[keyof typeof SubscriptionType]>> = {};
|
||||
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<OrderbookItem[]>((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,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -3,6 +3,7 @@ import MessageType from '../MessageType';
|
||||
|
||||
const baseMessageSchema = z.object({
|
||||
T: z.nativeEnum(MessageType),
|
||||
id: z.string().optional(),
|
||||
_: z.number(),
|
||||
});
|
||||
|
||||
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user