@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.31
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 +13 -13
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- 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 +16 -16
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +31 -28
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +22 -11
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +17 -17
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +37 -35
- 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/pda.d.ts +10 -4
- package/lib/addresses/pda.js +51 -14
- package/lib/admin.d.ts +42 -26
- package/lib/admin.js +228 -81
- package/lib/clearingHouse.d.ts +130 -49
- package/lib/clearingHouse.js +1256 -367
- package/lib/clearingHouseConfig.d.ts +4 -4
- package/lib/clearingHouseUser.d.ts +51 -39
- package/lib/clearingHouseUser.js +408 -191
- package/lib/clearingHouseUserStats.d.ts +18 -0
- package/lib/clearingHouseUserStats.js +49 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.d.ts +7 -9
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +25 -12
- package/lib/constants/numericConstants.js +35 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +7 -7
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +53 -0
- package/lib/dlob/DLOB.d.ts +74 -0
- package/lib/dlob/DLOB.js +595 -0
- package/lib/dlob/DLOBNode.d.ts +54 -0
- package/lib/dlob/DLOBNode.js +85 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +139 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +8 -2
- package/lib/events/types.js +6 -0
- package/lib/examples/makeTradeExample.js +20 -8
- package/lib/factory/bigNum.d.ts +2 -0
- package/lib/factory/bigNum.js +48 -10
- package/lib/idl/clearing_house.json +4889 -1529
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +21 -6
- package/lib/index.js +25 -6
- package/lib/math/amm.d.ts +9 -6
- package/lib/math/amm.js +91 -66
- package/lib/math/auction.js +4 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +11 -10
- 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 +77 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +72 -10
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +6 -2
- package/lib/math/orders.js +78 -3
- package/lib/math/position.d.ts +21 -13
- package/lib/math/position.js +76 -36
- package/lib/math/repeg.js +14 -5
- 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 +32 -39
- 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 +14 -5
- package/lib/orderParams.js +12 -92
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- 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/tx/utils.js +1 -1
- package/lib/types.d.ts +566 -138
- package/lib/types.js +129 -5
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +12 -6
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +46 -42
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +31 -11
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +64 -59
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +1 -2
- package/src/addresses/pda.ts +88 -14
- package/src/admin.ts +452 -147
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +2066 -467
- package/src/clearingHouseConfig.ts +4 -3
- package/src/clearingHouseUser.ts +747 -294
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +30 -31
- package/src/constants/numericConstants.ts +48 -25
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +73 -0
- package/src/dlob/DLOB.ts +953 -0
- package/src/dlob/DLOBNode.ts +167 -0
- package/src/dlob/NodeList.ts +189 -0
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +19 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/examples/makeTradeExample.ts +32 -14
- package/src/factory/bigNum.ts +65 -13
- package/src/idl/clearing_house.json +4889 -1529
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +21 -6
- package/src/math/amm.ts +171 -88
- package/src/math/auction.ts +5 -1
- package/src/math/conversion.ts +2 -2
- package/src/math/funding.ts +20 -18
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +127 -0
- package/src/math/market.ts +145 -14
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +138 -4
- package/src/math/position.ts +119 -58
- package/src/math/repeg.ts +16 -6
- 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 +53 -59
- 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 +24 -137
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +533 -140
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/src/util/computeUnits.js +21 -11
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/tests/bn/test.ts +24 -10
- package/tests/dlob/helpers.ts +397 -0
- package/tests/dlob/test.ts +3688 -0
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -34
- package/lib/constants/markets.d.ts +0 -19
- 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/constants/banks.ts +0 -43
- package/src/math/bankBalance.ts +0 -112
- package/src/math/state.ts +0 -14
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/orders.ts +0 -244
- package/src/util/computeUnits.js.map +0 -1
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataAndSlot,
|
|
3
|
+
AccountSubscriber,
|
|
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 { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
13
|
+
import { UserStatsAccount } from '../types';
|
|
14
|
+
|
|
15
|
+
export class WebSocketUserStatsAccountSubscriber
|
|
16
|
+
implements UserStatsAccountSubscriber
|
|
17
|
+
{
|
|
18
|
+
isSubscribed: boolean;
|
|
19
|
+
program: Program;
|
|
20
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
|
|
21
|
+
userStatsAccountPublicKey: PublicKey;
|
|
22
|
+
|
|
23
|
+
userStatsAccountSubscriber: AccountSubscriber<UserStatsAccount>;
|
|
24
|
+
|
|
25
|
+
public constructor(program: Program, userStatsAccountPublicKey: PublicKey) {
|
|
26
|
+
this.isSubscribed = false;
|
|
27
|
+
this.program = program;
|
|
28
|
+
this.userStatsAccountPublicKey = userStatsAccountPublicKey;
|
|
29
|
+
this.eventEmitter = new EventEmitter();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async subscribe(): Promise<boolean> {
|
|
33
|
+
if (this.isSubscribed) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.userStatsAccountSubscriber = new WebSocketAccountSubscriber(
|
|
38
|
+
'userStats',
|
|
39
|
+
this.program,
|
|
40
|
+
this.userStatsAccountPublicKey
|
|
41
|
+
);
|
|
42
|
+
await this.userStatsAccountSubscriber.subscribe(
|
|
43
|
+
(data: UserStatsAccount) => {
|
|
44
|
+
this.eventEmitter.emit('userStatsAccountUpdate', data);
|
|
45
|
+
this.eventEmitter.emit('update');
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
this.eventEmitter.emit('update');
|
|
50
|
+
this.isSubscribed = true;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async fetch(): Promise<void> {
|
|
55
|
+
await Promise.all([this.userStatsAccountSubscriber.fetch()]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async unsubscribe(): Promise<void> {
|
|
59
|
+
if (!this.isSubscribed) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
await Promise.all([this.userStatsAccountSubscriber.unsubscribe()]);
|
|
64
|
+
|
|
65
|
+
this.isSubscribed = false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
assertIsSubscribed(): void {
|
|
69
|
+
if (!this.isSubscribed) {
|
|
70
|
+
throw new NotSubscribedError(
|
|
71
|
+
'You must call `subscribe` before using this function'
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount> {
|
|
77
|
+
this.assertIsSubscribed();
|
|
78
|
+
return this.userStatsAccountSubscriber.dataAndSlot;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { PublicKey } from '@solana/web3.js';
|
|
2
|
-
import { BN } from '@project-serum/anchor';
|
|
3
2
|
import { getMarketPublicKey } from './pda';
|
|
4
3
|
|
|
5
4
|
const CACHE = new Map<string, PublicKey>();
|
|
6
5
|
export async function getMarketAddress(
|
|
7
6
|
programId: PublicKey,
|
|
8
|
-
marketIndex:
|
|
7
|
+
marketIndex: number
|
|
9
8
|
): Promise<PublicKey> {
|
|
10
9
|
const cacheKey = `${programId.toString()}-${marketIndex.toString()}`;
|
|
11
10
|
if (CACHE.has(cacheKey)) {
|
package/src/addresses/pda.ts
CHANGED
|
@@ -57,62 +57,136 @@ export function getUserAccountPublicKeySync(
|
|
|
57
57
|
)[0];
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
export function getUserStatsAccountPublicKey(
|
|
61
|
+
programId: PublicKey,
|
|
62
|
+
authority: PublicKey
|
|
63
|
+
): PublicKey {
|
|
64
|
+
return anchor.web3.PublicKey.findProgramAddressSync(
|
|
65
|
+
[
|
|
66
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('user_stats')),
|
|
67
|
+
authority.toBuffer(),
|
|
68
|
+
],
|
|
69
|
+
programId
|
|
70
|
+
)[0];
|
|
71
|
+
}
|
|
72
|
+
|
|
60
73
|
export async function getMarketPublicKey(
|
|
61
74
|
programId: PublicKey,
|
|
62
|
-
marketIndex:
|
|
75
|
+
marketIndex: number
|
|
63
76
|
): Promise<PublicKey> {
|
|
64
77
|
return (
|
|
65
78
|
await anchor.web3.PublicKey.findProgramAddress(
|
|
66
79
|
[
|
|
67
80
|
Buffer.from(anchor.utils.bytes.utf8.encode('market')),
|
|
68
|
-
marketIndex.toArrayLike(Buffer, 'le',
|
|
81
|
+
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
69
82
|
],
|
|
70
83
|
programId
|
|
71
84
|
)
|
|
72
85
|
)[0];
|
|
73
86
|
}
|
|
74
87
|
|
|
75
|
-
export async function
|
|
88
|
+
export async function getSpotMarketPublicKey(
|
|
76
89
|
programId: PublicKey,
|
|
77
|
-
|
|
90
|
+
marketIndex: number
|
|
78
91
|
): Promise<PublicKey> {
|
|
79
92
|
return (
|
|
80
93
|
await anchor.web3.PublicKey.findProgramAddress(
|
|
81
94
|
[
|
|
82
|
-
Buffer.from(anchor.utils.bytes.utf8.encode('
|
|
83
|
-
|
|
95
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('spot_market')),
|
|
96
|
+
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
84
97
|
],
|
|
85
98
|
programId
|
|
86
99
|
)
|
|
87
100
|
)[0];
|
|
88
101
|
}
|
|
89
102
|
|
|
90
|
-
export async function
|
|
103
|
+
export async function getSpotMarketVaultPublicKey(
|
|
91
104
|
programId: PublicKey,
|
|
92
|
-
|
|
105
|
+
marketIndex: number
|
|
93
106
|
): Promise<PublicKey> {
|
|
94
107
|
return (
|
|
95
108
|
await anchor.web3.PublicKey.findProgramAddress(
|
|
96
109
|
[
|
|
97
|
-
Buffer.from(anchor.utils.bytes.utf8.encode('
|
|
98
|
-
|
|
110
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('spot_market_vault')),
|
|
111
|
+
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
99
112
|
],
|
|
100
113
|
programId
|
|
101
114
|
)
|
|
102
115
|
)[0];
|
|
103
116
|
}
|
|
104
117
|
|
|
105
|
-
export async function
|
|
118
|
+
export async function getInsuranceFundVaultPublicKey(
|
|
106
119
|
programId: PublicKey,
|
|
107
|
-
|
|
120
|
+
marketIndex: number
|
|
108
121
|
): Promise<PublicKey> {
|
|
109
122
|
return (
|
|
110
123
|
await anchor.web3.PublicKey.findProgramAddress(
|
|
111
124
|
[
|
|
112
|
-
Buffer.from(anchor.utils.bytes.utf8.encode('
|
|
113
|
-
|
|
125
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_vault')),
|
|
126
|
+
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
114
127
|
],
|
|
115
128
|
programId
|
|
116
129
|
)
|
|
117
130
|
)[0];
|
|
118
131
|
}
|
|
132
|
+
|
|
133
|
+
export function getInsuranceFundStakeAccountPublicKey(
|
|
134
|
+
programId: PublicKey,
|
|
135
|
+
authority: PublicKey,
|
|
136
|
+
marketIndex: number
|
|
137
|
+
): PublicKey {
|
|
138
|
+
return anchor.web3.PublicKey.findProgramAddressSync(
|
|
139
|
+
[
|
|
140
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_stake')),
|
|
141
|
+
authority.toBuffer(),
|
|
142
|
+
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
143
|
+
],
|
|
144
|
+
programId
|
|
145
|
+
)[0];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function getClearingHouseSignerPublicKey(
|
|
149
|
+
programId: PublicKey
|
|
150
|
+
): PublicKey {
|
|
151
|
+
return anchor.web3.PublicKey.findProgramAddressSync(
|
|
152
|
+
[Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house_signer'))],
|
|
153
|
+
programId
|
|
154
|
+
)[0];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function getSerumOpenOrdersPublicKey(
|
|
158
|
+
programId: PublicKey,
|
|
159
|
+
market: PublicKey
|
|
160
|
+
): PublicKey {
|
|
161
|
+
return anchor.web3.PublicKey.findProgramAddressSync(
|
|
162
|
+
[
|
|
163
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('serum_open_orders')),
|
|
164
|
+
market.toBuffer(),
|
|
165
|
+
],
|
|
166
|
+
programId
|
|
167
|
+
)[0];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function getSerumSignerPublicKey(
|
|
171
|
+
programId: PublicKey,
|
|
172
|
+
market: PublicKey,
|
|
173
|
+
nonce: BN
|
|
174
|
+
): PublicKey {
|
|
175
|
+
return anchor.web3.PublicKey.createProgramAddressSync(
|
|
176
|
+
[market.toBuffer(), nonce.toArrayLike(Buffer, 'le', 8)],
|
|
177
|
+
programId
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function getSerumFulfillmentConfigPublicKey(
|
|
182
|
+
programId: PublicKey,
|
|
183
|
+
market: PublicKey
|
|
184
|
+
): PublicKey {
|
|
185
|
+
return anchor.web3.PublicKey.findProgramAddressSync(
|
|
186
|
+
[
|
|
187
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('serum_fulfillment_config')),
|
|
188
|
+
market.toBuffer(),
|
|
189
|
+
],
|
|
190
|
+
programId
|
|
191
|
+
)[0];
|
|
192
|
+
}
|