@gainsnetwork/sdk 0.0.24 → 0.0.25-rc1

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.
Files changed (45) hide show
  1. package/lib/contracts/addresses.json +12 -3
  2. package/lib/contracts/index.js +3 -1
  3. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
  4. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
  5. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
  6. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
  7. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
  8. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
  9. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
  10. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
  11. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  12. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  13. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
  14. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
  15. package/lib/contracts/types/generated/factories/index.d.ts +3 -0
  16. package/lib/contracts/types/generated/factories/index.js +7 -1
  17. package/lib/contracts/types/generated/index.d.ts +6 -0
  18. package/lib/contracts/types/generated/index.js +7 -1
  19. package/lib/contracts/types/index.d.ts +7 -2
  20. package/lib/contracts/utils/openLimitOrders.js +1 -1
  21. package/lib/contracts/utils/openTrades.js +14 -11
  22. package/lib/index.d.ts +2 -0
  23. package/lib/index.js +3 -0
  24. package/lib/trade/fees/borrowing/index.d.ts +53 -0
  25. package/lib/trade/fees/borrowing/index.js +156 -0
  26. package/lib/trade/fees/borrowing/types.d.ts +32 -0
  27. package/lib/trade/fees/borrowing/types.js +2 -0
  28. package/lib/trade/{fees.d.ts → fees/index.d.ts} +2 -1
  29. package/lib/trade/{fees.js → fees/index.js} +15 -0
  30. package/lib/trade/index.d.ts +3 -0
  31. package/lib/trade/index.js +3 -0
  32. package/lib/trade/liquidation.d.ts +7 -0
  33. package/lib/trade/liquidation.js +18 -0
  34. package/lib/trade/pnl.d.ts +5 -3
  35. package/lib/trade/pnl.js +4 -3
  36. package/lib/trade/types.d.ts +12 -0
  37. package/lib/vault/index.d.ts +7 -0
  38. package/lib/vault/index.js +11 -0
  39. package/package.json +1 -1
  40. package/lib/contracts/utils/tradeLastUpdated.d.ts +0 -6
  41. package/lib/contracts/utils/tradeLastUpdated.js +0 -25
  42. package/lib/prices/PricingNetworkClient.d.ts +0 -22
  43. package/lib/prices/PricingNetworkClient.js +0 -179
  44. package/lib/prices/index.d.ts +0 -1
  45. package/lib/prices/index.js +0 -17
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPendingAccBlockWeightedMarketCap = void 0;
4
+ const MC_PRECISION = 1e18;
5
+ const getPendingAccBlockWeightedMarketCap = (currentBlock, context) => {
6
+ const { marketCap, accBlockWeightedMarketCap, accBlockWeightedMarketCapLastStored, } = context;
7
+ return (accBlockWeightedMarketCap +
8
+ (currentBlock - accBlockWeightedMarketCapLastStored) /
9
+ Math.max(marketCap * MC_PRECISION, 1));
10
+ };
11
+ exports.getPendingAccBlockWeightedMarketCap = getPendingAccBlockWeightedMarketCap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.0.24",
3
+ "version": "0.0.25-rc1",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -1,6 +0,0 @@
1
- import { BlockTag } from "@/contracts/types";
2
- export type FetchTradesLastUpdated = {
3
- pairBatchSize?: number;
4
- useMulticall?: boolean;
5
- blockTag?: BlockTag;
6
- };
@@ -1,25 +0,0 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const ethcall_1 = require("ethcall");
13
- function populateTradesLastUpdated(openLimitOrders, pairTrades) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- appLogger.info("Fetching last updated info...");
16
- const ethersProvider = new ethers.providers.WebSocketProvider(currentlySelectedWeb3Client.currentProvider.connection._url);
17
- const multicallProvider = new ethcall_1.Provider();
18
- yield multicallProvider.init(ethersProvider);
19
- const callbacksContractMulticall = new ethcall_1.Contract(callbacksContract.options.address, abis.CALLBACKS);
20
- const olLastUpdated = yield multicallProvider.all(openLimitOrders.map(order => callbacksContractMulticall.tradeLastUpdated(order.trader, order.pairIndex, order.index, TRADE_TYPE.LIMIT)), "latest");
21
- const tLastUpdated = yield multicallProvider.all(pairTrades.map(order => callbacksContractMulticall.tradeLastUpdated(order.trader, order.pairIndex, order.index, TRADE_TYPE.MARKET)), "latest");
22
- appLogger.info(`Fetched last updated info for ${tLastUpdated.length + olLastUpdated.length} trade(s).`);
23
- return transformLastUpdated(openLimitOrders, olLastUpdated, pairTrades, tLastUpdated);
24
- });
25
- }
@@ -1,22 +0,0 @@
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
- }
@@ -1,179 +0,0 @@
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 +0,0 @@
1
- export * from "./PricingNetworkClient";
@@ -1,17 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./PricingNetworkClient"), exports);