@gainsnetwork/sdk 0.0.20-rc1 → 0.0.20-rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ import WebSocket from "ws";
2
+ export declare class PricingNetworkClient {
3
+ private endpoints;
4
+ private _onPriceMessage;
5
+ private concurrentConnections;
6
+ private bufferTimeoutMs;
7
+ private reconnectTimeoutMs;
8
+ private sockets;
9
+ private latencyMap;
10
+ private priceBuffer;
11
+ private priceBufferLastFlush;
12
+ private stagedPrices;
13
+ private pairsToIndex;
14
+ constructor(endpoints: string[], onPriceMessage: (prices: number[]) => void, concurrentConnections?: number, bufferTimeoutMs?: number, reconnectTimeoutMs?: number);
15
+ connect(): Promise<void>;
16
+ private _connect;
17
+ private fallbackToNextEndpoint;
18
+ private measureLatency;
19
+ private processMessage;
20
+ get activeSockets(): Map<string, WebSocket>;
21
+ set onPriceMessage(callback: (prices: number[]) => void);
22
+ }
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.PricingNetworkClient = void 0;
16
+ /* eslint-disable @typescript-eslint/restrict-template-expressions */
17
+ /* eslint-disable @typescript-eslint/no-unsafe-call */
18
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
19
+ const ws_1 = __importDefault(require("ws"));
20
+ const __1 = require("..");
21
+ class PricingNetworkClient {
22
+ constructor(endpoints, onPriceMessage, concurrentConnections = 3, bufferTimeoutMs = 500, reconnectTimeoutMs = 1000) {
23
+ this.endpoints = endpoints;
24
+ this._onPriceMessage = onPriceMessage;
25
+ this.concurrentConnections = concurrentConnections;
26
+ this.bufferTimeoutMs = bufferTimeoutMs;
27
+ this.reconnectTimeoutMs = reconnectTimeoutMs;
28
+ this.sockets = new Map();
29
+ this.latencyMap = new Map();
30
+ this.priceBuffer = new Map();
31
+ this.priceBufferLastFlush = new Map();
32
+ this.stagedPrices = [];
33
+ this.pairsToIndex = new Map();
34
+ Object.keys(__1.pairs).forEach((pair, index) => {
35
+ this.pairsToIndex.set(pair, index);
36
+ });
37
+ }
38
+ connect() {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ if (this.sockets.size > 0) {
41
+ return;
42
+ }
43
+ const latencies = yield Promise.all(this.endpoints.map(endpoint => this.measureLatency(endpoint)));
44
+ this.endpoints.forEach((endpoint, index) => this.latencyMap.set(endpoint, latencies[index]));
45
+ this.latencyMap.forEach((latency, endpoint) => console.log(`Latency to ${endpoint}: ${latency}ms`));
46
+ // Sort endpoints by latency
47
+ const sortedEndpoints = [...this.endpoints].sort((a, b) => {
48
+ const aLatency = latencies[this.endpoints.indexOf(a)];
49
+ const bLatency = latencies[this.endpoints.indexOf(b)];
50
+ return aLatency - bLatency;
51
+ });
52
+ for (let i = 0; i < this.concurrentConnections; i++) {
53
+ if (sortedEndpoints[i])
54
+ this._connect(sortedEndpoints[i]);
55
+ }
56
+ // Backstop to flush the buffer in case we don't get any messages
57
+ setInterval(() => {
58
+ const now = Date.now();
59
+ this.priceBuffer.forEach((buffer, pair) => {
60
+ const lastFlush = this.priceBufferLastFlush.get(pair);
61
+ if (buffer.length > 0 &&
62
+ lastFlush &&
63
+ now - lastFlush > this.bufferTimeoutMs) {
64
+ const median = buffer.sort()[Math.floor(buffer.length / 2)];
65
+ this.stagedPrices.push(pair, median);
66
+ buffer.length = 0;
67
+ }
68
+ });
69
+ }, this.bufferTimeoutMs);
70
+ });
71
+ }
72
+ _connect(endpoint) {
73
+ const socket = new ws_1.default(endpoint);
74
+ this.sockets.set(endpoint, socket);
75
+ socket.onmessage = message => {
76
+ // Ignore return pong messages
77
+ if (message.data === "pong") {
78
+ return;
79
+ }
80
+ this.processMessage(message);
81
+ };
82
+ socket.onclose = () => {
83
+ this.sockets.delete(endpoint);
84
+ setTimeout(() => {
85
+ this.fallbackToNextEndpoint();
86
+ }, this.reconnectTimeoutMs);
87
+ };
88
+ socket.onerror = error => {
89
+ console.error(`WebSocket error: ${error.message}`);
90
+ };
91
+ }
92
+ fallbackToNextEndpoint() {
93
+ const connectedEndpoints = [...this.sockets.keys()];
94
+ const sortedAvailableEndpoints = [...this.endpoints]
95
+ .sort((a, b) => {
96
+ const aLatency = this.latencyMap.get(a) || Number.MAX_VALUE;
97
+ const bLatency = this.latencyMap.get(b) || Number.MAX_VALUE;
98
+ return aLatency - bLatency;
99
+ })
100
+ .filter(endpoint => !connectedEndpoints.includes(endpoint));
101
+ if (sortedAvailableEndpoints.length > 0) {
102
+ this._connect(sortedAvailableEndpoints[0]);
103
+ }
104
+ }
105
+ measureLatency(endpoint) {
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ return new Promise(resolve => {
108
+ const startTime = Date.now();
109
+ const socket = new ws_1.default(endpoint);
110
+ socket.onopen = () => {
111
+ socket.send("ping");
112
+ };
113
+ socket.onmessage = message => {
114
+ if (message.data === "pong") {
115
+ const latency = Date.now() - startTime;
116
+ socket.close();
117
+ resolve(latency);
118
+ }
119
+ };
120
+ socket.on("error", e => {
121
+ resolve(Number.MAX_VALUE);
122
+ });
123
+ // Backstop so we don't wait forever
124
+ setTimeout(() => {
125
+ socket.close();
126
+ resolve(2000);
127
+ }, 2000);
128
+ });
129
+ });
130
+ }
131
+ processMessage(message) {
132
+ var _a;
133
+ try {
134
+ const priceUpdates = JSON.parse(message.data.toString());
135
+ if (!priceUpdates) {
136
+ console.log("Invalid price update message received", priceUpdates);
137
+ return;
138
+ }
139
+ const finalMessage = [];
140
+ for (let i = 0; i < priceUpdates.length; i += 1) {
141
+ const pairAndPrice = priceUpdates[i];
142
+ const pair = this.pairsToIndex.get(pairAndPrice[0]);
143
+ if (pair === undefined) {
144
+ console.log("Invalid pair received", pairAndPrice[0]);
145
+ continue;
146
+ }
147
+ const price = pairAndPrice[1];
148
+ if (!this.priceBuffer.has(pair)) {
149
+ this.priceBuffer.set(pair, []);
150
+ this.priceBufferLastFlush.set(pair, 0);
151
+ }
152
+ // Checking existence above, so this should be safe
153
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
154
+ const buffer = this.priceBuffer.get(pair);
155
+ if (buffer.length >= this.sockets.size) {
156
+ const median = buffer.sort()[Math.floor(buffer.length / 2)];
157
+ finalMessage.push(pair, median);
158
+ buffer.length = 0;
159
+ }
160
+ buffer.push(price);
161
+ }
162
+ if (finalMessage.length > 0 || this.stagedPrices.length > 0) {
163
+ finalMessage.push(...(this.stagedPrices || []));
164
+ (_a = this._onPriceMessage) === null || _a === void 0 ? void 0 : _a.call(this, finalMessage);
165
+ }
166
+ }
167
+ catch (e) {
168
+ console.error(e);
169
+ }
170
+ }
171
+ get activeSockets() {
172
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
173
+ return this.sockets;
174
+ }
175
+ set onPriceMessage(callback) {
176
+ this._onPriceMessage = callback;
177
+ }
178
+ }
179
+ exports.PricingNetworkClient = PricingNetworkClient;
@@ -1 +1 @@
1
- export * from "./PricingStreamClient";
1
+ export * from "./PricingNetworkClient";
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./PricingStreamClient"), exports);
17
+ __exportStar(require("./PricingNetworkClient"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.0.20-rc1",
3
+ "version": "0.0.20-rc2",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [