@drift-labs/sdk 0.2.0-master.31 → 0.2.0-master.33
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/lib/accounts/bulkAccountLoader.js +2 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +2 -2
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +8 -8
- package/lib/accounts/types.d.ts +1 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +2 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +7 -7
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +4 -4
- package/lib/addresses/pda.js +23 -22
- package/lib/admin.d.ts +30 -32
- package/lib/admin.js +111 -119
- package/lib/clearingHouse.d.ts +43 -34
- package/lib/clearingHouse.js +353 -193
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +26 -5
- package/lib/clearingHouseUser.js +151 -51
- package/lib/config.d.ts +2 -0
- package/lib/config.js +5 -1
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +3 -2
- package/lib/dlob/DLOB.d.ts +19 -11
- package/lib/dlob/DLOB.js +208 -107
- package/lib/dlob/DLOBNode.js +2 -10
- package/lib/dlob/NodeList.d.ts +1 -0
- package/lib/dlob/NodeList.js +6 -1
- package/lib/events/eventSubscriber.d.ts +1 -0
- package/lib/events/eventSubscriber.js +11 -4
- package/lib/events/fetchLogs.d.ts +3 -1
- package/lib/events/fetchLogs.js +13 -5
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/events/types.d.ts +1 -1
- package/lib/events/webSocketLogProvider.js +1 -1
- package/lib/factory/bigNum.d.ts +5 -4
- package/lib/factory/bigNum.js +36 -6
- package/lib/idl/clearing_house.json +1527 -1242
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/math/amm.js +9 -9
- package/lib/math/exchangeStatus.d.ts +4 -0
- package/lib/math/exchangeStatus.js +18 -0
- package/lib/math/funding.js +10 -10
- package/lib/math/margin.js +6 -1
- package/lib/math/market.js +9 -9
- package/lib/math/orders.d.ts +7 -3
- package/lib/math/orders.js +39 -31
- package/lib/math/repeg.js +3 -3
- package/lib/math/spotBalance.js +3 -3
- package/lib/math/spotPosition.js +2 -2
- package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/serum/serumSubscriber.d.ts +4 -0
- package/lib/serum/serumSubscriber.js +16 -1
- package/lib/types.d.ts +70 -85
- package/lib/types.js +12 -11
- package/lib/userMap/userMap.d.ts +17 -1
- package/lib/userMap/userMap.js +12 -0
- package/lib/userName.d.ts +1 -0
- package/lib/userName.js +3 -2
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +5 -1
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +9 -9
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +8 -8
- package/src/addresses/marketAddresses.ts +2 -2
- package/src/addresses/pda.ts +20 -20
- package/src/admin.ts +246 -221
- package/src/clearingHouse.ts +556 -236
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.ts +237 -87
- package/src/config.ts +8 -1
- package/src/constants/numericConstants.ts +5 -1
- package/src/dlob/DLOB.ts +290 -120
- package/src/dlob/DLOBNode.ts +2 -14
- package/src/dlob/NodeList.ts +7 -1
- package/src/events/eventSubscriber.ts +18 -4
- package/src/events/fetchLogs.ts +20 -5
- package/src/events/pollingLogProvider.ts +1 -1
- package/src/events/types.ts +2 -1
- package/src/events/webSocketLogProvider.ts +1 -1
- package/src/factory/bigNum.ts +59 -6
- package/src/idl/clearing_house.json +1527 -1242
- package/src/idl/pyth.json +98 -2
- package/src/index.ts +3 -0
- package/src/math/amm.ts +9 -9
- package/src/math/exchangeStatus.ts +31 -0
- package/src/math/funding.ts +20 -10
- package/src/math/margin.ts +7 -1
- package/src/math/market.ts +9 -9
- package/src/math/orders.ts +44 -29
- package/src/math/repeg.ts +3 -3
- package/src/math/spotBalance.ts +4 -4
- package/src/math/spotPosition.ts +2 -2
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +20 -1
- package/src/types.ts +75 -61
- package/src/userMap/userMap.ts +25 -1
- package/src/userName.ts +2 -1
- package/tests/bn/test.ts +22 -1
- package/tests/dlob/helpers.ts +252 -81
- package/tests/dlob/test.ts +1115 -215
package/lib/clearingHouse.js
CHANGED
|
@@ -64,8 +64,8 @@ class ClearingHouse {
|
|
|
64
64
|
this.provider = new anchor_1.AnchorProvider(config.connection, config.wallet, this.opts);
|
|
65
65
|
this.program = new anchor_1.Program(clearing_house_json_1.default, config.programID, this.provider);
|
|
66
66
|
this.authority = (_a = config.authority) !== null && _a !== void 0 ? _a : this.wallet.publicKey;
|
|
67
|
-
const
|
|
68
|
-
this.
|
|
67
|
+
const subAccountIds = (_b = config.subAccountIds) !== null && _b !== void 0 ? _b : [0];
|
|
68
|
+
this.activeSubAccountId = (_c = config.activeSubAccountId) !== null && _c !== void 0 ? _c : subAccountIds[0];
|
|
69
69
|
this.userAccountSubscriptionConfig =
|
|
70
70
|
((_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.type) === 'polling'
|
|
71
71
|
? {
|
|
@@ -75,7 +75,7 @@ class ClearingHouse {
|
|
|
75
75
|
: {
|
|
76
76
|
type: 'websocket',
|
|
77
77
|
};
|
|
78
|
-
this.createUsers(
|
|
78
|
+
this.createUsers(subAccountIds, this.userAccountSubscriptionConfig);
|
|
79
79
|
if (config.userStats) {
|
|
80
80
|
this.userStats = new clearingHouseUserStats_1.ClearingHouseUserStats({
|
|
81
81
|
clearingHouse: this,
|
|
@@ -111,14 +111,14 @@ class ClearingHouse {
|
|
|
111
111
|
set isSubscribed(val) {
|
|
112
112
|
this._isSubscribed = val;
|
|
113
113
|
}
|
|
114
|
-
createUsers(
|
|
115
|
-
for (const
|
|
116
|
-
const user = this.createUser(
|
|
117
|
-
this.users.set(
|
|
114
|
+
createUsers(subAccountIds, accountSubscriptionConfig) {
|
|
115
|
+
for (const subAccountId of subAccountIds) {
|
|
116
|
+
const user = this.createUser(subAccountId, accountSubscriptionConfig);
|
|
117
|
+
this.users.set(subAccountId, user);
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
|
-
createUser(
|
|
121
|
-
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.authority,
|
|
120
|
+
createUser(subAccountId, accountSubscriptionConfig) {
|
|
121
|
+
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.authority, subAccountId);
|
|
122
122
|
return new clearingHouseUser_1.ClearingHouseUser({
|
|
123
123
|
clearingHouse: this,
|
|
124
124
|
userAccountPublicKey,
|
|
@@ -206,10 +206,10 @@ class ClearingHouse {
|
|
|
206
206
|
/**
|
|
207
207
|
* Update the wallet to use for clearing house transactions and linked user account
|
|
208
208
|
* @param newWallet
|
|
209
|
-
* @param
|
|
210
|
-
* @param
|
|
209
|
+
* @param subAccountIds
|
|
210
|
+
* @param activeSubAccountId
|
|
211
211
|
*/
|
|
212
|
-
async updateWallet(newWallet,
|
|
212
|
+
async updateWallet(newWallet, subAccountIds = [0], activeSubAccountId = 0) {
|
|
213
213
|
const newProvider = new anchor_1.AnchorProvider(this.connection, newWallet, this.opts);
|
|
214
214
|
const newProgram = new anchor_1.Program(clearing_house_json_1.default, this.program.programId, newProvider);
|
|
215
215
|
// Update provider for txSender with new wallet details
|
|
@@ -222,28 +222,28 @@ class ClearingHouse {
|
|
|
222
222
|
await Promise.all(this.unsubscribeUsers());
|
|
223
223
|
}
|
|
224
224
|
this.users.clear();
|
|
225
|
-
this.createUsers(
|
|
225
|
+
this.createUsers(subAccountIds, this.userAccountSubscriptionConfig);
|
|
226
226
|
if (this.isSubscribed) {
|
|
227
227
|
await Promise.all(this.subscribeUsers());
|
|
228
228
|
}
|
|
229
|
-
this.
|
|
229
|
+
this.activeSubAccountId = activeSubAccountId;
|
|
230
230
|
this.userStatsAccountPublicKey = undefined;
|
|
231
231
|
}
|
|
232
|
-
async switchActiveUser(
|
|
233
|
-
this.
|
|
232
|
+
async switchActiveUser(subAccountId) {
|
|
233
|
+
this.activeSubAccountId = subAccountId;
|
|
234
234
|
}
|
|
235
|
-
async addUser(
|
|
236
|
-
if (this.users.has(
|
|
235
|
+
async addUser(subAccountId) {
|
|
236
|
+
if (this.users.has(subAccountId)) {
|
|
237
237
|
return;
|
|
238
238
|
}
|
|
239
|
-
const user = this.createUser(
|
|
239
|
+
const user = this.createUser(subAccountId, this.userAccountSubscriptionConfig);
|
|
240
240
|
await user.subscribe();
|
|
241
|
-
this.users.set(
|
|
241
|
+
this.users.set(subAccountId, user);
|
|
242
242
|
}
|
|
243
|
-
async initializeUserAccount(
|
|
244
|
-
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(
|
|
243
|
+
async initializeUserAccount(subAccountId = 0, name = userName_1.DEFAULT_USER_NAME, referrerInfo) {
|
|
244
|
+
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
|
|
245
245
|
const tx = new web3_js_1.Transaction();
|
|
246
|
-
if (
|
|
246
|
+
if (subAccountId === 0) {
|
|
247
247
|
// not the safest assumption, can explicitly check if user stats account exists if it causes problems
|
|
248
248
|
tx.add(await this.getInitializeUserStatsIx());
|
|
249
249
|
}
|
|
@@ -251,8 +251,8 @@ class ClearingHouse {
|
|
|
251
251
|
const { txSig } = await this.txSender.send(tx, [], this.opts);
|
|
252
252
|
return [txSig, userAccountPublicKey];
|
|
253
253
|
}
|
|
254
|
-
async getInitializeUserInstructions(
|
|
255
|
-
const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey,
|
|
254
|
+
async getInitializeUserInstructions(subAccountId = 0, name = userName_1.DEFAULT_USER_NAME, referrerInfo) {
|
|
255
|
+
const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, subAccountId);
|
|
256
256
|
const remainingAccounts = new Array();
|
|
257
257
|
if (referrerInfo !== undefined) {
|
|
258
258
|
remainingAccounts.push({
|
|
@@ -276,7 +276,7 @@ class ClearingHouse {
|
|
|
276
276
|
});
|
|
277
277
|
}
|
|
278
278
|
const nameBuffer = (0, userName_1.encodeName)(name);
|
|
279
|
-
const initializeUserAccountIx = await this.program.instruction.initializeUser(
|
|
279
|
+
const initializeUserAccountIx = await this.program.instruction.initializeUser(subAccountId, nameBuffer, {
|
|
280
280
|
accounts: {
|
|
281
281
|
user: userAccountPublicKey,
|
|
282
282
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
@@ -302,25 +302,26 @@ class ClearingHouse {
|
|
|
302
302
|
},
|
|
303
303
|
});
|
|
304
304
|
}
|
|
305
|
-
async updateUserName(name,
|
|
305
|
+
async updateUserName(name, subAccountId = 0) {
|
|
306
|
+
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, subAccountId);
|
|
306
307
|
const nameBuffer = (0, userName_1.encodeName)(name);
|
|
307
|
-
return await this.program.rpc.updateUserName(
|
|
308
|
+
return await this.program.rpc.updateUserName(subAccountId, nameBuffer, {
|
|
308
309
|
accounts: {
|
|
309
|
-
user:
|
|
310
|
+
user: userAccountPublicKey,
|
|
310
311
|
authority: this.wallet.publicKey,
|
|
311
312
|
},
|
|
312
313
|
});
|
|
313
314
|
}
|
|
314
|
-
async updateUserCustomMarginRatio(marginRatio,
|
|
315
|
-
return await this.program.rpc.updateUserCustomMarginRatio(
|
|
315
|
+
async updateUserCustomMarginRatio(marginRatio, subAccountId = 0) {
|
|
316
|
+
return await this.program.rpc.updateUserCustomMarginRatio(subAccountId, marginRatio, {
|
|
316
317
|
accounts: {
|
|
317
318
|
user: await this.getUserAccountPublicKey(),
|
|
318
319
|
authority: this.wallet.publicKey,
|
|
319
320
|
},
|
|
320
321
|
});
|
|
321
322
|
}
|
|
322
|
-
async updateUserDelegate(delegate,
|
|
323
|
-
return await this.program.rpc.updateUserDelegate(
|
|
323
|
+
async updateUserDelegate(delegate, subAccountId = 0) {
|
|
324
|
+
return await this.program.rpc.updateUserDelegate(subAccountId, delegate, {
|
|
324
325
|
accounts: {
|
|
325
326
|
user: await this.getUserAccountPublicKey(),
|
|
326
327
|
authority: this.wallet.publicKey,
|
|
@@ -339,9 +340,9 @@ class ClearingHouse {
|
|
|
339
340
|
]);
|
|
340
341
|
return programAccounts.map((programAccount) => programAccount.account);
|
|
341
342
|
}
|
|
342
|
-
async deleteUser(
|
|
343
|
+
async deleteUser(subAccountId = 0) {
|
|
343
344
|
var _a;
|
|
344
|
-
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey,
|
|
345
|
+
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, subAccountId);
|
|
345
346
|
const txSig = await this.program.rpc.deleteUser({
|
|
346
347
|
accounts: {
|
|
347
348
|
user: userAccountPublicKey,
|
|
@@ -350,16 +351,16 @@ class ClearingHouse {
|
|
|
350
351
|
state: await this.getStatePublicKey(),
|
|
351
352
|
},
|
|
352
353
|
});
|
|
353
|
-
await ((_a = this.users.get(
|
|
354
|
-
this.users.delete(
|
|
354
|
+
await ((_a = this.users.get(subAccountId)) === null || _a === void 0 ? void 0 : _a.unsubscribe());
|
|
355
|
+
this.users.delete(subAccountId);
|
|
355
356
|
return txSig;
|
|
356
357
|
}
|
|
357
|
-
getUser(
|
|
358
|
-
|
|
359
|
-
if (!this.users.has(
|
|
360
|
-
throw new Error(`Clearing House has no user for user id ${
|
|
358
|
+
getUser(subAccountId) {
|
|
359
|
+
subAccountId = subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId;
|
|
360
|
+
if (!this.users.has(subAccountId)) {
|
|
361
|
+
throw new Error(`Clearing House has no user for user id ${subAccountId}`);
|
|
361
362
|
}
|
|
362
|
-
return this.users.get(
|
|
363
|
+
return this.users.get(subAccountId);
|
|
363
364
|
}
|
|
364
365
|
getUsers() {
|
|
365
366
|
return [...this.users.values()];
|
|
@@ -377,11 +378,11 @@ class ClearingHouse {
|
|
|
377
378
|
async getUserAccountPublicKey() {
|
|
378
379
|
return this.getUser().userAccountPublicKey;
|
|
379
380
|
}
|
|
380
|
-
getUserAccount(
|
|
381
|
-
return this.getUser(
|
|
381
|
+
getUserAccount(subAccountId) {
|
|
382
|
+
return this.getUser(subAccountId).getUserAccount();
|
|
382
383
|
}
|
|
383
|
-
getUserAccountAndSlot(
|
|
384
|
-
return this.getUser(
|
|
384
|
+
getUserAccountAndSlot(subAccountId) {
|
|
385
|
+
return this.getUser(subAccountId).getUserAccountAndSlot();
|
|
385
386
|
}
|
|
386
387
|
getSpotPosition(marketIndex) {
|
|
387
388
|
return this.getUserAccount().spotPositions.find((spotPosition) => spotPosition.marketIndex === marketIndex);
|
|
@@ -389,7 +390,15 @@ class ClearingHouse {
|
|
|
389
390
|
getQuoteAssetTokenAmount() {
|
|
390
391
|
const spotMarket = this.getSpotMarketAccount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
391
392
|
const spotPosition = this.getSpotPosition(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
392
|
-
return (0, spotBalance_1.getTokenAmount)(spotPosition.
|
|
393
|
+
return (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarket, spotPosition.balanceType);
|
|
394
|
+
}
|
|
395
|
+
getTokenAmount(marketIndex) {
|
|
396
|
+
const spotPosition = this.getSpotPosition(marketIndex);
|
|
397
|
+
if (spotPosition === undefined) {
|
|
398
|
+
return numericConstants_1.ZERO;
|
|
399
|
+
}
|
|
400
|
+
const spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
401
|
+
return (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarket, spotPosition.balanceType);
|
|
393
402
|
}
|
|
394
403
|
getRemainingAccounts(params) {
|
|
395
404
|
var _a;
|
|
@@ -535,8 +544,12 @@ class ClearingHouse {
|
|
|
535
544
|
var _a;
|
|
536
545
|
return (_a = this.getUserAccount()) === null || _a === void 0 ? void 0 : _a.orders.find((order) => order.userOrderId === userOrderId);
|
|
537
546
|
}
|
|
538
|
-
async deposit(amount, marketIndex, collateralAccountPublicKey,
|
|
547
|
+
async deposit(amount, marketIndex, collateralAccountPublicKey, subAccountId, reduceOnly = false) {
|
|
539
548
|
const tx = new web3_js_1.Transaction();
|
|
549
|
+
tx.add(web3_js_1.ComputeBudgetProgram.requestUnits({
|
|
550
|
+
units: 600000,
|
|
551
|
+
additionalFee: 0,
|
|
552
|
+
}));
|
|
540
553
|
const additionalSigners = [];
|
|
541
554
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
542
555
|
const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
|
|
@@ -550,7 +563,7 @@ class ClearingHouse {
|
|
|
550
563
|
});
|
|
551
564
|
signers.forEach((signer) => additionalSigners.push(signer));
|
|
552
565
|
}
|
|
553
|
-
const depositCollateralIx = await this.getDepositInstruction(amount, marketIndex, collateralAccountPublicKey,
|
|
566
|
+
const depositCollateralIx = await this.getDepositInstruction(amount, marketIndex, collateralAccountPublicKey, subAccountId, reduceOnly, true);
|
|
554
567
|
tx.add(depositCollateralIx);
|
|
555
568
|
// Close the wrapped sol account at the end of the transaction
|
|
556
569
|
if (createWSOLTokenAccount) {
|
|
@@ -559,9 +572,9 @@ class ClearingHouse {
|
|
|
559
572
|
const { txSig } = await this.txSender.send(tx, additionalSigners, this.opts);
|
|
560
573
|
return txSig;
|
|
561
574
|
}
|
|
562
|
-
async getDepositInstruction(amount, marketIndex, userTokenAccount,
|
|
563
|
-
const userAccountPublicKey =
|
|
564
|
-
? await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority,
|
|
575
|
+
async getDepositInstruction(amount, marketIndex, userTokenAccount, subAccountId, reduceOnly = false, userInitialized = true) {
|
|
576
|
+
const userAccountPublicKey = subAccountId
|
|
577
|
+
? await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId)
|
|
565
578
|
: await this.getUserAccountPublicKey();
|
|
566
579
|
let remainingAccounts = [];
|
|
567
580
|
if (userInitialized) {
|
|
@@ -634,13 +647,13 @@ class ClearingHouse {
|
|
|
634
647
|
* @param amount
|
|
635
648
|
* @param userTokenAccount
|
|
636
649
|
* @param marketIndex
|
|
637
|
-
* @param
|
|
650
|
+
* @param subAccountId
|
|
638
651
|
* @param name
|
|
639
|
-
* @param
|
|
652
|
+
* @param fromSubAccountId
|
|
640
653
|
* @returns
|
|
641
654
|
*/
|
|
642
|
-
async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0,
|
|
643
|
-
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(
|
|
655
|
+
async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name = userName_1.DEFAULT_USER_NAME, fromSubAccountId, referrerInfo) {
|
|
656
|
+
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
|
|
644
657
|
const additionalSigners = [];
|
|
645
658
|
const spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
646
659
|
const isSolMarket = spotMarket.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
|
|
@@ -655,10 +668,10 @@ class ClearingHouse {
|
|
|
655
668
|
});
|
|
656
669
|
signers.forEach((signer) => additionalSigners.push(signer));
|
|
657
670
|
}
|
|
658
|
-
const depositCollateralIx =
|
|
659
|
-
? await this.getTransferDepositIx(amount, marketIndex,
|
|
660
|
-
: await this.getDepositInstruction(amount, marketIndex, userTokenAccount,
|
|
661
|
-
if (
|
|
671
|
+
const depositCollateralIx = fromSubAccountId != null
|
|
672
|
+
? await this.getTransferDepositIx(amount, marketIndex, fromSubAccountId, subAccountId)
|
|
673
|
+
: await this.getDepositInstruction(amount, marketIndex, userTokenAccount, subAccountId, false, false);
|
|
674
|
+
if (subAccountId === 0) {
|
|
662
675
|
tx.add(await this.getInitializeUserStatsIx());
|
|
663
676
|
}
|
|
664
677
|
tx.add(initializeUserAccountIx).add(depositCollateralIx);
|
|
@@ -669,12 +682,12 @@ class ClearingHouse {
|
|
|
669
682
|
const { txSig } = await this.txSender.send(tx, additionalSigners, this.opts);
|
|
670
683
|
return [txSig, userAccountPublicKey];
|
|
671
684
|
}
|
|
672
|
-
async initializeUserAccountForDevnet(
|
|
685
|
+
async initializeUserAccountForDevnet(subAccountId = 0, name = userName_1.DEFAULT_USER_NAME, marketIndex, tokenFaucet, amount, referrerInfo) {
|
|
673
686
|
const [associateTokenPublicKey, createAssociatedAccountIx, mintToIx] = await tokenFaucet.createAssociatedTokenAccountAndMintToInstructions(this.wallet.publicKey, amount);
|
|
674
|
-
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(
|
|
675
|
-
const depositCollateralIx = await this.getDepositInstruction(amount, marketIndex, associateTokenPublicKey,
|
|
687
|
+
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
|
|
688
|
+
const depositCollateralIx = await this.getDepositInstruction(amount, marketIndex, associateTokenPublicKey, subAccountId, false, false);
|
|
676
689
|
const tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToIx);
|
|
677
|
-
if (
|
|
690
|
+
if (subAccountId === 0) {
|
|
678
691
|
tx.add(await this.getInitializeUserStatsIx());
|
|
679
692
|
}
|
|
680
693
|
tx.add(initializeUserAccountIx).add(depositCollateralIx);
|
|
@@ -683,6 +696,10 @@ class ClearingHouse {
|
|
|
683
696
|
}
|
|
684
697
|
async withdraw(amount, marketIndex, userTokenAccount, reduceOnly = false) {
|
|
685
698
|
const tx = new web3_js_1.Transaction();
|
|
699
|
+
tx.add(web3_js_1.ComputeBudgetProgram.requestUnits({
|
|
700
|
+
units: 600000,
|
|
701
|
+
additionalFee: 0,
|
|
702
|
+
}));
|
|
686
703
|
const additionalSigners = [];
|
|
687
704
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
688
705
|
const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
|
|
@@ -735,23 +752,23 @@ class ClearingHouse {
|
|
|
735
752
|
remainingAccounts,
|
|
736
753
|
});
|
|
737
754
|
}
|
|
738
|
-
async transferDeposit(amount, marketIndex,
|
|
739
|
-
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getTransferDepositIx(amount, marketIndex,
|
|
755
|
+
async transferDeposit(amount, marketIndex, fromSubAccountId, toSubAccountId) {
|
|
756
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getTransferDepositIx(amount, marketIndex, fromSubAccountId, toSubAccountId)), [], this.opts);
|
|
740
757
|
return txSig;
|
|
741
758
|
}
|
|
742
|
-
async getTransferDepositIx(amount, marketIndex,
|
|
743
|
-
const fromUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey,
|
|
744
|
-
const toUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey,
|
|
759
|
+
async getTransferDepositIx(amount, marketIndex, fromSubAccountId, toSubAccountId) {
|
|
760
|
+
const fromUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, fromSubAccountId);
|
|
761
|
+
const toUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, toSubAccountId);
|
|
745
762
|
let remainingAccounts;
|
|
746
|
-
if (this.users.has(
|
|
763
|
+
if (this.users.has(fromSubAccountId)) {
|
|
747
764
|
remainingAccounts = this.getRemainingAccounts({
|
|
748
|
-
userAccounts: [this.users.get(
|
|
765
|
+
userAccounts: [this.users.get(fromSubAccountId).getUserAccount()],
|
|
749
766
|
useMarketLastSlotCache: true,
|
|
750
767
|
writableSpotMarketIndexes: [marketIndex],
|
|
751
768
|
});
|
|
752
769
|
}
|
|
753
770
|
else {
|
|
754
|
-
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.authority,
|
|
771
|
+
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.authority, fromSubAccountId);
|
|
755
772
|
const fromUserAccount = (await this.program.account.user.fetch(userAccountPublicKey));
|
|
756
773
|
remainingAccounts = this.getRemainingAccounts({
|
|
757
774
|
userAccounts: [fromUserAccount],
|
|
@@ -780,6 +797,7 @@ class ClearingHouse {
|
|
|
780
797
|
accounts: {
|
|
781
798
|
state: await this.getStatePublicKey(),
|
|
782
799
|
spotMarket: spotMarket.pubkey,
|
|
800
|
+
oracle: spotMarket.oracle,
|
|
783
801
|
},
|
|
784
802
|
});
|
|
785
803
|
}
|
|
@@ -801,11 +819,35 @@ class ClearingHouse {
|
|
|
801
819
|
remainingAccounts: remainingAccounts,
|
|
802
820
|
});
|
|
803
821
|
}
|
|
804
|
-
async
|
|
805
|
-
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.
|
|
822
|
+
async removePerpLpShares(marketIndex, sharesToBurn) {
|
|
823
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getRemovePerpLpSharesIx(marketIndex, sharesToBurn)), [], this.opts);
|
|
806
824
|
return txSig;
|
|
807
825
|
}
|
|
808
|
-
async
|
|
826
|
+
async removePerpLpSharesInExpiringMarket(marketIndex, userAccountPublicKey, sharesToBurn) {
|
|
827
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getRemovePerpLpSharesInExpiringMarket(marketIndex, userAccountPublicKey, sharesToBurn)), [], this.opts);
|
|
828
|
+
return txSig;
|
|
829
|
+
}
|
|
830
|
+
async getRemovePerpLpSharesInExpiringMarket(marketIndex, userAccountPublicKey, sharesToBurn) {
|
|
831
|
+
const userAccount = (await this.program.account.user.fetch(userAccountPublicKey));
|
|
832
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
833
|
+
userAccounts: [userAccount],
|
|
834
|
+
useMarketLastSlotCache: true,
|
|
835
|
+
writablePerpMarketIndexes: [marketIndex],
|
|
836
|
+
});
|
|
837
|
+
if (sharesToBurn == undefined) {
|
|
838
|
+
const perpPosition = userAccount.perpPositions.filter((position) => position.marketIndex === marketIndex)[0];
|
|
839
|
+
sharesToBurn = perpPosition.lpShares;
|
|
840
|
+
console.log('burning lp shares:', sharesToBurn.toString());
|
|
841
|
+
}
|
|
842
|
+
return this.program.instruction.removePerpLpSharesInExpiringMarket(sharesToBurn, marketIndex, {
|
|
843
|
+
accounts: {
|
|
844
|
+
state: await this.getStatePublicKey(),
|
|
845
|
+
user: userAccountPublicKey,
|
|
846
|
+
},
|
|
847
|
+
remainingAccounts: remainingAccounts,
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
async getRemovePerpLpSharesIx(marketIndex, sharesToBurn) {
|
|
809
851
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
810
852
|
const remainingAccounts = this.getRemainingAccounts({
|
|
811
853
|
userAccounts: [this.getUserAccount()],
|
|
@@ -818,7 +860,7 @@ class ClearingHouse {
|
|
|
818
860
|
sharesToBurn = perpPosition.lpShares;
|
|
819
861
|
console.log('burning lp shares:', sharesToBurn.toString());
|
|
820
862
|
}
|
|
821
|
-
return this.program.instruction.
|
|
863
|
+
return this.program.instruction.removePerpLpShares(sharesToBurn, marketIndex, {
|
|
822
864
|
accounts: {
|
|
823
865
|
state: await this.getStatePublicKey(),
|
|
824
866
|
user: userAccountPublicKey,
|
|
@@ -827,19 +869,19 @@ class ClearingHouse {
|
|
|
827
869
|
remainingAccounts: remainingAccounts,
|
|
828
870
|
});
|
|
829
871
|
}
|
|
830
|
-
async
|
|
831
|
-
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.
|
|
872
|
+
async addPerpLpShares(amount, marketIndex) {
|
|
873
|
+
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getAddPerpLpSharesIx(amount, marketIndex)), [], this.opts);
|
|
832
874
|
this.marketLastSlotCache.set(marketIndex, slot);
|
|
833
875
|
return txSig;
|
|
834
876
|
}
|
|
835
|
-
async
|
|
877
|
+
async getAddPerpLpSharesIx(amount, marketIndex) {
|
|
836
878
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
837
879
|
const remainingAccounts = this.getRemainingAccounts({
|
|
838
880
|
userAccounts: [this.getUserAccount()],
|
|
839
881
|
useMarketLastSlotCache: true,
|
|
840
882
|
writablePerpMarketIndexes: [marketIndex],
|
|
841
883
|
});
|
|
842
|
-
return this.program.instruction.
|
|
884
|
+
return this.program.instruction.addPerpLpShares(amount, marketIndex, {
|
|
843
885
|
accounts: {
|
|
844
886
|
state: await this.getStatePublicKey(),
|
|
845
887
|
user: userAccountPublicKey,
|
|
@@ -984,6 +1026,20 @@ class ClearingHouse {
|
|
|
984
1026
|
remainingAccounts,
|
|
985
1027
|
});
|
|
986
1028
|
}
|
|
1029
|
+
async settleExpiredMarketPoolsToRevenuePool(perpMarketIndex) {
|
|
1030
|
+
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
|
|
1031
|
+
const spotMarketPublicKey = await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
1032
|
+
const ix = await this.program.instruction.settleExpiredMarketPoolsToRevenuePool({
|
|
1033
|
+
accounts: {
|
|
1034
|
+
state: await this.getStatePublicKey(),
|
|
1035
|
+
admin: this.wallet.publicKey,
|
|
1036
|
+
spotMarket: spotMarketPublicKey,
|
|
1037
|
+
perpMarket: perpMarketPublicKey,
|
|
1038
|
+
},
|
|
1039
|
+
});
|
|
1040
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(ix), [], this.opts);
|
|
1041
|
+
return txSig;
|
|
1042
|
+
}
|
|
987
1043
|
async cancelOrder(orderId) {
|
|
988
1044
|
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getCancelOrderIx(orderId)), [], this.opts);
|
|
989
1045
|
return txSig;
|
|
@@ -1025,6 +1081,25 @@ class ClearingHouse {
|
|
|
1025
1081
|
remainingAccounts,
|
|
1026
1082
|
});
|
|
1027
1083
|
}
|
|
1084
|
+
async cancelOrders(marketType, marketIndex, direction) {
|
|
1085
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getCancelOrdersIx(marketType, marketIndex, direction)), [], this.opts);
|
|
1086
|
+
return txSig;
|
|
1087
|
+
}
|
|
1088
|
+
async getCancelOrdersIx(marketType, marketIndex, direction) {
|
|
1089
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1090
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1091
|
+
userAccounts: [this.getUserAccount()],
|
|
1092
|
+
useMarketLastSlotCache: true,
|
|
1093
|
+
});
|
|
1094
|
+
return await this.program.instruction.cancelOrders(marketType !== null && marketType !== void 0 ? marketType : null, marketIndex !== null && marketIndex !== void 0 ? marketIndex : null, direction !== null && direction !== void 0 ? direction : null, {
|
|
1095
|
+
accounts: {
|
|
1096
|
+
state: await this.getStatePublicKey(),
|
|
1097
|
+
user: userAccountPublicKey,
|
|
1098
|
+
authority: this.wallet.publicKey,
|
|
1099
|
+
},
|
|
1100
|
+
remainingAccounts,
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1028
1103
|
async fillOrder(userAccountPublicKey, user, order, makerInfo, referrerInfo) {
|
|
1029
1104
|
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo, referrerInfo)), [], this.opts);
|
|
1030
1105
|
return txSig;
|
|
@@ -1150,86 +1225,7 @@ class ClearingHouse {
|
|
|
1150
1225
|
const orderId = order.orderId;
|
|
1151
1226
|
const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
|
|
1152
1227
|
if (fulfillmentConfig) {
|
|
1153
|
-
|
|
1154
|
-
pubkey: fulfillmentConfig.pubkey,
|
|
1155
|
-
isWritable: false,
|
|
1156
|
-
isSigner: false,
|
|
1157
|
-
});
|
|
1158
|
-
remainingAccounts.push({
|
|
1159
|
-
pubkey: fulfillmentConfig.serumProgramId,
|
|
1160
|
-
isWritable: false,
|
|
1161
|
-
isSigner: false,
|
|
1162
|
-
});
|
|
1163
|
-
remainingAccounts.push({
|
|
1164
|
-
pubkey: fulfillmentConfig.serumMarket,
|
|
1165
|
-
isWritable: true,
|
|
1166
|
-
isSigner: false,
|
|
1167
|
-
});
|
|
1168
|
-
remainingAccounts.push({
|
|
1169
|
-
pubkey: fulfillmentConfig.serumRequestQueue,
|
|
1170
|
-
isWritable: true,
|
|
1171
|
-
isSigner: false,
|
|
1172
|
-
});
|
|
1173
|
-
remainingAccounts.push({
|
|
1174
|
-
pubkey: fulfillmentConfig.serumEventQueue,
|
|
1175
|
-
isWritable: true,
|
|
1176
|
-
isSigner: false,
|
|
1177
|
-
});
|
|
1178
|
-
remainingAccounts.push({
|
|
1179
|
-
pubkey: fulfillmentConfig.serumBids,
|
|
1180
|
-
isWritable: true,
|
|
1181
|
-
isSigner: false,
|
|
1182
|
-
});
|
|
1183
|
-
remainingAccounts.push({
|
|
1184
|
-
pubkey: fulfillmentConfig.serumAsks,
|
|
1185
|
-
isWritable: true,
|
|
1186
|
-
isSigner: false,
|
|
1187
|
-
});
|
|
1188
|
-
remainingAccounts.push({
|
|
1189
|
-
pubkey: fulfillmentConfig.serumBaseVault,
|
|
1190
|
-
isWritable: true,
|
|
1191
|
-
isSigner: false,
|
|
1192
|
-
});
|
|
1193
|
-
remainingAccounts.push({
|
|
1194
|
-
pubkey: fulfillmentConfig.serumQuoteVault,
|
|
1195
|
-
isWritable: true,
|
|
1196
|
-
isSigner: false,
|
|
1197
|
-
});
|
|
1198
|
-
remainingAccounts.push({
|
|
1199
|
-
pubkey: fulfillmentConfig.serumOpenOrders,
|
|
1200
|
-
isWritable: true,
|
|
1201
|
-
isSigner: false,
|
|
1202
|
-
});
|
|
1203
|
-
remainingAccounts.push({
|
|
1204
|
-
pubkey: (0, pda_1.getSerumSignerPublicKey)(fulfillmentConfig.serumProgramId, fulfillmentConfig.serumMarket, fulfillmentConfig.serumSignerNonce),
|
|
1205
|
-
isWritable: false,
|
|
1206
|
-
isSigner: false,
|
|
1207
|
-
});
|
|
1208
|
-
remainingAccounts.push({
|
|
1209
|
-
pubkey: this.getSignerPublicKey(),
|
|
1210
|
-
isWritable: false,
|
|
1211
|
-
isSigner: false,
|
|
1212
|
-
});
|
|
1213
|
-
remainingAccounts.push({
|
|
1214
|
-
pubkey: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1215
|
-
isWritable: false,
|
|
1216
|
-
isSigner: false,
|
|
1217
|
-
});
|
|
1218
|
-
remainingAccounts.push({
|
|
1219
|
-
pubkey: this.getSpotMarketAccount(marketIndex).vault,
|
|
1220
|
-
isWritable: true,
|
|
1221
|
-
isSigner: false,
|
|
1222
|
-
});
|
|
1223
|
-
remainingAccounts.push({
|
|
1224
|
-
pubkey: this.getQuoteSpotMarketAccount().vault,
|
|
1225
|
-
isWritable: true,
|
|
1226
|
-
isSigner: false,
|
|
1227
|
-
});
|
|
1228
|
-
remainingAccounts.push({
|
|
1229
|
-
pubkey: this.getStateAccount().srmVault,
|
|
1230
|
-
isWritable: false,
|
|
1231
|
-
isSigner: false,
|
|
1232
|
-
});
|
|
1228
|
+
this.addSerumRemainingAccounts(marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1233
1229
|
}
|
|
1234
1230
|
return await this.program.instruction.fillSpotOrder(orderId, fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null, makerOrderId, {
|
|
1235
1231
|
accounts: {
|
|
@@ -1243,6 +1239,88 @@ class ClearingHouse {
|
|
|
1243
1239
|
remainingAccounts,
|
|
1244
1240
|
});
|
|
1245
1241
|
}
|
|
1242
|
+
addSerumRemainingAccounts(marketIndex, remainingAccounts, fulfillmentConfig) {
|
|
1243
|
+
remainingAccounts.push({
|
|
1244
|
+
pubkey: fulfillmentConfig.pubkey,
|
|
1245
|
+
isWritable: false,
|
|
1246
|
+
isSigner: false,
|
|
1247
|
+
});
|
|
1248
|
+
remainingAccounts.push({
|
|
1249
|
+
pubkey: fulfillmentConfig.serumProgramId,
|
|
1250
|
+
isWritable: false,
|
|
1251
|
+
isSigner: false,
|
|
1252
|
+
});
|
|
1253
|
+
remainingAccounts.push({
|
|
1254
|
+
pubkey: fulfillmentConfig.serumMarket,
|
|
1255
|
+
isWritable: true,
|
|
1256
|
+
isSigner: false,
|
|
1257
|
+
});
|
|
1258
|
+
remainingAccounts.push({
|
|
1259
|
+
pubkey: fulfillmentConfig.serumRequestQueue,
|
|
1260
|
+
isWritable: true,
|
|
1261
|
+
isSigner: false,
|
|
1262
|
+
});
|
|
1263
|
+
remainingAccounts.push({
|
|
1264
|
+
pubkey: fulfillmentConfig.serumEventQueue,
|
|
1265
|
+
isWritable: true,
|
|
1266
|
+
isSigner: false,
|
|
1267
|
+
});
|
|
1268
|
+
remainingAccounts.push({
|
|
1269
|
+
pubkey: fulfillmentConfig.serumBids,
|
|
1270
|
+
isWritable: true,
|
|
1271
|
+
isSigner: false,
|
|
1272
|
+
});
|
|
1273
|
+
remainingAccounts.push({
|
|
1274
|
+
pubkey: fulfillmentConfig.serumAsks,
|
|
1275
|
+
isWritable: true,
|
|
1276
|
+
isSigner: false,
|
|
1277
|
+
});
|
|
1278
|
+
remainingAccounts.push({
|
|
1279
|
+
pubkey: fulfillmentConfig.serumBaseVault,
|
|
1280
|
+
isWritable: true,
|
|
1281
|
+
isSigner: false,
|
|
1282
|
+
});
|
|
1283
|
+
remainingAccounts.push({
|
|
1284
|
+
pubkey: fulfillmentConfig.serumQuoteVault,
|
|
1285
|
+
isWritable: true,
|
|
1286
|
+
isSigner: false,
|
|
1287
|
+
});
|
|
1288
|
+
remainingAccounts.push({
|
|
1289
|
+
pubkey: fulfillmentConfig.serumOpenOrders,
|
|
1290
|
+
isWritable: true,
|
|
1291
|
+
isSigner: false,
|
|
1292
|
+
});
|
|
1293
|
+
remainingAccounts.push({
|
|
1294
|
+
pubkey: (0, pda_1.getSerumSignerPublicKey)(fulfillmentConfig.serumProgramId, fulfillmentConfig.serumMarket, fulfillmentConfig.serumSignerNonce),
|
|
1295
|
+
isWritable: false,
|
|
1296
|
+
isSigner: false,
|
|
1297
|
+
});
|
|
1298
|
+
remainingAccounts.push({
|
|
1299
|
+
pubkey: this.getSignerPublicKey(),
|
|
1300
|
+
isWritable: false,
|
|
1301
|
+
isSigner: false,
|
|
1302
|
+
});
|
|
1303
|
+
remainingAccounts.push({
|
|
1304
|
+
pubkey: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1305
|
+
isWritable: false,
|
|
1306
|
+
isSigner: false,
|
|
1307
|
+
});
|
|
1308
|
+
remainingAccounts.push({
|
|
1309
|
+
pubkey: this.getSpotMarketAccount(marketIndex).vault,
|
|
1310
|
+
isWritable: true,
|
|
1311
|
+
isSigner: false,
|
|
1312
|
+
});
|
|
1313
|
+
remainingAccounts.push({
|
|
1314
|
+
pubkey: this.getQuoteSpotMarketAccount().vault,
|
|
1315
|
+
isWritable: true,
|
|
1316
|
+
isSigner: false,
|
|
1317
|
+
});
|
|
1318
|
+
remainingAccounts.push({
|
|
1319
|
+
pubkey: this.getStateAccount().srmVault,
|
|
1320
|
+
isWritable: false,
|
|
1321
|
+
isSigner: false,
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1246
1324
|
async triggerOrder(userAccountPublicKey, user, order) {
|
|
1247
1325
|
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getTriggerOrderIx(userAccountPublicKey, user, order)), [], this.opts);
|
|
1248
1326
|
return txSig;
|
|
@@ -1367,6 +1445,106 @@ class ClearingHouse {
|
|
|
1367
1445
|
remainingAccounts,
|
|
1368
1446
|
});
|
|
1369
1447
|
}
|
|
1448
|
+
async placeAndTakeSpotOrder(orderParams, fulfillmentConfig, makerInfo, referrerInfo) {
|
|
1449
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndTakeSpotOrderIx(orderParams, fulfillmentConfig, makerInfo, referrerInfo)), [], this.opts);
|
|
1450
|
+
return txSig;
|
|
1451
|
+
}
|
|
1452
|
+
async getPlaceAndTakeSpotOrderIx(orderParams, fulfillmentConfig, makerInfo, referrerInfo) {
|
|
1453
|
+
orderParams = this.getOrderParams(orderParams, types_1.MarketType.SPOT);
|
|
1454
|
+
const userStatsPublicKey = await this.getUserStatsAccountPublicKey();
|
|
1455
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1456
|
+
const userAccounts = [this.getUserAccount()];
|
|
1457
|
+
if (makerInfo !== undefined) {
|
|
1458
|
+
userAccounts.push(makerInfo.makerUserAccount);
|
|
1459
|
+
}
|
|
1460
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1461
|
+
userAccounts,
|
|
1462
|
+
useMarketLastSlotCache: true,
|
|
1463
|
+
writableSpotMarketIndexes: [
|
|
1464
|
+
orderParams.marketIndex,
|
|
1465
|
+
numericConstants_1.QUOTE_SPOT_MARKET_INDEX,
|
|
1466
|
+
],
|
|
1467
|
+
});
|
|
1468
|
+
let makerOrderId = null;
|
|
1469
|
+
if (makerInfo) {
|
|
1470
|
+
makerOrderId = makerInfo.order.orderId;
|
|
1471
|
+
remainingAccounts.push({
|
|
1472
|
+
pubkey: makerInfo.maker,
|
|
1473
|
+
isSigner: false,
|
|
1474
|
+
isWritable: true,
|
|
1475
|
+
});
|
|
1476
|
+
remainingAccounts.push({
|
|
1477
|
+
pubkey: makerInfo.makerStats,
|
|
1478
|
+
isSigner: false,
|
|
1479
|
+
isWritable: true,
|
|
1480
|
+
});
|
|
1481
|
+
}
|
|
1482
|
+
if (referrerInfo) {
|
|
1483
|
+
remainingAccounts.push({
|
|
1484
|
+
pubkey: referrerInfo.referrer,
|
|
1485
|
+
isWritable: true,
|
|
1486
|
+
isSigner: false,
|
|
1487
|
+
});
|
|
1488
|
+
remainingAccounts.push({
|
|
1489
|
+
pubkey: referrerInfo.referrerStats,
|
|
1490
|
+
isWritable: true,
|
|
1491
|
+
isSigner: false,
|
|
1492
|
+
});
|
|
1493
|
+
}
|
|
1494
|
+
if (fulfillmentConfig) {
|
|
1495
|
+
this.addSerumRemainingAccounts(orderParams.marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1496
|
+
}
|
|
1497
|
+
return await this.program.instruction.placeAndTakeSpotOrder(orderParams, fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null, makerOrderId, {
|
|
1498
|
+
accounts: {
|
|
1499
|
+
state: await this.getStatePublicKey(),
|
|
1500
|
+
user: userAccountPublicKey,
|
|
1501
|
+
userStats: userStatsPublicKey,
|
|
1502
|
+
authority: this.wallet.publicKey,
|
|
1503
|
+
},
|
|
1504
|
+
remainingAccounts,
|
|
1505
|
+
});
|
|
1506
|
+
}
|
|
1507
|
+
async placeAndMakeSpotOrder(orderParams, takerInfo, referrerInfo) {
|
|
1508
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndMakeSpotOrderIx(orderParams, takerInfo, referrerInfo)), [], this.opts);
|
|
1509
|
+
return txSig;
|
|
1510
|
+
}
|
|
1511
|
+
async getPlaceAndMakeSpotOrderIx(orderParams, takerInfo, referrerInfo) {
|
|
1512
|
+
orderParams = this.getOrderParams(orderParams, types_1.MarketType.SPOT);
|
|
1513
|
+
const userStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
1514
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1515
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1516
|
+
userAccounts: [this.getUserAccount(), takerInfo.takerUserAccount],
|
|
1517
|
+
useMarketLastSlotCache: true,
|
|
1518
|
+
writableSpotMarketIndexes: [
|
|
1519
|
+
orderParams.marketIndex,
|
|
1520
|
+
numericConstants_1.QUOTE_SPOT_MARKET_INDEX,
|
|
1521
|
+
],
|
|
1522
|
+
});
|
|
1523
|
+
if (referrerInfo) {
|
|
1524
|
+
remainingAccounts.push({
|
|
1525
|
+
pubkey: referrerInfo.referrer,
|
|
1526
|
+
isWritable: true,
|
|
1527
|
+
isSigner: false,
|
|
1528
|
+
});
|
|
1529
|
+
remainingAccounts.push({
|
|
1530
|
+
pubkey: referrerInfo.referrerStats,
|
|
1531
|
+
isWritable: true,
|
|
1532
|
+
isSigner: false,
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
const takerOrderId = takerInfo.order.orderId;
|
|
1536
|
+
return await this.program.instruction.placeAndMakeSpotOrder(orderParams, takerOrderId, {
|
|
1537
|
+
accounts: {
|
|
1538
|
+
state: await this.getStatePublicKey(),
|
|
1539
|
+
user: userAccountPublicKey,
|
|
1540
|
+
userStats: userStatsPublicKey,
|
|
1541
|
+
taker: takerInfo.taker,
|
|
1542
|
+
takerStats: takerInfo.takerStats,
|
|
1543
|
+
authority: this.wallet.publicKey,
|
|
1544
|
+
},
|
|
1545
|
+
remainingAccounts,
|
|
1546
|
+
});
|
|
1547
|
+
}
|
|
1370
1548
|
/**
|
|
1371
1549
|
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
1372
1550
|
* @param marketIndex
|
|
@@ -1419,25 +1597,6 @@ class ClearingHouse {
|
|
|
1419
1597
|
remainingAccounts: remainingAccounts,
|
|
1420
1598
|
});
|
|
1421
1599
|
}
|
|
1422
|
-
async settleExpiredPosition(settleeUserAccountPublicKey, settleeUserAccount, marketIndex) {
|
|
1423
|
-
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getSettleExpiredPositionIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex)), [], this.opts);
|
|
1424
|
-
return txSig;
|
|
1425
|
-
}
|
|
1426
|
-
async getSettleExpiredPositionIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex) {
|
|
1427
|
-
const remainingAccounts = this.getRemainingAccounts({
|
|
1428
|
-
userAccounts: [settleeUserAccount],
|
|
1429
|
-
writablePerpMarketIndexes: [marketIndex],
|
|
1430
|
-
writableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
|
|
1431
|
-
});
|
|
1432
|
-
return await this.program.instruction.settleExpiredPosition(marketIndex, {
|
|
1433
|
-
accounts: {
|
|
1434
|
-
state: await this.getStatePublicKey(),
|
|
1435
|
-
authority: this.wallet.publicKey,
|
|
1436
|
-
user: settleeUserAccountPublicKey,
|
|
1437
|
-
},
|
|
1438
|
-
remainingAccounts: remainingAccounts,
|
|
1439
|
-
});
|
|
1440
|
-
}
|
|
1441
1600
|
async liquidatePerp(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount) {
|
|
1442
1601
|
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount)), [], this.opts);
|
|
1443
1602
|
this.marketLastSlotCache.set(marketIndex, slot);
|
|
@@ -1564,7 +1723,7 @@ class ClearingHouse {
|
|
|
1564
1723
|
liquidator: liquidatorPublicKey,
|
|
1565
1724
|
liquidatorStats: liquidatorStatsPublicKey,
|
|
1566
1725
|
spotMarketVault: spotMarket.vault,
|
|
1567
|
-
insuranceFundVault: spotMarket.
|
|
1726
|
+
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1568
1727
|
clearingHouseSigner: this.getSignerPublicKey(),
|
|
1569
1728
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1570
1729
|
},
|
|
@@ -1593,22 +1752,23 @@ class ClearingHouse {
|
|
|
1593
1752
|
liquidatorStats: liquidatorStatsPublicKey,
|
|
1594
1753
|
liquidator: liquidatorPublicKey,
|
|
1595
1754
|
spotMarketVault: spotMarket.vault,
|
|
1596
|
-
insuranceFundVault: spotMarket.
|
|
1755
|
+
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1597
1756
|
clearingHouseSigner: this.getSignerPublicKey(),
|
|
1598
1757
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1599
1758
|
},
|
|
1600
1759
|
remainingAccounts: remainingAccounts,
|
|
1601
1760
|
});
|
|
1602
1761
|
}
|
|
1603
|
-
async updateFundingRate(
|
|
1604
|
-
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getUpdateFundingRateIx(
|
|
1762
|
+
async updateFundingRate(perpMarketIndex, oracle) {
|
|
1763
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getUpdateFundingRateIx(perpMarketIndex, oracle)), [], this.opts);
|
|
1605
1764
|
return txSig;
|
|
1606
1765
|
}
|
|
1607
|
-
async getUpdateFundingRateIx(
|
|
1608
|
-
|
|
1766
|
+
async getUpdateFundingRateIx(perpMarketIndex, oracle) {
|
|
1767
|
+
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
|
|
1768
|
+
return await this.program.instruction.updateFundingRate(perpMarketIndex, {
|
|
1609
1769
|
accounts: {
|
|
1610
1770
|
state: await this.getStatePublicKey(),
|
|
1611
|
-
|
|
1771
|
+
perpMarket: perpMarketPublicKey,
|
|
1612
1772
|
oracle: oracle,
|
|
1613
1773
|
},
|
|
1614
1774
|
});
|
|
@@ -1640,7 +1800,7 @@ class ClearingHouse {
|
|
|
1640
1800
|
triggerEvent(eventName, data) {
|
|
1641
1801
|
this.eventEmitter.emit(eventName, data);
|
|
1642
1802
|
}
|
|
1643
|
-
|
|
1803
|
+
getOracleDataForPerpMarket(marketIndex) {
|
|
1644
1804
|
const oracleKey = this.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
1645
1805
|
const oracleData = this.getOraclePriceDataAndSlot(oracleKey).data;
|
|
1646
1806
|
return oracleData;
|
|
@@ -1685,7 +1845,7 @@ class ClearingHouse {
|
|
|
1685
1845
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
1686
1846
|
authority: this.wallet.publicKey,
|
|
1687
1847
|
spotMarketVault: spotMarket.vault,
|
|
1688
|
-
insuranceFundVault: spotMarket.
|
|
1848
|
+
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1689
1849
|
clearingHouseSigner: this.getSignerPublicKey(),
|
|
1690
1850
|
userTokenAccount: collateralAccountPublicKey,
|
|
1691
1851
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
@@ -1708,7 +1868,7 @@ class ClearingHouse {
|
|
|
1708
1868
|
insuranceFundStake: ifStakeAccountPublicKey,
|
|
1709
1869
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
1710
1870
|
authority: this.wallet.publicKey,
|
|
1711
|
-
insuranceFundVault: spotMarketAccount.
|
|
1871
|
+
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
1712
1872
|
},
|
|
1713
1873
|
remainingAccounts,
|
|
1714
1874
|
});
|
|
@@ -1728,7 +1888,7 @@ class ClearingHouse {
|
|
|
1728
1888
|
insuranceFundStake: ifStakeAccountPublicKey,
|
|
1729
1889
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
1730
1890
|
authority: this.wallet.publicKey,
|
|
1731
|
-
insuranceFundVault: spotMarketAccount.
|
|
1891
|
+
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
1732
1892
|
},
|
|
1733
1893
|
remainingAccounts,
|
|
1734
1894
|
});
|
|
@@ -1748,7 +1908,7 @@ class ClearingHouse {
|
|
|
1748
1908
|
insuranceFundStake: ifStakeAccountPublicKey,
|
|
1749
1909
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
1750
1910
|
authority: this.wallet.publicKey,
|
|
1751
|
-
insuranceFundVault: spotMarketAccount.
|
|
1911
|
+
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
1752
1912
|
clearingHouseSigner: this.getSignerPublicKey(),
|
|
1753
1913
|
userTokenAccount: collateralAccountPublicKey,
|
|
1754
1914
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
@@ -1769,7 +1929,7 @@ class ClearingHouse {
|
|
|
1769
1929
|
spotMarket: spotMarketAccount.pubkey,
|
|
1770
1930
|
spotMarketVault: spotMarketAccount.vault,
|
|
1771
1931
|
clearingHouseSigner: this.getSignerPublicKey(),
|
|
1772
|
-
insuranceFundVault: spotMarketAccount.
|
|
1932
|
+
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
1773
1933
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1774
1934
|
},
|
|
1775
1935
|
remainingAccounts,
|
|
@@ -1792,7 +1952,7 @@ class ClearingHouse {
|
|
|
1792
1952
|
state: await this.getStatePublicKey(),
|
|
1793
1953
|
authority: this.wallet.publicKey,
|
|
1794
1954
|
spotMarketVault: spotMarket.vault,
|
|
1795
|
-
insuranceFundVault: spotMarket.
|
|
1955
|
+
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1796
1956
|
clearingHouseSigner: this.getSignerPublicKey(),
|
|
1797
1957
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1798
1958
|
},
|