@drift-labs/sdk 0.2.0-temp.1 → 2.0.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/README.md +27 -29
- package/lib/accounts/bulkAccountLoader.d.ts +2 -0
- package/lib/accounts/bulkAccountLoader.js +36 -29
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.d.ts +2 -2
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +20 -25
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
- package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
- package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -1
- package/lib/accounts/pollingUserAccountSubscriber.js +5 -11
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +107 -0
- package/lib/accounts/types.d.ts +26 -15
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +15 -9
- package/lib/addresses/pda.js +73 -35
- package/lib/adminClient.d.ts +65 -0
- package/lib/adminClient.js +637 -0
- package/lib/config.d.ts +10 -10
- package/lib/config.js +26 -22
- package/lib/constants/numericConstants.d.ts +29 -12
- package/lib/constants/numericConstants.js +40 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +20 -9
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +63 -0
- package/lib/dlob/DLOB.d.ts +82 -0
- package/lib/dlob/DLOB.js +694 -0
- package/lib/dlob/DLOBNode.d.ts +54 -0
- package/lib/dlob/DLOBNode.js +77 -0
- package/lib/dlob/NodeList.d.ts +27 -0
- package/lib/dlob/NodeList.js +144 -0
- package/lib/driftClient.d.ts +234 -0
- package/lib/driftClient.js +2108 -0
- package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
- package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +5 -2
- package/lib/events/eventSubscriber.js +25 -11
- package/lib/events/fetchLogs.d.ts +13 -2
- package/lib/events/fetchLogs.js +45 -14
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +7 -3
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +11 -3
- package/lib/events/types.js +8 -0
- package/lib/events/webSocketLogProvider.js +1 -1
- package/lib/examples/makeTradeExample.js +30 -18
- package/lib/factory/bigNum.d.ts +8 -4
- package/lib/factory/bigNum.js +109 -19
- package/lib/idl/drift.json +8392 -0
- package/lib/index.d.ts +29 -12
- package/lib/index.js +29 -12
- package/lib/math/amm.d.ts +9 -6
- package/lib/math/amm.js +91 -38
- package/lib/math/conversion.js +1 -1
- package/lib/math/exchangeStatus.d.ts +4 -0
- package/lib/math/exchangeStatus.js +18 -0
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +23 -21
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +82 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +70 -10
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +16 -6
- package/lib/math/orders.js +97 -17
- package/lib/math/position.d.ts +27 -13
- package/lib/math/position.js +91 -37
- package/lib/math/repeg.js +17 -8
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +192 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/trade.d.ts +10 -10
- package/lib/math/trade.js +27 -31
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/serum/serumSubscriber.d.ts +27 -0
- package/lib/serum/serumSubscriber.js +56 -0
- package/lib/serum/types.d.ts +11 -0
- package/{src/oracles → lib/serum}/types.js +0 -0
- package/lib/tokenFaucet.d.ts +1 -0
- package/lib/tokenFaucet.js +23 -12
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +13 -4
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +631 -222
- package/lib/types.js +137 -24
- package/lib/user.d.ts +228 -0
- package/lib/user.js +959 -0
- package/lib/userConfig.d.ts +14 -0
- package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
- package/lib/userMap/userMap.d.ts +41 -0
- package/lib/userMap/userMap.js +85 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/userName.d.ts +1 -0
- package/lib/userName.js +3 -2
- package/lib/userStats.d.ts +18 -0
- package/lib/userStats.js +49 -0
- package/lib/userStatsConfig.d.ts +14 -0
- package/{src/clearingHouseConfig.js → lib/userStatsConfig.js} +0 -0
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +12 -5
- package/src/accounts/bulkAccountLoader.ts +44 -34
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +2 -3
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
- package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
- package/src/accounts/pollingUserAccountSubscriber.ts +5 -12
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
- package/src/accounts/types.ts +35 -15
- package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +3 -4
- package/src/addresses/pda.ts +105 -33
- package/src/adminClient.ts +1207 -0
- package/src/config.ts +41 -34
- package/src/constants/numericConstants.ts +59 -26
- package/src/constants/{markets.ts → perpMarkets.ts} +22 -11
- package/src/constants/spotMarkets.ts +83 -0
- package/src/dlob/DLOB.ts +1120 -0
- package/src/dlob/DLOBNode.ts +155 -0
- package/src/dlob/NodeList.ts +195 -0
- package/src/driftClient.ts +3594 -0
- package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +36 -14
- package/src/events/fetchLogs.ts +60 -15
- package/src/events/pollingLogProvider.ts +11 -3
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +27 -2
- package/src/events/webSocketLogProvider.ts +1 -1
- package/src/examples/makeTradeExample.js +152 -75
- package/src/examples/makeTradeExample.ts +44 -28
- package/src/factory/bigNum.ts +150 -22
- package/src/idl/drift.json +8392 -0
- package/src/idl/pyth.json +98 -2
- package/src/index.ts +29 -12
- package/src/math/amm.ts +161 -48
- package/src/math/conversion.ts +2 -2
- package/src/math/exchangeStatus.ts +31 -0
- package/src/math/funding.ts +41 -31
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +133 -0
- package/src/math/market.ts +143 -14
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +168 -26
- package/src/math/position.ts +133 -59
- package/src/math/repeg.ts +19 -9
- package/src/math/spotBalance.ts +319 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +33 -37
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/orderParams.ts +16 -8
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +99 -0
- package/src/serum/types.ts +13 -0
- package/src/tokenFaucet.ts +38 -15
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.ts +2 -1
- package/src/types.ts +594 -195
- package/src/user.ts +1599 -0
- package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
- package/src/userMap/userMap.ts +124 -0
- package/src/userMap/userStatsMap.ts +108 -0
- package/src/userName.ts +2 -1
- package/src/userStats.ts +75 -0
- package/src/userStatsConfig.ts +18 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/tests/bn/test.ts +46 -11
- package/tests/dlob/helpers.ts +619 -0
- package/tests/dlob/test.ts +4586 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
- package/lib/admin.d.ts +0 -44
- package/lib/admin.js +0 -438
- package/lib/clearingHouse.d.ts +0 -146
- package/lib/clearingHouse.js +0 -1154
- package/lib/clearingHouseUser.d.ts +0 -187
- package/lib/clearingHouseUser.js +0 -634
- package/lib/clearingHouseUserConfig.d.ts +0 -14
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -41
- package/lib/constants/markets.d.ts +0 -19
- package/lib/idl/clearing_house.json +0 -4464
- package/lib/math/bankBalance.d.ts +0 -9
- package/lib/math/bankBalance.js +0 -75
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -134
- package/src/accounts/bulkAccountLoader.js +0 -197
- package/src/accounts/bulkUserSubscription.js +0 -33
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
- package/src/accounts/pollingOracleSubscriber.js +0 -93
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
- package/src/accounts/pollingUserAccountSubscriber.js +0 -132
- package/src/accounts/types.js +0 -10
- package/src/accounts/utils.js +0 -7
- package/src/accounts/webSocketAccountSubscriber.js +0 -93
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
- package/src/addresses/marketAddresses.js +0 -26
- package/src/admin.js +0 -517
- package/src/admin.ts +0 -730
- package/src/clearingHouse.ts +0 -1828
- package/src/clearingHouseUser.ts +0 -978
- package/src/clearingHouseUserConfig.js +0 -2
- package/src/config.js +0 -67
- package/src/constants/banks.js +0 -42
- package/src/constants/banks.ts +0 -50
- package/src/constants/markets.js +0 -42
- package/src/constants/numericConstants.js +0 -41
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/fetchLogs.js +0 -50
- package/src/events/pollingLogProvider.js +0 -64
- package/src/events/sort.js +0 -44
- package/src/events/txEventCache.js +0 -71
- package/src/events/types.js +0 -20
- package/src/events/webSocketLogProvider.js +0 -41
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/idl/clearing_house.json +0 -4464
- package/src/index.js +0 -69
- package/src/math/amm.js +0 -369
- package/src/math/auction.js +0 -42
- package/src/math/bankBalance.ts +0 -112
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/oracles.js +0 -26
- package/src/math/repeg.js +0 -128
- package/src/math/state.js +0 -15
- package/src/math/state.ts +0 -14
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/mockUSDCFaucet.js +0 -280
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/orderParams.js +0 -20
- package/src/orders.ts +0 -245
- package/src/slot/SlotSubscriber.js +0 -39
- package/src/tokenFaucet.js +0 -189
- package/src/types.js +0 -125
- package/src/userName.js +0 -20
- package/src/wallet.js +0 -35
package/README.md
CHANGED
|
@@ -54,13 +54,13 @@ a BigNum: 10,500,000, with precision 10^6, is equal to 10.5 because 10,500,000 /
|
|
|
54
54
|
|
|
55
55
|
The Drift SDK uses some common precisions, which are available as constants to import from the SDK.
|
|
56
56
|
|
|
57
|
-
| Precision Name
|
|
58
|
-
|
|
|
59
|
-
|
|
|
60
|
-
|
|
|
61
|
-
|
|
|
62
|
-
|
|
|
63
|
-
|
|
|
57
|
+
| Precision Name | Value |
|
|
58
|
+
| --------------------- | ----- |
|
|
59
|
+
| FUNDING_RATE_BUFFER | 10^3 |
|
|
60
|
+
| QUOTE_PRECISION | 10^6 |
|
|
61
|
+
| PEG_PRECISION | 10^6 |
|
|
62
|
+
| PRICE_PRECISION | 10^6 |
|
|
63
|
+
| AMM_RESERVE_PRECISION | 10^9 |
|
|
64
64
|
|
|
65
65
|
**Important Note for BigNum division**
|
|
66
66
|
|
|
@@ -88,17 +88,17 @@ import { BN, Provider } from '@project-serum/anchor';
|
|
|
88
88
|
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
|
89
89
|
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
|
90
90
|
import {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
calculateReservePrice,
|
|
92
|
+
DriftClient,
|
|
93
|
+
User,
|
|
94
94
|
initialize,
|
|
95
95
|
Markets,
|
|
96
96
|
PositionDirection,
|
|
97
97
|
convertToNumber,
|
|
98
98
|
calculateTradeSlippage,
|
|
99
|
-
|
|
99
|
+
PRICE_PRECISION,
|
|
100
100
|
QUOTE_PRECISION,
|
|
101
|
-
Wallet
|
|
101
|
+
Wallet,
|
|
102
102
|
} from '@drift-labs/sdk';
|
|
103
103
|
|
|
104
104
|
export const getTokenAddress = (
|
|
@@ -141,19 +141,17 @@ const main = async () => {
|
|
|
141
141
|
wallet.publicKey.toString()
|
|
142
142
|
);
|
|
143
143
|
|
|
144
|
-
// Set up the Drift
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
);
|
|
148
|
-
const clearingHouse = ClearingHouse.from(
|
|
144
|
+
// Set up the Drift Client
|
|
145
|
+
const driftClientPublicKey = new PublicKey(sdkConfig.DRIFT_PROGRAM_ID);
|
|
146
|
+
const driftClient = DriftClient.from(
|
|
149
147
|
connection,
|
|
150
148
|
provider.wallet,
|
|
151
|
-
|
|
149
|
+
driftClientPublicKey
|
|
152
150
|
);
|
|
153
|
-
await
|
|
151
|
+
await driftClient.subscribe();
|
|
154
152
|
|
|
155
153
|
// Set up Clearing House user client
|
|
156
|
-
const user =
|
|
154
|
+
const user = User.from(driftClient, wallet.publicKey);
|
|
157
155
|
|
|
158
156
|
//// Check if clearing house account exists for the current wallet
|
|
159
157
|
const userAccountExists = await user.exists();
|
|
@@ -161,7 +159,7 @@ const main = async () => {
|
|
|
161
159
|
if (!userAccountExists) {
|
|
162
160
|
//// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
|
|
163
161
|
const depositAmount = new BN(10000).mul(QUOTE_PRECISION);
|
|
164
|
-
await
|
|
162
|
+
await driftClient.initializeUserAccountAndDepositCollateral(
|
|
165
163
|
depositAmount,
|
|
166
164
|
await getTokenAddress(
|
|
167
165
|
usdcTokenAddress.toString(),
|
|
@@ -177,16 +175,16 @@ const main = async () => {
|
|
|
177
175
|
(market) => market.baseAssetSymbol === 'SOL'
|
|
178
176
|
);
|
|
179
177
|
|
|
180
|
-
const currentMarketPrice =
|
|
181
|
-
|
|
178
|
+
const currentMarketPrice = calculateReservePrice(
|
|
179
|
+
driftClient.getMarket(solMarketInfo.marketIndex)
|
|
182
180
|
);
|
|
183
181
|
|
|
184
|
-
const formattedPrice = convertToNumber(currentMarketPrice,
|
|
182
|
+
const formattedPrice = convertToNumber(currentMarketPrice, PRICE_PRECISION);
|
|
185
183
|
|
|
186
184
|
console.log(`Current Market Price is $${formattedPrice}`);
|
|
187
185
|
|
|
188
186
|
// Estimate the slippage for a $5000 LONG trade
|
|
189
|
-
const solMarketAccount =
|
|
187
|
+
const solMarketAccount = driftClient.getMarket(solMarketInfo.marketIndex);
|
|
190
188
|
|
|
191
189
|
const slippage = convertToNumber(
|
|
192
190
|
calculateTradeSlippage(
|
|
@@ -194,7 +192,7 @@ const main = async () => {
|
|
|
194
192
|
new BN(5000).mul(QUOTE_PRECISION),
|
|
195
193
|
solMarketAccount
|
|
196
194
|
)[0],
|
|
197
|
-
|
|
195
|
+
PRICE_PRECISION
|
|
198
196
|
);
|
|
199
197
|
|
|
200
198
|
console.log(
|
|
@@ -202,7 +200,7 @@ const main = async () => {
|
|
|
202
200
|
);
|
|
203
201
|
|
|
204
202
|
// Make a $5000 LONG trade
|
|
205
|
-
await
|
|
203
|
+
await driftClient.openPosition(
|
|
206
204
|
PositionDirection.LONG,
|
|
207
205
|
new BN(5000).mul(QUOTE_PRECISION),
|
|
208
206
|
solMarketInfo.marketIndex
|
|
@@ -210,14 +208,14 @@ const main = async () => {
|
|
|
210
208
|
console.log(`LONGED $5000 worth of SOL`);
|
|
211
209
|
|
|
212
210
|
// Reduce the position by $2000
|
|
213
|
-
await
|
|
211
|
+
await driftClient.openPosition(
|
|
214
212
|
PositionDirection.SHORT,
|
|
215
213
|
new BN(2000).mul(QUOTE_PRECISION),
|
|
216
214
|
solMarketInfo.marketIndex
|
|
217
215
|
);
|
|
218
216
|
|
|
219
217
|
// Close the rest of the position
|
|
220
|
-
await
|
|
218
|
+
await driftClient.closePosition(solMarketInfo.marketIndex);
|
|
221
219
|
};
|
|
222
220
|
|
|
223
221
|
main();
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Commitment, Connection, PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BufferAndSlot } from './types';
|
|
4
|
+
import { Mutex } from 'async-mutex';
|
|
4
5
|
declare type AccountToLoad = {
|
|
5
6
|
publicKey: PublicKey;
|
|
6
7
|
callbacks: Map<string, (buffer: Buffer, slot: number) => void>;
|
|
@@ -14,6 +15,7 @@ export declare class BulkAccountLoader {
|
|
|
14
15
|
errorCallbacks: Map<string, (e: any) => void>;
|
|
15
16
|
intervalId?: NodeJS.Timer;
|
|
16
17
|
loadPromise?: Promise<void>;
|
|
18
|
+
loadPromiseLock: Mutex;
|
|
17
19
|
loadPromiseResolver: () => void;
|
|
18
20
|
lastTimeLoadingPromiseCleared: number;
|
|
19
21
|
mostRecentSlot: number;
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BulkAccountLoader = void 0;
|
|
4
4
|
const uuid_1 = require("uuid");
|
|
5
5
|
const promiseTimeout_1 = require("../util/promiseTimeout");
|
|
6
|
+
const async_mutex_1 = require("async-mutex");
|
|
6
7
|
const GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
|
|
7
8
|
const oneMinute = 60 * 1000;
|
|
8
9
|
class BulkAccountLoader {
|
|
@@ -10,6 +11,7 @@ class BulkAccountLoader {
|
|
|
10
11
|
this.accountsToLoad = new Map();
|
|
11
12
|
this.bufferAndSlotMap = new Map();
|
|
12
13
|
this.errorCallbacks = new Map();
|
|
14
|
+
this.loadPromiseLock = new async_mutex_1.Mutex();
|
|
13
15
|
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
14
16
|
this.mostRecentSlot = 0;
|
|
15
17
|
this.connection = connection;
|
|
@@ -36,7 +38,9 @@ class BulkAccountLoader {
|
|
|
36
38
|
this.startPolling();
|
|
37
39
|
}
|
|
38
40
|
// if a new account needs to be polled, remove the cached loadPromise in case client calls load immediately after
|
|
39
|
-
this.
|
|
41
|
+
this.loadPromiseLock.runExclusive(() => {
|
|
42
|
+
this.loadPromise = undefined;
|
|
43
|
+
});
|
|
40
44
|
return callbackId;
|
|
41
45
|
}
|
|
42
46
|
removeAccount(publicKey, callbackId) {
|
|
@@ -66,36 +70,38 @@ class BulkAccountLoader {
|
|
|
66
70
|
.map((begin) => array.slice(begin, begin + size));
|
|
67
71
|
}
|
|
68
72
|
async load() {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this.
|
|
73
|
+
await this.loadPromiseLock.runExclusive(async () => {
|
|
74
|
+
if (this.loadPromise) {
|
|
75
|
+
const now = Date.now();
|
|
76
|
+
if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
|
|
77
|
+
this.loadPromise = undefined;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return this.loadPromise;
|
|
81
|
+
}
|
|
73
82
|
}
|
|
74
|
-
|
|
75
|
-
|
|
83
|
+
this.loadPromise = new Promise((resolver) => {
|
|
84
|
+
this.loadPromiseResolver = resolver;
|
|
85
|
+
});
|
|
86
|
+
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
87
|
+
try {
|
|
88
|
+
const chunks = this.chunks(Array.from(this.accountsToLoad.values()), GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE);
|
|
89
|
+
await Promise.all(chunks.map((chunk) => {
|
|
90
|
+
return this.loadChunk(chunk);
|
|
91
|
+
}));
|
|
76
92
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const chunks = this.chunks(Array.from(this.accountsToLoad.values()), GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE);
|
|
84
|
-
await Promise.all(chunks.map((chunk) => {
|
|
85
|
-
return this.loadChunk(chunk);
|
|
86
|
-
}));
|
|
87
|
-
}
|
|
88
|
-
catch (e) {
|
|
89
|
-
console.error(`Error in bulkAccountLoader.load()`);
|
|
90
|
-
console.error(e);
|
|
91
|
-
for (const [_, callback] of this.errorCallbacks) {
|
|
92
|
-
callback(e);
|
|
93
|
+
catch (e) {
|
|
94
|
+
console.error(`Error in bulkAccountLoader.load()`);
|
|
95
|
+
console.error(e);
|
|
96
|
+
for (const [_, callback] of this.errorCallbacks) {
|
|
97
|
+
callback(e);
|
|
98
|
+
}
|
|
93
99
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
100
|
+
finally {
|
|
101
|
+
this.loadPromiseResolver();
|
|
102
|
+
this.loadPromise = undefined;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
99
105
|
}
|
|
100
106
|
async loadChunk(accountsToLoad) {
|
|
101
107
|
if (accountsToLoad.length === 0) {
|
|
@@ -162,7 +168,8 @@ class BulkAccountLoader {
|
|
|
162
168
|
if (this.intervalId) {
|
|
163
169
|
return;
|
|
164
170
|
}
|
|
165
|
-
|
|
171
|
+
if (this.pollingFrequency !== 0)
|
|
172
|
+
this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
|
|
166
173
|
}
|
|
167
174
|
stopPolling() {
|
|
168
175
|
if (this.intervalId) {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { UserStats } from '../userStats';
|
|
2
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
3
|
+
/**
|
|
4
|
+
* @param userStats
|
|
5
|
+
* @param accountLoader
|
|
6
|
+
*/
|
|
7
|
+
export declare function bulkPollingUserStatsSubscribe(userStats: UserStats[], accountLoader: BulkAccountLoader): Promise<void>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bulkPollingUserStatsSubscribe = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @param userStats
|
|
6
|
+
* @param accountLoader
|
|
7
|
+
*/
|
|
8
|
+
async function bulkPollingUserStatsSubscribe(userStats, accountLoader) {
|
|
9
|
+
if (userStats.length === 0) {
|
|
10
|
+
await accountLoader.load();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
await Promise.all(userStats.map((userStat) => {
|
|
14
|
+
return userStat.accountSubscriber.addToAccountLoader();
|
|
15
|
+
}));
|
|
16
|
+
await accountLoader.load();
|
|
17
|
+
await Promise.all(userStats.map(async (userStat) => {
|
|
18
|
+
return userStat.subscribe();
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
exports.bulkPollingUserStatsSubscribe = bulkPollingUserStatsSubscribe;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { User } from '../user';
|
|
2
2
|
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
3
3
|
/**
|
|
4
4
|
* @param users
|
|
5
5
|
* @param accountLoader
|
|
6
6
|
*/
|
|
7
|
-
export declare function bulkPollingUserSubscribe(users:
|
|
7
|
+
export declare function bulkPollingUserSubscribe(users: User[], accountLoader: BulkAccountLoader): Promise<void>;
|
|
@@ -11,7 +11,6 @@ async function bulkPollingUserSubscribe(users, accountLoader) {
|
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
13
13
|
await Promise.all(users.map((user) => {
|
|
14
|
-
// Pull the keys from the authority map so we can skip fetching them in addToAccountLoader
|
|
15
14
|
return user.accountSubscriber.addToAccountLoader();
|
|
16
15
|
}));
|
|
17
16
|
await accountLoader.load();
|
package/lib/accounts/fetch.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
-
import { UserAccount } from '../types';
|
|
2
|
+
import { UserAccount, UserStatsAccount } from '../types';
|
|
3
3
|
import { Program } from '@project-serum/anchor';
|
|
4
4
|
export declare function fetchUserAccounts(connection: Connection, program: Program, authority: PublicKey, limit?: number): Promise<(UserAccount | undefined)[]>;
|
|
5
|
+
export declare function fetchUserStatsAccount(connection: Connection, program: Program, authority: PublicKey): Promise<UserStatsAccount | undefined>;
|
package/lib/accounts/fetch.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fetchUserAccounts = void 0;
|
|
3
|
+
exports.fetchUserStatsAccount = exports.fetchUserAccounts = void 0;
|
|
4
4
|
const pda_1 = require("../addresses/pda");
|
|
5
5
|
async function fetchUserAccounts(connection, program, authority, limit = 8) {
|
|
6
6
|
const userAccountPublicKeys = new Array();
|
|
@@ -16,3 +16,11 @@ async function fetchUserAccounts(connection, program, authority, limit = 8) {
|
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
exports.fetchUserAccounts = fetchUserAccounts;
|
|
19
|
+
async function fetchUserStatsAccount(connection, program, authority) {
|
|
20
|
+
const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(program.programId, authority);
|
|
21
|
+
const accountInfo = await connection.getAccountInfo(userStatsPublicKey, 'confirmed');
|
|
22
|
+
return accountInfo
|
|
23
|
+
? program.account.user.coder.accounts.decode('UserStats', accountInfo.data)
|
|
24
|
+
: undefined;
|
|
25
|
+
}
|
|
26
|
+
exports.fetchUserStatsAccount = fetchUserStatsAccount;
|
|
@@ -1,61 +1,56 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
1
|
/// <reference types="node" />
|
|
3
|
-
import { DataAndSlot, AccountToPoll,
|
|
4
|
-
import {
|
|
2
|
+
import { DataAndSlot, AccountToPoll, DriftClientAccountEvents, DriftClientAccountSubscriber, OraclesToPoll } from './types';
|
|
3
|
+
import { Program } from '@project-serum/anchor';
|
|
5
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
6
5
|
import { EventEmitter } from 'events';
|
|
7
|
-
import {
|
|
6
|
+
import { SpotMarketAccount, PerpMarketAccount, StateAccount, UserAccount } from '../types';
|
|
8
7
|
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
9
8
|
import { PublicKey } from '@solana/web3.js';
|
|
10
9
|
import { OracleInfo, OraclePriceData } from '../oracles/types';
|
|
11
10
|
import { OracleClientCache } from '../oracles/oracleClientCache';
|
|
12
|
-
export declare class
|
|
11
|
+
export declare class PollingDriftClientAccountSubscriber implements DriftClientAccountSubscriber {
|
|
13
12
|
isSubscribed: boolean;
|
|
14
13
|
program: Program;
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
perpMarketIndexes: number[];
|
|
15
|
+
spotMarketIndexes: number[];
|
|
17
16
|
oracleInfos: OracleInfo[];
|
|
18
17
|
oracleClientCache: OracleClientCache;
|
|
19
|
-
eventEmitter: StrictEventEmitter<EventEmitter,
|
|
18
|
+
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
|
|
20
19
|
accountLoader: BulkAccountLoader;
|
|
21
20
|
accountsToPoll: Map<string, AccountToPoll>;
|
|
22
21
|
oraclesToPoll: Map<string, OraclesToPoll>;
|
|
23
22
|
errorCallbackId?: string;
|
|
24
23
|
state?: DataAndSlot<StateAccount>;
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
perpMarket: Map<number, DataAndSlot<PerpMarketAccount>>;
|
|
25
|
+
spotMarket: Map<number, DataAndSlot<SpotMarketAccount>>;
|
|
27
26
|
oracles: Map<string, DataAndSlot<OraclePriceData>>;
|
|
28
27
|
user?: DataAndSlot<UserAccount>;
|
|
29
28
|
private isSubscribing;
|
|
30
29
|
private subscriptionPromise;
|
|
31
30
|
private subscriptionPromiseResolver;
|
|
32
|
-
constructor(program: Program, accountLoader: BulkAccountLoader,
|
|
31
|
+
constructor(program: Program, accountLoader: BulkAccountLoader, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[]);
|
|
33
32
|
subscribe(): Promise<boolean>;
|
|
34
33
|
updateAccountsToPoll(): Promise<void>;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
updatePerpMarketAccountsToPoll(): Promise<boolean>;
|
|
35
|
+
addPerpMarketAccountToPoll(marketIndex: number): Promise<boolean>;
|
|
36
|
+
updateSpotMarketAccountsToPoll(): Promise<boolean>;
|
|
37
|
+
addSpotMarketAccountToPoll(marketIndex: number): Promise<boolean>;
|
|
39
38
|
updateOraclesToPoll(): boolean;
|
|
40
39
|
addOracleToPoll(oracleInfo: OracleInfo): boolean;
|
|
41
|
-
getClearingHouseAccounts(): Promise<ClearingHouseAccounts>;
|
|
42
40
|
addToAccountLoader(): Promise<void>;
|
|
43
41
|
addAccountToAccountLoader(accountToPoll: AccountToPoll): void;
|
|
44
42
|
addOracleToAccountLoader(oracleToPoll: OraclesToPoll): void;
|
|
45
43
|
fetch(): Promise<void>;
|
|
46
44
|
didSubscriptionSucceed(): boolean;
|
|
47
45
|
unsubscribe(): Promise<void>;
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
addSpotMarket(marketIndex: number): Promise<boolean>;
|
|
47
|
+
addPerpMarket(marketIndex: number): Promise<boolean>;
|
|
50
48
|
addOracle(oracleInfo: OracleInfo): Promise<boolean>;
|
|
51
49
|
assertIsSubscribed(): void;
|
|
52
50
|
getStateAccountAndSlot(): DataAndSlot<StateAccount>;
|
|
53
|
-
getMarketAccountAndSlot(marketIndex:
|
|
54
|
-
getMarketAccountsAndSlots(): DataAndSlot<
|
|
55
|
-
|
|
51
|
+
getMarketAccountAndSlot(marketIndex: number): DataAndSlot<PerpMarketAccount> | undefined;
|
|
52
|
+
getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
|
|
53
|
+
getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
|
|
54
|
+
getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
|
|
56
55
|
getOraclePriceDataAndSlot(oraclePublicKey: PublicKey): DataAndSlot<OraclePriceData> | undefined;
|
|
57
56
|
}
|
|
58
|
-
declare type ClearingHouseAccounts = {
|
|
59
|
-
state: PublicKey;
|
|
60
|
-
};
|
|
61
|
-
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.PollingDriftClientAccountSubscriber = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const events_1 = require("events");
|
|
6
6
|
const pda_1 = require("../addresses/pda");
|
|
@@ -8,21 +8,21 @@ const utils_1 = require("./utils");
|
|
|
8
8
|
const web3_js_1 = require("@solana/web3.js");
|
|
9
9
|
const oracleClientCache_1 = require("../oracles/oracleClientCache");
|
|
10
10
|
const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
|
|
11
|
-
class
|
|
12
|
-
constructor(program, accountLoader,
|
|
11
|
+
class PollingDriftClientAccountSubscriber {
|
|
12
|
+
constructor(program, accountLoader, perpMarketIndexes, spotMarketIndexes, oracleInfos) {
|
|
13
13
|
this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
|
|
14
14
|
this.accountsToPoll = new Map();
|
|
15
15
|
this.oraclesToPoll = new Map();
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
16
|
+
this.perpMarket = new Map();
|
|
17
|
+
this.spotMarket = new Map();
|
|
18
18
|
this.oracles = new Map();
|
|
19
19
|
this.isSubscribing = false;
|
|
20
20
|
this.isSubscribed = false;
|
|
21
21
|
this.program = program;
|
|
22
22
|
this.eventEmitter = new events_1.EventEmitter();
|
|
23
23
|
this.accountLoader = accountLoader;
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
24
|
+
this.perpMarketIndexes = perpMarketIndexes;
|
|
25
|
+
this.spotMarketIndexes = spotMarketIndexes;
|
|
26
26
|
this.oracleInfos = oracleInfos;
|
|
27
27
|
}
|
|
28
28
|
async subscribe() {
|
|
@@ -58,44 +58,44 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
58
58
|
if (this.accountsToPoll.size > 0) {
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
|
-
const
|
|
62
|
-
this.accountsToPoll.set(
|
|
61
|
+
const statePublicKey = await (0, pda_1.getDriftStateAccountPublicKey)(this.program.programId);
|
|
62
|
+
this.accountsToPoll.set(statePublicKey.toString(), {
|
|
63
63
|
key: 'state',
|
|
64
|
-
publicKey:
|
|
64
|
+
publicKey: statePublicKey,
|
|
65
65
|
eventType: 'stateAccountUpdate',
|
|
66
66
|
});
|
|
67
|
-
await this.
|
|
68
|
-
await this.
|
|
67
|
+
await this.updatePerpMarketAccountsToPoll();
|
|
68
|
+
await this.updateSpotMarketAccountsToPoll();
|
|
69
69
|
}
|
|
70
|
-
async
|
|
71
|
-
for (const marketIndex of this.
|
|
72
|
-
await this.
|
|
70
|
+
async updatePerpMarketAccountsToPoll() {
|
|
71
|
+
for (const marketIndex of this.perpMarketIndexes) {
|
|
72
|
+
await this.addPerpMarketAccountToPoll(marketIndex);
|
|
73
73
|
}
|
|
74
74
|
return true;
|
|
75
75
|
}
|
|
76
|
-
async
|
|
77
|
-
const
|
|
78
|
-
this.accountsToPoll.set(
|
|
79
|
-
key: '
|
|
80
|
-
publicKey:
|
|
81
|
-
eventType: '
|
|
82
|
-
mapKey: marketIndex
|
|
76
|
+
async addPerpMarketAccountToPoll(marketIndex) {
|
|
77
|
+
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
|
|
78
|
+
this.accountsToPoll.set(perpMarketPublicKey.toString(), {
|
|
79
|
+
key: 'perpMarket',
|
|
80
|
+
publicKey: perpMarketPublicKey,
|
|
81
|
+
eventType: 'perpMarketAccountUpdate',
|
|
82
|
+
mapKey: marketIndex,
|
|
83
83
|
});
|
|
84
84
|
return true;
|
|
85
85
|
}
|
|
86
|
-
async
|
|
87
|
-
for (const
|
|
88
|
-
await this.
|
|
86
|
+
async updateSpotMarketAccountsToPoll() {
|
|
87
|
+
for (const marketIndex of this.spotMarketIndexes) {
|
|
88
|
+
await this.addSpotMarketAccountToPoll(marketIndex);
|
|
89
89
|
}
|
|
90
90
|
return true;
|
|
91
91
|
}
|
|
92
|
-
async
|
|
93
|
-
const
|
|
94
|
-
this.accountsToPoll.set(
|
|
95
|
-
key: '
|
|
96
|
-
publicKey:
|
|
97
|
-
eventType: '
|
|
98
|
-
mapKey:
|
|
92
|
+
async addSpotMarketAccountToPoll(marketIndex) {
|
|
93
|
+
const marketPublicKey = await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex);
|
|
94
|
+
this.accountsToPoll.set(marketPublicKey.toString(), {
|
|
95
|
+
key: 'spotMarket',
|
|
96
|
+
publicKey: marketPublicKey,
|
|
97
|
+
eventType: 'spotMarketAccountUpdate',
|
|
98
|
+
mapKey: marketIndex,
|
|
99
99
|
});
|
|
100
100
|
return true;
|
|
101
101
|
}
|
|
@@ -114,13 +114,6 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
114
114
|
});
|
|
115
115
|
return true;
|
|
116
116
|
}
|
|
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
117
|
async addToAccountLoader() {
|
|
125
118
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
126
119
|
this.addAccountToAccountLoader(accountToPoll);
|
|
@@ -223,14 +216,14 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
223
216
|
this.oraclesToPoll.clear();
|
|
224
217
|
this.isSubscribed = false;
|
|
225
218
|
}
|
|
226
|
-
async
|
|
227
|
-
await this.
|
|
228
|
-
const accountToPoll = this.accountsToPoll.get(
|
|
219
|
+
async addSpotMarket(marketIndex) {
|
|
220
|
+
await this.addSpotMarketAccountToPoll(marketIndex);
|
|
221
|
+
const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
|
|
229
222
|
this.addAccountToAccountLoader(accountToPoll);
|
|
230
223
|
return true;
|
|
231
224
|
}
|
|
232
|
-
async
|
|
233
|
-
await this.
|
|
225
|
+
async addPerpMarket(marketIndex) {
|
|
226
|
+
await this.addPerpMarketAccountToPoll(marketIndex);
|
|
234
227
|
const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
|
|
235
228
|
this.addAccountToAccountLoader(accountToPoll);
|
|
236
229
|
return true;
|
|
@@ -254,13 +247,16 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
254
247
|
return this.state;
|
|
255
248
|
}
|
|
256
249
|
getMarketAccountAndSlot(marketIndex) {
|
|
257
|
-
return this.
|
|
250
|
+
return this.perpMarket.get(marketIndex);
|
|
258
251
|
}
|
|
259
252
|
getMarketAccountsAndSlots() {
|
|
260
|
-
return Array.from(this.
|
|
253
|
+
return Array.from(this.perpMarket.values());
|
|
254
|
+
}
|
|
255
|
+
getSpotMarketAccountAndSlot(marketIndex) {
|
|
256
|
+
return this.spotMarket.get(marketIndex);
|
|
261
257
|
}
|
|
262
|
-
|
|
263
|
-
return this.
|
|
258
|
+
getSpotMarketAccountsAndSlots() {
|
|
259
|
+
return Array.from(this.spotMarket.values());
|
|
264
260
|
}
|
|
265
261
|
getOraclePriceDataAndSlot(oraclePublicKey) {
|
|
266
262
|
this.assertIsSubscribed();
|
|
@@ -273,4 +269,4 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
273
269
|
return this.oracles.get(oraclePublicKey.toString());
|
|
274
270
|
}
|
|
275
271
|
}
|
|
276
|
-
exports.
|
|
272
|
+
exports.PollingDriftClientAccountSubscriber = PollingDriftClientAccountSubscriber;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { DataAndSlot, OracleEvents,
|
|
2
|
+
import { DataAndSlot, OracleEvents, OracleAccountSubscriber } 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';
|
|
6
6
|
import { PublicKey } from '@solana/web3.js';
|
|
7
7
|
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
8
8
|
import { OracleClient, OraclePriceData } from '../oracles/types';
|
|
9
|
-
export declare class
|
|
9
|
+
export declare class PollingOracleAccountSubscriber implements OracleAccountSubscriber {
|
|
10
10
|
isSubscribed: boolean;
|
|
11
11
|
program: Program;
|
|
12
12
|
eventEmitter: StrictEventEmitter<EventEmitter, OracleEvents>;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.PollingOracleAccountSubscriber = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const events_1 = require("events");
|
|
6
|
-
class
|
|
6
|
+
class PollingOracleAccountSubscriber {
|
|
7
7
|
constructor(publicKey, oracleClient, accountLoader) {
|
|
8
8
|
this.isSubscribed = false;
|
|
9
9
|
this.publicKey = publicKey;
|
|
@@ -75,4 +75,4 @@ class PollingOracleSubscriber {
|
|
|
75
75
|
return !!this.oraclePriceData;
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
-
exports.
|
|
78
|
+
exports.PollingOracleAccountSubscriber = PollingOracleAccountSubscriber;
|
|
@@ -20,7 +20,7 @@ export declare class PollingUserAccountSubscriber implements UserAccountSubscrib
|
|
|
20
20
|
addToAccountLoader(): Promise<void>;
|
|
21
21
|
fetchIfUnloaded(): Promise<void>;
|
|
22
22
|
fetch(): Promise<void>;
|
|
23
|
-
|
|
23
|
+
doAccountsExist(): boolean;
|
|
24
24
|
unsubscribe(): Promise<void>;
|
|
25
25
|
assertIsSubscribed(): void;
|
|
26
26
|
getUserAccountAndSlot(): DataAndSlot<UserAccount>;
|
|
@@ -18,18 +18,12 @@ class PollingUserAccountSubscriber {
|
|
|
18
18
|
return true;
|
|
19
19
|
}
|
|
20
20
|
await this.addToAccountLoader();
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
while (!subscriptionSucceeded && retries < 5) {
|
|
24
|
-
await this.fetchIfUnloaded();
|
|
25
|
-
subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
26
|
-
retries++;
|
|
27
|
-
}
|
|
28
|
-
if (subscriptionSucceeded) {
|
|
21
|
+
await this.fetchIfUnloaded();
|
|
22
|
+
if (this.doAccountsExist()) {
|
|
29
23
|
this.eventEmitter.emit('update');
|
|
30
24
|
}
|
|
31
|
-
this.isSubscribed =
|
|
32
|
-
return
|
|
25
|
+
this.isSubscribed = true;
|
|
26
|
+
return true;
|
|
33
27
|
}
|
|
34
28
|
async addToAccountLoader() {
|
|
35
29
|
if (this.accountsToPoll.size > 0) {
|
|
@@ -78,7 +72,7 @@ class PollingUserAccountSubscriber {
|
|
|
78
72
|
}
|
|
79
73
|
}
|
|
80
74
|
}
|
|
81
|
-
|
|
75
|
+
doAccountsExist() {
|
|
82
76
|
let success = true;
|
|
83
77
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
84
78
|
if (!this[accountToPoll.key]) {
|