@drift-labs/sdk 0.1.21-master.0 → 0.1.21-master.3
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.d.ts +0 -1
- package/lib/accounts/bulkAccountLoader.js +4 -2
- package/lib/accounts/bulkUserSubscription.d.ts +0 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +5 -2
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +23 -1
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +0 -1
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +3 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +13 -0
- package/lib/accounts/types.d.ts +8 -3
- package/lib/accounts/utils.d.ts +0 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +0 -1
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +5 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +25 -0
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +3 -2
- package/lib/accounts/webSocketUserAccountSubscriber.js +11 -0
- package/lib/addresses.d.ts +4 -1
- package/lib/addresses.js +28 -1
- package/lib/admin.d.ts +10 -4
- package/lib/admin.js +48 -3
- package/lib/assert/assert.d.ts +0 -1
- package/lib/clearingHouse.d.ts +27 -2
- package/lib/clearingHouse.js +307 -7
- package/lib/clearingHouseUser.d.ts +15 -17
- package/lib/clearingHouseUser.js +105 -84
- package/lib/config.d.ts +0 -1
- package/lib/constants/markets.d.ts +0 -1
- package/lib/constants/markets.js +8 -0
- package/lib/constants/numericConstants.d.ts +1 -1
- package/lib/constants/numericConstants.js +2 -1
- package/lib/examples/makeTradeExample.d.ts +0 -1
- package/lib/factory/clearingHouse.d.ts +0 -1
- package/lib/factory/clearingHouseUser.d.ts +0 -1
- package/lib/idl/clearing_house.json +1066 -39
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -0
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.js +31 -8
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/funding.d.ts +0 -1
- package/lib/math/insuranceFund.d.ts +0 -1
- package/lib/math/market.d.ts +2 -2
- package/lib/math/market.js +11 -1
- package/lib/math/orders.d.ts +3 -0
- package/lib/math/orders.js +32 -0
- package/lib/math/position.d.ts +4 -2
- package/lib/math/position.js +18 -4
- package/lib/math/trade.d.ts +0 -1
- package/lib/math/utils.d.ts +0 -1
- package/lib/mockUSDCFaucet.d.ts +0 -1
- package/lib/orderParams.d.ts +7 -0
- package/lib/orderParams.js +108 -0
- package/lib/orders.d.ts +6 -0
- package/lib/orders.js +136 -0
- package/lib/pythClient.d.ts +0 -1
- package/lib/token/index.d.ts +0 -1
- package/lib/tx/defaultTxSender.d.ts +0 -1
- package/lib/tx/types.d.ts +0 -1
- package/lib/tx/utils.d.ts +0 -1
- package/lib/types.d.ts +145 -2
- package/lib/types.js +36 -1
- package/lib/util/computeUnits.d.ts +0 -1
- package/lib/util/tps.d.ts +0 -1
- package/lib/wallet.d.ts +0 -1
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +3 -3
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +33 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +30 -2
- package/src/accounts/types.ts +11 -2
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +47 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +29 -2
- package/src/addresses.ts +37 -0
- package/src/admin.ts +84 -6
- package/src/clearingHouse.ts +430 -5
- package/src/clearingHouseUser.ts +164 -102
- package/src/constants/markets.ts +8 -0
- package/src/constants/numericConstants.ts +1 -0
- package/src/idl/clearing_house.json +1066 -39
- package/src/index.ts +3 -0
- package/src/math/amm.ts +47 -14
- package/src/math/market.ts +28 -2
- package/src/math/orders.ts +44 -0
- package/src/math/position.ts +23 -3
- package/src/orderParams.ts +151 -0
- package/src/orders.ts +236 -0
- package/src/types.ts +129 -1
- package/tsconfig.json +0 -1
- package/lib/accounts/bulkAccountLoader.d.ts.map +0 -1
- package/lib/accounts/bulkUserSubscription.d.ts.map +0 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/pollingUserAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/types.d.ts.map +0 -1
- package/lib/accounts/utils.d.ts.map +0 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts.map +0 -1
- package/lib/addresses.d.ts.map +0 -1
- package/lib/admin.d.ts.map +0 -1
- package/lib/assert/assert.d.ts.map +0 -1
- package/lib/clearingHouse.d.ts.map +0 -1
- package/lib/clearingHouseUser.d.ts.map +0 -1
- package/lib/config.d.ts.map +0 -1
- package/lib/constants/markets.d.ts.map +0 -1
- package/lib/constants/numericConstants.d.ts.map +0 -1
- package/lib/examples/makeTradeExample.d.ts.map +0 -1
- package/lib/factory/clearingHouse.d.ts.map +0 -1
- package/lib/factory/clearingHouseUser.d.ts.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/math/amm.d.ts.map +0 -1
- package/lib/math/conversion.d.ts.map +0 -1
- package/lib/math/funding.d.ts.map +0 -1
- package/lib/math/insuranceFund.d.ts.map +0 -1
- package/lib/math/market.d.ts.map +0 -1
- package/lib/math/position.d.ts.map +0 -1
- package/lib/math/trade.d.ts.map +0 -1
- package/lib/math/utils.d.ts.map +0 -1
- package/lib/mockUSDCFaucet.d.ts.map +0 -1
- package/lib/pythClient.d.ts.map +0 -1
- package/lib/token/index.d.ts.map +0 -1
- package/lib/tx/defaultTxSender.d.ts.map +0 -1
- package/lib/tx/types.d.ts.map +0 -1
- package/lib/tx/utils.d.ts.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/util/computeUnits.d.ts.map +0 -1
- package/lib/util/tps.d.ts.map +0 -1
- package/lib/wallet.d.ts.map +0 -1
package/lib/clearingHouse.js
CHANGED
|
@@ -101,6 +101,7 @@ class ClearingHouse {
|
|
|
101
101
|
'fundingRateHistoryAccount',
|
|
102
102
|
'liquidationHistoryAccount',
|
|
103
103
|
'tradeHistoryAccount',
|
|
104
|
+
'orderHistoryAccount',
|
|
104
105
|
]);
|
|
105
106
|
});
|
|
106
107
|
}
|
|
@@ -160,6 +161,21 @@ class ClearingHouse {
|
|
|
160
161
|
getCurveHistoryAccount() {
|
|
161
162
|
return this.accountSubscriber.getCurveHistoryAccount();
|
|
162
163
|
}
|
|
164
|
+
getOrderHistoryAccount() {
|
|
165
|
+
return this.accountSubscriber.getOrderHistoryAccount();
|
|
166
|
+
}
|
|
167
|
+
getOrderStatePublicKey() {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
if (this.orderStatePublicKey) {
|
|
170
|
+
return this.orderStatePublicKey;
|
|
171
|
+
}
|
|
172
|
+
this.orderStatePublicKey = yield addresses_1.getOrderStateAccountPublicKey(this.program.programId);
|
|
173
|
+
return this.orderStatePublicKey;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
getOrderStateAccount() {
|
|
177
|
+
return this.accountSubscriber.getOrderStateAccount();
|
|
178
|
+
}
|
|
163
179
|
/**
|
|
164
180
|
* Update the wallet to use for clearing house transactions and linked user account
|
|
165
181
|
* @param newWallet
|
|
@@ -177,15 +193,17 @@ class ClearingHouse {
|
|
|
177
193
|
}
|
|
178
194
|
initializeUserAccount() {
|
|
179
195
|
return __awaiter(this, void 0, void 0, function* () {
|
|
180
|
-
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx,] = yield this.getInitializeUserInstructions();
|
|
181
|
-
const tx = new web3_js_1.Transaction()
|
|
196
|
+
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx, initializeUserOrdersAccountIx,] = yield this.getInitializeUserInstructions();
|
|
197
|
+
const tx = new web3_js_1.Transaction()
|
|
198
|
+
.add(initializeUserAccountIx)
|
|
199
|
+
.add(initializeUserOrdersAccountIx);
|
|
182
200
|
const txSig = yield this.txSender.send(tx, [userPositionsAccount], this.opts);
|
|
183
201
|
return [txSig, userAccountPublicKey];
|
|
184
202
|
});
|
|
185
203
|
}
|
|
186
204
|
getInitializeUserInstructions() {
|
|
187
205
|
return __awaiter(this, void 0, void 0, function* () {
|
|
188
|
-
const [
|
|
206
|
+
const [userAccountPublicKey, userAccountNonce] = yield addresses_1.getUserAccountPublicKeyAndNonce(this.program.programId, this.wallet.publicKey);
|
|
189
207
|
const remainingAccounts = [];
|
|
190
208
|
const optionalAccounts = {
|
|
191
209
|
whitelistToken: false,
|
|
@@ -203,7 +221,7 @@ class ClearingHouse {
|
|
|
203
221
|
const userPositions = new web3_js_1.Keypair();
|
|
204
222
|
const initializeUserAccountIx = yield this.program.instruction.initializeUser(userAccountNonce, optionalAccounts, {
|
|
205
223
|
accounts: {
|
|
206
|
-
user:
|
|
224
|
+
user: userAccountPublicKey,
|
|
207
225
|
authority: this.wallet.publicKey,
|
|
208
226
|
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
209
227
|
systemProgram: anchor.web3.SystemProgram.programId,
|
|
@@ -212,7 +230,31 @@ class ClearingHouse {
|
|
|
212
230
|
},
|
|
213
231
|
remainingAccounts: remainingAccounts,
|
|
214
232
|
});
|
|
215
|
-
|
|
233
|
+
const initializeUserOrdersAccountIx = yield this.getInitializeUserOrdersInstruction(userAccountPublicKey);
|
|
234
|
+
return [
|
|
235
|
+
userPositions,
|
|
236
|
+
userAccountPublicKey,
|
|
237
|
+
initializeUserAccountIx,
|
|
238
|
+
initializeUserOrdersAccountIx,
|
|
239
|
+
];
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
getInitializeUserOrdersInstruction(userAccountPublicKey) {
|
|
243
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
244
|
+
if (!userAccountPublicKey) {
|
|
245
|
+
userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
246
|
+
}
|
|
247
|
+
const [userOrdersAccountPublicKey, userOrdersAccountNonce] = yield addresses_1.getUserOrdersAccountPublicKeyAndNonce(this.program.programId, userAccountPublicKey);
|
|
248
|
+
return yield this.program.instruction.initializeUserOrders(userOrdersAccountNonce, {
|
|
249
|
+
accounts: {
|
|
250
|
+
user: userAccountPublicKey,
|
|
251
|
+
authority: this.wallet.publicKey,
|
|
252
|
+
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
253
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
254
|
+
userOrders: userOrdersAccountPublicKey,
|
|
255
|
+
state: yield this.getStatePublicKey(),
|
|
256
|
+
},
|
|
257
|
+
});
|
|
216
258
|
});
|
|
217
259
|
}
|
|
218
260
|
/**
|
|
@@ -237,6 +279,29 @@ class ClearingHouse {
|
|
|
237
279
|
return this.userAccount;
|
|
238
280
|
});
|
|
239
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Get the address for the Clearing House User Order's account. NOT the user's wallet address.
|
|
284
|
+
* @returns
|
|
285
|
+
*/
|
|
286
|
+
getUserOrdersAccountPublicKey() {
|
|
287
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
288
|
+
if (this.userOrdersAccountPublicKey) {
|
|
289
|
+
return this.userOrdersAccountPublicKey;
|
|
290
|
+
}
|
|
291
|
+
this.userOrdersAccountPublicKey = yield addresses_1.getUserOrdersAccountPublicKey(this.program.programId, yield this.getUserAccountPublicKey());
|
|
292
|
+
return this.userOrdersAccountPublicKey;
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
userOrdersAccountExists() {
|
|
296
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
297
|
+
if (this.userOrdersExist) {
|
|
298
|
+
return this.userOrdersExist;
|
|
299
|
+
}
|
|
300
|
+
const userOrdersAccountRPCResponse = yield this.connection.getParsedAccountInfo(yield this.getUserOrdersAccountPublicKey());
|
|
301
|
+
this.userOrdersExist = userOrdersAccountRPCResponse.value !== null;
|
|
302
|
+
return this.userOrdersExist;
|
|
303
|
+
});
|
|
304
|
+
}
|
|
240
305
|
depositCollateral(amount, collateralAccountPublicKey, userPositionsAccountPublicKey) {
|
|
241
306
|
return __awaiter(this, void 0, void 0, function* () {
|
|
242
307
|
const depositCollateralIx = yield this.getDepositCollateralInstruction(amount, collateralAccountPublicKey, userPositionsAccountPublicKey);
|
|
@@ -275,10 +340,11 @@ class ClearingHouse {
|
|
|
275
340
|
*/
|
|
276
341
|
initializeUserAccountAndDepositCollateral(amount, collateralAccountPublicKey) {
|
|
277
342
|
return __awaiter(this, void 0, void 0, function* () {
|
|
278
|
-
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx,] = yield this.getInitializeUserInstructions();
|
|
343
|
+
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx, initializeUserOrdersAccountIx,] = yield this.getInitializeUserInstructions();
|
|
279
344
|
const depositCollateralIx = yield this.getDepositCollateralInstruction(amount, collateralAccountPublicKey, userPositionsAccount.publicKey);
|
|
280
345
|
const tx = new web3_js_1.Transaction()
|
|
281
346
|
.add(initializeUserAccountIx)
|
|
347
|
+
.add(initializeUserOrdersAccountIx)
|
|
282
348
|
.add(depositCollateralIx);
|
|
283
349
|
const txSig = yield this.program.provider.send(tx, [userPositionsAccount]);
|
|
284
350
|
return [txSig, userAccountPublicKey];
|
|
@@ -287,12 +353,13 @@ class ClearingHouse {
|
|
|
287
353
|
initializeUserAccountForDevnet(mockUSDCFaucet, amount) {
|
|
288
354
|
return __awaiter(this, void 0, void 0, function* () {
|
|
289
355
|
const [associateTokenPublicKey, createAssociatedAccountIx, mintToIx] = yield mockUSDCFaucet.createAssociatedTokenAccountAndMintToInstructions(this.wallet.publicKey, amount);
|
|
290
|
-
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx,] = yield this.getInitializeUserInstructions();
|
|
356
|
+
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx, initializeUserOrdersAccountIx,] = yield this.getInitializeUserInstructions();
|
|
291
357
|
const depositCollateralIx = yield this.getDepositCollateralInstruction(amount, associateTokenPublicKey, userPositionsAccount.publicKey);
|
|
292
358
|
const tx = new web3_js_1.Transaction()
|
|
293
359
|
.add(createAssociatedAccountIx)
|
|
294
360
|
.add(mintToIx)
|
|
295
361
|
.add(initializeUserAccountIx)
|
|
362
|
+
.add(initializeUserOrdersAccountIx)
|
|
296
363
|
.add(depositCollateralIx);
|
|
297
364
|
const txSig = yield this.program.provider.send(tx, [userPositionsAccount]);
|
|
298
365
|
return [txSig, userAccountPublicKey];
|
|
@@ -392,6 +459,239 @@ class ClearingHouse {
|
|
|
392
459
|
});
|
|
393
460
|
});
|
|
394
461
|
}
|
|
462
|
+
initializeUserOrdersThenPlaceOrder(orderParams, discountToken, referrer) {
|
|
463
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
464
|
+
const instructions = [];
|
|
465
|
+
const userOrdersAccountExists = yield this.userOrdersAccountExists();
|
|
466
|
+
if (!userOrdersAccountExists) {
|
|
467
|
+
instructions.push(yield this.getInitializeUserOrdersInstruction());
|
|
468
|
+
}
|
|
469
|
+
instructions.push(yield this.getPlaceOrderIx(orderParams, discountToken, referrer));
|
|
470
|
+
const tx = new web3_js_1.Transaction();
|
|
471
|
+
for (const instruction of instructions) {
|
|
472
|
+
tx.add(instruction);
|
|
473
|
+
}
|
|
474
|
+
return yield this.txSender.send(tx, [], this.opts);
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
placeOrder(orderParams, discountToken, referrer) {
|
|
478
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
479
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getPlaceOrderIx(orderParams, discountToken, referrer)), [], this.opts);
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
getPlaceOrderIx(orderParams, discountToken, referrer) {
|
|
483
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
484
|
+
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
485
|
+
const userAccount = yield this.getUserAccount();
|
|
486
|
+
const priceOracle = this.getMarketsAccount().markets[orderParams.marketIndex.toNumber()].amm
|
|
487
|
+
.oracle;
|
|
488
|
+
const remainingAccounts = [];
|
|
489
|
+
if (orderParams.optionalAccounts.discountToken) {
|
|
490
|
+
if (!discountToken) {
|
|
491
|
+
throw Error('Optional accounts specified discount token but no discount token present');
|
|
492
|
+
}
|
|
493
|
+
remainingAccounts.push({
|
|
494
|
+
pubkey: discountToken,
|
|
495
|
+
isWritable: false,
|
|
496
|
+
isSigner: false,
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
if (orderParams.optionalAccounts.referrer) {
|
|
500
|
+
if (!referrer) {
|
|
501
|
+
throw Error('Optional accounts specified referrer but no referrer present');
|
|
502
|
+
}
|
|
503
|
+
remainingAccounts.push({
|
|
504
|
+
pubkey: referrer,
|
|
505
|
+
isWritable: false,
|
|
506
|
+
isSigner: false,
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
const state = this.getStateAccount();
|
|
510
|
+
const orderState = this.getOrderStateAccount();
|
|
511
|
+
return yield this.program.instruction.placeOrder(orderParams, {
|
|
512
|
+
accounts: {
|
|
513
|
+
state: yield this.getStatePublicKey(),
|
|
514
|
+
user: userAccountPublicKey,
|
|
515
|
+
authority: this.wallet.publicKey,
|
|
516
|
+
markets: state.markets,
|
|
517
|
+
userOrders: yield this.getUserOrdersAccountPublicKey(),
|
|
518
|
+
userPositions: userAccount.positions,
|
|
519
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
520
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
521
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
522
|
+
orderHistory: orderState.orderHistory,
|
|
523
|
+
oracle: priceOracle,
|
|
524
|
+
},
|
|
525
|
+
remainingAccounts,
|
|
526
|
+
});
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
cancelOrder(orderId) {
|
|
530
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
531
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getCancelOrderIx(orderId)), [], this.opts);
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
getCancelOrderIx(orderId) {
|
|
535
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
536
|
+
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
537
|
+
const userAccount = yield this.getUserAccount();
|
|
538
|
+
const state = this.getStateAccount();
|
|
539
|
+
const orderState = this.getOrderStateAccount();
|
|
540
|
+
return yield this.program.instruction.cancelOrder(orderId, {
|
|
541
|
+
accounts: {
|
|
542
|
+
state: yield this.getStatePublicKey(),
|
|
543
|
+
user: userAccountPublicKey,
|
|
544
|
+
authority: this.wallet.publicKey,
|
|
545
|
+
markets: state.markets,
|
|
546
|
+
userOrders: yield this.getUserOrdersAccountPublicKey(),
|
|
547
|
+
userPositions: userAccount.positions,
|
|
548
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
549
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
550
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
551
|
+
orderHistory: orderState.orderHistory,
|
|
552
|
+
},
|
|
553
|
+
});
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
cancelOrderByUserId(userOrderId) {
|
|
557
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
558
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getCancelOrderByUserIdIx(userOrderId)), [], this.opts);
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
getCancelOrderByUserIdIx(userOrderId) {
|
|
562
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
563
|
+
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
564
|
+
const userAccount = yield this.getUserAccount();
|
|
565
|
+
const state = this.getStateAccount();
|
|
566
|
+
const orderState = this.getOrderStateAccount();
|
|
567
|
+
return yield this.program.instruction.cancelOrderByUserId(userOrderId, {
|
|
568
|
+
accounts: {
|
|
569
|
+
state: yield this.getStatePublicKey(),
|
|
570
|
+
user: userAccountPublicKey,
|
|
571
|
+
authority: this.wallet.publicKey,
|
|
572
|
+
markets: state.markets,
|
|
573
|
+
userOrders: yield this.getUserOrdersAccountPublicKey(),
|
|
574
|
+
userPositions: userAccount.positions,
|
|
575
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
576
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
577
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
578
|
+
orderHistory: orderState.orderHistory,
|
|
579
|
+
},
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
fillOrder(userAccountPublicKey, userOrdersAccountPublicKey, order) {
|
|
584
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
585
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getFillOrderIx(userAccountPublicKey, userOrdersAccountPublicKey, order)), [], this.opts);
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
getFillOrderIx(userAccountPublicKey, userOrdersAccountPublicKey, order) {
|
|
589
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
590
|
+
const fillerPublicKey = yield this.getUserAccountPublicKey();
|
|
591
|
+
const userAccount = yield this.program.account.user.fetch(userAccountPublicKey);
|
|
592
|
+
const marketIndex = order.marketIndex;
|
|
593
|
+
const oracle = this.getMarket(marketIndex).amm.oracle;
|
|
594
|
+
const state = this.getStateAccount();
|
|
595
|
+
const orderState = this.getOrderStateAccount();
|
|
596
|
+
const remainingAccounts = [];
|
|
597
|
+
if (!order.referrer.equals(web3_js_1.PublicKey.default)) {
|
|
598
|
+
remainingAccounts.push({
|
|
599
|
+
pubkey: order.referrer,
|
|
600
|
+
isWritable: true,
|
|
601
|
+
isSigner: false,
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
const orderId = order.orderId;
|
|
605
|
+
return yield this.program.instruction.fillOrder(orderId, {
|
|
606
|
+
accounts: {
|
|
607
|
+
state: yield this.getStatePublicKey(),
|
|
608
|
+
filler: fillerPublicKey,
|
|
609
|
+
user: userAccountPublicKey,
|
|
610
|
+
authority: this.wallet.publicKey,
|
|
611
|
+
markets: state.markets,
|
|
612
|
+
userPositions: userAccount.positions,
|
|
613
|
+
userOrders: userOrdersAccountPublicKey,
|
|
614
|
+
tradeHistory: state.tradeHistory,
|
|
615
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
616
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
617
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
618
|
+
orderHistory: orderState.orderHistory,
|
|
619
|
+
extendedCurveHistory: state.extendedCurveHistory,
|
|
620
|
+
oracle: oracle,
|
|
621
|
+
},
|
|
622
|
+
remainingAccounts,
|
|
623
|
+
});
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
initializeUserOrdersThenPlaceAndFillOrder(orderParams, discountToken, referrer) {
|
|
627
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
628
|
+
const instructions = [];
|
|
629
|
+
const userOrdersAccountExists = yield this.userOrdersAccountExists();
|
|
630
|
+
if (!userOrdersAccountExists) {
|
|
631
|
+
instructions.push(yield this.getInitializeUserOrdersInstruction());
|
|
632
|
+
}
|
|
633
|
+
instructions.push(yield this.getPlaceAndFillOrderIx(orderParams, discountToken, referrer));
|
|
634
|
+
const tx = new web3_js_1.Transaction();
|
|
635
|
+
for (const instruction of instructions) {
|
|
636
|
+
tx.add(instruction);
|
|
637
|
+
}
|
|
638
|
+
return yield this.txSender.send(tx, [], this.opts);
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
placeAndFillOrder(orderParams, discountToken, referrer) {
|
|
642
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
643
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getPlaceAndFillOrderIx(orderParams, discountToken, referrer)), [], this.opts);
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
getPlaceAndFillOrderIx(orderParams, discountToken, referrer) {
|
|
647
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
648
|
+
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
649
|
+
const userAccount = yield this.getUserAccount();
|
|
650
|
+
const priceOracle = this.getMarketsAccount().markets[orderParams.marketIndex.toNumber()].amm
|
|
651
|
+
.oracle;
|
|
652
|
+
const remainingAccounts = [];
|
|
653
|
+
if (orderParams.optionalAccounts.discountToken) {
|
|
654
|
+
if (!discountToken) {
|
|
655
|
+
throw Error('Optional accounts specified discount token but no discount token present');
|
|
656
|
+
}
|
|
657
|
+
remainingAccounts.push({
|
|
658
|
+
pubkey: discountToken,
|
|
659
|
+
isWritable: false,
|
|
660
|
+
isSigner: false,
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
if (orderParams.optionalAccounts.referrer) {
|
|
664
|
+
if (!referrer) {
|
|
665
|
+
throw Error('Optional accounts specified referrer but no referrer present');
|
|
666
|
+
}
|
|
667
|
+
remainingAccounts.push({
|
|
668
|
+
pubkey: referrer,
|
|
669
|
+
isWritable: true,
|
|
670
|
+
isSigner: false,
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
const state = this.getStateAccount();
|
|
674
|
+
const orderState = this.getOrderStateAccount();
|
|
675
|
+
return yield this.program.instruction.placeAndFillOrder(orderParams, {
|
|
676
|
+
accounts: {
|
|
677
|
+
state: yield this.getStatePublicKey(),
|
|
678
|
+
user: userAccountPublicKey,
|
|
679
|
+
authority: this.wallet.publicKey,
|
|
680
|
+
markets: state.markets,
|
|
681
|
+
userOrders: yield this.getUserOrdersAccountPublicKey(),
|
|
682
|
+
userPositions: userAccount.positions,
|
|
683
|
+
tradeHistory: state.tradeHistory,
|
|
684
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
685
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
686
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
687
|
+
orderHistory: orderState.orderHistory,
|
|
688
|
+
extendedCurveHistory: state.extendedCurveHistory,
|
|
689
|
+
oracle: priceOracle,
|
|
690
|
+
},
|
|
691
|
+
remainingAccounts,
|
|
692
|
+
});
|
|
693
|
+
});
|
|
694
|
+
}
|
|
395
695
|
/**
|
|
396
696
|
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
397
697
|
* @param marketIndex
|
|
@@ -4,7 +4,7 @@ import { PublicKey } from '@solana/web3.js';
|
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
5
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
6
6
|
import { ClearingHouse } from './clearingHouse';
|
|
7
|
-
import { UserAccount, UserPosition, UserPositionsAccount } from './types';
|
|
7
|
+
import { Order, UserAccount, UserOrdersAccount, UserPosition, UserPositionsAccount } from './types';
|
|
8
8
|
import { UserAccountSubscriber, UserAccountEvents } from './accounts/types';
|
|
9
9
|
import { PositionDirection, BN } from '.';
|
|
10
10
|
export declare class ClearingHouseUser {
|
|
@@ -12,6 +12,7 @@ export declare class ClearingHouseUser {
|
|
|
12
12
|
authority: PublicKey;
|
|
13
13
|
accountSubscriber: UserAccountSubscriber;
|
|
14
14
|
userAccountPublicKey?: PublicKey;
|
|
15
|
+
userOrdersAccountPublicKey?: PublicKey;
|
|
15
16
|
_isSubscribed: boolean;
|
|
16
17
|
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
17
18
|
get isSubscribed(): boolean;
|
|
@@ -36,6 +37,7 @@ export declare class ClearingHouseUser {
|
|
|
36
37
|
unsubscribe(): Promise<void>;
|
|
37
38
|
getUserAccount(): UserAccount;
|
|
38
39
|
getUserPositionsAccount(): UserPositionsAccount;
|
|
40
|
+
getUserOrdersAccount(): UserOrdersAccount | undefined;
|
|
39
41
|
/**
|
|
40
42
|
* Gets the user's current position for a given market. If the user has no position returns undefined
|
|
41
43
|
* @param marketIndex
|
|
@@ -43,7 +45,18 @@ export declare class ClearingHouseUser {
|
|
|
43
45
|
*/
|
|
44
46
|
getUserPosition(marketIndex: BN): UserPosition | undefined;
|
|
45
47
|
getEmptyPosition(marketIndex: BN): UserPosition;
|
|
48
|
+
/**
|
|
49
|
+
* @param orderId
|
|
50
|
+
* @returns Order
|
|
51
|
+
*/
|
|
52
|
+
getOrder(orderId: BN): Order | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* @param userOrderId
|
|
55
|
+
* @returns Order
|
|
56
|
+
*/
|
|
57
|
+
getOrderByUserOrderId(userOrderId: number): Order | undefined;
|
|
46
58
|
getUserAccountPublicKey(): Promise<PublicKey>;
|
|
59
|
+
getUserOrdersAccountPublicKey(): Promise<PublicKey>;
|
|
47
60
|
exists(): Promise<boolean>;
|
|
48
61
|
/**
|
|
49
62
|
* calculates Buying Power = FC * MAX_LEVERAGE
|
|
@@ -133,21 +146,6 @@ export declare class ClearingHouseUser {
|
|
|
133
146
|
liquidationPriceAfterClose(positionMarketIndex: BN, closeQuoteAmount: BN): BN;
|
|
134
147
|
/**
|
|
135
148
|
* Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
|
|
136
|
-
*
|
|
137
|
-
* To Calculate Max Quote Available:
|
|
138
|
-
*
|
|
139
|
-
* Case 1: SameSide
|
|
140
|
-
* => Remaining quote to get to maxLeverage
|
|
141
|
-
*
|
|
142
|
-
* Case 2: NOT SameSide && currentLeverage <= maxLeverage
|
|
143
|
-
* => Current opposite position x2 + remaining to get to maxLeverage
|
|
144
|
-
*
|
|
145
|
-
* Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
|
|
146
|
-
* => strictly reduce current position size
|
|
147
|
-
*
|
|
148
|
-
* Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
|
|
149
|
-
* => current position + remaining to get to maxLeverage
|
|
150
|
-
*
|
|
151
149
|
* @param marketIndex
|
|
152
150
|
* @param tradeSide
|
|
153
151
|
* @param userMaxLeverageSetting - leverage : Precision TEN_THOUSAND
|
|
@@ -174,5 +172,5 @@ export declare class ClearingHouseUser {
|
|
|
174
172
|
* @returns positionValue : Precision QUOTE_PRECISION
|
|
175
173
|
*/
|
|
176
174
|
private getTotalPositionValueExcludingMarket;
|
|
175
|
+
canFillOrder(order: Order): boolean;
|
|
177
176
|
}
|
|
178
|
-
//# sourceMappingURL=clearingHouseUser.d.ts.map
|