Schema transform optimization

This commit is contained in:
Aleksandr Kraiz
2022-12-13 12:46:03 +04:00
parent 990aa04e46
commit 60a2bc0bcd
3 changed files with 45 additions and 53 deletions

View File

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

View File

@@ -1,7 +1,6 @@
import { z } from 'zod';
import WebSocket from 'isomorphic-ws';
import { validate as uuidValidate, v4 as uuidv4 } from 'uuid';
import { fullOrderSchema, orderUpdateSchema } from './schemas/addressUpdateSchema';
import MessageType from './MessageType';
import SubscriptionType from './SubscriptionType';
import {
@@ -18,50 +17,6 @@ import unsubscriptionDoneSchema from './schemas/unsubscriptionDoneSchema';
import assetPairConfigSchema from './schemas/assetPairConfigSchema';
// import errorSchema from './schemas/errorSchema';
const mapFullOrder = (o: z.infer<typeof fullOrderSchema>): FullOrder => ({
kind: 'full',
id: o.I,
settledAmount: o.A,
feeAsset: o.F,
fee: o.f,
status: o.S,
date: o.T,
clientOrdId: o.O,
type: o.s,
pair: o.P,
amount: o.a,
price: o.p,
subOrders: o.c.map((so) => ({
pair: so.P,
exchange: so.e,
id: so.i,
amount: so.a,
settledAmount: so.A,
price: so.p,
status: so.S,
side: so.s,
subOrdQty: so.A,
})),
});
const mapOrderUpdate = (o: z.infer<typeof orderUpdateSchema>): OrderUpdate => ({
kind: 'update',
id: o.I,
settledAmount: o.A,
status: o.S,
subOrders: o.c.map((so) => ({
pair: so.P,
exchange: so.e,
id: so.i,
amount: so.a,
settledAmount: so.A,
price: so.p,
status: so.S,
side: so.s,
subOrdQty: so.A,
})),
});
const UNSUBSCRIBE = 'u';
// https://github.com/orionprotocol/orion-aggregator/tree/feature/OP-1752-symmetric-swap#swap-info-subscribe
@@ -478,9 +433,7 @@ class OrionAggregatorWS {
case 'i': { // initial
const fullOrders = json.o
? json.o.reduce<FullOrder[]>((prev, o) => {
const fullOrder = mapFullOrder(o);
prev.push(fullOrder);
prev.push(o);
return prev;
}, [])
@@ -499,9 +452,7 @@ class OrionAggregatorWS {
let orderUpdate: OrderUpdate | FullOrder | undefined;
if (json.o) {
const firstOrder = json.o[0];
orderUpdate = firstOrder.k === 'full'
? mapFullOrder(firstOrder)
: mapOrderUpdate(firstOrder);
orderUpdate = firstOrder;
}
this.subscriptions[

View File

@@ -37,6 +37,22 @@ export const orderUpdateSchema = z.object({
.transform((val) => ({
...val,
k: 'update' as const,
})).transform((o) => ({
kind: o.k,
id: o.I,
settledAmount: o.A,
status: o.S,
subOrders: o.c.map((so) => ({
pair: so.P,
exchange: so.e,
id: so.i,
amount: so.a,
settledAmount: so.A,
price: so.p,
status: so.S,
side: so.s,
subOrdQty: so.A,
})),
}));
export const fullOrderSchema = z.object({
@@ -57,6 +73,30 @@ export const fullOrderSchema = z.object({
}).transform((val) => ({
...val,
k: 'full' as const,
})).transform((o) => ({
kind: o.k,
id: o.I,
settledAmount: o.A,
feeAsset: o.F,
fee: o.f,
status: o.S,
date: o.T,
clientOrdId: o.O,
type: o.s,
pair: o.P,
amount: o.a,
price: o.p,
subOrders: o.c.map((so) => ({
pair: so.P,
exchange: so.e,
id: so.i,
amount: so.a,
settledAmount: so.A,
price: so.p,
status: so.S,
side: so.s,
subOrdQty: so.A,
})),
}));
const updateMessageSchema = baseAddressUpdate.extend({
@@ -69,7 +109,8 @@ const updateMessageSchema = baseAddressUpdate.extend({
const initialMessageSchema = baseAddressUpdate.extend({
k: z.literal('i'), // kind of message: "i" - initial
b: balancesSchema,
o: z.array(fullOrderSchema).optional(), // When no orders — no field
o: z.array(fullOrderSchema)
.optional(), // When no orders — no field
});
const addressUpdateSchema = z.union([