@drift-labs/sdk 0.2.0-master.24 → 0.2.0-master.26
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.
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
- package/lib/accounts/types.d.ts +8 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
- package/lib/addresses/pda.d.ts +8 -6
- package/lib/addresses/pda.js +35 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +90 -54
- package/lib/clearingHouse.d.ts +71 -42
- package/lib/clearingHouse.js +765 -282
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +19 -17
- package/lib/clearingHouseUser.js +145 -123
- package/lib/config.d.ts +7 -7
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +12 -12
- package/lib/constants/numericConstants.js +13 -13
- package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
- package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
- package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
- package/lib/constants/{banks.js → spotMarkets.js} +16 -16
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/types.d.ts +4 -2
- package/lib/events/types.js +2 -0
- package/lib/examples/makeTradeExample.js +18 -6
- package/lib/idl/clearing_house.json +1128 -347
- package/lib/index.d.ts +6 -3
- package/lib/index.js +6 -3
- package/lib/math/amm.d.ts +5 -3
- package/lib/math/amm.js +42 -4
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +2 -1
- package/lib/math/margin.d.ts +4 -4
- package/lib/math/margin.js +18 -11
- package/lib/math/market.d.ts +10 -9
- package/lib/math/market.js +29 -6
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +5 -5
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +19 -0
- package/lib/math/spotBalance.js +176 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +2 -0
- package/lib/math/spotPosition.js +8 -0
- package/lib/math/state.js +2 -2
- package/lib/math/trade.d.ts +4 -4
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +4 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +148 -39
- package/lib/types.js +37 -9
- package/my-script/.env +7 -0
- package/my-script/getUserStats.ts +106 -0
- package/my-script/multiConnections.ts +119 -0
- package/my-script/test-regex.ts +11 -0
- package/my-script/utils.ts +52 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +249 -0
- package/src/accounts/bulkUserStatsSubscription.js +75 -0
- package/src/accounts/bulkUserSubscription.js +75 -0
- package/src/accounts/fetch.js +92 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
- package/src/accounts/pollingOracleSubscriber.js +156 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
- package/src/accounts/pollingUserAccountSubscriber.js +208 -0
- package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
- package/src/accounts/types.js +28 -0
- package/src/accounts/types.ts +11 -9
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +138 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
- package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
- package/src/addresses/pda.js +186 -0
- package/src/addresses/pda.ts +56 -42
- package/src/admin.js +1284 -0
- package/src/admin.ts +149 -75
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1097 -380
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +237 -172
- package/src/clearingHouseUserConfig.js +2 -0
- package/src/clearingHouseUserStats.js +115 -0
- package/src/clearingHouseUserStatsConfig.js +2 -0
- package/src/config.js +80 -0
- package/src/config.ts +30 -30
- package/src/constants/numericConstants.js +18 -11
- package/src/constants/numericConstants.ts +17 -15
- package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/spotMarkets.js +51 -0
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/events/eventList.js +66 -23
- package/src/events/eventSubscriber.js +202 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.js +117 -0
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.js +113 -0
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.js +41 -0
- package/src/events/txEventCache.js +22 -19
- package/src/events/types.js +25 -0
- package/src/events/types.ts +7 -1
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +27 -6
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1128 -347
- package/src/index.js +75 -0
- package/src/index.ts +6 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +73 -5
- package/src/math/auction.js +10 -10
- package/src/math/conversion.js +4 -3
- package/src/math/funding.js +223 -175
- package/src/math/funding.ts +7 -7
- package/src/math/insurance.js +27 -0
- package/src/math/margin.js +77 -0
- package/src/math/margin.ts +34 -23
- package/src/math/market.js +105 -0
- package/src/math/market.ts +71 -19
- package/src/math/oracles.js +40 -10
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.js +153 -0
- package/src/math/orders.ts +5 -5
- package/src/math/position.js +172 -0
- package/src/math/position.ts +31 -31
- package/src/math/repeg.js +40 -40
- package/src/math/spotBalance.js +176 -0
- package/src/math/spotBalance.ts +290 -0
- package/src/math/spotMarket.js +8 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.js +8 -0
- package/src/math/spotPosition.ts +6 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.js +81 -74
- package/src/math/trade.ts +4 -4
- package/src/math/utils.js +8 -7
- package/src/oracles/oracleClientCache.js +10 -9
- package/src/oracles/pythClient.js +52 -17
- package/src/oracles/quoteAssetOracleClient.js +44 -13
- package/src/oracles/switchboardClient.js +69 -37
- package/src/oracles/types.js +1 -1
- package/src/orderParams.js +14 -6
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.js +102 -0
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.js +2 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.js +67 -20
- package/src/token/index.js +4 -4
- package/src/tokenFaucet.js +288 -154
- package/src/tx/retryTxSender.js +280 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.js +1 -1
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +7 -6
- package/src/types.js +216 -0
- package/src/types.ts +131 -39
- package/src/userName.js +5 -5
- package/src/util/computeUnits.js +46 -11
- package/src/util/promiseTimeout.js +5 -5
- package/src/util/tps.js +46 -12
- package/src/wallet.js +55 -18
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/examples/makeTradeExample.js +0 -80
- package/src/math/bankBalance.ts +0 -258
- package/src/math/state.js +0 -15
- package/src/math/utils.js.map +0 -1
- package/src/util/getTokenAddress.js +0 -9
|
@@ -0,0 +1,92 @@
|
|
|
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
39
|
+
exports.fetchUserStatsAccount = exports.fetchUserAccounts = void 0;
|
|
40
|
+
var pda_1 = require("../addresses/pda");
|
|
41
|
+
function fetchUserAccounts(connection, program, authority, limit) {
|
|
42
|
+
if (limit === void 0) { limit = 8; }
|
|
43
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
44
|
+
var userAccountPublicKeys, i, _a, _b, accountInfos;
|
|
45
|
+
return __generator(this, function (_c) {
|
|
46
|
+
switch (_c.label) {
|
|
47
|
+
case 0:
|
|
48
|
+
userAccountPublicKeys = new Array();
|
|
49
|
+
i = 0;
|
|
50
|
+
_c.label = 1;
|
|
51
|
+
case 1:
|
|
52
|
+
if (!(i < limit)) return [3 /*break*/, 4];
|
|
53
|
+
_b = (_a = userAccountPublicKeys).push;
|
|
54
|
+
return [4 /*yield*/, (0, pda_1.getUserAccountPublicKey)(program.programId, authority, i)];
|
|
55
|
+
case 2:
|
|
56
|
+
_b.apply(_a, [_c.sent()]);
|
|
57
|
+
_c.label = 3;
|
|
58
|
+
case 3:
|
|
59
|
+
i++;
|
|
60
|
+
return [3 /*break*/, 1];
|
|
61
|
+
case 4: return [4 /*yield*/, connection.getMultipleAccountsInfo(userAccountPublicKeys, 'confirmed')];
|
|
62
|
+
case 5:
|
|
63
|
+
accountInfos = _c.sent();
|
|
64
|
+
return [2 /*return*/, accountInfos.map(function (accountInfo) {
|
|
65
|
+
if (!accountInfo) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
return program.account.user.coder.accounts.decode('User', accountInfo.data);
|
|
69
|
+
})];
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
exports.fetchUserAccounts = fetchUserAccounts;
|
|
75
|
+
function fetchUserStatsAccount(connection, program, authority) {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
77
|
+
var userStatsPublicKey, accountInfo;
|
|
78
|
+
return __generator(this, function (_a) {
|
|
79
|
+
switch (_a.label) {
|
|
80
|
+
case 0:
|
|
81
|
+
userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(program.programId, authority);
|
|
82
|
+
return [4 /*yield*/, connection.getAccountInfo(userStatsPublicKey, 'confirmed')];
|
|
83
|
+
case 1:
|
|
84
|
+
accountInfo = _a.sent();
|
|
85
|
+
return [2 /*return*/, accountInfo
|
|
86
|
+
? program.account.user.coder.accounts.decode('UserStats', accountInfo.data)
|
|
87
|
+
: undefined];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
exports.fetchUserStatsAccount = fetchUserStatsAccount;
|
|
@@ -0,0 +1,465 @@
|
|
|
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
39
|
+
exports.PollingClearingHouseAccountSubscriber = void 0;
|
|
40
|
+
var types_1 = require("./types");
|
|
41
|
+
var events_1 = require("events");
|
|
42
|
+
var pda_1 = require("../addresses/pda");
|
|
43
|
+
var utils_1 = require("./utils");
|
|
44
|
+
var web3_js_1 = require("@solana/web3.js");
|
|
45
|
+
var oracleClientCache_1 = require("../oracles/oracleClientCache");
|
|
46
|
+
var quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
|
|
47
|
+
var PollingClearingHouseAccountSubscriber = /** @class */ (function () {
|
|
48
|
+
function PollingClearingHouseAccountSubscriber(program, accountLoader, perpMarketIndexes, spotMarketIndexes, oracleInfos) {
|
|
49
|
+
this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
|
|
50
|
+
this.accountsToPoll = new Map();
|
|
51
|
+
this.oraclesToPoll = new Map();
|
|
52
|
+
this.perpMarket = new Map();
|
|
53
|
+
this.spotMarket = new Map();
|
|
54
|
+
this.oracles = new Map();
|
|
55
|
+
this.isSubscribing = false;
|
|
56
|
+
this.isSubscribed = false;
|
|
57
|
+
this.program = program;
|
|
58
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
59
|
+
this.accountLoader = accountLoader;
|
|
60
|
+
this.perpMarketIndexes = perpMarketIndexes;
|
|
61
|
+
this.spotMarketIndexes = spotMarketIndexes;
|
|
62
|
+
this.oracleInfos = oracleInfos;
|
|
63
|
+
}
|
|
64
|
+
PollingClearingHouseAccountSubscriber.prototype.subscribe = function () {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
66
|
+
var subscriptionSucceeded, retries;
|
|
67
|
+
var _this = this;
|
|
68
|
+
return __generator(this, function (_a) {
|
|
69
|
+
switch (_a.label) {
|
|
70
|
+
case 0:
|
|
71
|
+
if (this.isSubscribed) {
|
|
72
|
+
return [2 /*return*/, true];
|
|
73
|
+
}
|
|
74
|
+
if (!this.isSubscribing) return [3 /*break*/, 2];
|
|
75
|
+
return [4 /*yield*/, this.subscriptionPromise];
|
|
76
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
77
|
+
case 2:
|
|
78
|
+
this.isSubscribing = true;
|
|
79
|
+
this.subscriptionPromise = new Promise(function (res) {
|
|
80
|
+
_this.subscriptionPromiseResolver = res;
|
|
81
|
+
});
|
|
82
|
+
return [4 /*yield*/, this.updateAccountsToPoll()];
|
|
83
|
+
case 3:
|
|
84
|
+
_a.sent();
|
|
85
|
+
return [4 /*yield*/, this.updateOraclesToPoll()];
|
|
86
|
+
case 4:
|
|
87
|
+
_a.sent();
|
|
88
|
+
return [4 /*yield*/, this.addToAccountLoader()];
|
|
89
|
+
case 5:
|
|
90
|
+
_a.sent();
|
|
91
|
+
subscriptionSucceeded = false;
|
|
92
|
+
retries = 0;
|
|
93
|
+
_a.label = 6;
|
|
94
|
+
case 6:
|
|
95
|
+
if (!(!subscriptionSucceeded && retries < 5)) return [3 /*break*/, 8];
|
|
96
|
+
return [4 /*yield*/, this.fetch()];
|
|
97
|
+
case 7:
|
|
98
|
+
_a.sent();
|
|
99
|
+
subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
100
|
+
retries++;
|
|
101
|
+
return [3 /*break*/, 6];
|
|
102
|
+
case 8:
|
|
103
|
+
if (subscriptionSucceeded) {
|
|
104
|
+
this.eventEmitter.emit('update');
|
|
105
|
+
}
|
|
106
|
+
this.isSubscribing = false;
|
|
107
|
+
this.isSubscribed = subscriptionSucceeded;
|
|
108
|
+
this.subscriptionPromiseResolver(subscriptionSucceeded);
|
|
109
|
+
return [2 /*return*/, subscriptionSucceeded];
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
PollingClearingHouseAccountSubscriber.prototype.updateAccountsToPoll = function () {
|
|
115
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
116
|
+
var accounts;
|
|
117
|
+
return __generator(this, function (_a) {
|
|
118
|
+
switch (_a.label) {
|
|
119
|
+
case 0:
|
|
120
|
+
if (this.accountsToPoll.size > 0) {
|
|
121
|
+
return [2 /*return*/];
|
|
122
|
+
}
|
|
123
|
+
return [4 /*yield*/, this.getClearingHouseAccounts()];
|
|
124
|
+
case 1:
|
|
125
|
+
accounts = _a.sent();
|
|
126
|
+
this.accountsToPoll.set(accounts.state.toString(), {
|
|
127
|
+
key: 'state',
|
|
128
|
+
publicKey: accounts.state,
|
|
129
|
+
eventType: 'stateAccountUpdate'
|
|
130
|
+
});
|
|
131
|
+
return [4 /*yield*/, this.updateMarketAccountsToPoll()];
|
|
132
|
+
case 2:
|
|
133
|
+
_a.sent();
|
|
134
|
+
return [4 /*yield*/, this.updateSpotMarketAccountsToPoll()];
|
|
135
|
+
case 3:
|
|
136
|
+
_a.sent();
|
|
137
|
+
return [2 /*return*/];
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
PollingClearingHouseAccountSubscriber.prototype.updateMarketAccountsToPoll = function () {
|
|
143
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
144
|
+
var _i, _a, marketIndex;
|
|
145
|
+
return __generator(this, function (_b) {
|
|
146
|
+
switch (_b.label) {
|
|
147
|
+
case 0:
|
|
148
|
+
_i = 0, _a = this.perpMarketIndexes;
|
|
149
|
+
_b.label = 1;
|
|
150
|
+
case 1:
|
|
151
|
+
if (!(_i < _a.length)) return [3 /*break*/, 4];
|
|
152
|
+
marketIndex = _a[_i];
|
|
153
|
+
return [4 /*yield*/, this.addMarketAccountToPoll(marketIndex)];
|
|
154
|
+
case 2:
|
|
155
|
+
_b.sent();
|
|
156
|
+
_b.label = 3;
|
|
157
|
+
case 3:
|
|
158
|
+
_i++;
|
|
159
|
+
return [3 /*break*/, 1];
|
|
160
|
+
case 4: return [2 /*return*/, true];
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
PollingClearingHouseAccountSubscriber.prototype.addMarketAccountToPoll = function (marketIndex) {
|
|
166
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
167
|
+
var marketPublicKey;
|
|
168
|
+
return __generator(this, function (_a) {
|
|
169
|
+
switch (_a.label) {
|
|
170
|
+
case 0: return [4 /*yield*/, (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex)];
|
|
171
|
+
case 1:
|
|
172
|
+
marketPublicKey = _a.sent();
|
|
173
|
+
this.accountsToPoll.set(marketPublicKey.toString(), {
|
|
174
|
+
key: 'perpMarket',
|
|
175
|
+
publicKey: marketPublicKey,
|
|
176
|
+
eventType: 'perpMarketAccountUpdate',
|
|
177
|
+
mapKey: marketIndex.toNumber()
|
|
178
|
+
});
|
|
179
|
+
return [2 /*return*/, true];
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
PollingClearingHouseAccountSubscriber.prototype.updateSpotMarketAccountsToPoll = function () {
|
|
185
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
186
|
+
var _i, _a, marketIndex;
|
|
187
|
+
return __generator(this, function (_b) {
|
|
188
|
+
switch (_b.label) {
|
|
189
|
+
case 0:
|
|
190
|
+
_i = 0, _a = this.spotMarketIndexes;
|
|
191
|
+
_b.label = 1;
|
|
192
|
+
case 1:
|
|
193
|
+
if (!(_i < _a.length)) return [3 /*break*/, 4];
|
|
194
|
+
marketIndex = _a[_i];
|
|
195
|
+
return [4 /*yield*/, this.addSpotMarketAccountToPoll(marketIndex)];
|
|
196
|
+
case 2:
|
|
197
|
+
_b.sent();
|
|
198
|
+
_b.label = 3;
|
|
199
|
+
case 3:
|
|
200
|
+
_i++;
|
|
201
|
+
return [3 /*break*/, 1];
|
|
202
|
+
case 4: return [2 /*return*/, true];
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
PollingClearingHouseAccountSubscriber.prototype.addSpotMarketAccountToPoll = function (marketIndex) {
|
|
208
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
209
|
+
var marketPublicKey;
|
|
210
|
+
return __generator(this, function (_a) {
|
|
211
|
+
switch (_a.label) {
|
|
212
|
+
case 0: return [4 /*yield*/, (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex)];
|
|
213
|
+
case 1:
|
|
214
|
+
marketPublicKey = _a.sent();
|
|
215
|
+
this.accountsToPoll.set(marketPublicKey.toString(), {
|
|
216
|
+
key: 'spotMarket',
|
|
217
|
+
publicKey: marketPublicKey,
|
|
218
|
+
eventType: 'spotMarketAccountUpdate',
|
|
219
|
+
mapKey: marketIndex.toNumber()
|
|
220
|
+
});
|
|
221
|
+
return [2 /*return*/, true];
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
PollingClearingHouseAccountSubscriber.prototype.updateOraclesToPoll = function () {
|
|
227
|
+
for (var _i = 0, _a = this.oracleInfos; _i < _a.length; _i++) {
|
|
228
|
+
var oracleInfo = _a[_i];
|
|
229
|
+
if (!oracleInfo.publicKey.equals(web3_js_1.PublicKey["default"])) {
|
|
230
|
+
this.addOracleToPoll(oracleInfo);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return true;
|
|
234
|
+
};
|
|
235
|
+
PollingClearingHouseAccountSubscriber.prototype.addOracleToPoll = function (oracleInfo) {
|
|
236
|
+
this.oraclesToPoll.set(oracleInfo.publicKey.toString(), {
|
|
237
|
+
publicKey: oracleInfo.publicKey,
|
|
238
|
+
source: oracleInfo.source
|
|
239
|
+
});
|
|
240
|
+
return true;
|
|
241
|
+
};
|
|
242
|
+
PollingClearingHouseAccountSubscriber.prototype.getClearingHouseAccounts = function () {
|
|
243
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
244
|
+
var statePublicKey, accounts;
|
|
245
|
+
return __generator(this, function (_a) {
|
|
246
|
+
switch (_a.label) {
|
|
247
|
+
case 0: return [4 /*yield*/, (0, pda_1.getClearingHouseStateAccountPublicKey)(this.program.programId)];
|
|
248
|
+
case 1:
|
|
249
|
+
statePublicKey = _a.sent();
|
|
250
|
+
accounts = {
|
|
251
|
+
state: statePublicKey
|
|
252
|
+
};
|
|
253
|
+
return [2 /*return*/, accounts];
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
};
|
|
258
|
+
PollingClearingHouseAccountSubscriber.prototype.addToAccountLoader = function () {
|
|
259
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
260
|
+
var _i, _a, _b, _1, accountToPoll, _c, _d, _e, _2, oracleToPoll;
|
|
261
|
+
var _this = this;
|
|
262
|
+
return __generator(this, function (_f) {
|
|
263
|
+
for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
|
|
264
|
+
_b = _a[_i], _1 = _b[0], accountToPoll = _b[1];
|
|
265
|
+
this.addAccountToAccountLoader(accountToPoll);
|
|
266
|
+
}
|
|
267
|
+
for (_c = 0, _d = this.oraclesToPoll; _c < _d.length; _c++) {
|
|
268
|
+
_e = _d[_c], _2 = _e[0], oracleToPoll = _e[1];
|
|
269
|
+
this.addOracleToAccountLoader(oracleToPoll);
|
|
270
|
+
}
|
|
271
|
+
this.errorCallbackId = this.accountLoader.addErrorCallbacks(function (error) {
|
|
272
|
+
_this.eventEmitter.emit('error', error);
|
|
273
|
+
});
|
|
274
|
+
return [2 /*return*/];
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
};
|
|
278
|
+
PollingClearingHouseAccountSubscriber.prototype.addAccountToAccountLoader = function (accountToPoll) {
|
|
279
|
+
var _this = this;
|
|
280
|
+
accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, function (buffer, slot) {
|
|
281
|
+
if (!buffer)
|
|
282
|
+
return;
|
|
283
|
+
var account = _this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
284
|
+
var dataAndSlot = {
|
|
285
|
+
data: account,
|
|
286
|
+
slot: slot
|
|
287
|
+
};
|
|
288
|
+
if (accountToPoll.mapKey != undefined) {
|
|
289
|
+
_this[accountToPoll.key].set(accountToPoll.mapKey, dataAndSlot);
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
_this[accountToPoll.key] = dataAndSlot;
|
|
293
|
+
}
|
|
294
|
+
// @ts-ignore
|
|
295
|
+
_this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
296
|
+
_this.eventEmitter.emit('update');
|
|
297
|
+
if (!_this.isSubscribed) {
|
|
298
|
+
_this.isSubscribed = _this.didSubscriptionSucceed();
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
PollingClearingHouseAccountSubscriber.prototype.addOracleToAccountLoader = function (oracleToPoll) {
|
|
303
|
+
var _this = this;
|
|
304
|
+
var oracleClient = this.oracleClientCache.get(oracleToPoll.source, this.program.provider.connection);
|
|
305
|
+
oracleToPoll.callbackId = this.accountLoader.addAccount(oracleToPoll.publicKey, function (buffer, slot) {
|
|
306
|
+
if (!buffer)
|
|
307
|
+
return;
|
|
308
|
+
var oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(buffer);
|
|
309
|
+
var dataAndSlot = {
|
|
310
|
+
data: oraclePriceData,
|
|
311
|
+
slot: slot
|
|
312
|
+
};
|
|
313
|
+
_this.oracles.set(oracleToPoll.publicKey.toString(), dataAndSlot);
|
|
314
|
+
_this.eventEmitter.emit('oraclePriceUpdate', oracleToPoll.publicKey, oraclePriceData);
|
|
315
|
+
_this.eventEmitter.emit('update');
|
|
316
|
+
});
|
|
317
|
+
};
|
|
318
|
+
PollingClearingHouseAccountSubscriber.prototype.fetch = function () {
|
|
319
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
320
|
+
var _i, _a, _b, _3, accountToPoll, _c, buffer, slot, account, _d, _e, _f, _4, oracleToPoll, _g, buffer, slot, oracleClient, oraclePriceData;
|
|
321
|
+
return __generator(this, function (_h) {
|
|
322
|
+
switch (_h.label) {
|
|
323
|
+
case 0: return [4 /*yield*/, this.accountLoader.load()];
|
|
324
|
+
case 1:
|
|
325
|
+
_h.sent();
|
|
326
|
+
for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
|
|
327
|
+
_b = _a[_i], _3 = _b[0], accountToPoll = _b[1];
|
|
328
|
+
_c = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey), buffer = _c.buffer, slot = _c.slot;
|
|
329
|
+
if (buffer) {
|
|
330
|
+
account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
331
|
+
if (accountToPoll.mapKey != undefined) {
|
|
332
|
+
this[accountToPoll.key].set(accountToPoll.mapKey, {
|
|
333
|
+
data: account,
|
|
334
|
+
slot: slot
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
this[accountToPoll.key] = {
|
|
339
|
+
data: account,
|
|
340
|
+
slot: slot
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
for (_d = 0, _e = this.oraclesToPoll; _d < _e.length; _d++) {
|
|
346
|
+
_f = _e[_d], _4 = _f[0], oracleToPoll = _f[1];
|
|
347
|
+
_g = this.accountLoader.getBufferAndSlot(oracleToPoll.publicKey), buffer = _g.buffer, slot = _g.slot;
|
|
348
|
+
if (buffer) {
|
|
349
|
+
oracleClient = this.oracleClientCache.get(oracleToPoll.source, this.program.provider.connection);
|
|
350
|
+
oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(buffer);
|
|
351
|
+
this.oracles.set(oracleToPoll.publicKey.toString(), {
|
|
352
|
+
data: oraclePriceData,
|
|
353
|
+
slot: slot
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return [2 /*return*/];
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
PollingClearingHouseAccountSubscriber.prototype.didSubscriptionSucceed = function () {
|
|
363
|
+
if (this.state)
|
|
364
|
+
return true;
|
|
365
|
+
return false;
|
|
366
|
+
};
|
|
367
|
+
PollingClearingHouseAccountSubscriber.prototype.unsubscribe = function () {
|
|
368
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
369
|
+
var _i, _a, _b, _5, accountToPoll, _c, _d, _e, _6, oracleToPoll;
|
|
370
|
+
return __generator(this, function (_f) {
|
|
371
|
+
if (!this.isSubscribed) {
|
|
372
|
+
return [2 /*return*/];
|
|
373
|
+
}
|
|
374
|
+
for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
|
|
375
|
+
_b = _a[_i], _5 = _b[0], accountToPoll = _b[1];
|
|
376
|
+
this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
|
|
377
|
+
}
|
|
378
|
+
for (_c = 0, _d = this.oraclesToPoll; _c < _d.length; _c++) {
|
|
379
|
+
_e = _d[_c], _6 = _e[0], oracleToPoll = _e[1];
|
|
380
|
+
this.accountLoader.removeAccount(oracleToPoll.publicKey, oracleToPoll.callbackId);
|
|
381
|
+
}
|
|
382
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
383
|
+
this.errorCallbackId = undefined;
|
|
384
|
+
this.accountsToPoll.clear();
|
|
385
|
+
this.oraclesToPoll.clear();
|
|
386
|
+
this.isSubscribed = false;
|
|
387
|
+
return [2 /*return*/];
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
};
|
|
391
|
+
PollingClearingHouseAccountSubscriber.prototype.addSpotMarket = function (marketIndex) {
|
|
392
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
393
|
+
var accountToPoll;
|
|
394
|
+
return __generator(this, function (_a) {
|
|
395
|
+
switch (_a.label) {
|
|
396
|
+
case 0: return [4 /*yield*/, this.addSpotMarketAccountToPoll(marketIndex)];
|
|
397
|
+
case 1:
|
|
398
|
+
_a.sent();
|
|
399
|
+
accountToPoll = this.accountsToPoll.get(marketIndex.toString());
|
|
400
|
+
this.addAccountToAccountLoader(accountToPoll);
|
|
401
|
+
return [2 /*return*/, true];
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
};
|
|
406
|
+
PollingClearingHouseAccountSubscriber.prototype.addPerpMarket = function (marketIndex) {
|
|
407
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
408
|
+
var accountToPoll;
|
|
409
|
+
return __generator(this, function (_a) {
|
|
410
|
+
switch (_a.label) {
|
|
411
|
+
case 0: return [4 /*yield*/, this.addMarketAccountToPoll(marketIndex)];
|
|
412
|
+
case 1:
|
|
413
|
+
_a.sent();
|
|
414
|
+
accountToPoll = this.accountsToPoll.get(marketIndex.toString());
|
|
415
|
+
this.addAccountToAccountLoader(accountToPoll);
|
|
416
|
+
return [2 /*return*/, true];
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
};
|
|
421
|
+
PollingClearingHouseAccountSubscriber.prototype.addOracle = function (oracleInfo) {
|
|
422
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
423
|
+
var oracleToPoll;
|
|
424
|
+
return __generator(this, function (_a) {
|
|
425
|
+
if (oracleInfo.publicKey.equals(web3_js_1.PublicKey["default"])) {
|
|
426
|
+
return [2 /*return*/, true];
|
|
427
|
+
}
|
|
428
|
+
this.addOracleToPoll(oracleInfo);
|
|
429
|
+
oracleToPoll = this.oraclesToPoll.get(oracleInfo.publicKey.toString());
|
|
430
|
+
this.addOracleToAccountLoader(oracleToPoll);
|
|
431
|
+
return [2 /*return*/, true];
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
};
|
|
435
|
+
PollingClearingHouseAccountSubscriber.prototype.assertIsSubscribed = function () {
|
|
436
|
+
if (!this.isSubscribed) {
|
|
437
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
PollingClearingHouseAccountSubscriber.prototype.getStateAccountAndSlot = function () {
|
|
441
|
+
this.assertIsSubscribed();
|
|
442
|
+
return this.state;
|
|
443
|
+
};
|
|
444
|
+
PollingClearingHouseAccountSubscriber.prototype.getMarketAccountAndSlot = function (marketIndex) {
|
|
445
|
+
return this.perpMarket.get(marketIndex.toNumber());
|
|
446
|
+
};
|
|
447
|
+
PollingClearingHouseAccountSubscriber.prototype.getMarketAccountsAndSlots = function () {
|
|
448
|
+
return Array.from(this.perpMarket.values());
|
|
449
|
+
};
|
|
450
|
+
PollingClearingHouseAccountSubscriber.prototype.getSpotMarketAccountAndSlot = function (marketIndex) {
|
|
451
|
+
return this.spotMarket.get(marketIndex.toNumber());
|
|
452
|
+
};
|
|
453
|
+
PollingClearingHouseAccountSubscriber.prototype.getOraclePriceDataAndSlot = function (oraclePublicKey) {
|
|
454
|
+
this.assertIsSubscribed();
|
|
455
|
+
if (oraclePublicKey.equals(web3_js_1.PublicKey["default"])) {
|
|
456
|
+
return {
|
|
457
|
+
data: quoteAssetOracleClient_1.QUOTE_ORACLE_PRICE_DATA,
|
|
458
|
+
slot: 0
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
return this.oracles.get(oraclePublicKey.toString());
|
|
462
|
+
};
|
|
463
|
+
return PollingClearingHouseAccountSubscriber;
|
|
464
|
+
}());
|
|
465
|
+
exports.PollingClearingHouseAccountSubscriber = PollingClearingHouseAccountSubscriber;
|