@drift-labs/sdk 2.49.0-beta.9 → 2.52.0-beta.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/VERSION +1 -1
- package/lib/accounts/{mockUserAccountSubscriber.d.ts → basicUserAccountSubscriber.d.ts} +2 -2
- package/lib/accounts/{mockUserAccountSubscriber.js → basicUserAccountSubscriber.js} +9 -6
- package/lib/adminClient.d.ts +1 -0
- package/lib/adminClient.js +8 -0
- 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 +5 -1
- package/lib/driftClient.js +28 -7
- package/lib/examples/loadDlob.js +10 -5
- package/lib/idl/drift.json +32 -2
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -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/package.json +2 -1
- package/src/accounts/{mockUserAccountSubscriber.ts → basicUserAccountSubscriber.ts} +8 -6
- package/src/adminClient.ts +14 -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 +48 -7
- package/src/examples/loadDlob.ts +11 -6
- package/src/idl/drift.json +33 -3
- package/src/index.ts +2 -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 +103 -70
- package/src/userMap/userMapConfig.ts +34 -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
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.
|
|
1
|
+
2.52.0-beta.0
|
|
@@ -4,14 +4,14 @@ import { PublicKey } from '@solana/web3.js';
|
|
|
4
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
5
|
import { EventEmitter } from 'events';
|
|
6
6
|
import { UserAccount } from '../types';
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class BasicUserAccountSubscriber implements UserAccountSubscriber {
|
|
8
8
|
isSubscribed: boolean;
|
|
9
9
|
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
10
10
|
userAccountPublicKey: PublicKey;
|
|
11
11
|
callbackId?: string;
|
|
12
12
|
errorCallbackId?: string;
|
|
13
13
|
user: DataAndSlot<UserAccount>;
|
|
14
|
-
constructor(userAccountPublicKey: PublicKey, data
|
|
14
|
+
constructor(userAccountPublicKey: PublicKey, data?: UserAccount, slot?: number);
|
|
15
15
|
subscribe(_userAccount?: UserAccount): Promise<boolean>;
|
|
16
16
|
addToAccountLoader(): Promise<void>;
|
|
17
17
|
fetch(): Promise<void>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.BasicUserAccountSubscriber = void 0;
|
|
4
4
|
const events_1 = require("events");
|
|
5
|
-
class
|
|
5
|
+
class BasicUserAccountSubscriber {
|
|
6
6
|
constructor(userAccountPublicKey, data, slot) {
|
|
7
7
|
this.isSubscribed = true;
|
|
8
8
|
this.eventEmitter = new events_1.EventEmitter();
|
|
@@ -23,9 +23,12 @@ class MockUserAccountSubscriber {
|
|
|
23
23
|
return this.user;
|
|
24
24
|
}
|
|
25
25
|
updateData(userAccount, slot) {
|
|
26
|
-
|
|
27
|
-
this.
|
|
28
|
-
|
|
26
|
+
var _a;
|
|
27
|
+
if (!this.user || slot >= ((_a = this.user.slot) !== null && _a !== void 0 ? _a : 0)) {
|
|
28
|
+
this.user = { data: userAccount, slot };
|
|
29
|
+
this.eventEmitter.emit('userAccountUpdate', userAccount);
|
|
30
|
+
this.eventEmitter.emit('update');
|
|
31
|
+
}
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
|
-
exports.
|
|
34
|
+
exports.BasicUserAccountSubscriber = BasicUserAccountSubscriber;
|
package/lib/adminClient.d.ts
CHANGED
|
@@ -37,6 +37,7 @@ export declare class AdminClient extends DriftClient {
|
|
|
37
37
|
updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
|
|
38
38
|
updateStateSettlementDuration(settlementDuration: number): Promise<TransactionSignature>;
|
|
39
39
|
updateStateMaxNumberOfSubAccounts(maxNumberOfSubAccounts: number): Promise<TransactionSignature>;
|
|
40
|
+
updateStateMaxInitializeUserFee(maxInitializeUserFee: number): Promise<TransactionSignature>;
|
|
40
41
|
updateWithdrawGuardThreshold(spotMarketIndex: number, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
|
|
41
42
|
updateSpotMarketIfFactor(spotMarketIndex: number, userIfFactor: BN, totalIfFactor: BN): Promise<TransactionSignature>;
|
|
42
43
|
updateSpotMarketRevenueSettlePeriod(spotMarketIndex: number, revenueSettlePeriod: BN): Promise<TransactionSignature>;
|
package/lib/adminClient.js
CHANGED
|
@@ -455,6 +455,14 @@ class AdminClient extends driftClient_1.DriftClient {
|
|
|
455
455
|
},
|
|
456
456
|
});
|
|
457
457
|
}
|
|
458
|
+
async updateStateMaxInitializeUserFee(maxInitializeUserFee) {
|
|
459
|
+
return await this.program.rpc.updateStateMaxInitializeUserFee(maxInitializeUserFee, {
|
|
460
|
+
accounts: {
|
|
461
|
+
admin: this.wallet.publicKey,
|
|
462
|
+
state: await this.getStatePublicKey(),
|
|
463
|
+
},
|
|
464
|
+
});
|
|
465
|
+
}
|
|
458
466
|
async updateWithdrawGuardThreshold(spotMarketIndex, withdrawGuardThreshold) {
|
|
459
467
|
const tx = await this.program.transaction.updateWithdrawGuardThreshold(withdrawGuardThreshold, {
|
|
460
468
|
accounts: {
|
|
@@ -194,6 +194,26 @@ exports.DevnetPerpMarkets = [
|
|
|
194
194
|
launchTs: 1700542800000,
|
|
195
195
|
oracleSource: __1.OracleSource.PYTH,
|
|
196
196
|
},
|
|
197
|
+
{
|
|
198
|
+
fullName: 'Celestia',
|
|
199
|
+
category: ['Data'],
|
|
200
|
+
symbol: 'TIA-PERP',
|
|
201
|
+
baseAssetSymbol: 'TIA',
|
|
202
|
+
marketIndex: 19,
|
|
203
|
+
oracle: new web3_js_1.PublicKey('4GiL1Y6u6JkPb7ckakzJgc414h6P7qoYnEKFcd1YtSB9'),
|
|
204
|
+
launchTs: 1701880540000,
|
|
205
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
fullName: 'Jito',
|
|
209
|
+
category: ['MEV'],
|
|
210
|
+
symbol: 'JTO-PERP',
|
|
211
|
+
baseAssetSymbol: 'JTO',
|
|
212
|
+
marketIndex: 20,
|
|
213
|
+
oracle: new web3_js_1.PublicKey('29xQnTzyyuRtgJ7RtSKEgBWwRzZqtrrKmyQQ5m3x629f'),
|
|
214
|
+
launchTs: 1701967240000,
|
|
215
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
216
|
+
},
|
|
197
217
|
];
|
|
198
218
|
exports.MainnetPerpMarkets = [
|
|
199
219
|
{
|
|
@@ -386,6 +406,26 @@ exports.MainnetPerpMarkets = [
|
|
|
386
406
|
launchTs: 1700542800000,
|
|
387
407
|
oracleSource: __1.OracleSource.PYTH,
|
|
388
408
|
},
|
|
409
|
+
{
|
|
410
|
+
fullName: 'Celestia',
|
|
411
|
+
category: ['Data'],
|
|
412
|
+
symbol: 'TIA-PERP',
|
|
413
|
+
baseAssetSymbol: 'TIA',
|
|
414
|
+
marketIndex: 19,
|
|
415
|
+
oracle: new web3_js_1.PublicKey('funeUsHgi2QKkLdUPASRLuYkaK8JaazCEz3HikbkhVt'),
|
|
416
|
+
launchTs: 1701880540000,
|
|
417
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
fullName: 'Jito',
|
|
421
|
+
category: ['MEV'],
|
|
422
|
+
symbol: 'JTO-PERP',
|
|
423
|
+
baseAssetSymbol: 'JTO',
|
|
424
|
+
marketIndex: 20,
|
|
425
|
+
oracle: new web3_js_1.PublicKey('D8UUgr8a3aR3yUeHLu7v8FWK7E8Y5sSU7qrYBXUJXBQ5'),
|
|
426
|
+
launchTs: 1701967240000,
|
|
427
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
428
|
+
},
|
|
389
429
|
];
|
|
390
430
|
exports.PerpMarkets = {
|
|
391
431
|
devnet: exports.DevnetPerpMarkets,
|
|
@@ -130,6 +130,16 @@ exports.MainnetSpotMarkets = [
|
|
|
130
130
|
precisionExp: numericConstants_1.NINE,
|
|
131
131
|
serumMarket: new web3_js_1.PublicKey('ARjaHVxGCQfTvvKjLd7U7srvk6orthZSE6uqWchCczZc'),
|
|
132
132
|
},
|
|
133
|
+
{
|
|
134
|
+
symbol: 'JTO',
|
|
135
|
+
marketIndex: 9,
|
|
136
|
+
oracle: new web3_js_1.PublicKey('D8UUgr8a3aR3yUeHLu7v8FWK7E8Y5sSU7qrYBXUJXBQ5'),
|
|
137
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
138
|
+
mint: new web3_js_1.PublicKey('jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL'),
|
|
139
|
+
precision: new __1.BN(10).pow(numericConstants_1.NINE),
|
|
140
|
+
precisionExp: numericConstants_1.NINE,
|
|
141
|
+
serumMarket: new web3_js_1.PublicKey('H87FfmHABiZLRGrDsXRZtqq25YpARzaokCzL1vMYGiep'),
|
|
142
|
+
},
|
|
133
143
|
];
|
|
134
144
|
exports.SpotMarkets = {
|
|
135
145
|
devnet: exports.DevnetSpotMarkets,
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeUser = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
const __1 = require("../");
|
|
7
|
+
const __2 = require("../");
|
|
8
|
+
function readUnsignedBigInt64LE(buffer, offset) {
|
|
9
|
+
return new __1.BN(buffer.subarray(offset, offset + 8), 10, 'le');
|
|
10
|
+
}
|
|
11
|
+
function readSignedBigInt64LE(buffer, offset) {
|
|
12
|
+
const unsignedValue = new __1.BN(buffer.subarray(offset, offset + 8), 10, 'le');
|
|
13
|
+
if (unsignedValue.testn(63)) {
|
|
14
|
+
const inverted = unsignedValue.notn(64).addn(1);
|
|
15
|
+
return inverted.neg();
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return unsignedValue;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function decodeUser(buffer) {
|
|
22
|
+
let offset = 8;
|
|
23
|
+
const authority = new web3_js_1.PublicKey(buffer.slice(offset, offset + 32));
|
|
24
|
+
offset += 32;
|
|
25
|
+
const delegate = new web3_js_1.PublicKey(buffer.slice(offset, offset + 32));
|
|
26
|
+
offset += 32;
|
|
27
|
+
const name = [];
|
|
28
|
+
for (let i = 0; i < 32; i++) {
|
|
29
|
+
name.push(buffer.readUint8(offset + i));
|
|
30
|
+
}
|
|
31
|
+
offset += 32;
|
|
32
|
+
const spotPositions = [];
|
|
33
|
+
for (let i = 0; i < 8; i++) {
|
|
34
|
+
const scaledBalance = readUnsignedBigInt64LE(buffer, offset);
|
|
35
|
+
const openOrders = buffer.readUInt8(offset + 35);
|
|
36
|
+
if (scaledBalance.eq(__2.ZERO) && openOrders === 0) {
|
|
37
|
+
offset += 40;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
offset += 8;
|
|
41
|
+
const openBids = readSignedBigInt64LE(buffer, offset);
|
|
42
|
+
offset += 8;
|
|
43
|
+
const openAsks = readSignedBigInt64LE(buffer, offset);
|
|
44
|
+
offset += 8;
|
|
45
|
+
const cumulativeDeposits = readSignedBigInt64LE(buffer, offset);
|
|
46
|
+
offset += 8;
|
|
47
|
+
const marketIndex = buffer.readUInt16LE(offset);
|
|
48
|
+
offset += 2;
|
|
49
|
+
const balanceTypeNum = buffer.readUInt8(offset);
|
|
50
|
+
let balanceType;
|
|
51
|
+
if (balanceTypeNum === 0) {
|
|
52
|
+
balanceType = types_1.SpotBalanceType.DEPOSIT;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
balanceType = types_1.SpotBalanceType.BORROW;
|
|
56
|
+
}
|
|
57
|
+
offset += 6;
|
|
58
|
+
spotPositions.push({
|
|
59
|
+
scaledBalance,
|
|
60
|
+
openBids,
|
|
61
|
+
openAsks,
|
|
62
|
+
cumulativeDeposits,
|
|
63
|
+
marketIndex,
|
|
64
|
+
balanceType,
|
|
65
|
+
openOrders,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const perpPositions = [];
|
|
69
|
+
for (let i = 0; i < 8; i++) {
|
|
70
|
+
const baseAssetAmount = readSignedBigInt64LE(buffer, offset + 8);
|
|
71
|
+
const quoteAssetAmount = readSignedBigInt64LE(buffer, offset + 16);
|
|
72
|
+
const lpShares = readUnsignedBigInt64LE(buffer, offset + 64);
|
|
73
|
+
const openOrders = buffer.readUInt8(offset + 94);
|
|
74
|
+
if (baseAssetAmount.eq(__2.ZERO) &&
|
|
75
|
+
openOrders === 0 &&
|
|
76
|
+
quoteAssetAmount.eq(__2.ZERO) &&
|
|
77
|
+
lpShares.eq(__2.ZERO)) {
|
|
78
|
+
offset += 96;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const lastCumulativeFundingRate = readSignedBigInt64LE(buffer, offset);
|
|
82
|
+
offset += 24;
|
|
83
|
+
const quoteBreakEvenAmount = readSignedBigInt64LE(buffer, offset);
|
|
84
|
+
offset += 8;
|
|
85
|
+
const quoteEntryAmount = readSignedBigInt64LE(buffer, offset);
|
|
86
|
+
offset += 8;
|
|
87
|
+
const openBids = readSignedBigInt64LE(buffer, offset);
|
|
88
|
+
offset += 8;
|
|
89
|
+
const openAsks = readSignedBigInt64LE(buffer, offset);
|
|
90
|
+
offset += 8;
|
|
91
|
+
const settledPnl = readSignedBigInt64LE(buffer, offset);
|
|
92
|
+
offset += 16;
|
|
93
|
+
const lastBaseAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
|
|
94
|
+
offset += 8;
|
|
95
|
+
const lastQuoteAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
|
|
96
|
+
offset += 8;
|
|
97
|
+
const remainderBaseAssetAmount = buffer.readUint32LE(offset);
|
|
98
|
+
offset += 4;
|
|
99
|
+
const marketIndex = buffer.readUInt16LE(offset);
|
|
100
|
+
offset += 3;
|
|
101
|
+
const perLpBase = buffer.readUInt8(offset);
|
|
102
|
+
offset += 1;
|
|
103
|
+
perpPositions.push({
|
|
104
|
+
lastCumulativeFundingRate,
|
|
105
|
+
baseAssetAmount,
|
|
106
|
+
quoteAssetAmount,
|
|
107
|
+
quoteBreakEvenAmount,
|
|
108
|
+
quoteEntryAmount,
|
|
109
|
+
openBids,
|
|
110
|
+
openAsks,
|
|
111
|
+
settledPnl,
|
|
112
|
+
lpShares,
|
|
113
|
+
lastBaseAssetAmountPerLp,
|
|
114
|
+
lastQuoteAssetAmountPerLp,
|
|
115
|
+
remainderBaseAssetAmount,
|
|
116
|
+
marketIndex,
|
|
117
|
+
openOrders,
|
|
118
|
+
perLpBase,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
const orders = [];
|
|
122
|
+
for (let i = 0; i < 32; i++) {
|
|
123
|
+
// skip order if it's not open
|
|
124
|
+
if (buffer.readUint8(offset + 82) === 0) {
|
|
125
|
+
offset += 96;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const slot = readUnsignedBigInt64LE(buffer, offset);
|
|
129
|
+
offset += 8;
|
|
130
|
+
const price = readUnsignedBigInt64LE(buffer, offset);
|
|
131
|
+
offset += 8;
|
|
132
|
+
const baseAssetAmount = readUnsignedBigInt64LE(buffer, offset);
|
|
133
|
+
offset += 8;
|
|
134
|
+
const baseAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
|
|
135
|
+
offset += 8;
|
|
136
|
+
const quoteAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
|
|
137
|
+
offset += 8;
|
|
138
|
+
const triggerPrice = readUnsignedBigInt64LE(buffer, offset);
|
|
139
|
+
offset += 8;
|
|
140
|
+
const auctionStartPrice = readSignedBigInt64LE(buffer, offset);
|
|
141
|
+
offset += 8;
|
|
142
|
+
const auctionEndPrice = readSignedBigInt64LE(buffer, offset);
|
|
143
|
+
offset += 8;
|
|
144
|
+
const maxTs = readSignedBigInt64LE(buffer, offset);
|
|
145
|
+
offset += 8;
|
|
146
|
+
const oraclePriceOffset = buffer.readInt32LE(offset);
|
|
147
|
+
offset += 4;
|
|
148
|
+
const orderId = buffer.readUInt32LE(offset);
|
|
149
|
+
offset += 4;
|
|
150
|
+
const marketIndex = buffer.readUInt16LE(offset);
|
|
151
|
+
offset += 2;
|
|
152
|
+
const orderStatusNum = buffer.readUInt8(offset);
|
|
153
|
+
let status;
|
|
154
|
+
if (orderStatusNum === 0) {
|
|
155
|
+
status = types_1.OrderStatus.INIT;
|
|
156
|
+
}
|
|
157
|
+
else if (orderStatusNum === 1) {
|
|
158
|
+
status = types_1.OrderStatus.OPEN;
|
|
159
|
+
}
|
|
160
|
+
offset += 1;
|
|
161
|
+
const orderTypeNum = buffer.readUInt8(offset);
|
|
162
|
+
let orderType;
|
|
163
|
+
if (orderTypeNum === 0) {
|
|
164
|
+
orderType = types_1.OrderType.MARKET;
|
|
165
|
+
}
|
|
166
|
+
else if (orderTypeNum === 1) {
|
|
167
|
+
orderType = types_1.OrderType.LIMIT;
|
|
168
|
+
}
|
|
169
|
+
else if (orderTypeNum === 2) {
|
|
170
|
+
orderType = types_1.OrderType.TRIGGER_MARKET;
|
|
171
|
+
}
|
|
172
|
+
else if (orderTypeNum === 3) {
|
|
173
|
+
orderType = types_1.OrderType.TRIGGER_LIMIT;
|
|
174
|
+
}
|
|
175
|
+
else if (orderTypeNum === 4) {
|
|
176
|
+
orderType = types_1.OrderType.ORACLE;
|
|
177
|
+
}
|
|
178
|
+
offset += 1;
|
|
179
|
+
const marketTypeNum = buffer.readUInt8(offset);
|
|
180
|
+
let marketType;
|
|
181
|
+
if (marketTypeNum === 0) {
|
|
182
|
+
marketType = types_1.MarketType.SPOT;
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
marketType = types_1.MarketType.PERP;
|
|
186
|
+
}
|
|
187
|
+
offset += 1;
|
|
188
|
+
const userOrderId = buffer.readUint8(offset);
|
|
189
|
+
offset += 1;
|
|
190
|
+
const existingPositionDirectionNum = buffer.readUInt8(offset);
|
|
191
|
+
let existingPositionDirection;
|
|
192
|
+
if (existingPositionDirectionNum === 0) {
|
|
193
|
+
existingPositionDirection = types_1.PositionDirection.LONG;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
existingPositionDirection = types_1.PositionDirection.SHORT;
|
|
197
|
+
}
|
|
198
|
+
offset += 1;
|
|
199
|
+
const positionDirectionNum = buffer.readUInt8(offset);
|
|
200
|
+
let direction;
|
|
201
|
+
if (positionDirectionNum === 0) {
|
|
202
|
+
direction = types_1.PositionDirection.LONG;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
direction = types_1.PositionDirection.SHORT;
|
|
206
|
+
}
|
|
207
|
+
offset += 1;
|
|
208
|
+
const reduceOnly = buffer.readUInt8(offset) === 1;
|
|
209
|
+
offset += 1;
|
|
210
|
+
const postOnly = buffer.readUInt8(offset) === 1;
|
|
211
|
+
offset += 1;
|
|
212
|
+
const immediateOrCancel = buffer.readUInt8(offset) === 1;
|
|
213
|
+
offset += 1;
|
|
214
|
+
const triggerConditionNum = buffer.readUInt8(offset);
|
|
215
|
+
let triggerCondition;
|
|
216
|
+
if (triggerConditionNum === 0) {
|
|
217
|
+
triggerCondition = types_1.OrderTriggerCondition.ABOVE;
|
|
218
|
+
}
|
|
219
|
+
else if (triggerConditionNum === 1) {
|
|
220
|
+
triggerCondition = types_1.OrderTriggerCondition.BELOW;
|
|
221
|
+
}
|
|
222
|
+
else if (triggerConditionNum === 2) {
|
|
223
|
+
triggerCondition = types_1.OrderTriggerCondition.TRIGGERED_ABOVE;
|
|
224
|
+
}
|
|
225
|
+
else if (triggerConditionNum === 3) {
|
|
226
|
+
triggerCondition = types_1.OrderTriggerCondition.TRIGGERED_BELOW;
|
|
227
|
+
}
|
|
228
|
+
offset += 1;
|
|
229
|
+
const auctionDuration = buffer.readUInt8(offset);
|
|
230
|
+
offset += 1;
|
|
231
|
+
offset += 3; // padding
|
|
232
|
+
orders.push({
|
|
233
|
+
slot,
|
|
234
|
+
price,
|
|
235
|
+
baseAssetAmount,
|
|
236
|
+
quoteAssetAmount: undefined,
|
|
237
|
+
baseAssetAmountFilled,
|
|
238
|
+
quoteAssetAmountFilled,
|
|
239
|
+
triggerPrice,
|
|
240
|
+
auctionStartPrice,
|
|
241
|
+
auctionEndPrice,
|
|
242
|
+
maxTs,
|
|
243
|
+
oraclePriceOffset,
|
|
244
|
+
orderId,
|
|
245
|
+
marketIndex,
|
|
246
|
+
status,
|
|
247
|
+
orderType,
|
|
248
|
+
marketType,
|
|
249
|
+
userOrderId,
|
|
250
|
+
existingPositionDirection,
|
|
251
|
+
direction,
|
|
252
|
+
reduceOnly,
|
|
253
|
+
postOnly,
|
|
254
|
+
immediateOrCancel,
|
|
255
|
+
triggerCondition,
|
|
256
|
+
auctionDuration,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
const lastAddPerpLpSharesTs = readSignedBigInt64LE(buffer, offset);
|
|
260
|
+
offset += 8;
|
|
261
|
+
const totalDeposits = readUnsignedBigInt64LE(buffer, offset);
|
|
262
|
+
offset += 8;
|
|
263
|
+
const totalWithdraws = readUnsignedBigInt64LE(buffer, offset);
|
|
264
|
+
offset += 8;
|
|
265
|
+
const totalSocialLoss = readUnsignedBigInt64LE(buffer, offset);
|
|
266
|
+
offset += 8;
|
|
267
|
+
const settledPerpPnl = readSignedBigInt64LE(buffer, offset);
|
|
268
|
+
offset += 8;
|
|
269
|
+
const cumulativeSpotFees = readSignedBigInt64LE(buffer, offset);
|
|
270
|
+
offset += 8;
|
|
271
|
+
const cumulativePerpFunding = readSignedBigInt64LE(buffer, offset);
|
|
272
|
+
offset += 8;
|
|
273
|
+
const liquidationMarginFreed = readUnsignedBigInt64LE(buffer, offset);
|
|
274
|
+
offset += 8;
|
|
275
|
+
const lastActiveSlot = readUnsignedBigInt64LE(buffer, offset);
|
|
276
|
+
offset += 8;
|
|
277
|
+
const nextOrderId = buffer.readUInt32LE(offset);
|
|
278
|
+
offset += 4;
|
|
279
|
+
const maxMarginRatio = buffer.readUInt32LE(offset);
|
|
280
|
+
offset += 4;
|
|
281
|
+
const nextLiquidationId = buffer.readUInt16LE(offset);
|
|
282
|
+
offset += 2;
|
|
283
|
+
const subAccountId = buffer.readUInt16LE(offset);
|
|
284
|
+
offset += 2;
|
|
285
|
+
const status = buffer.readUInt8(offset);
|
|
286
|
+
offset += 1;
|
|
287
|
+
const isMarginTradingEnabled = buffer.readUInt8(offset) === 1;
|
|
288
|
+
offset += 1;
|
|
289
|
+
const idle = buffer.readUInt8(offset) === 1;
|
|
290
|
+
offset += 1;
|
|
291
|
+
const openOrders = buffer.readUInt8(offset);
|
|
292
|
+
offset += 1;
|
|
293
|
+
const hasOpenOrder = buffer.readUInt8(offset) === 1;
|
|
294
|
+
offset += 1;
|
|
295
|
+
const openAuctions = buffer.readUInt8(offset);
|
|
296
|
+
offset += 1;
|
|
297
|
+
const hasOpenAuction = buffer.readUInt8(offset) === 1;
|
|
298
|
+
offset += 1;
|
|
299
|
+
// @ts-ignore
|
|
300
|
+
return {
|
|
301
|
+
authority,
|
|
302
|
+
delegate,
|
|
303
|
+
name,
|
|
304
|
+
spotPositions,
|
|
305
|
+
perpPositions,
|
|
306
|
+
orders,
|
|
307
|
+
lastAddPerpLpSharesTs,
|
|
308
|
+
totalDeposits,
|
|
309
|
+
totalWithdraws,
|
|
310
|
+
totalSocialLoss,
|
|
311
|
+
settledPerpPnl,
|
|
312
|
+
cumulativeSpotFees,
|
|
313
|
+
cumulativePerpFunding,
|
|
314
|
+
liquidationMarginFreed,
|
|
315
|
+
lastActiveSlot,
|
|
316
|
+
nextOrderId,
|
|
317
|
+
maxMarginRatio,
|
|
318
|
+
nextLiquidationId,
|
|
319
|
+
subAccountId,
|
|
320
|
+
status,
|
|
321
|
+
isMarginTradingEnabled,
|
|
322
|
+
idle,
|
|
323
|
+
openOrders,
|
|
324
|
+
hasOpenOrder,
|
|
325
|
+
openAuctions,
|
|
326
|
+
hasOpenAuction,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
exports.decodeUser = decodeUser;
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -228,9 +228,12 @@ export declare class DriftClient {
|
|
|
228
228
|
* @param subAccountId
|
|
229
229
|
* @param name
|
|
230
230
|
* @param fromSubAccountId
|
|
231
|
+
* @param referrerInfo
|
|
232
|
+
* @param donateAmount
|
|
233
|
+
* @param txParams
|
|
231
234
|
* @returns
|
|
232
235
|
*/
|
|
233
|
-
initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, marketIndex?: number, subAccountId?: number, name?: string, fromSubAccountId?: number, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
|
|
236
|
+
initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, marketIndex?: number, subAccountId?: number, name?: string, fromSubAccountId?: number, referrerInfo?: ReferrerInfo, donateAmount?: BN, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
|
|
234
237
|
initializeUserAccountForDevnet(subAccountId: number, name: string, marketIndex: number, tokenFaucet: TokenFaucet, amount: BN, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
|
|
235
238
|
/**
|
|
236
239
|
* Withdraws from a user account. If deposit doesn't already exist, creates a borrow
|
|
@@ -619,6 +622,7 @@ export declare class DriftClient {
|
|
|
619
622
|
settleRevenueToInsuranceFund(marketIndex: number): Promise<TransactionSignature>;
|
|
620
623
|
resolvePerpPnlDeficit(spotMarketIndex: number, perpMarketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
621
624
|
getResolvePerpPnlDeficitIx(spotMarketIndex: number, perpMarketIndex: number): Promise<TransactionInstruction>;
|
|
625
|
+
getDepositIntoSpotMarketRevenuePoolIx(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
|
|
622
626
|
depositIntoSpotMarketRevenuePool(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionSignature>;
|
|
623
627
|
getPerpMarketExtendedInfo(marketIndex: number): PerpMarketExtendedInfo;
|
|
624
628
|
/**
|
package/lib/driftClient.js
CHANGED
|
@@ -1061,9 +1061,12 @@ class DriftClient {
|
|
|
1061
1061
|
* @param subAccountId
|
|
1062
1062
|
* @param name
|
|
1063
1063
|
* @param fromSubAccountId
|
|
1064
|
+
* @param referrerInfo
|
|
1065
|
+
* @param donateAmount
|
|
1066
|
+
* @param txParams
|
|
1064
1067
|
* @returns
|
|
1065
1068
|
*/
|
|
1066
|
-
async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, txParams) {
|
|
1069
|
+
async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, donateAmount, txParams) {
|
|
1067
1070
|
var _a;
|
|
1068
1071
|
const ixs = [];
|
|
1069
1072
|
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
|
|
@@ -1083,10 +1086,19 @@ class DriftClient {
|
|
|
1083
1086
|
const isFromSubaccount = fromSubAccountId !== null &&
|
|
1084
1087
|
fromSubAccountId !== undefined &&
|
|
1085
1088
|
!isNaN(fromSubAccountId);
|
|
1086
|
-
|
|
1089
|
+
donateAmount = donateAmount ? donateAmount : numericConstants_1.ZERO;
|
|
1090
|
+
const createWSOLTokenAccount = (isSolMarket &&
|
|
1091
|
+
userTokenAccount.equals(authority) &&
|
|
1092
|
+
!isFromSubaccount) ||
|
|
1093
|
+
!donateAmount.eq(numericConstants_1.ZERO);
|
|
1094
|
+
const wSolAmount = isSolMarket ? amount.add(donateAmount) : donateAmount;
|
|
1095
|
+
let wsolTokenAccount;
|
|
1087
1096
|
if (createWSOLTokenAccount) {
|
|
1088
|
-
const { ixs: startIxs, pubkey } = await this.getWrappedSolAccountCreationIxs(
|
|
1089
|
-
|
|
1097
|
+
const { ixs: startIxs, pubkey } = await this.getWrappedSolAccountCreationIxs(wSolAmount, true);
|
|
1098
|
+
wsolTokenAccount = pubkey;
|
|
1099
|
+
if (isSolMarket) {
|
|
1100
|
+
userTokenAccount = pubkey;
|
|
1101
|
+
}
|
|
1090
1102
|
ixs.push(...startIxs);
|
|
1091
1103
|
}
|
|
1092
1104
|
const depositCollateralIx = isFromSubaccount
|
|
@@ -1098,9 +1110,13 @@ class DriftClient {
|
|
|
1098
1110
|
}
|
|
1099
1111
|
}
|
|
1100
1112
|
ixs.push(initializeUserAccountIx, depositCollateralIx);
|
|
1113
|
+
if (!donateAmount.eq(numericConstants_1.ZERO)) {
|
|
1114
|
+
const donateIx = await this.getDepositIntoSpotMarketRevenuePoolIx(1, donateAmount, wsolTokenAccount);
|
|
1115
|
+
ixs.push(donateIx);
|
|
1116
|
+
}
|
|
1101
1117
|
// Close the wrapped sol account at the end of the transaction
|
|
1102
1118
|
if (createWSOLTokenAccount) {
|
|
1103
|
-
ixs.push((0, spl_token_1.createCloseAccountInstruction)(
|
|
1119
|
+
ixs.push((0, spl_token_1.createCloseAccountInstruction)(wsolTokenAccount, authority, authority, []));
|
|
1104
1120
|
}
|
|
1105
1121
|
const tx = await this.buildTransaction(ixs, params);
|
|
1106
1122
|
const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
@@ -3237,9 +3253,9 @@ class DriftClient {
|
|
|
3237
3253
|
remainingAccounts: remainingAccounts,
|
|
3238
3254
|
});
|
|
3239
3255
|
}
|
|
3240
|
-
async
|
|
3256
|
+
async getDepositIntoSpotMarketRevenuePoolIx(marketIndex, amount, userTokenAccountPublicKey) {
|
|
3241
3257
|
const spotMarket = await this.getSpotMarketAccount(marketIndex);
|
|
3242
|
-
const
|
|
3258
|
+
const ix = await this.program.instruction.depositIntoSpotMarketRevenuePool(amount, {
|
|
3243
3259
|
accounts: {
|
|
3244
3260
|
state: await this.getStatePublicKey(),
|
|
3245
3261
|
spotMarket: spotMarket.pubkey,
|
|
@@ -3249,6 +3265,11 @@ class DriftClient {
|
|
|
3249
3265
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
3250
3266
|
},
|
|
3251
3267
|
});
|
|
3268
|
+
return ix;
|
|
3269
|
+
}
|
|
3270
|
+
async depositIntoSpotMarketRevenuePool(marketIndex, amount, userTokenAccountPublicKey) {
|
|
3271
|
+
const ix = await this.getDepositIntoSpotMarketRevenuePoolIx(marketIndex, amount, userTokenAccountPublicKey);
|
|
3272
|
+
const tx = await this.buildTransaction([ix]);
|
|
3252
3273
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
3253
3274
|
return txSig;
|
|
3254
3275
|
}
|
package/lib/examples/loadDlob.js
CHANGED
|
@@ -35,15 +35,20 @@ const main = async () => {
|
|
|
35
35
|
console.log('Subscribing drift client...');
|
|
36
36
|
await driftClient.subscribe();
|
|
37
37
|
console.log('Loading user map...');
|
|
38
|
-
const userMap = new __1.UserMap(
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
const userMap = new __1.UserMap({
|
|
39
|
+
driftClient,
|
|
40
|
+
subscriptionConfig: {
|
|
41
|
+
type: 'websocket',
|
|
42
|
+
commitment: 'processed',
|
|
43
|
+
},
|
|
44
|
+
skipInitialLoad: false,
|
|
45
|
+
includeIdle: false,
|
|
41
46
|
});
|
|
42
47
|
// fetches all users and subscribes for updates
|
|
43
48
|
await userMap.subscribe();
|
|
44
49
|
console.log('Loading dlob from user map...');
|
|
45
|
-
const
|
|
46
|
-
await
|
|
50
|
+
const slot = await driftClient.connection.getSlot();
|
|
51
|
+
const dlob = await userMap.getDLOB(slot);
|
|
47
52
|
console.log('number of orders', dlob.getDLOBOrders().length);
|
|
48
53
|
dlob.clear();
|
|
49
54
|
console.log('Unsubscribing users...');
|
package/lib/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
|
}
|