From 363dafc45c7379369b7f7d0a0b7e2c6793f8b948 Mon Sep 17 00:00:00 2001 From: Aleksandr Kraiz Date: Wed, 14 Dec 2022 18:37:50 +0400 Subject: [PATCH] Added exchanges map --- package-lock.json | 4 +-- package.json | 2 +- src/constants/exchanges.ts | 1 + src/constants/exchangesMap.ts | 26 ++++++++++++++++++ src/constants/index.ts | 2 ++ .../ws/schemas/orderBookSchema.ts | 5 +++- .../ws/schemas/swapInfoSchema.ts | 3 ++- src/types.ts | 27 +++---------------- src/utils/objectKeys.ts | 17 ++++++++++++ 9 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 src/constants/exchangesMap.ts create mode 100644 src/utils/objectKeys.ts diff --git a/package-lock.json b/package-lock.json index 9b5e2ef..10d8fe5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.15.15", + "version": "0.15.21", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.15.15", + "version": "0.15.21", "license": "ISC", "dependencies": { "@ethersproject/abstract-signer": "^5.6.0", diff --git a/package.json b/package.json index f4f1dff..8ffaa3e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.15.21", + "version": "0.15.22", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", diff --git a/src/constants/exchanges.ts b/src/constants/exchanges.ts index 43e4907..5c646f6 100644 --- a/src/constants/exchanges.ts +++ b/src/constants/exchanges.ts @@ -15,4 +15,5 @@ export default [ 'CHERRYSWAP', 'OKXSWAP', 'CURVE', + 'CURVE_FACTORY', ] as const; diff --git a/src/constants/exchangesMap.ts b/src/constants/exchangesMap.ts new file mode 100644 index 0000000..da95748 --- /dev/null +++ b/src/constants/exchangesMap.ts @@ -0,0 +1,26 @@ +import exchanges from './exchanges'; + +const mapping: Record< + typeof exchanges[number], + string +> = { + // CEXes + ASCENDEX: 'AscendEx', + OKX: 'OKX', + BINANCE: 'Binance', + KUCOIN: 'KuCoin', + ORION: 'Orion', // Internal + + // DEXes + SPOOKYSWAP: 'SpookySwap', + PANCAKESWAP: 'PancakeSwap', + UNISWAP: 'Uniswap', + QUICKSWAP: 'QuickSwap', + ORION_POOL: 'Orion Pool', + CHERRYSWAP: 'CherrySwap', + OKXSWAP: 'OKXSwap', + CURVE: 'Curve', + CURVE_FACTORY: 'Curve Factory', +} as const; + +export default mapping; diff --git a/src/constants/index.ts b/src/constants/index.ts index 7eadf35..ff8dad3 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -3,6 +3,8 @@ export { default as orderStatuses } from './orderStatuses'; export { default as orderTypes } from './orderTypes'; export { default as subOrderStatuses } from './subOrderStatuses'; export { default as networkCodes } from './networkCodes'; +export { default as exchanges } from './exchanges'; +export { default as exchangesMap } from './exchangesMap'; export * from './chains'; export * from './precisions'; diff --git a/src/services/OrionAggregator/ws/schemas/orderBookSchema.ts b/src/services/OrionAggregator/ws/schemas/orderBookSchema.ts index 78e2117..4de8315 100644 --- a/src/services/OrionAggregator/ws/schemas/orderBookSchema.ts +++ b/src/services/OrionAggregator/ws/schemas/orderBookSchema.ts @@ -1,11 +1,14 @@ import { z } from 'zod'; +import exchanges from '../../../../constants/exchanges'; import MessageType from '../MessageType'; import baseMessageSchema from './baseMessageSchema'; export const orderBookItemSchema = z.tuple([ z.string(), // price z.string(), // size - z.array(z.string()), // exchanges + z.array( + z.enum(exchanges), + ), // exchanges z.array(z.tuple([ z.enum(['SELL', 'BUY']), // side z.string(), // pairname diff --git a/src/services/OrionAggregator/ws/schemas/swapInfoSchema.ts b/src/services/OrionAggregator/ws/schemas/swapInfoSchema.ts index 0343db4..488af4c 100644 --- a/src/services/OrionAggregator/ws/schemas/swapInfoSchema.ts +++ b/src/services/OrionAggregator/ws/schemas/swapInfoSchema.ts @@ -1,4 +1,5 @@ import { z } from 'zod'; +import exchanges from '../../../../constants/exchanges'; import MessageType from '../MessageType'; import baseMessageSchema from './baseMessageSchema'; @@ -13,7 +14,7 @@ const swapInfoSchemaBase = baseMessageSchema.extend({ mao: z.number(), // min amount out ps: z.string().array(), // path po: z.boolean(), // is swap through pool optimal - e: z.string().array().optional(), // Exchanges + e: z.enum(exchanges).array().optional(), // Exchanges p: z.number().optional(), // price mp: z.number().optional(), // market price oi: z.object({ // info about order equivalent to this swap diff --git a/src/types.ts b/src/types.ts index ed3111c..2ff19c8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,7 +1,8 @@ import BigNumber from 'bignumber.js'; +import { z } from 'zod'; import exchanges from './constants/exchanges'; -import orderStatuses from './constants/orderStatuses'; import subOrderStatuses from './constants/subOrderStatuses'; +import { fullOrderSchema, orderUpdateSchema } from './services/OrionAggregator/ws/schemas/addressUpdateSchema'; export type OrderbookItem = { price: string, @@ -28,29 +29,9 @@ export type SubOrder = { 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 FullOrder = z.infer; -export type OrderUpdate = { - kind: 'update', - id: string, - settledAmount: number, - status: typeof orderStatuses[number], - subOrders: SubOrder[] -} +export type OrderUpdate = z.infer; export type Balance = { tradable: string, diff --git a/src/utils/objectKeys.ts b/src/utils/objectKeys.ts new file mode 100644 index 0000000..b5a1514 --- /dev/null +++ b/src/utils/objectKeys.ts @@ -0,0 +1,17 @@ +export type ObjectKeys = `${Exclude}`; + +/** +A strongly-typed version of `Object.keys()`. +This is useful since `Object.keys()` always returns an array of strings. This function returns a strongly-typed array of the keys of the given object. +- [Explanation](https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript) +- [TypeScript issues about this](https://github.com/microsoft/TypeScript/issues/45390) +@example +``` +const stronglyTypedItems = objectKeys({a: 1, b: 2, c: 3}); // => Array<'a' | 'b' | 'c'> +const untypedItems = Object.keys(items); // => Array +``` +@category Improved builtin +@category Type guard +*/ +// eslint-disable-next-line @typescript-eslint/consistent-type-assertions +export const objectKeys = Object.keys as (value: Type) => ObjectKeys[];