@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.0
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/bulkAccountLoader.d.ts +6 -6
- package/lib/accounts/bulkAccountLoader.js +81 -95
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +8 -17
- package/lib/admin.js +366 -558
- package/lib/clearingHouse.d.ts +84 -109
- package/lib/clearingHouse.js +778 -899
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -22
- package/lib/clearingHouseUser.js +155 -141
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +34 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +16 -0
- package/lib/constants/numericConstants.js +22 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +112 -0
- package/lib/factory/bigNum.js +356 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +2213 -2951
- package/lib/index.d.ts +12 -4
- package/lib/index.js +12 -4
- package/lib/math/amm.d.ts +19 -29
- package/lib/math/amm.js +129 -179
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +39 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/orders.d.ts +5 -0
- package/lib/math/orders.js +31 -1
- package/lib/math/position.d.ts +7 -5
- package/lib/math/position.js +27 -27
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +5 -4
- package/lib/math/trade.js +29 -21
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -31
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -50
- package/lib/oracles/types.d.ts +6 -1
- package/lib/orders.d.ts +6 -6
- package/lib/orders.js +10 -9
- package/lib/slot/SlotSubscriber.d.ts +12 -0
- package/lib/slot/SlotSubscriber.js +23 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +105 -109
- package/lib/types.js +13 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +10 -3
- package/src/accounts/bulkAccountLoader.ts +21 -15
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.ts +41 -70
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.ts +18 -0
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +205 -346
- package/src/clearingHouse.ts +918 -961
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +275 -185
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +43 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +33 -5
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.ts +507 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +2213 -2951
- package/src/index.ts +12 -4
- package/src/math/amm.ts +229 -245
- package/src/math/auction.ts +39 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.ts +12 -9
- package/src/math/market.ts +37 -30
- package/src/math/orders.ts +38 -0
- package/src/math/position.ts +48 -35
- package/src/math/repeg.ts +175 -0
- package/src/math/trade.ts +45 -35
- package/src/math/utils.js +27 -0
- package/src/math/utils.js.map +1 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.ts +4 -11
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.ts +11 -24
- package/src/oracles/types.ts +7 -1
- package/src/orders.ts +35 -20
- package/src/slot/SlotSubscriber.ts +32 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.ts +22 -3
- package/src/types.ts +108 -122
- package/src/userName.ts +20 -0
- package/src/util/computeUnits.js +17 -0
- package/src/util/computeUnits.js.map +1 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -10
- package/lib/addresses.js +0 -93
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/settlement.d.ts +0 -4
- package/lib/settlement.js +0 -10
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -82
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/settlement.ts +0 -9
- package/src/tx/defaultTxSender.ts +0 -24
|
@@ -1,276 +1,276 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.PollingClearingHouseAccountSubscriber = void 0;
|
|
13
4
|
const types_1 = require("./types");
|
|
14
5
|
const events_1 = require("events");
|
|
15
|
-
const
|
|
6
|
+
const pda_1 = require("../addresses/pda");
|
|
16
7
|
const utils_1 = require("./utils");
|
|
17
|
-
const
|
|
8
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
9
|
+
const oracleClientCache_1 = require("../oracles/oracleClientCache");
|
|
10
|
+
const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
|
|
18
11
|
class PollingClearingHouseAccountSubscriber {
|
|
19
|
-
constructor(program, accountLoader) {
|
|
12
|
+
constructor(program, accountLoader, marketIndexes, bankIndexes, oracleInfos) {
|
|
13
|
+
this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
|
|
20
14
|
this.accountsToPoll = new Map();
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
15
|
+
this.oraclesToPoll = new Map();
|
|
16
|
+
this.market = new Map();
|
|
17
|
+
this.bank = new Map();
|
|
18
|
+
this.oracles = new Map();
|
|
23
19
|
this.isSubscribing = false;
|
|
24
20
|
this.isSubscribed = false;
|
|
25
21
|
this.program = program;
|
|
26
22
|
this.eventEmitter = new events_1.EventEmitter();
|
|
27
23
|
this.accountLoader = accountLoader;
|
|
24
|
+
this.marketIndexes = marketIndexes;
|
|
25
|
+
this.bankIndexes = bankIndexes;
|
|
26
|
+
this.oracleInfos = oracleInfos;
|
|
28
27
|
}
|
|
29
|
-
subscribe(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.
|
|
39
|
-
this.subscriptionPromise = new Promise((res) => {
|
|
40
|
-
this.subscriptionPromiseResolver = res;
|
|
41
|
-
});
|
|
42
|
-
yield this.updateAccountsToPoll();
|
|
43
|
-
yield this.addToAccountLoader();
|
|
44
|
-
let subscriptionSucceeded = false;
|
|
45
|
-
let retries = 0;
|
|
46
|
-
while (!subscriptionSucceeded && retries < 5) {
|
|
47
|
-
yield this.fetch();
|
|
48
|
-
subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
49
|
-
retries++;
|
|
50
|
-
}
|
|
51
|
-
if (subscriptionSucceeded) {
|
|
52
|
-
this.eventEmitter.emit('update');
|
|
53
|
-
}
|
|
54
|
-
this.isSubscribing = false;
|
|
55
|
-
this.isSubscribed = subscriptionSucceeded;
|
|
56
|
-
this.subscriptionPromiseResolver(subscriptionSucceeded);
|
|
57
|
-
return subscriptionSucceeded;
|
|
28
|
+
async subscribe() {
|
|
29
|
+
if (this.isSubscribed) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
if (this.isSubscribing) {
|
|
33
|
+
return await this.subscriptionPromise;
|
|
34
|
+
}
|
|
35
|
+
this.isSubscribing = true;
|
|
36
|
+
this.subscriptionPromise = new Promise((res) => {
|
|
37
|
+
this.subscriptionPromiseResolver = res;
|
|
58
38
|
});
|
|
39
|
+
await this.updateAccountsToPoll();
|
|
40
|
+
await this.updateOraclesToPoll();
|
|
41
|
+
await this.addToAccountLoader();
|
|
42
|
+
let subscriptionSucceeded = false;
|
|
43
|
+
let retries = 0;
|
|
44
|
+
while (!subscriptionSucceeded && retries < 5) {
|
|
45
|
+
await this.fetch();
|
|
46
|
+
subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
47
|
+
retries++;
|
|
48
|
+
}
|
|
49
|
+
if (subscriptionSucceeded) {
|
|
50
|
+
this.eventEmitter.emit('update');
|
|
51
|
+
}
|
|
52
|
+
this.isSubscribing = false;
|
|
53
|
+
this.isSubscribed = subscriptionSucceeded;
|
|
54
|
+
this.subscriptionPromiseResolver(subscriptionSucceeded);
|
|
55
|
+
return subscriptionSucceeded;
|
|
59
56
|
}
|
|
60
|
-
updateAccountsToPoll() {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
publicKey: accounts.state,
|
|
70
|
-
eventType: 'stateAccountUpdate',
|
|
71
|
-
});
|
|
72
|
-
this.accountsToPoll.set(accounts.markets.toString(), {
|
|
73
|
-
key: 'markets',
|
|
74
|
-
publicKey: accounts.markets,
|
|
75
|
-
eventType: 'marketsAccountUpdate',
|
|
76
|
-
});
|
|
77
|
-
this.accountsToPoll.set(accounts.orderState.toString(), {
|
|
78
|
-
key: 'orderState',
|
|
79
|
-
publicKey: accounts.orderState,
|
|
80
|
-
eventType: 'orderStateAccountUpdate',
|
|
81
|
-
});
|
|
82
|
-
if ((_a = this.optionalExtraSubscriptions) === null || _a === void 0 ? void 0 : _a.includes('tradeHistoryAccount')) {
|
|
83
|
-
this.accountsToPoll.set(accounts.tradeHistory.toString(), {
|
|
84
|
-
key: 'tradeHistory',
|
|
85
|
-
publicKey: accounts.tradeHistory,
|
|
86
|
-
eventType: 'tradeHistoryAccountUpdate',
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
if ((_b = this.optionalExtraSubscriptions) === null || _b === void 0 ? void 0 : _b.includes('depositHistoryAccount')) {
|
|
90
|
-
this.accountsToPoll.set(accounts.depositHistory.toString(), {
|
|
91
|
-
key: 'depositHistory',
|
|
92
|
-
publicKey: accounts.depositHistory,
|
|
93
|
-
eventType: 'depositHistoryAccountUpdate',
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
if ((_c = this.optionalExtraSubscriptions) === null || _c === void 0 ? void 0 : _c.includes('fundingPaymentHistoryAccount')) {
|
|
97
|
-
this.accountsToPoll.set(accounts.fundingPaymentHistory.toString(), {
|
|
98
|
-
key: 'fundingPaymentHistory',
|
|
99
|
-
publicKey: accounts.fundingPaymentHistory,
|
|
100
|
-
eventType: 'fundingPaymentHistoryAccountUpdate',
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
if ((_d = this.optionalExtraSubscriptions) === null || _d === void 0 ? void 0 : _d.includes('fundingRateHistoryAccount')) {
|
|
104
|
-
this.accountsToPoll.set(accounts.fundingRateHistory.toString(), {
|
|
105
|
-
key: 'fundingRateHistory',
|
|
106
|
-
publicKey: accounts.fundingRateHistory,
|
|
107
|
-
eventType: 'fundingRateHistoryAccountUpdate',
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
if ((_e = this.optionalExtraSubscriptions) === null || _e === void 0 ? void 0 : _e.includes('curveHistoryAccount')) {
|
|
111
|
-
this.accountsToPoll.set(accounts.extendedCurveHistory.toString(), {
|
|
112
|
-
key: 'extendedCurveHistory',
|
|
113
|
-
publicKey: accounts.extendedCurveHistory,
|
|
114
|
-
eventType: 'curveHistoryAccountUpdate',
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
if ((_f = this.optionalExtraSubscriptions) === null || _f === void 0 ? void 0 : _f.includes('liquidationHistoryAccount')) {
|
|
118
|
-
this.accountsToPoll.set(accounts.liquidationHistory.toString(), {
|
|
119
|
-
key: 'liquidationHistory',
|
|
120
|
-
publicKey: accounts.liquidationHistory,
|
|
121
|
-
eventType: 'liquidationHistoryAccountUpdate',
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
if ((_g = this.optionalExtraSubscriptions) === null || _g === void 0 ? void 0 : _g.includes('orderHistoryAccount')) {
|
|
125
|
-
this.accountsToPoll.set(accounts.orderHistory.toString(), {
|
|
126
|
-
key: 'orderHistory',
|
|
127
|
-
publicKey: accounts.orderHistory,
|
|
128
|
-
eventType: 'orderHistoryAccountUpdate',
|
|
129
|
-
});
|
|
130
|
-
}
|
|
57
|
+
async updateAccountsToPoll() {
|
|
58
|
+
if (this.accountsToPoll.size > 0) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const accounts = await this.getClearingHouseAccounts();
|
|
62
|
+
this.accountsToPoll.set(accounts.state.toString(), {
|
|
63
|
+
key: 'state',
|
|
64
|
+
publicKey: accounts.state,
|
|
65
|
+
eventType: 'stateAccountUpdate',
|
|
131
66
|
});
|
|
67
|
+
await this.updateMarketAccountsToPoll();
|
|
68
|
+
await this.updateBankAccountsToPoll();
|
|
132
69
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
tradeHistory: state.tradeHistory,
|
|
147
|
-
depositHistory: state.depositHistory,
|
|
148
|
-
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
149
|
-
fundingRateHistory: state.fundingRateHistory,
|
|
150
|
-
extendedCurveHistory: state.extendedCurveHistory,
|
|
151
|
-
liquidationHistory: state.liquidationHistory,
|
|
152
|
-
orderHistory: undefined,
|
|
153
|
-
};
|
|
154
|
-
if ((_a = this.optionalExtraSubscriptions) === null || _a === void 0 ? void 0 : _a.includes('orderHistoryAccount')) {
|
|
155
|
-
const orderState = (yield this.program.account.orderState.fetch(state.orderState));
|
|
156
|
-
accounts.orderHistory = orderState.orderHistory;
|
|
157
|
-
}
|
|
158
|
-
return accounts;
|
|
70
|
+
async updateMarketAccountsToPoll() {
|
|
71
|
+
for (const marketIndex of this.marketIndexes) {
|
|
72
|
+
await this.addMarketAccountToPoll(marketIndex);
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
async addMarketAccountToPoll(marketIndex) {
|
|
77
|
+
const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
|
|
78
|
+
this.accountsToPoll.set(marketPublicKey.toString(), {
|
|
79
|
+
key: 'market',
|
|
80
|
+
publicKey: marketPublicKey,
|
|
81
|
+
eventType: 'marketAccountUpdate',
|
|
82
|
+
mapKey: marketIndex.toNumber(),
|
|
159
83
|
});
|
|
84
|
+
return true;
|
|
160
85
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
176
|
-
this.eventEmitter.emit('error', error);
|
|
177
|
-
});
|
|
86
|
+
async updateBankAccountsToPoll() {
|
|
87
|
+
for (const bankIndex of this.bankIndexes) {
|
|
88
|
+
await this.addBankAccountToPoll(bankIndex);
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
async addBankAccountToPoll(bankIndex) {
|
|
93
|
+
const bankPublicKey = await (0, pda_1.getBankPublicKey)(this.program.programId, bankIndex);
|
|
94
|
+
this.accountsToPoll.set(bankPublicKey.toString(), {
|
|
95
|
+
key: 'bank',
|
|
96
|
+
publicKey: bankPublicKey,
|
|
97
|
+
eventType: 'bankAccountUpdate',
|
|
98
|
+
mapKey: bankIndex.toNumber(),
|
|
178
99
|
});
|
|
100
|
+
return true;
|
|
179
101
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const buffer = this.accountLoader.getAccountData(accountToPoll.publicKey);
|
|
185
|
-
if (buffer) {
|
|
186
|
-
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
187
|
-
}
|
|
102
|
+
updateOraclesToPoll() {
|
|
103
|
+
for (const oracleInfo of this.oracleInfos) {
|
|
104
|
+
if (!oracleInfo.publicKey.equals(web3_js_1.PublicKey.default)) {
|
|
105
|
+
this.addOracleToPoll(oracleInfo);
|
|
188
106
|
}
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
addOracleToPoll(oracleInfo) {
|
|
111
|
+
this.oraclesToPoll.set(oracleInfo.publicKey.toString(), {
|
|
112
|
+
publicKey: oracleInfo.publicKey,
|
|
113
|
+
source: oracleInfo.source,
|
|
189
114
|
});
|
|
115
|
+
return true;
|
|
190
116
|
}
|
|
191
|
-
|
|
192
|
-
|
|
117
|
+
async getClearingHouseAccounts() {
|
|
118
|
+
const statePublicKey = await (0, pda_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
|
|
119
|
+
const accounts = {
|
|
120
|
+
state: statePublicKey,
|
|
121
|
+
};
|
|
122
|
+
return accounts;
|
|
123
|
+
}
|
|
124
|
+
async addToAccountLoader() {
|
|
193
125
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
194
|
-
|
|
195
|
-
success = false;
|
|
196
|
-
break;
|
|
197
|
-
}
|
|
126
|
+
this.addAccountToAccountLoader(accountToPoll);
|
|
198
127
|
}
|
|
199
|
-
|
|
128
|
+
for (const [_, oracleToPoll] of this.oraclesToPoll) {
|
|
129
|
+
this.addOracleToAccountLoader(oracleToPoll);
|
|
130
|
+
}
|
|
131
|
+
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
132
|
+
this.eventEmitter.emit('error', error);
|
|
133
|
+
});
|
|
200
134
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
if (!
|
|
135
|
+
addAccountToAccountLoader(accountToPoll) {
|
|
136
|
+
accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer, slot) => {
|
|
137
|
+
if (!buffer)
|
|
204
138
|
return;
|
|
139
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
140
|
+
const dataAndSlot = {
|
|
141
|
+
data: account,
|
|
142
|
+
slot,
|
|
143
|
+
};
|
|
144
|
+
if (accountToPoll.mapKey != undefined) {
|
|
145
|
+
this[accountToPoll.key].set(accountToPoll.mapKey, dataAndSlot);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
this[accountToPoll.key] = dataAndSlot;
|
|
205
149
|
}
|
|
206
|
-
|
|
207
|
-
|
|
150
|
+
// @ts-ignore
|
|
151
|
+
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
152
|
+
this.eventEmitter.emit('update');
|
|
153
|
+
if (!this.isSubscribed) {
|
|
154
|
+
this.isSubscribed = this.didSubscriptionSucceed();
|
|
208
155
|
}
|
|
209
|
-
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
210
|
-
this.errorCallbackId = undefined;
|
|
211
|
-
this.accountsToPoll.clear();
|
|
212
|
-
this.isSubscribed = false;
|
|
213
156
|
});
|
|
214
157
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
158
|
+
addOracleToAccountLoader(oracleToPoll) {
|
|
159
|
+
const oracleClient = this.oracleClientCache.get(oracleToPoll.source, this.program.provider.connection);
|
|
160
|
+
oracleToPoll.callbackId = this.accountLoader.addAccount(oracleToPoll.publicKey, (buffer, slot) => {
|
|
161
|
+
if (!buffer)
|
|
162
|
+
return;
|
|
163
|
+
const oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(buffer);
|
|
164
|
+
const dataAndSlot = {
|
|
165
|
+
data: oraclePriceData,
|
|
166
|
+
slot,
|
|
167
|
+
};
|
|
168
|
+
this.oracles.set(oracleToPoll.publicKey.toString(), dataAndSlot);
|
|
169
|
+
this.eventEmitter.emit('oraclePriceUpdate', oracleToPoll.publicKey, oraclePriceData);
|
|
170
|
+
this.eventEmitter.emit('update');
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
async fetch() {
|
|
174
|
+
await this.accountLoader.load();
|
|
175
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
176
|
+
const { buffer, slot } = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey);
|
|
177
|
+
if (buffer) {
|
|
178
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
179
|
+
if (accountToPoll.mapKey != undefined) {
|
|
180
|
+
this[accountToPoll.key].set(accountToPoll.mapKey, {
|
|
181
|
+
data: account,
|
|
182
|
+
slot,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
this[accountToPoll.key] = {
|
|
187
|
+
data: account,
|
|
188
|
+
slot,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
for (const [_, oracleToPoll] of this.oraclesToPoll) {
|
|
194
|
+
const { buffer, slot } = this.accountLoader.getBufferAndSlot(oracleToPoll.publicKey);
|
|
195
|
+
if (buffer) {
|
|
196
|
+
const oracleClient = this.oracleClientCache.get(oracleToPoll.source, this.program.provider.connection);
|
|
197
|
+
const oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(buffer);
|
|
198
|
+
this.oracles.set(oracleToPoll.publicKey.toString(), {
|
|
199
|
+
data: oraclePriceData,
|
|
200
|
+
slot,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
218
203
|
}
|
|
219
204
|
}
|
|
220
|
-
|
|
205
|
+
didSubscriptionSucceed() {
|
|
206
|
+
if (this.state)
|
|
207
|
+
return true;
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
async unsubscribe() {
|
|
221
211
|
if (!this.isSubscribed) {
|
|
222
|
-
|
|
212
|
+
return;
|
|
223
213
|
}
|
|
224
|
-
|
|
225
|
-
|
|
214
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
215
|
+
this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
|
|
216
|
+
}
|
|
217
|
+
for (const [_, oracleToPoll] of this.oraclesToPoll) {
|
|
218
|
+
this.accountLoader.removeAccount(oracleToPoll.publicKey, oracleToPoll.callbackId);
|
|
226
219
|
}
|
|
220
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
221
|
+
this.errorCallbackId = undefined;
|
|
222
|
+
this.accountsToPoll.clear();
|
|
223
|
+
this.oraclesToPoll.clear();
|
|
224
|
+
this.isSubscribed = false;
|
|
227
225
|
}
|
|
228
|
-
|
|
229
|
-
this.
|
|
230
|
-
|
|
226
|
+
async addBank(bankIndex) {
|
|
227
|
+
await this.addBankAccountToPoll(bankIndex);
|
|
228
|
+
const accountToPoll = this.accountsToPoll.get(bankIndex.toString());
|
|
229
|
+
this.addAccountToAccountLoader(accountToPoll);
|
|
230
|
+
return true;
|
|
231
231
|
}
|
|
232
|
-
|
|
233
|
-
this.
|
|
234
|
-
|
|
232
|
+
async addMarket(marketIndex) {
|
|
233
|
+
await this.addMarketAccountToPoll(marketIndex);
|
|
234
|
+
const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
|
|
235
|
+
this.addAccountToAccountLoader(accountToPoll);
|
|
236
|
+
return true;
|
|
235
237
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
this.
|
|
242
|
-
this.
|
|
243
|
-
return
|
|
238
|
+
async addOracle(oracleInfo) {
|
|
239
|
+
if (oracleInfo.publicKey.equals(web3_js_1.PublicKey.default)) {
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
this.addOracleToPoll(oracleInfo);
|
|
243
|
+
const oracleToPoll = this.oraclesToPoll.get(oracleInfo.publicKey.toString());
|
|
244
|
+
this.addOracleToAccountLoader(oracleToPoll);
|
|
245
|
+
return true;
|
|
244
246
|
}
|
|
245
|
-
|
|
246
|
-
this.
|
|
247
|
-
|
|
248
|
-
|
|
247
|
+
assertIsSubscribed() {
|
|
248
|
+
if (!this.isSubscribed) {
|
|
249
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
250
|
+
}
|
|
249
251
|
}
|
|
250
|
-
|
|
252
|
+
getStateAccountAndSlot() {
|
|
251
253
|
this.assertIsSubscribed();
|
|
252
|
-
this.
|
|
253
|
-
return this.fundingPaymentHistory;
|
|
254
|
+
return this.state;
|
|
254
255
|
}
|
|
255
|
-
|
|
256
|
-
this.
|
|
257
|
-
this.assertOptionalIsSubscribed('fundingRateHistoryAccount');
|
|
258
|
-
return this.fundingRateHistory;
|
|
256
|
+
getMarketAccountAndSlot(marketIndex) {
|
|
257
|
+
return this.market.get(marketIndex.toNumber());
|
|
259
258
|
}
|
|
260
|
-
|
|
261
|
-
this.
|
|
262
|
-
this.assertOptionalIsSubscribed('curveHistoryAccount');
|
|
263
|
-
return this.extendedCurveHistory;
|
|
259
|
+
getMarketAccountsAndSlots() {
|
|
260
|
+
return Array.from(this.market.values());
|
|
264
261
|
}
|
|
265
|
-
|
|
266
|
-
this.
|
|
267
|
-
this.assertOptionalIsSubscribed('liquidationHistoryAccount');
|
|
268
|
-
return this.liquidationHistory;
|
|
262
|
+
getBankAccountAndSlot(bankIndex) {
|
|
263
|
+
return this.bank.get(bankIndex.toNumber());
|
|
269
264
|
}
|
|
270
|
-
|
|
265
|
+
getOraclePriceDataAndSlot(oraclePublicKey) {
|
|
271
266
|
this.assertIsSubscribed();
|
|
272
|
-
|
|
273
|
-
|
|
267
|
+
if (oraclePublicKey.equals(web3_js_1.PublicKey.default)) {
|
|
268
|
+
return {
|
|
269
|
+
data: quoteAssetOracleClient_1.QUOTE_ORACLE_PRICE_DATA,
|
|
270
|
+
slot: 0,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
return this.oracles.get(oraclePublicKey.toString());
|
|
274
274
|
}
|
|
275
275
|
}
|
|
276
276
|
exports.PollingClearingHouseAccountSubscriber = PollingClearingHouseAccountSubscriber;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { OracleEvents, OracleSubscriber } from './types';
|
|
2
|
+
import { DataAndSlot, OracleEvents, OracleSubscriber } from './types';
|
|
3
3
|
import { Program } from '@project-serum/anchor';
|
|
4
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
5
|
import { EventEmitter } from 'events';
|
|
@@ -15,13 +15,13 @@ export declare class PollingOracleSubscriber implements OracleSubscriber {
|
|
|
15
15
|
oracleClient: OracleClient;
|
|
16
16
|
callbackId?: string;
|
|
17
17
|
errorCallbackId?: string;
|
|
18
|
-
oraclePriceData?: OraclePriceData
|
|
18
|
+
oraclePriceData?: DataAndSlot<OraclePriceData>;
|
|
19
19
|
constructor(publicKey: PublicKey, oracleClient: OracleClient, accountLoader: BulkAccountLoader);
|
|
20
20
|
subscribe(): Promise<boolean>;
|
|
21
21
|
addToAccountLoader(): void;
|
|
22
22
|
fetch(): Promise<void>;
|
|
23
23
|
unsubscribe(): Promise<void>;
|
|
24
24
|
assertIsSubscribed(): void;
|
|
25
|
-
getOraclePriceData(): OraclePriceData
|
|
25
|
+
getOraclePriceData(): DataAndSlot<OraclePriceData>;
|
|
26
26
|
didSubscriptionSucceed(): boolean;
|
|
27
27
|
}
|
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.PollingOracleSubscriber = void 0;
|
|
13
4
|
const types_1 = require("./types");
|
|
@@ -20,59 +11,56 @@ class PollingOracleSubscriber {
|
|
|
20
11
|
this.accountLoader = accountLoader;
|
|
21
12
|
this.eventEmitter = new events_1.EventEmitter();
|
|
22
13
|
}
|
|
23
|
-
subscribe() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return subscriptionSucceeded;
|
|
41
|
-
});
|
|
14
|
+
async subscribe() {
|
|
15
|
+
if (this.isSubscribed) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
this.addToAccountLoader();
|
|
19
|
+
let subscriptionSucceeded = false;
|
|
20
|
+
let retries = 0;
|
|
21
|
+
while (!subscriptionSucceeded && retries < 5) {
|
|
22
|
+
await this.fetch();
|
|
23
|
+
subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
24
|
+
retries++;
|
|
25
|
+
}
|
|
26
|
+
if (subscriptionSucceeded) {
|
|
27
|
+
this.eventEmitter.emit('update');
|
|
28
|
+
}
|
|
29
|
+
this.isSubscribed = subscriptionSucceeded;
|
|
30
|
+
return subscriptionSucceeded;
|
|
42
31
|
}
|
|
43
32
|
addToAccountLoader() {
|
|
44
33
|
if (this.callbackId) {
|
|
45
34
|
return;
|
|
46
35
|
}
|
|
47
|
-
this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer) =>
|
|
48
|
-
const oraclePriceData =
|
|
49
|
-
this.oraclePriceData = oraclePriceData;
|
|
36
|
+
this.callbackId = this.accountLoader.addAccount(this.publicKey, async (buffer, slot) => {
|
|
37
|
+
const oraclePriceData = await this.oracleClient.getOraclePriceDataFromBuffer(buffer);
|
|
38
|
+
this.oraclePriceData = { data: oraclePriceData, slot };
|
|
50
39
|
// @ts-ignore
|
|
51
40
|
this.eventEmitter.emit('oracleUpdate', oraclePriceData);
|
|
52
41
|
this.eventEmitter.emit('update');
|
|
53
|
-
})
|
|
42
|
+
});
|
|
54
43
|
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
55
44
|
this.eventEmitter.emit('error', error);
|
|
56
45
|
});
|
|
57
46
|
}
|
|
58
|
-
fetch() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
47
|
+
async fetch() {
|
|
48
|
+
await this.accountLoader.load();
|
|
49
|
+
const { buffer, slot } = this.accountLoader.getBufferAndSlot(this.publicKey);
|
|
50
|
+
this.oraclePriceData = {
|
|
51
|
+
data: await this.oracleClient.getOraclePriceDataFromBuffer(buffer),
|
|
52
|
+
slot,
|
|
53
|
+
};
|
|
64
54
|
}
|
|
65
|
-
unsubscribe() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.isSubscribed = false;
|
|
75
|
-
});
|
|
55
|
+
async unsubscribe() {
|
|
56
|
+
if (!this.isSubscribed) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
this.accountLoader.removeAccount(this.publicKey, this.callbackId);
|
|
60
|
+
this.callbackId = undefined;
|
|
61
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
62
|
+
this.errorCallbackId = undefined;
|
|
63
|
+
this.isSubscribed = false;
|
|
76
64
|
}
|
|
77
65
|
assertIsSubscribed() {
|
|
78
66
|
if (!this.isSubscribed) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { TokenAccountEvents, TokenAccountSubscriber } from './types';
|
|
2
|
+
import { DataAndSlot, TokenAccountEvents, TokenAccountSubscriber } from './types';
|
|
3
3
|
import { Program } from '@project-serum/anchor';
|
|
4
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
5
|
import { EventEmitter } from 'events';
|
|
@@ -14,13 +14,13 @@ export declare class PollingTokenAccountSubscriber implements TokenAccountSubscr
|
|
|
14
14
|
accountLoader: BulkAccountLoader;
|
|
15
15
|
callbackId?: string;
|
|
16
16
|
errorCallbackId?: string;
|
|
17
|
-
|
|
17
|
+
tokenAccountAndSlot?: DataAndSlot<AccountInfo>;
|
|
18
18
|
constructor(publicKey: PublicKey, accountLoader: BulkAccountLoader);
|
|
19
19
|
subscribe(): Promise<boolean>;
|
|
20
20
|
addToAccountLoader(): void;
|
|
21
21
|
fetch(): Promise<void>;
|
|
22
22
|
unsubscribe(): Promise<void>;
|
|
23
23
|
assertIsSubscribed(): void;
|
|
24
|
-
|
|
24
|
+
getTokenAccountAndSlot(): DataAndSlot<AccountInfo>;
|
|
25
25
|
didSubscriptionSucceed(): boolean;
|
|
26
26
|
}
|