@drift-labs/sdk 2.49.0-beta.9 → 2.52.0-beta.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/VERSION +1 -1
- package/lib/accounts/{mockUserAccountSubscriber.d.ts → basicUserAccountSubscriber.d.ts} +6 -2
- package/lib/accounts/{mockUserAccountSubscriber.js → basicUserAccountSubscriber.js} +13 -6
- package/lib/accounts/bulkAccountLoader.js +7 -1
- package/lib/accounts/oneShotUserAccountSubscriber.d.ts +17 -0
- package/lib/accounts/oneShotUserAccountSubscriber.js +48 -0
- package/lib/accounts/pollingInsuranceFundStakeAccountSubscriber.js +1 -0
- package/lib/adminClient.d.ts +2 -0
- package/lib/adminClient.js +17 -0
- package/lib/constants/numericConstants.d.ts +3 -0
- package/lib/constants/numericConstants.js +4 -1
- package/lib/constants/perpMarkets.js +40 -0
- package/lib/constants/spotMarkets.js +10 -0
- package/lib/decode/user.d.ts +3 -0
- package/lib/decode/user.js +329 -0
- package/lib/driftClient.d.ts +6 -1
- package/lib/driftClient.js +35 -10
- package/lib/examples/loadDlob.js +10 -5
- package/lib/idl/drift.json +32 -2
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -1
- package/lib/math/state.d.ts +5 -0
- package/lib/math/state.js +27 -0
- package/lib/math/superStake.d.ts +43 -0
- package/lib/math/superStake.js +64 -22
- package/lib/orderSubscriber/OrderSubscriber.d.ts +3 -0
- package/lib/orderSubscriber/OrderSubscriber.js +17 -3
- package/lib/orderSubscriber/WebsocketSubscription.d.ts +1 -1
- package/lib/orderSubscriber/WebsocketSubscription.js +8 -6
- package/lib/orderSubscriber/types.d.ts +4 -1
- package/lib/types.d.ts +2 -1
- package/lib/user.d.ts +2 -1
- package/lib/user.js +13 -5
- package/lib/userMap/PollingSubscription.d.ts +15 -0
- package/lib/userMap/PollingSubscription.js +28 -0
- package/lib/userMap/WebsocketSubscription.d.ts +23 -0
- package/lib/userMap/WebsocketSubscription.js +41 -0
- package/lib/userMap/userMap.d.ts +16 -18
- package/lib/userMap/userMap.js +73 -34
- package/lib/userMap/userMapConfig.d.ts +21 -0
- package/lib/userMap/userMapConfig.js +2 -0
- package/lib/userStats.d.ts +1 -0
- package/lib/userStats.js +3 -0
- package/package.json +2 -1
- package/src/accounts/{mockUserAccountSubscriber.ts → basicUserAccountSubscriber.ts} +12 -6
- package/src/accounts/bulkAccountLoader.ts +10 -3
- package/src/accounts/oneShotUserAccountSubscriber.ts +64 -0
- package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +1 -0
- package/src/adminClient.ts +27 -0
- package/src/constants/numericConstants.ts +4 -0
- package/src/constants/perpMarkets.ts +40 -0
- package/src/constants/spotMarkets.ts +10 -0
- package/src/decode/user.ts +358 -0
- package/src/driftClient.ts +62 -15
- package/src/examples/loadDlob.ts +11 -6
- package/src/idl/drift.json +33 -3
- package/src/index.ts +3 -1
- package/src/math/state.ts +26 -0
- package/src/math/superStake.ts +108 -20
- package/src/orderSubscriber/OrderSubscriber.ts +33 -7
- package/src/orderSubscriber/WebsocketSubscription.ts +17 -16
- package/src/orderSubscriber/types.ts +15 -2
- package/src/types.ts +2 -1
- package/src/user.ts +19 -6
- package/src/userMap/PollingSubscription.ts +48 -0
- package/src/userMap/WebsocketSubscription.ts +76 -0
- package/src/userMap/userMap.ts +105 -70
- package/src/userMap/userMapConfig.ts +34 -0
- package/src/userStats.ts +8 -0
- package/tests/amm/test.ts +6 -3
- package/tests/decode/test.ts +266 -0
- package/tests/decode/userAccountBufferStrings.ts +102 -0
- package/tests/dlob/helpers.ts +1 -0
- package/tests/dlob/test.ts +10 -8
- package/tests/user/helpers.ts +0 -1
|
@@ -204,6 +204,26 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
204
204
|
launchTs: 1700542800000,
|
|
205
205
|
oracleSource: OracleSource.PYTH,
|
|
206
206
|
},
|
|
207
|
+
{
|
|
208
|
+
fullName: 'Celestia',
|
|
209
|
+
category: ['Data'],
|
|
210
|
+
symbol: 'TIA-PERP',
|
|
211
|
+
baseAssetSymbol: 'TIA',
|
|
212
|
+
marketIndex: 19,
|
|
213
|
+
oracle: new PublicKey('4GiL1Y6u6JkPb7ckakzJgc414h6P7qoYnEKFcd1YtSB9'),
|
|
214
|
+
launchTs: 1701880540000,
|
|
215
|
+
oracleSource: OracleSource.PYTH,
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
fullName: 'Jito',
|
|
219
|
+
category: ['MEV'],
|
|
220
|
+
symbol: 'JTO-PERP',
|
|
221
|
+
baseAssetSymbol: 'JTO',
|
|
222
|
+
marketIndex: 20,
|
|
223
|
+
oracle: new PublicKey('29xQnTzyyuRtgJ7RtSKEgBWwRzZqtrrKmyQQ5m3x629f'),
|
|
224
|
+
launchTs: 1701967240000,
|
|
225
|
+
oracleSource: OracleSource.PYTH,
|
|
226
|
+
},
|
|
207
227
|
];
|
|
208
228
|
|
|
209
229
|
export const MainnetPerpMarkets: PerpMarketConfig[] = [
|
|
@@ -397,6 +417,26 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
397
417
|
launchTs: 1700542800000,
|
|
398
418
|
oracleSource: OracleSource.PYTH,
|
|
399
419
|
},
|
|
420
|
+
{
|
|
421
|
+
fullName: 'Celestia',
|
|
422
|
+
category: ['Data'],
|
|
423
|
+
symbol: 'TIA-PERP',
|
|
424
|
+
baseAssetSymbol: 'TIA',
|
|
425
|
+
marketIndex: 19,
|
|
426
|
+
oracle: new PublicKey('funeUsHgi2QKkLdUPASRLuYkaK8JaazCEz3HikbkhVt'),
|
|
427
|
+
launchTs: 1701880540000,
|
|
428
|
+
oracleSource: OracleSource.PYTH,
|
|
429
|
+
},
|
|
430
|
+
{
|
|
431
|
+
fullName: 'Jito',
|
|
432
|
+
category: ['MEV'],
|
|
433
|
+
symbol: 'JTO-PERP',
|
|
434
|
+
baseAssetSymbol: 'JTO',
|
|
435
|
+
marketIndex: 20,
|
|
436
|
+
oracle: new PublicKey('D8UUgr8a3aR3yUeHLu7v8FWK7E8Y5sSU7qrYBXUJXBQ5'),
|
|
437
|
+
launchTs: 1701967240000,
|
|
438
|
+
oracleSource: OracleSource.PYTH,
|
|
439
|
+
},
|
|
400
440
|
];
|
|
401
441
|
|
|
402
442
|
export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
|
|
@@ -160,6 +160,16 @@ export const MainnetSpotMarkets: SpotMarketConfig[] = [
|
|
|
160
160
|
precisionExp: NINE,
|
|
161
161
|
serumMarket: new PublicKey('ARjaHVxGCQfTvvKjLd7U7srvk6orthZSE6uqWchCczZc'),
|
|
162
162
|
},
|
|
163
|
+
{
|
|
164
|
+
symbol: 'JTO',
|
|
165
|
+
marketIndex: 9,
|
|
166
|
+
oracle: new PublicKey('D8UUgr8a3aR3yUeHLu7v8FWK7E8Y5sSU7qrYBXUJXBQ5'),
|
|
167
|
+
oracleSource: OracleSource.PYTH,
|
|
168
|
+
mint: new PublicKey('jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL'),
|
|
169
|
+
precision: new BN(10).pow(NINE),
|
|
170
|
+
precisionExp: NINE,
|
|
171
|
+
serumMarket: new PublicKey('H87FfmHABiZLRGrDsXRZtqq25YpARzaokCzL1vMYGiep'),
|
|
172
|
+
},
|
|
163
173
|
];
|
|
164
174
|
|
|
165
175
|
export const SpotMarkets: { [key in DriftEnv]: SpotMarketConfig[] } = {
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MarketType,
|
|
3
|
+
Order,
|
|
4
|
+
OrderStatus,
|
|
5
|
+
OrderTriggerCondition,
|
|
6
|
+
OrderType,
|
|
7
|
+
PerpPosition,
|
|
8
|
+
PositionDirection,
|
|
9
|
+
SpotBalanceType,
|
|
10
|
+
SpotPosition,
|
|
11
|
+
UserAccount,
|
|
12
|
+
} from '../types';
|
|
13
|
+
import { PublicKey } from '@solana/web3.js';
|
|
14
|
+
import { BN } from '../';
|
|
15
|
+
import { ZERO } from '../';
|
|
16
|
+
|
|
17
|
+
function readUnsignedBigInt64LE(buffer: Buffer, offset: number): BN {
|
|
18
|
+
return new BN(buffer.subarray(offset, offset + 8), 10, 'le');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function readSignedBigInt64LE(buffer: Buffer, offset: number): BN {
|
|
22
|
+
const unsignedValue = new BN(buffer.subarray(offset, offset + 8), 10, 'le');
|
|
23
|
+
if (unsignedValue.testn(63)) {
|
|
24
|
+
const inverted = unsignedValue.notn(64).addn(1);
|
|
25
|
+
return inverted.neg();
|
|
26
|
+
} else {
|
|
27
|
+
return unsignedValue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function decodeUser(buffer: Buffer): UserAccount {
|
|
32
|
+
let offset = 8;
|
|
33
|
+
const authority = new PublicKey(buffer.slice(offset, offset + 32));
|
|
34
|
+
offset += 32;
|
|
35
|
+
const delegate = new PublicKey(buffer.slice(offset, offset + 32));
|
|
36
|
+
offset += 32;
|
|
37
|
+
const name = [];
|
|
38
|
+
for (let i = 0; i < 32; i++) {
|
|
39
|
+
name.push(buffer.readUint8(offset + i));
|
|
40
|
+
}
|
|
41
|
+
offset += 32;
|
|
42
|
+
|
|
43
|
+
const spotPositions: SpotPosition[] = [];
|
|
44
|
+
for (let i = 0; i < 8; i++) {
|
|
45
|
+
const scaledBalance = readUnsignedBigInt64LE(buffer, offset);
|
|
46
|
+
const openOrders = buffer.readUInt8(offset + 35);
|
|
47
|
+
if (scaledBalance.eq(ZERO) && openOrders === 0) {
|
|
48
|
+
offset += 40;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
offset += 8;
|
|
53
|
+
const openBids = readSignedBigInt64LE(buffer, offset);
|
|
54
|
+
offset += 8;
|
|
55
|
+
const openAsks = readSignedBigInt64LE(buffer, offset);
|
|
56
|
+
offset += 8;
|
|
57
|
+
const cumulativeDeposits = readSignedBigInt64LE(buffer, offset);
|
|
58
|
+
offset += 8;
|
|
59
|
+
const marketIndex = buffer.readUInt16LE(offset);
|
|
60
|
+
offset += 2;
|
|
61
|
+
const balanceTypeNum = buffer.readUInt8(offset);
|
|
62
|
+
let balanceType: SpotBalanceType;
|
|
63
|
+
if (balanceTypeNum === 0) {
|
|
64
|
+
balanceType = SpotBalanceType.DEPOSIT;
|
|
65
|
+
} else {
|
|
66
|
+
balanceType = SpotBalanceType.BORROW;
|
|
67
|
+
}
|
|
68
|
+
offset += 6;
|
|
69
|
+
spotPositions.push({
|
|
70
|
+
scaledBalance,
|
|
71
|
+
openBids,
|
|
72
|
+
openAsks,
|
|
73
|
+
cumulativeDeposits,
|
|
74
|
+
marketIndex,
|
|
75
|
+
balanceType,
|
|
76
|
+
openOrders,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const perpPositions: PerpPosition[] = [];
|
|
81
|
+
for (let i = 0; i < 8; i++) {
|
|
82
|
+
const baseAssetAmount = readSignedBigInt64LE(buffer, offset + 8);
|
|
83
|
+
const quoteAssetAmount = readSignedBigInt64LE(buffer, offset + 16);
|
|
84
|
+
const lpShares = readUnsignedBigInt64LE(buffer, offset + 64);
|
|
85
|
+
const openOrders = buffer.readUInt8(offset + 94);
|
|
86
|
+
|
|
87
|
+
if (
|
|
88
|
+
baseAssetAmount.eq(ZERO) &&
|
|
89
|
+
openOrders === 0 &&
|
|
90
|
+
quoteAssetAmount.eq(ZERO) &&
|
|
91
|
+
lpShares.eq(ZERO)
|
|
92
|
+
) {
|
|
93
|
+
offset += 96;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const lastCumulativeFundingRate = readSignedBigInt64LE(buffer, offset);
|
|
98
|
+
offset += 24;
|
|
99
|
+
const quoteBreakEvenAmount = readSignedBigInt64LE(buffer, offset);
|
|
100
|
+
offset += 8;
|
|
101
|
+
const quoteEntryAmount = readSignedBigInt64LE(buffer, offset);
|
|
102
|
+
offset += 8;
|
|
103
|
+
const openBids = readSignedBigInt64LE(buffer, offset);
|
|
104
|
+
offset += 8;
|
|
105
|
+
const openAsks = readSignedBigInt64LE(buffer, offset);
|
|
106
|
+
offset += 8;
|
|
107
|
+
const settledPnl = readSignedBigInt64LE(buffer, offset);
|
|
108
|
+
offset += 16;
|
|
109
|
+
const lastBaseAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
|
|
110
|
+
offset += 8;
|
|
111
|
+
const lastQuoteAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
|
|
112
|
+
offset += 8;
|
|
113
|
+
const remainderBaseAssetAmount = buffer.readUint32LE(offset);
|
|
114
|
+
offset += 4;
|
|
115
|
+
const marketIndex = buffer.readUInt16LE(offset);
|
|
116
|
+
offset += 3;
|
|
117
|
+
const perLpBase = buffer.readUInt8(offset);
|
|
118
|
+
offset += 1;
|
|
119
|
+
|
|
120
|
+
perpPositions.push({
|
|
121
|
+
lastCumulativeFundingRate,
|
|
122
|
+
baseAssetAmount,
|
|
123
|
+
quoteAssetAmount,
|
|
124
|
+
quoteBreakEvenAmount,
|
|
125
|
+
quoteEntryAmount,
|
|
126
|
+
openBids,
|
|
127
|
+
openAsks,
|
|
128
|
+
settledPnl,
|
|
129
|
+
lpShares,
|
|
130
|
+
lastBaseAssetAmountPerLp,
|
|
131
|
+
lastQuoteAssetAmountPerLp,
|
|
132
|
+
remainderBaseAssetAmount,
|
|
133
|
+
marketIndex,
|
|
134
|
+
openOrders,
|
|
135
|
+
perLpBase,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const orders: Order[] = [];
|
|
140
|
+
for (let i = 0; i < 32; i++) {
|
|
141
|
+
// skip order if it's not open
|
|
142
|
+
if (buffer.readUint8(offset + 82) === 0) {
|
|
143
|
+
offset += 96;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const slot = readUnsignedBigInt64LE(buffer, offset);
|
|
148
|
+
offset += 8;
|
|
149
|
+
const price = readUnsignedBigInt64LE(buffer, offset);
|
|
150
|
+
offset += 8;
|
|
151
|
+
const baseAssetAmount = readUnsignedBigInt64LE(buffer, offset);
|
|
152
|
+
offset += 8;
|
|
153
|
+
const baseAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
|
|
154
|
+
offset += 8;
|
|
155
|
+
const quoteAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
|
|
156
|
+
offset += 8;
|
|
157
|
+
const triggerPrice = readUnsignedBigInt64LE(buffer, offset);
|
|
158
|
+
offset += 8;
|
|
159
|
+
const auctionStartPrice = readSignedBigInt64LE(buffer, offset);
|
|
160
|
+
offset += 8;
|
|
161
|
+
const auctionEndPrice = readSignedBigInt64LE(buffer, offset);
|
|
162
|
+
offset += 8;
|
|
163
|
+
const maxTs = readSignedBigInt64LE(buffer, offset);
|
|
164
|
+
offset += 8;
|
|
165
|
+
const oraclePriceOffset = buffer.readInt32LE(offset);
|
|
166
|
+
offset += 4;
|
|
167
|
+
const orderId = buffer.readUInt32LE(offset);
|
|
168
|
+
offset += 4;
|
|
169
|
+
const marketIndex = buffer.readUInt16LE(offset);
|
|
170
|
+
offset += 2;
|
|
171
|
+
const orderStatusNum = buffer.readUInt8(offset);
|
|
172
|
+
|
|
173
|
+
let status: OrderStatus;
|
|
174
|
+
if (orderStatusNum === 0) {
|
|
175
|
+
status = OrderStatus.INIT;
|
|
176
|
+
} else if (orderStatusNum === 1) {
|
|
177
|
+
status = OrderStatus.OPEN;
|
|
178
|
+
}
|
|
179
|
+
offset += 1;
|
|
180
|
+
const orderTypeNum = buffer.readUInt8(offset);
|
|
181
|
+
let orderType: OrderType;
|
|
182
|
+
if (orderTypeNum === 0) {
|
|
183
|
+
orderType = OrderType.MARKET;
|
|
184
|
+
} else if (orderTypeNum === 1) {
|
|
185
|
+
orderType = OrderType.LIMIT;
|
|
186
|
+
} else if (orderTypeNum === 2) {
|
|
187
|
+
orderType = OrderType.TRIGGER_MARKET;
|
|
188
|
+
} else if (orderTypeNum === 3) {
|
|
189
|
+
orderType = OrderType.TRIGGER_LIMIT;
|
|
190
|
+
} else if (orderTypeNum === 4) {
|
|
191
|
+
orderType = OrderType.ORACLE;
|
|
192
|
+
}
|
|
193
|
+
offset += 1;
|
|
194
|
+
const marketTypeNum = buffer.readUInt8(offset);
|
|
195
|
+
let marketType: MarketType;
|
|
196
|
+
if (marketTypeNum === 0) {
|
|
197
|
+
marketType = MarketType.SPOT;
|
|
198
|
+
} else {
|
|
199
|
+
marketType = MarketType.PERP;
|
|
200
|
+
}
|
|
201
|
+
offset += 1;
|
|
202
|
+
const userOrderId = buffer.readUint8(offset);
|
|
203
|
+
offset += 1;
|
|
204
|
+
const existingPositionDirectionNum = buffer.readUInt8(offset);
|
|
205
|
+
let existingPositionDirection: PositionDirection;
|
|
206
|
+
if (existingPositionDirectionNum === 0) {
|
|
207
|
+
existingPositionDirection = PositionDirection.LONG;
|
|
208
|
+
} else {
|
|
209
|
+
existingPositionDirection = PositionDirection.SHORT;
|
|
210
|
+
}
|
|
211
|
+
offset += 1;
|
|
212
|
+
const positionDirectionNum = buffer.readUInt8(offset);
|
|
213
|
+
let direction: PositionDirection;
|
|
214
|
+
if (positionDirectionNum === 0) {
|
|
215
|
+
direction = PositionDirection.LONG;
|
|
216
|
+
} else {
|
|
217
|
+
direction = PositionDirection.SHORT;
|
|
218
|
+
}
|
|
219
|
+
offset += 1;
|
|
220
|
+
const reduceOnly = buffer.readUInt8(offset) === 1;
|
|
221
|
+
offset += 1;
|
|
222
|
+
const postOnly = buffer.readUInt8(offset) === 1;
|
|
223
|
+
offset += 1;
|
|
224
|
+
const immediateOrCancel = buffer.readUInt8(offset) === 1;
|
|
225
|
+
offset += 1;
|
|
226
|
+
const triggerConditionNum = buffer.readUInt8(offset);
|
|
227
|
+
let triggerCondition: OrderTriggerCondition;
|
|
228
|
+
if (triggerConditionNum === 0) {
|
|
229
|
+
triggerCondition = OrderTriggerCondition.ABOVE;
|
|
230
|
+
} else if (triggerConditionNum === 1) {
|
|
231
|
+
triggerCondition = OrderTriggerCondition.BELOW;
|
|
232
|
+
} else if (triggerConditionNum === 2) {
|
|
233
|
+
triggerCondition = OrderTriggerCondition.TRIGGERED_ABOVE;
|
|
234
|
+
} else if (triggerConditionNum === 3) {
|
|
235
|
+
triggerCondition = OrderTriggerCondition.TRIGGERED_BELOW;
|
|
236
|
+
}
|
|
237
|
+
offset += 1;
|
|
238
|
+
const auctionDuration = buffer.readUInt8(offset);
|
|
239
|
+
offset += 1;
|
|
240
|
+
offset += 3; // padding
|
|
241
|
+
orders.push({
|
|
242
|
+
slot,
|
|
243
|
+
price,
|
|
244
|
+
baseAssetAmount,
|
|
245
|
+
quoteAssetAmount: undefined,
|
|
246
|
+
baseAssetAmountFilled,
|
|
247
|
+
quoteAssetAmountFilled,
|
|
248
|
+
triggerPrice,
|
|
249
|
+
auctionStartPrice,
|
|
250
|
+
auctionEndPrice,
|
|
251
|
+
maxTs,
|
|
252
|
+
oraclePriceOffset,
|
|
253
|
+
orderId,
|
|
254
|
+
marketIndex,
|
|
255
|
+
status,
|
|
256
|
+
orderType,
|
|
257
|
+
marketType,
|
|
258
|
+
userOrderId,
|
|
259
|
+
existingPositionDirection,
|
|
260
|
+
direction,
|
|
261
|
+
reduceOnly,
|
|
262
|
+
postOnly,
|
|
263
|
+
immediateOrCancel,
|
|
264
|
+
triggerCondition,
|
|
265
|
+
auctionDuration,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const lastAddPerpLpSharesTs = readSignedBigInt64LE(buffer, offset);
|
|
270
|
+
offset += 8;
|
|
271
|
+
|
|
272
|
+
const totalDeposits = readUnsignedBigInt64LE(buffer, offset);
|
|
273
|
+
offset += 8;
|
|
274
|
+
|
|
275
|
+
const totalWithdraws = readUnsignedBigInt64LE(buffer, offset);
|
|
276
|
+
offset += 8;
|
|
277
|
+
|
|
278
|
+
const totalSocialLoss = readUnsignedBigInt64LE(buffer, offset);
|
|
279
|
+
offset += 8;
|
|
280
|
+
|
|
281
|
+
const settledPerpPnl = readSignedBigInt64LE(buffer, offset);
|
|
282
|
+
offset += 8;
|
|
283
|
+
|
|
284
|
+
const cumulativeSpotFees = readSignedBigInt64LE(buffer, offset);
|
|
285
|
+
offset += 8;
|
|
286
|
+
|
|
287
|
+
const cumulativePerpFunding = readSignedBigInt64LE(buffer, offset);
|
|
288
|
+
offset += 8;
|
|
289
|
+
|
|
290
|
+
const liquidationMarginFreed = readUnsignedBigInt64LE(buffer, offset);
|
|
291
|
+
offset += 8;
|
|
292
|
+
|
|
293
|
+
const lastActiveSlot = readUnsignedBigInt64LE(buffer, offset);
|
|
294
|
+
offset += 8;
|
|
295
|
+
|
|
296
|
+
const nextOrderId = buffer.readUInt32LE(offset);
|
|
297
|
+
offset += 4;
|
|
298
|
+
|
|
299
|
+
const maxMarginRatio = buffer.readUInt32LE(offset);
|
|
300
|
+
offset += 4;
|
|
301
|
+
|
|
302
|
+
const nextLiquidationId = buffer.readUInt16LE(offset);
|
|
303
|
+
offset += 2;
|
|
304
|
+
|
|
305
|
+
const subAccountId = buffer.readUInt16LE(offset);
|
|
306
|
+
offset += 2;
|
|
307
|
+
|
|
308
|
+
const status = buffer.readUInt8(offset);
|
|
309
|
+
offset += 1;
|
|
310
|
+
|
|
311
|
+
const isMarginTradingEnabled = buffer.readUInt8(offset) === 1;
|
|
312
|
+
offset += 1;
|
|
313
|
+
|
|
314
|
+
const idle = buffer.readUInt8(offset) === 1;
|
|
315
|
+
offset += 1;
|
|
316
|
+
|
|
317
|
+
const openOrders = buffer.readUInt8(offset);
|
|
318
|
+
offset += 1;
|
|
319
|
+
|
|
320
|
+
const hasOpenOrder = buffer.readUInt8(offset) === 1;
|
|
321
|
+
offset += 1;
|
|
322
|
+
|
|
323
|
+
const openAuctions = buffer.readUInt8(offset);
|
|
324
|
+
offset += 1;
|
|
325
|
+
|
|
326
|
+
const hasOpenAuction = buffer.readUInt8(offset) === 1;
|
|
327
|
+
offset += 1;
|
|
328
|
+
|
|
329
|
+
// @ts-ignore
|
|
330
|
+
return {
|
|
331
|
+
authority,
|
|
332
|
+
delegate,
|
|
333
|
+
name,
|
|
334
|
+
spotPositions,
|
|
335
|
+
perpPositions,
|
|
336
|
+
orders,
|
|
337
|
+
lastAddPerpLpSharesTs,
|
|
338
|
+
totalDeposits,
|
|
339
|
+
totalWithdraws,
|
|
340
|
+
totalSocialLoss,
|
|
341
|
+
settledPerpPnl,
|
|
342
|
+
cumulativeSpotFees,
|
|
343
|
+
cumulativePerpFunding,
|
|
344
|
+
liquidationMarginFreed,
|
|
345
|
+
lastActiveSlot,
|
|
346
|
+
nextOrderId,
|
|
347
|
+
maxMarginRatio,
|
|
348
|
+
nextLiquidationId,
|
|
349
|
+
subAccountId,
|
|
350
|
+
status,
|
|
351
|
+
isMarginTradingEnabled,
|
|
352
|
+
idle,
|
|
353
|
+
openOrders,
|
|
354
|
+
hasOpenOrder,
|
|
355
|
+
openAuctions,
|
|
356
|
+
hasOpenAuction,
|
|
357
|
+
};
|
|
358
|
+
}
|
package/src/driftClient.ts
CHANGED
|
@@ -1112,6 +1112,19 @@ export class DriftClient {
|
|
|
1112
1112
|
);
|
|
1113
1113
|
}
|
|
1114
1114
|
|
|
1115
|
+
public async getUserDeletionIx(userAccountPublicKey: PublicKey) {
|
|
1116
|
+
const ix = await this.program.instruction.deleteUser({
|
|
1117
|
+
accounts: {
|
|
1118
|
+
user: userAccountPublicKey,
|
|
1119
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
1120
|
+
authority: this.wallet.publicKey,
|
|
1121
|
+
state: await this.getStatePublicKey(),
|
|
1122
|
+
},
|
|
1123
|
+
});
|
|
1124
|
+
|
|
1125
|
+
return ix;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1115
1128
|
public async deleteUser(
|
|
1116
1129
|
subAccountId = 0,
|
|
1117
1130
|
txParams?: TxParams
|
|
@@ -1122,14 +1135,7 @@ export class DriftClient {
|
|
|
1122
1135
|
subAccountId
|
|
1123
1136
|
);
|
|
1124
1137
|
|
|
1125
|
-
const ix = await this.
|
|
1126
|
-
accounts: {
|
|
1127
|
-
user: userAccountPublicKey,
|
|
1128
|
-
userStats: this.getUserStatsAccountPublicKey(),
|
|
1129
|
-
authority: this.wallet.publicKey,
|
|
1130
|
-
state: await this.getStatePublicKey(),
|
|
1131
|
-
},
|
|
1132
|
-
});
|
|
1138
|
+
const ix = await this.getUserDeletionIx(userAccountPublicKey);
|
|
1133
1139
|
|
|
1134
1140
|
const { txSig } = await this.sendTransaction(
|
|
1135
1141
|
await this.buildTransaction(ix, txParams),
|
|
@@ -1811,6 +1817,9 @@ export class DriftClient {
|
|
|
1811
1817
|
* @param subAccountId
|
|
1812
1818
|
* @param name
|
|
1813
1819
|
* @param fromSubAccountId
|
|
1820
|
+
* @param referrerInfo
|
|
1821
|
+
* @param donateAmount
|
|
1822
|
+
* @param txParams
|
|
1814
1823
|
* @returns
|
|
1815
1824
|
*/
|
|
1816
1825
|
public async initializeUserAccountAndDepositCollateral(
|
|
@@ -1821,6 +1830,7 @@ export class DriftClient {
|
|
|
1821
1830
|
name?: string,
|
|
1822
1831
|
fromSubAccountId?: number,
|
|
1823
1832
|
referrerInfo?: ReferrerInfo,
|
|
1833
|
+
donateAmount?: BN,
|
|
1824
1834
|
txParams?: TxParams
|
|
1825
1835
|
): Promise<[TransactionSignature, PublicKey]> {
|
|
1826
1836
|
const ixs = [];
|
|
@@ -1856,14 +1866,26 @@ export class DriftClient {
|
|
|
1856
1866
|
fromSubAccountId !== undefined &&
|
|
1857
1867
|
!isNaN(fromSubAccountId);
|
|
1858
1868
|
|
|
1869
|
+
donateAmount = donateAmount ? donateAmount : ZERO;
|
|
1870
|
+
|
|
1859
1871
|
const createWSOLTokenAccount =
|
|
1860
|
-
isSolMarket &&
|
|
1872
|
+
(isSolMarket &&
|
|
1873
|
+
userTokenAccount.equals(authority) &&
|
|
1874
|
+
!isFromSubaccount) ||
|
|
1875
|
+
!donateAmount.eq(ZERO);
|
|
1876
|
+
|
|
1877
|
+
const wSolAmount = isSolMarket ? amount.add(donateAmount) : donateAmount;
|
|
1861
1878
|
|
|
1879
|
+
let wsolTokenAccount: PublicKey;
|
|
1862
1880
|
if (createWSOLTokenAccount) {
|
|
1863
1881
|
const { ixs: startIxs, pubkey } =
|
|
1864
|
-
await this.getWrappedSolAccountCreationIxs(
|
|
1882
|
+
await this.getWrappedSolAccountCreationIxs(wSolAmount, true);
|
|
1865
1883
|
|
|
1866
|
-
|
|
1884
|
+
wsolTokenAccount = pubkey;
|
|
1885
|
+
|
|
1886
|
+
if (isSolMarket) {
|
|
1887
|
+
userTokenAccount = pubkey;
|
|
1888
|
+
}
|
|
1867
1889
|
|
|
1868
1890
|
ixs.push(...startIxs);
|
|
1869
1891
|
}
|
|
@@ -1893,11 +1915,21 @@ export class DriftClient {
|
|
|
1893
1915
|
}
|
|
1894
1916
|
ixs.push(initializeUserAccountIx, depositCollateralIx);
|
|
1895
1917
|
|
|
1918
|
+
if (!donateAmount.eq(ZERO)) {
|
|
1919
|
+
const donateIx = await this.getDepositIntoSpotMarketRevenuePoolIx(
|
|
1920
|
+
1,
|
|
1921
|
+
donateAmount,
|
|
1922
|
+
wsolTokenAccount
|
|
1923
|
+
);
|
|
1924
|
+
|
|
1925
|
+
ixs.push(donateIx);
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1896
1928
|
// Close the wrapped sol account at the end of the transaction
|
|
1897
1929
|
if (createWSOLTokenAccount) {
|
|
1898
1930
|
ixs.push(
|
|
1899
1931
|
createCloseAccountInstruction(
|
|
1900
|
-
|
|
1932
|
+
wsolTokenAccount,
|
|
1901
1933
|
authority,
|
|
1902
1934
|
authority,
|
|
1903
1935
|
[]
|
|
@@ -5995,13 +6027,13 @@ export class DriftClient {
|
|
|
5995
6027
|
);
|
|
5996
6028
|
}
|
|
5997
6029
|
|
|
5998
|
-
public async
|
|
6030
|
+
public async getDepositIntoSpotMarketRevenuePoolIx(
|
|
5999
6031
|
marketIndex: number,
|
|
6000
6032
|
amount: BN,
|
|
6001
6033
|
userTokenAccountPublicKey: PublicKey
|
|
6002
|
-
): Promise<
|
|
6034
|
+
): Promise<TransactionInstruction> {
|
|
6003
6035
|
const spotMarket = await this.getSpotMarketAccount(marketIndex);
|
|
6004
|
-
const
|
|
6036
|
+
const ix = await this.program.instruction.depositIntoSpotMarketRevenuePool(
|
|
6005
6037
|
amount,
|
|
6006
6038
|
{
|
|
6007
6039
|
accounts: {
|
|
@@ -6015,6 +6047,21 @@ export class DriftClient {
|
|
|
6015
6047
|
}
|
|
6016
6048
|
);
|
|
6017
6049
|
|
|
6050
|
+
return ix;
|
|
6051
|
+
}
|
|
6052
|
+
|
|
6053
|
+
public async depositIntoSpotMarketRevenuePool(
|
|
6054
|
+
marketIndex: number,
|
|
6055
|
+
amount: BN,
|
|
6056
|
+
userTokenAccountPublicKey: PublicKey
|
|
6057
|
+
): Promise<TransactionSignature> {
|
|
6058
|
+
const ix = await this.getDepositIntoSpotMarketRevenuePoolIx(
|
|
6059
|
+
marketIndex,
|
|
6060
|
+
amount,
|
|
6061
|
+
userTokenAccountPublicKey
|
|
6062
|
+
);
|
|
6063
|
+
const tx = await this.buildTransaction([ix]);
|
|
6064
|
+
|
|
6018
6065
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
6019
6066
|
return txSig;
|
|
6020
6067
|
}
|
package/src/examples/loadDlob.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AnchorProvider } from '@coral-xyz/anchor';
|
|
2
|
-
import {
|
|
2
|
+
import { UserMap, Wallet } from '..';
|
|
3
3
|
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
|
4
4
|
import {
|
|
5
5
|
DriftClient,
|
|
@@ -55,17 +55,22 @@ const main = async () => {
|
|
|
55
55
|
await driftClient.subscribe();
|
|
56
56
|
|
|
57
57
|
console.log('Loading user map...');
|
|
58
|
-
const userMap = new UserMap(
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
const userMap = new UserMap({
|
|
59
|
+
driftClient,
|
|
60
|
+
subscriptionConfig: {
|
|
61
|
+
type: 'websocket',
|
|
62
|
+
commitment: 'processed',
|
|
63
|
+
},
|
|
64
|
+
skipInitialLoad: false,
|
|
65
|
+
includeIdle: false,
|
|
61
66
|
});
|
|
62
67
|
|
|
63
68
|
// fetches all users and subscribes for updates
|
|
64
69
|
await userMap.subscribe();
|
|
65
70
|
|
|
66
71
|
console.log('Loading dlob from user map...');
|
|
67
|
-
const
|
|
68
|
-
await
|
|
72
|
+
const slot = await driftClient.connection.getSlot();
|
|
73
|
+
const dlob = await userMap.getDLOB(slot);
|
|
69
74
|
|
|
70
75
|
console.log('number of orders', dlob.getDLOBOrders().length);
|
|
71
76
|
|
package/src/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.
|
|
2
|
+
"version": "2.51.0",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -4184,6 +4184,27 @@
|
|
|
4184
4184
|
}
|
|
4185
4185
|
]
|
|
4186
4186
|
},
|
|
4187
|
+
{
|
|
4188
|
+
"name": "updateStateMaxInitializeUserFee",
|
|
4189
|
+
"accounts": [
|
|
4190
|
+
{
|
|
4191
|
+
"name": "admin",
|
|
4192
|
+
"isMut": false,
|
|
4193
|
+
"isSigner": true
|
|
4194
|
+
},
|
|
4195
|
+
{
|
|
4196
|
+
"name": "state",
|
|
4197
|
+
"isMut": true,
|
|
4198
|
+
"isSigner": false
|
|
4199
|
+
}
|
|
4200
|
+
],
|
|
4201
|
+
"args": [
|
|
4202
|
+
{
|
|
4203
|
+
"name": "maxInitializeUserFee",
|
|
4204
|
+
"type": "u16"
|
|
4205
|
+
}
|
|
4206
|
+
]
|
|
4207
|
+
},
|
|
4187
4208
|
{
|
|
4188
4209
|
"name": "updatePerpMarketOracle",
|
|
4189
4210
|
"accounts": [
|
|
@@ -5747,12 +5768,16 @@
|
|
|
5747
5768
|
"name": "maxNumberOfSubAccounts",
|
|
5748
5769
|
"type": "u16"
|
|
5749
5770
|
},
|
|
5771
|
+
{
|
|
5772
|
+
"name": "maxInitializeUserFee",
|
|
5773
|
+
"type": "u16"
|
|
5774
|
+
},
|
|
5750
5775
|
{
|
|
5751
5776
|
"name": "padding",
|
|
5752
5777
|
"type": {
|
|
5753
5778
|
"array": [
|
|
5754
5779
|
"u8",
|
|
5755
|
-
|
|
5780
|
+
10
|
|
5756
5781
|
]
|
|
5757
5782
|
}
|
|
5758
5783
|
}
|
|
@@ -11094,6 +11119,11 @@
|
|
|
11094
11119
|
"code": 6255,
|
|
11095
11120
|
"name": "InvalidMarginCalculation",
|
|
11096
11121
|
"msg": "InvalidMarginCalculation"
|
|
11122
|
+
},
|
|
11123
|
+
{
|
|
11124
|
+
"code": 6256,
|
|
11125
|
+
"name": "CantPayUserInitFee",
|
|
11126
|
+
"msg": "CantPayUserInitFee"
|
|
11097
11127
|
}
|
|
11098
11128
|
]
|
|
11099
|
-
}
|
|
11129
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -20,7 +20,8 @@ export * from './accounts/pollingTokenAccountSubscriber';
|
|
|
20
20
|
export * from './accounts/pollingUserAccountSubscriber';
|
|
21
21
|
export * from './accounts/pollingUserStatsAccountSubscriber';
|
|
22
22
|
export * from './accounts/pollingInsuranceFundStakeAccountSubscriber';
|
|
23
|
-
export * from './accounts/
|
|
23
|
+
export * from './accounts/basicUserAccountSubscriber';
|
|
24
|
+
export * from './accounts/oneShotUserAccountSubscriber';
|
|
24
25
|
export * from './accounts/types';
|
|
25
26
|
export * from './addresses/pda';
|
|
26
27
|
export * from './adminClient';
|
|
@@ -56,6 +57,7 @@ export * from './math/margin';
|
|
|
56
57
|
export * from './math/insurance';
|
|
57
58
|
export * from './math/superStake';
|
|
58
59
|
export * from './math/spotPosition';
|
|
60
|
+
export * from './math/state';
|
|
59
61
|
export * from './marinade';
|
|
60
62
|
export * from './orderParams';
|
|
61
63
|
export * from './slot/SlotSubscriber';
|