diff --git a/package-lock.json b/package-lock.json index cabf24c..7012a7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.17.26", + "version": "0.17.27", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.17.26", + "version": "0.17.27", "license": "ISC", "dependencies": { "@babel/runtime": "^7.21.0", @@ -31,7 +31,7 @@ "ts-node": "^10.9.1", "uuid": "^9.0.0", "ws": "^8.12.1", - "zod": "^3.21.2" + "zod": "3.21.2" }, "devDependencies": { "@babel/core": "^7.21.0", diff --git a/package.json b/package.json index d7b547f..cab4618 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.17.27", + "version": "0.17.28", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", @@ -92,7 +92,7 @@ "ts-node": "^10.9.1", "uuid": "^9.0.0", "ws": "^8.12.1", - "zod": "^3.21.2" + "zod": "3.21.2" }, "homepage": "https://github.com/orionprotocol/sdk#readme", "files": [ diff --git a/src/__tests__/priceFeed.test.ts b/src/__tests__/priceFeed.test.ts index c3544b6..95971ff 100644 --- a/src/__tests__/priceFeed.test.ts +++ b/src/__tests__/priceFeed.test.ts @@ -16,9 +16,30 @@ describe('Price Feed', () => { clearTimeout(timeout); unsubscribe() resolve(true); - } + }, }); }); } }); + + test('Handle error', async () => { + const orion = new Orion('testing'); + const bscUnit = orion.getUnit('bsc') + + await new Promise((resolve, reject) => { + const timeout = setTimeout(() => { + reject(new Error('Timeout')); + }, 10000); + const { unsubscribe } = bscUnit.priceFeed.ws.subscribe('ticker', { + payload: 'SGERGEWRGWERG', + callback: () => null, + errorCallback: (error) => { + expect(error.message).toContain('Can\'t recognize PriceFeed "ticker" subscription message "{"message":"Wrong pair"}"') + clearTimeout(timeout); + unsubscribe() + resolve(true); + } + }) + }); + }); }); diff --git a/src/services/PriceFeed/ws/PriceFeedSubscription.ts b/src/services/PriceFeed/ws/PriceFeedSubscription.ts index 0b6277b..5ef2637 100644 --- a/src/services/PriceFeed/ws/PriceFeedSubscription.ts +++ b/src/services/PriceFeed/ws/PriceFeedSubscription.ts @@ -5,33 +5,7 @@ import priceFeedSubscriptions from './priceFeedSubscriptions'; import { tickerInfoSchema, candleSchema } from './schemas'; import priceSchema from './schemas/priceSchema'; import type { AnyJSON } from '../../../types'; - -type TickerInfo = { - pairName: string - lastPrice: string - openPrice: string - highPrice: string - lowPrice: string - volume24h: string -} - -const allTickersSchema = z.unknown().array() - .transform((tickers) => { - const data = [...tickers]; - data.shift(); - const parsedData = tickerInfoSchema.array().parse(data); - return parsedData.reduce< - Partial< - Record< - string, - TickerInfo - > - > - >((prev, pairData) => ({ - ...prev, - [pairData.pairName]: pairData, - }), {}); - }); +import allTickersSchema from './schemas/allTickersSchema'; export const subscriptions = { [priceFeedSubscriptions.ALL_TICKERS]: { @@ -59,9 +33,11 @@ export type Subscription< > = typeof subscriptions[T] extends { payload: true } ? { callback: (data: Schema) => void + errorCallback?: (error: Error) => void payload: string } : { callback: (data: Schema) => void + errorCallback?: (error: Error) => void } export default class PriceFeedSubscription { @@ -69,6 +45,8 @@ export default class PriceFeedSubscription['callback']; + private readonly errorCallback?: Subscription['errorCallback']; + private readonly payload?: string; private ws?: WebSocket; @@ -100,6 +78,7 @@ export default class PriceFeedSubscription `[${error.path.join('.')}] ${error.message}`).join(', '); - throw new Error(`Can't recognize PriceFeed "${type}" subscription message "${dataString}": ${errorsMessage}`); + const error = new Error(`Can't recognize PriceFeed "${type}" subscription message "${dataString}": ${errorsMessage}`); + if (this.errorCallback !== undefined) { + this.errorCallback(error); + } else throw error; + } else { + this.callback(parseResult.data); } - this.callback(parseResult.data); }; this.ws.onclose = () => { diff --git a/src/services/PriceFeed/ws/schemas/allTickersSchema.ts b/src/services/PriceFeed/ws/schemas/allTickersSchema.ts new file mode 100644 index 0000000..17b917e --- /dev/null +++ b/src/services/PriceFeed/ws/schemas/allTickersSchema.ts @@ -0,0 +1,24 @@ +import { z } from 'zod'; +import tickerInfoSchema from './tickerInfoSchema'; + +type TickerInfo = z.infer + +const allTickersSchema = z.unknown().array() + .transform((tickers) => { + const data = [...tickers]; + data.shift(); + const parsedData = tickerInfoSchema.array().parse(data); + return parsedData.reduce< + Partial< + Record< + string, + TickerInfo + > + > + >((prev, pairData) => ({ + ...prev, + [pairData.pairName]: pairData, + }), {}); + }); + +export default allTickersSchema; diff --git a/src/services/PriceFeed/ws/schemas/index.ts b/src/services/PriceFeed/ws/schemas/index.ts index 74329ef..a5ff843 100644 --- a/src/services/PriceFeed/ws/schemas/index.ts +++ b/src/services/PriceFeed/ws/schemas/index.ts @@ -1,2 +1,4 @@ export { default as tickerInfoSchema } from './tickerInfoSchema'; export { default as candleSchema } from './candleSchema'; +export { default as priceSchema } from './priceSchema'; +export { default as allTickersSchema } from './allTickersSchema';