Fix PriceFeed

This commit is contained in:
Aleksandr Kraiz
2023-03-03 22:14:01 +04:00
parent 44bbed0ca1
commit 66f068e7a0
5 changed files with 33 additions and 10 deletions

View File

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

View File

@@ -308,7 +308,7 @@ export default async function swap({
options?.logger?.('Redeem tx mined.');
options?.logger?.('Atomic swap completed.');
if (options?.withdrawToWallet) {
if (options?.withdrawToWallet !== undefined && options.withdrawToWallet) {
options.logger?.('Withdrawing to wallet...');
const unsignedWithdrawTx = await targetExchangeContract.populateTransaction.withdraw(
targetChainAssetAddress,

View File

@@ -11,7 +11,7 @@ import {
import UnsubscriptionType from './UnsubscriptionType';
import type {
SwapInfoBase, AssetPairUpdate, OrderbookItem,
Balance, Exchange, CFDBalance, FuturesTradeInfo, SwapInfo,
Balance, Exchange, CFDBalance, FuturesTradeInfo, SwapInfo, AnyJSON,
} from '../../../types';
import unsubscriptionDoneSchema from './schemas/unsubscriptionDoneSchema';
import assetPairConfigSchema from './schemas/assetPairConfigSchema';
@@ -202,14 +202,14 @@ class OrionAggregatorWS {
}
}
private send(data: unknown) {
if (this.ws?.readyState === 1) {
const jsonData = JSON.stringify(data);
private send(jsonObject: AnyJSON) {
if (this.ws?.readyState === WebSocket.OPEN) {
const jsonData = JSON.stringify(jsonObject);
this.ws.send(jsonData);
this.logger?.(`Sent: ${jsonData}`);
} else {
setTimeout(() => {
this.send(data);
this.send(jsonObject);
}, 50);
}
}
@@ -228,7 +228,7 @@ class OrionAggregatorWS {
const id = type === 'aobus'
? ((subscription as any).payload as string) // TODO: Refactor!!!
: uuidv4();
const subRequest: Partial<Record<string, unknown>> = {};
const subRequest: AnyJSON = {};
subRequest['T'] = type;
subRequest['id'] = id;
@@ -259,7 +259,7 @@ class OrionAggregatorWS {
this.send({
T: UNSUBSCRIBE,
S: subscription,
d: details,
...(details !== undefined) && { d: details },
});
if (subscription.includes('0x')) { // is wallet address (ADDRESS_UPDATE)

View File

@@ -4,6 +4,7 @@ import { v4 as uuidv4 } from 'uuid';
import priceFeedSubscriptions from './priceFeedSubscriptions';
import { tickerInfoSchema, candleSchema } from './schemas';
import priceSchema from './schemas/priceSchema';
import type { AnyJSON } from '../../../types';
type TickerInfo = {
pairName: string
@@ -100,6 +101,17 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
this.init();
}
private send(jsonObject: AnyJSON) {
if (this.ws?.readyState === WebSocket.OPEN) {
const jsonData = JSON.stringify(jsonObject);
this.ws.send(jsonData);
} else {
setTimeout(() => {
this.send(jsonObject);
}, 50);
}
}
init() {
this.isClosedIntentionally = false;
@@ -139,12 +151,13 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
};
this.heartbeatInterval = setInterval(() => {
this.ws?.send('heartbeat');
this.send('heartbeat');
}, 15000);
}
kill() {
this.isClosedIntentionally = true;
this.ws?.close();
clearInterval(this.heartbeatInterval);
}
}

View File

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/consistent-type-definitions */
import type BigNumber from 'bignumber.js';
import type exchanges from './constants/exchanges';
import type subOrderStatuses from './constants/subOrderStatuses';
@@ -292,3 +293,12 @@ export type VerboseOrionUnitConfig = {
}
export type KnownEnv = typeof knownEnvs[number];
export type AnyJSON = string | number | boolean | null | JSONObject | JSONArray;
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
interface JSONObject {
[x: string]: AnyJSON
}
interface JSONArray extends Array<AnyJSON> {}