diff --git a/package.json b/package.json index 7489a90..0bcf190 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.17.9", + "version": "0.17.10", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", diff --git a/src/__tests__/priceFeed.test.ts b/src/__tests__/priceFeed.test.ts new file mode 100644 index 0000000..dfd9240 --- /dev/null +++ b/src/__tests__/priceFeed.test.ts @@ -0,0 +1,23 @@ +import Orion from '../Orion'; + +describe('Price Feed', () => { + test('Ticker', async () => { + const { unitsArray } = new Orion('testing'); + for (const unit of unitsArray) { + const ticker = 'ORN-USDT'; + await new Promise((resolve, reject) => { + const timeout = setTimeout(() => { + reject(new Error('Timeout')); + }, 10000); + const { unsubscribe } = unit.priceFeed.ws.subscribe('ticker', { + payload: ticker, + callback: () => { + clearTimeout(timeout); + unsubscribe() + resolve(true); + } + }); + }); + } + }); +}); diff --git a/src/services/PriceFeed/ws/PriceFeedSubscription.ts b/src/services/PriceFeed/ws/PriceFeedSubscription.ts index 4ed9773..44999f1 100644 --- a/src/services/PriceFeed/ws/PriceFeedSubscription.ts +++ b/src/services/PriceFeed/ws/PriceFeedSubscription.ts @@ -109,11 +109,19 @@ export default class PriceFeedSubscription { const { data } = e; - // const isBufferArray = Array.isArray(data); - // const isArrayBuffer = data instanceof ArrayBuffer; - const isBuffer = Buffer.isBuffer(data); - if (!isBuffer) throw new Error('Not a buffer'); - const dataString = data.toString(); + // Convert data to string + + let dataString: string; + if (typeof data === 'string') { + dataString = data; + } else if (Buffer.isBuffer(data)) { + dataString = data.toString(); + } else if (Array.isArray(data)) { + dataString = Buffer.concat(data).toString(); + } else { // ArrayBuffer + dataString = Buffer.from(data).toString(); + } + if (dataString === 'pong') return; const json: unknown = JSON.parse(dataString); const subscription = subscriptions[type];