@drift-labs/sdk 0.2.0-temp.2 → 2.0.1
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.d.ts +0 -1
- package/lib/events/eventList.js +0 -7
- 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/txEventCache.d.ts +0 -2
- package/lib/events/txEventCache.js +0 -14
- 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 +1 -8
- 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/txEventCache.ts +0 -16
- 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
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DataAndSlot,
|
|
3
3
|
AccountToPoll,
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
DriftClientAccountEvents,
|
|
5
|
+
DriftClientAccountSubscriber,
|
|
6
6
|
NotSubscribedError,
|
|
7
7
|
OraclesToPoll,
|
|
8
8
|
} from './types';
|
|
9
|
-
import {
|
|
9
|
+
import { Program } from '@project-serum/anchor';
|
|
10
10
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
11
11
|
import { EventEmitter } from 'events';
|
|
12
12
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
SpotMarketAccount,
|
|
14
|
+
PerpMarketAccount,
|
|
15
15
|
StateAccount,
|
|
16
16
|
UserAccount,
|
|
17
17
|
} from '../types';
|
|
18
18
|
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
getDriftStateAccountPublicKey,
|
|
20
|
+
getSpotMarketPublicKey,
|
|
21
|
+
getPerpMarketPublicKey,
|
|
22
22
|
} from '../addresses/pda';
|
|
23
23
|
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
24
24
|
import { capitalize } from './utils';
|
|
@@ -27,17 +27,17 @@ import { OracleInfo, OraclePriceData } from '../oracles/types';
|
|
|
27
27
|
import { OracleClientCache } from '../oracles/oracleClientCache';
|
|
28
28
|
import { QUOTE_ORACLE_PRICE_DATA } from '../oracles/quoteAssetOracleClient';
|
|
29
29
|
|
|
30
|
-
export class
|
|
31
|
-
implements
|
|
30
|
+
export class PollingDriftClientAccountSubscriber
|
|
31
|
+
implements DriftClientAccountSubscriber
|
|
32
32
|
{
|
|
33
33
|
isSubscribed: boolean;
|
|
34
34
|
program: Program;
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
perpMarketIndexes: number[];
|
|
36
|
+
spotMarketIndexes: number[];
|
|
37
37
|
oracleInfos: OracleInfo[];
|
|
38
38
|
oracleClientCache = new OracleClientCache();
|
|
39
39
|
|
|
40
|
-
eventEmitter: StrictEventEmitter<EventEmitter,
|
|
40
|
+
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
|
|
41
41
|
|
|
42
42
|
accountLoader: BulkAccountLoader;
|
|
43
43
|
accountsToPoll = new Map<string, AccountToPoll>();
|
|
@@ -45,8 +45,8 @@ export class PollingClearingHouseAccountSubscriber
|
|
|
45
45
|
errorCallbackId?: string;
|
|
46
46
|
|
|
47
47
|
state?: DataAndSlot<StateAccount>;
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
perpMarket = new Map<number, DataAndSlot<PerpMarketAccount>>();
|
|
49
|
+
spotMarket = new Map<number, DataAndSlot<SpotMarketAccount>>();
|
|
50
50
|
oracles = new Map<string, DataAndSlot<OraclePriceData>>();
|
|
51
51
|
user?: DataAndSlot<UserAccount>;
|
|
52
52
|
|
|
@@ -57,16 +57,16 @@ export class PollingClearingHouseAccountSubscriber
|
|
|
57
57
|
public constructor(
|
|
58
58
|
program: Program,
|
|
59
59
|
accountLoader: BulkAccountLoader,
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
perpMarketIndexes: number[],
|
|
61
|
+
spotMarketIndexes: number[],
|
|
62
62
|
oracleInfos: OracleInfo[]
|
|
63
63
|
) {
|
|
64
64
|
this.isSubscribed = false;
|
|
65
65
|
this.program = program;
|
|
66
66
|
this.eventEmitter = new EventEmitter();
|
|
67
67
|
this.accountLoader = accountLoader;
|
|
68
|
-
this.
|
|
69
|
-
this.
|
|
68
|
+
this.perpMarketIndexes = perpMarketIndexes;
|
|
69
|
+
this.spotMarketIndexes = spotMarketIndexes;
|
|
70
70
|
this.oracleInfos = oracleInfos;
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -113,60 +113,62 @@ export class PollingClearingHouseAccountSubscriber
|
|
|
113
113
|
return;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
const
|
|
116
|
+
const statePublicKey = await getDriftStateAccountPublicKey(
|
|
117
|
+
this.program.programId
|
|
118
|
+
);
|
|
117
119
|
|
|
118
|
-
this.accountsToPoll.set(
|
|
120
|
+
this.accountsToPoll.set(statePublicKey.toString(), {
|
|
119
121
|
key: 'state',
|
|
120
|
-
publicKey:
|
|
122
|
+
publicKey: statePublicKey,
|
|
121
123
|
eventType: 'stateAccountUpdate',
|
|
122
124
|
});
|
|
123
125
|
|
|
124
|
-
await this.
|
|
125
|
-
await this.
|
|
126
|
+
await this.updatePerpMarketAccountsToPoll();
|
|
127
|
+
await this.updateSpotMarketAccountsToPoll();
|
|
126
128
|
}
|
|
127
129
|
|
|
128
|
-
async
|
|
129
|
-
for (const marketIndex of this.
|
|
130
|
-
await this.
|
|
130
|
+
async updatePerpMarketAccountsToPoll(): Promise<boolean> {
|
|
131
|
+
for (const marketIndex of this.perpMarketIndexes) {
|
|
132
|
+
await this.addPerpMarketAccountToPoll(marketIndex);
|
|
131
133
|
}
|
|
132
134
|
return true;
|
|
133
135
|
}
|
|
134
136
|
|
|
135
|
-
async
|
|
136
|
-
const
|
|
137
|
+
async addPerpMarketAccountToPoll(marketIndex: number): Promise<boolean> {
|
|
138
|
+
const perpMarketPublicKey = await getPerpMarketPublicKey(
|
|
137
139
|
this.program.programId,
|
|
138
140
|
marketIndex
|
|
139
141
|
);
|
|
140
142
|
|
|
141
|
-
this.accountsToPoll.set(
|
|
142
|
-
key: '
|
|
143
|
-
publicKey:
|
|
144
|
-
eventType: '
|
|
145
|
-
mapKey: marketIndex
|
|
143
|
+
this.accountsToPoll.set(perpMarketPublicKey.toString(), {
|
|
144
|
+
key: 'perpMarket',
|
|
145
|
+
publicKey: perpMarketPublicKey,
|
|
146
|
+
eventType: 'perpMarketAccountUpdate',
|
|
147
|
+
mapKey: marketIndex,
|
|
146
148
|
});
|
|
147
149
|
|
|
148
150
|
return true;
|
|
149
151
|
}
|
|
150
152
|
|
|
151
|
-
async
|
|
152
|
-
for (const
|
|
153
|
-
await this.
|
|
153
|
+
async updateSpotMarketAccountsToPoll(): Promise<boolean> {
|
|
154
|
+
for (const marketIndex of this.spotMarketIndexes) {
|
|
155
|
+
await this.addSpotMarketAccountToPoll(marketIndex);
|
|
154
156
|
}
|
|
155
157
|
|
|
156
158
|
return true;
|
|
157
159
|
}
|
|
158
160
|
|
|
159
|
-
async
|
|
160
|
-
const
|
|
161
|
+
async addSpotMarketAccountToPoll(marketIndex: number): Promise<boolean> {
|
|
162
|
+
const marketPublicKey = await getSpotMarketPublicKey(
|
|
161
163
|
this.program.programId,
|
|
162
|
-
|
|
164
|
+
marketIndex
|
|
163
165
|
);
|
|
164
166
|
|
|
165
|
-
this.accountsToPoll.set(
|
|
166
|
-
key: '
|
|
167
|
-
publicKey:
|
|
168
|
-
eventType: '
|
|
169
|
-
mapKey:
|
|
167
|
+
this.accountsToPoll.set(marketPublicKey.toString(), {
|
|
168
|
+
key: 'spotMarket',
|
|
169
|
+
publicKey: marketPublicKey,
|
|
170
|
+
eventType: 'spotMarketAccountUpdate',
|
|
171
|
+
mapKey: marketIndex,
|
|
170
172
|
});
|
|
171
173
|
return true;
|
|
172
174
|
}
|
|
@@ -190,18 +192,6 @@ export class PollingClearingHouseAccountSubscriber
|
|
|
190
192
|
return true;
|
|
191
193
|
}
|
|
192
194
|
|
|
193
|
-
async getClearingHouseAccounts(): Promise<ClearingHouseAccounts> {
|
|
194
|
-
const statePublicKey = await getClearingHouseStateAccountPublicKey(
|
|
195
|
-
this.program.programId
|
|
196
|
-
);
|
|
197
|
-
|
|
198
|
-
const accounts = {
|
|
199
|
-
state: statePublicKey,
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
return accounts;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
195
|
async addToAccountLoader(): Promise<void> {
|
|
206
196
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
207
197
|
this.addAccountToAccountLoader(accountToPoll);
|
|
@@ -353,15 +343,15 @@ export class PollingClearingHouseAccountSubscriber
|
|
|
353
343
|
this.isSubscribed = false;
|
|
354
344
|
}
|
|
355
345
|
|
|
356
|
-
async
|
|
357
|
-
await this.
|
|
358
|
-
const accountToPoll = this.accountsToPoll.get(
|
|
346
|
+
async addSpotMarket(marketIndex: number): Promise<boolean> {
|
|
347
|
+
await this.addSpotMarketAccountToPoll(marketIndex);
|
|
348
|
+
const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
|
|
359
349
|
this.addAccountToAccountLoader(accountToPoll);
|
|
360
350
|
return true;
|
|
361
351
|
}
|
|
362
352
|
|
|
363
|
-
async
|
|
364
|
-
await this.
|
|
353
|
+
async addPerpMarket(marketIndex: number): Promise<boolean> {
|
|
354
|
+
await this.addPerpMarketAccountToPoll(marketIndex);
|
|
365
355
|
const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
|
|
366
356
|
this.addAccountToAccountLoader(accountToPoll);
|
|
367
357
|
return true;
|
|
@@ -394,19 +384,23 @@ export class PollingClearingHouseAccountSubscriber
|
|
|
394
384
|
}
|
|
395
385
|
|
|
396
386
|
public getMarketAccountAndSlot(
|
|
397
|
-
marketIndex:
|
|
398
|
-
): DataAndSlot<
|
|
399
|
-
return this.
|
|
387
|
+
marketIndex: number
|
|
388
|
+
): DataAndSlot<PerpMarketAccount> | undefined {
|
|
389
|
+
return this.perpMarket.get(marketIndex);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
public getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[] {
|
|
393
|
+
return Array.from(this.perpMarket.values());
|
|
400
394
|
}
|
|
401
395
|
|
|
402
|
-
public
|
|
403
|
-
|
|
396
|
+
public getSpotMarketAccountAndSlot(
|
|
397
|
+
marketIndex: number
|
|
398
|
+
): DataAndSlot<SpotMarketAccount> | undefined {
|
|
399
|
+
return this.spotMarket.get(marketIndex);
|
|
404
400
|
}
|
|
405
401
|
|
|
406
|
-
public
|
|
407
|
-
|
|
408
|
-
): DataAndSlot<BankAccount> | undefined {
|
|
409
|
-
return this.bank.get(bankIndex.toNumber());
|
|
402
|
+
public getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[] {
|
|
403
|
+
return Array.from(this.spotMarket.values());
|
|
410
404
|
}
|
|
411
405
|
|
|
412
406
|
public getOraclePriceDataAndSlot(
|
|
@@ -423,7 +417,3 @@ export class PollingClearingHouseAccountSubscriber
|
|
|
423
417
|
return this.oracles.get(oraclePublicKey.toString());
|
|
424
418
|
}
|
|
425
419
|
}
|
|
426
|
-
|
|
427
|
-
type ClearingHouseAccounts = {
|
|
428
|
-
state: PublicKey;
|
|
429
|
-
};
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
DataAndSlot,
|
|
3
3
|
NotSubscribedError,
|
|
4
4
|
OracleEvents,
|
|
5
|
-
|
|
5
|
+
OracleAccountSubscriber,
|
|
6
6
|
} from './types';
|
|
7
7
|
import { Program } from '@project-serum/anchor';
|
|
8
8
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
@@ -11,7 +11,7 @@ import { PublicKey } from '@solana/web3.js';
|
|
|
11
11
|
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
12
12
|
import { OracleClient, OraclePriceData } from '../oracles/types';
|
|
13
13
|
|
|
14
|
-
export class
|
|
14
|
+
export class PollingOracleAccountSubscriber implements OracleAccountSubscriber {
|
|
15
15
|
isSubscribed: boolean;
|
|
16
16
|
program: Program;
|
|
17
17
|
eventEmitter: StrictEventEmitter<EventEmitter, OracleEvents>;
|
|
@@ -44,20 +44,13 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
44
44
|
|
|
45
45
|
await this.addToAccountLoader();
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
while (!subscriptionSucceeded && retries < 5) {
|
|
50
|
-
await this.fetchIfUnloaded();
|
|
51
|
-
subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
52
|
-
retries++;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (subscriptionSucceeded) {
|
|
47
|
+
await this.fetchIfUnloaded();
|
|
48
|
+
if (this.doAccountsExist()) {
|
|
56
49
|
this.eventEmitter.emit('update');
|
|
57
50
|
}
|
|
58
51
|
|
|
59
|
-
this.isSubscribed =
|
|
60
|
-
return
|
|
52
|
+
this.isSubscribed = true;
|
|
53
|
+
return true;
|
|
61
54
|
}
|
|
62
55
|
|
|
63
56
|
async addToAccountLoader(): Promise<void> {
|
|
@@ -124,7 +117,7 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
124
117
|
}
|
|
125
118
|
}
|
|
126
119
|
|
|
127
|
-
|
|
120
|
+
doAccountsExist(): boolean {
|
|
128
121
|
let success = true;
|
|
129
122
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
130
123
|
if (!this[accountToPoll.key]) {
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataAndSlot,
|
|
3
|
+
AccountToPoll,
|
|
4
|
+
NotSubscribedError,
|
|
5
|
+
UserStatsAccountSubscriber,
|
|
6
|
+
UserStatsAccountEvents,
|
|
7
|
+
} from './types';
|
|
8
|
+
import { Program } from '@project-serum/anchor';
|
|
9
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
10
|
+
import { EventEmitter } from 'events';
|
|
11
|
+
import { PublicKey } from '@solana/web3.js';
|
|
12
|
+
import { UserStatsAccount } from '../types';
|
|
13
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
14
|
+
import { capitalize } from './utils';
|
|
15
|
+
|
|
16
|
+
export class PollingUserStatsAccountSubscriber
|
|
17
|
+
implements UserStatsAccountSubscriber
|
|
18
|
+
{
|
|
19
|
+
isSubscribed: boolean;
|
|
20
|
+
program: Program;
|
|
21
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
|
|
22
|
+
userStatsAccountPublicKey: PublicKey;
|
|
23
|
+
|
|
24
|
+
accountLoader: BulkAccountLoader;
|
|
25
|
+
accountsToPoll = new Map<string, AccountToPoll>();
|
|
26
|
+
errorCallbackId?: string;
|
|
27
|
+
|
|
28
|
+
userStats?: DataAndSlot<UserStatsAccount>;
|
|
29
|
+
|
|
30
|
+
public constructor(
|
|
31
|
+
program: Program,
|
|
32
|
+
userStatsAccountPublicKey: PublicKey,
|
|
33
|
+
accountLoader: BulkAccountLoader
|
|
34
|
+
) {
|
|
35
|
+
this.isSubscribed = false;
|
|
36
|
+
this.program = program;
|
|
37
|
+
this.accountLoader = accountLoader;
|
|
38
|
+
this.eventEmitter = new EventEmitter();
|
|
39
|
+
this.userStatsAccountPublicKey = userStatsAccountPublicKey;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async subscribe(): Promise<boolean> {
|
|
43
|
+
if (this.isSubscribed) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
await this.addToAccountLoader();
|
|
48
|
+
|
|
49
|
+
await this.fetchIfUnloaded();
|
|
50
|
+
|
|
51
|
+
if (this.doAccountsExist()) {
|
|
52
|
+
this.eventEmitter.emit('update');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.isSubscribed = true;
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async addToAccountLoader(): Promise<void> {
|
|
60
|
+
if (this.accountsToPoll.size > 0) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.accountsToPoll.set(this.userStatsAccountPublicKey.toString(), {
|
|
65
|
+
key: 'userStats',
|
|
66
|
+
publicKey: this.userStatsAccountPublicKey,
|
|
67
|
+
eventType: 'userStatsAccountUpdate',
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
71
|
+
accountToPoll.callbackId = this.accountLoader.addAccount(
|
|
72
|
+
accountToPoll.publicKey,
|
|
73
|
+
(buffer, slot) => {
|
|
74
|
+
if (!buffer) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const account = this.program.account[
|
|
79
|
+
accountToPoll.key
|
|
80
|
+
].coder.accounts.decode(capitalize(accountToPoll.key), buffer);
|
|
81
|
+
this[accountToPoll.key] = { data: account, slot };
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
84
|
+
this.eventEmitter.emit('update');
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
90
|
+
this.eventEmitter.emit('error', error);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async fetchIfUnloaded(): Promise<void> {
|
|
95
|
+
let shouldFetch = false;
|
|
96
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
97
|
+
if (this[accountToPoll.key] === undefined) {
|
|
98
|
+
shouldFetch = true;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (shouldFetch) {
|
|
104
|
+
await this.fetch();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async fetch(): Promise<void> {
|
|
109
|
+
await this.accountLoader.load();
|
|
110
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
111
|
+
const { buffer, slot } = this.accountLoader.getBufferAndSlot(
|
|
112
|
+
accountToPoll.publicKey
|
|
113
|
+
);
|
|
114
|
+
if (buffer) {
|
|
115
|
+
const account = this.program.account[
|
|
116
|
+
accountToPoll.key
|
|
117
|
+
].coder.accounts.decode(capitalize(accountToPoll.key), buffer);
|
|
118
|
+
this[accountToPoll.key] = { data: account, slot };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
doAccountsExist(): boolean {
|
|
124
|
+
let success = true;
|
|
125
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
126
|
+
if (!this[accountToPoll.key]) {
|
|
127
|
+
success = false;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return success;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async unsubscribe(): Promise<void> {
|
|
135
|
+
if (!this.isSubscribed) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
140
|
+
this.accountLoader.removeAccount(
|
|
141
|
+
accountToPoll.publicKey,
|
|
142
|
+
accountToPoll.callbackId
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
147
|
+
this.errorCallbackId = undefined;
|
|
148
|
+
|
|
149
|
+
this.accountsToPoll.clear();
|
|
150
|
+
|
|
151
|
+
this.isSubscribed = false;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
assertIsSubscribed(): void {
|
|
155
|
+
if (!this.isSubscribed) {
|
|
156
|
+
throw new NotSubscribedError(
|
|
157
|
+
'You must call `subscribe` before using this function'
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount> {
|
|
163
|
+
this.assertIsSubscribed();
|
|
164
|
+
return this.userStats;
|
|
165
|
+
}
|
|
166
|
+
}
|
package/src/accounts/types.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
SpotMarketAccount,
|
|
3
|
+
PerpMarketAccount,
|
|
4
4
|
OracleSource,
|
|
5
5
|
StateAccount,
|
|
6
6
|
UserAccount,
|
|
7
|
+
UserStatsAccount,
|
|
7
8
|
} from '../types';
|
|
8
9
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
9
10
|
import { EventEmitter } from 'events';
|
|
10
11
|
import { PublicKey } from '@solana/web3.js';
|
|
11
12
|
import { AccountInfo } from '@solana/spl-token';
|
|
12
13
|
import { OracleInfo, OraclePriceData } from '..';
|
|
13
|
-
import { BN } from '@project-serum/anchor';
|
|
14
14
|
|
|
15
15
|
export interface AccountSubscriber<T> {
|
|
16
16
|
dataAndSlot?: DataAndSlot<T>;
|
|
@@ -23,34 +23,37 @@ export class NotSubscribedError extends Error {
|
|
|
23
23
|
name = 'NotSubscribedError';
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
export interface
|
|
26
|
+
export interface DriftClientAccountEvents {
|
|
27
27
|
stateAccountUpdate: (payload: StateAccount) => void;
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
perpMarketAccountUpdate: (payload: PerpMarketAccount) => void;
|
|
29
|
+
spotMarketAccountUpdate: (payload: SpotMarketAccount) => void;
|
|
30
30
|
oraclePriceUpdate: (publicKey: PublicKey, data: OraclePriceData) => void;
|
|
31
31
|
userAccountUpdate: (payload: UserAccount) => void;
|
|
32
32
|
update: void;
|
|
33
33
|
error: (e: Error) => void;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
export interface
|
|
37
|
-
eventEmitter: StrictEventEmitter<EventEmitter,
|
|
36
|
+
export interface DriftClientAccountSubscriber {
|
|
37
|
+
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
|
|
38
38
|
isSubscribed: boolean;
|
|
39
39
|
|
|
40
40
|
subscribe(): Promise<boolean>;
|
|
41
41
|
fetch(): Promise<void>;
|
|
42
42
|
unsubscribe(): Promise<void>;
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
addPerpMarket(marketIndex: number): Promise<boolean>;
|
|
45
|
+
addSpotMarket(marketIndex: number): Promise<boolean>;
|
|
46
46
|
addOracle(oracleInfo: OracleInfo): Promise<boolean>;
|
|
47
47
|
|
|
48
48
|
getStateAccountAndSlot(): DataAndSlot<StateAccount>;
|
|
49
49
|
getMarketAccountAndSlot(
|
|
50
|
-
marketIndex:
|
|
51
|
-
): DataAndSlot<
|
|
52
|
-
getMarketAccountsAndSlots(): DataAndSlot<
|
|
53
|
-
|
|
50
|
+
marketIndex: number
|
|
51
|
+
): DataAndSlot<PerpMarketAccount> | undefined;
|
|
52
|
+
getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
|
|
53
|
+
getSpotMarketAccountAndSlot(
|
|
54
|
+
marketIndex: number
|
|
55
|
+
): DataAndSlot<SpotMarketAccount> | undefined;
|
|
56
|
+
getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
|
|
54
57
|
getOraclePriceDataAndSlot(
|
|
55
58
|
oraclePublicKey: PublicKey
|
|
56
59
|
): DataAndSlot<OraclePriceData> | undefined;
|
|
@@ -96,7 +99,7 @@ export interface OracleEvents {
|
|
|
96
99
|
error: (e: Error) => void;
|
|
97
100
|
}
|
|
98
101
|
|
|
99
|
-
export interface
|
|
102
|
+
export interface OracleAccountSubscriber {
|
|
100
103
|
eventEmitter: StrictEventEmitter<EventEmitter, OracleEvents>;
|
|
101
104
|
isSubscribed: boolean;
|
|
102
105
|
|
|
@@ -130,3 +133,20 @@ export type DataAndSlot<T> = {
|
|
|
130
133
|
data: T;
|
|
131
134
|
slot: number;
|
|
132
135
|
};
|
|
136
|
+
|
|
137
|
+
export interface UserStatsAccountEvents {
|
|
138
|
+
userStatsAccountUpdate: (payload: UserStatsAccount) => void;
|
|
139
|
+
update: void;
|
|
140
|
+
error: (e: Error) => void;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface UserStatsAccountSubscriber {
|
|
144
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
|
|
145
|
+
isSubscribed: boolean;
|
|
146
|
+
|
|
147
|
+
subscribe(): Promise<boolean>;
|
|
148
|
+
fetch(): Promise<void>;
|
|
149
|
+
unsubscribe(): Promise<void>;
|
|
150
|
+
|
|
151
|
+
getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount>;
|
|
152
|
+
}
|