@drift-labs/sdk 0.1.18-master.8 → 0.1.18-orders.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 +32 -0
- package/lib/accounts/bulkAccountLoader.js +156 -0
- package/lib/accounts/bulkUserSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserSubscription.js +28 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +49 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +228 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +25 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +32 -0
- package/lib/accounts/pollingUserAccountSubscriber.js +133 -0
- package/lib/accounts/types.d.ts +34 -3
- package/lib/accounts/utils.d.ts +1 -0
- package/lib/accounts/utils.js +7 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +0 -1
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +8 -3
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +30 -4
- package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +6 -3
- package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +16 -4
- package/lib/addresses.d.ts +4 -1
- package/lib/addresses.js +28 -1
- package/lib/admin.d.ts +11 -4
- package/lib/admin.js +71 -17
- package/lib/assert/assert.d.ts +0 -1
- package/lib/clearingHouse.d.ts +35 -4
- package/lib/clearingHouse.js +294 -23
- package/lib/clearingHouseUser.d.ts +19 -18
- package/lib/clearingHouseUser.js +129 -101
- package/lib/config.d.ts +0 -1
- package/lib/config.js +1 -1
- package/lib/constants/markets.d.ts +0 -1
- package/lib/constants/numericConstants.d.ts +0 -1
- package/lib/examples/makeTradeExample.d.ts +0 -1
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/factory/clearingHouse.d.ts +25 -0
- package/lib/factory/clearingHouse.js +64 -0
- package/lib/factory/clearingHouseUser.d.ts +19 -0
- package/lib/factory/clearingHouseUser.js +34 -0
- package/lib/idl/clearing_house.json +1077 -54
- package/lib/index.d.ts +11 -3
- package/lib/index.js +12 -2
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.js +38 -15
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +0 -1
- package/lib/math/funding.js +1 -1
- package/lib/math/insuranceFund.d.ts +0 -1
- package/lib/math/market.d.ts +2 -2
- package/lib/math/market.js +12 -2
- package/lib/math/orders.d.ts +3 -0
- package/lib/math/orders.js +30 -0
- package/lib/math/position.d.ts +4 -2
- package/lib/math/position.js +19 -5
- package/lib/math/trade.d.ts +0 -1
- package/lib/math/trade.js +16 -16
- package/lib/math/utils.d.ts +2 -3
- package/lib/math/utils.js +6 -3
- 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 +5 -0
- package/lib/orders.js +136 -0
- package/lib/pythClient.d.ts +0 -1
- package/lib/pythClient.js +1 -1
- package/lib/token/index.d.ts +3 -0
- package/lib/token/index.js +38 -0
- 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 +144 -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 +3 -3
- package/src/accounts/bulkAccountLoader.ts +195 -0
- package/src/accounts/bulkUserSubscription.ts +28 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +186 -0
- package/src/accounts/types.ts +43 -1
- package/src/accounts/utils.ts +3 -0
- package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +51 -1
- package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +33 -3
- package/src/addresses.ts +37 -0
- package/src/admin.ts +111 -24
- package/src/clearingHouse.ts +399 -22
- package/src/clearingHouseUser.ts +179 -107
- package/src/config.ts +1 -1
- package/src/factory/clearingHouse.ts +125 -0
- package/src/factory/clearingHouseUser.ts +73 -0
- package/src/idl/clearing_house.json +1077 -54
- package/src/index.ts +11 -2
- package/src/math/amm.ts +47 -14
- package/src/math/market.ts +28 -2
- package/src/math/orders.ts +39 -0
- package/src/math/position.ts +23 -3
- package/src/math/utils.ts +1 -1
- package/src/orderParams.ts +151 -0
- package/src/orders.ts +230 -0
- package/src/token/index.ts +37 -0
- package/src/types.ts +128 -1
- package/tsconfig.json +0 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/types.d.ts.map +0 -1
- package/lib/accounts/webSocketAccountSubscriber.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/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/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
|
@@ -38,19 +38,19 @@ const anchor = __importStar(require("@project-serum/anchor"));
|
|
|
38
38
|
const clearing_house_json_1 = __importDefault(require("./idl/clearing_house.json"));
|
|
39
39
|
const web3_js_1 = require("@solana/web3.js");
|
|
40
40
|
const addresses_1 = require("./addresses");
|
|
41
|
-
const defaultClearingHouseAccountSubscriber_1 = require("./accounts/defaultClearingHouseAccountSubscriber");
|
|
42
41
|
const defaultTxSender_1 = require("./tx/defaultTxSender");
|
|
43
42
|
const utils_1 = require("./tx/utils");
|
|
43
|
+
const clearingHouse_1 = require("./factory/clearingHouse");
|
|
44
44
|
/**
|
|
45
45
|
* # ClearingHouse
|
|
46
46
|
* This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
|
|
47
47
|
*
|
|
48
|
-
* The default way to construct a ClearingHouse instance is using the {@link from} method. This will create an instance using the static {@link
|
|
48
|
+
* The default way to construct a ClearingHouse instance is using the {@link from} method. This will create an instance using the static {@link WebSocketClearingHouseAccountSubscriber}, which will use a websocket for each state account subscription.
|
|
49
49
|
* Alternatively, if you want to implement your own method of subscribing to the state accounts on the blockchain, you can implement a {@link ClearingHouseAccountSubscriber} and use it in the {@link ClearingHouse.constructor}
|
|
50
50
|
*/
|
|
51
51
|
class ClearingHouse {
|
|
52
52
|
constructor(connection, wallet, program, accountSubscriber, txSender, opts) {
|
|
53
|
-
this.
|
|
53
|
+
this._isSubscribed = false;
|
|
54
54
|
this.connection = connection;
|
|
55
55
|
this.wallet = wallet;
|
|
56
56
|
this.opts = opts;
|
|
@@ -59,12 +59,23 @@ class ClearingHouse {
|
|
|
59
59
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
60
60
|
this.txSender = txSender;
|
|
61
61
|
}
|
|
62
|
+
get isSubscribed() {
|
|
63
|
+
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
64
|
+
}
|
|
65
|
+
set isSubscribed(val) {
|
|
66
|
+
this._isSubscribed = val;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @deprecated You should use the getClearingHouse factory method instead
|
|
70
|
+
* @param connection
|
|
71
|
+
* @param wallet
|
|
72
|
+
* @param clearingHouseProgramId
|
|
73
|
+
* @param opts
|
|
74
|
+
* @returns
|
|
75
|
+
*/
|
|
62
76
|
static from(connection, wallet, clearingHouseProgramId, opts = anchor_1.Provider.defaultOptions()) {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
const accountSubscriber = new defaultClearingHouseAccountSubscriber_1.DefaultClearingHouseAccountSubscriber(program);
|
|
66
|
-
const txSender = new defaultTxSender_1.DefaultTxSender(provider);
|
|
67
|
-
return new ClearingHouse(connection, wallet, program, accountSubscriber, txSender, opts);
|
|
77
|
+
const config = clearingHouse_1.getWebSocketClearingHouseConfig(connection, wallet, clearingHouseProgramId, opts);
|
|
78
|
+
return clearingHouse_1.getClearingHouse(config);
|
|
68
79
|
}
|
|
69
80
|
/**
|
|
70
81
|
*
|
|
@@ -90,6 +101,7 @@ class ClearingHouse {
|
|
|
90
101
|
'fundingRateHistoryAccount',
|
|
91
102
|
'liquidationHistoryAccount',
|
|
92
103
|
'tradeHistoryAccount',
|
|
104
|
+
'orderHistoryAccount',
|
|
93
105
|
]);
|
|
94
106
|
});
|
|
95
107
|
}
|
|
@@ -115,7 +127,7 @@ class ClearingHouse {
|
|
|
115
127
|
if (this.statePublicKey) {
|
|
116
128
|
return this.statePublicKey;
|
|
117
129
|
}
|
|
118
|
-
this.statePublicKey = yield
|
|
130
|
+
this.statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
119
131
|
return this.statePublicKey;
|
|
120
132
|
});
|
|
121
133
|
}
|
|
@@ -149,6 +161,21 @@ class ClearingHouse {
|
|
|
149
161
|
getCurveHistoryAccount() {
|
|
150
162
|
return this.accountSubscriber.getCurveHistoryAccount();
|
|
151
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
|
+
}
|
|
152
179
|
/**
|
|
153
180
|
* Update the wallet to use for clearing house transactions and linked user account
|
|
154
181
|
* @param newWallet
|
|
@@ -166,15 +193,17 @@ class ClearingHouse {
|
|
|
166
193
|
}
|
|
167
194
|
initializeUserAccount() {
|
|
168
195
|
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
-
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx,] = yield this.getInitializeUserInstructions();
|
|
170
|
-
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);
|
|
171
200
|
const txSig = yield this.txSender.send(tx, [userPositionsAccount], this.opts);
|
|
172
201
|
return [txSig, userAccountPublicKey];
|
|
173
202
|
});
|
|
174
203
|
}
|
|
175
204
|
getInitializeUserInstructions() {
|
|
176
205
|
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
-
const [
|
|
206
|
+
const [userAccountPublicKey, userAccountNonce] = yield addresses_1.getUserAccountPublicKeyAndNonce(this.program.programId, this.wallet.publicKey);
|
|
178
207
|
const remainingAccounts = [];
|
|
179
208
|
const optionalAccounts = {
|
|
180
209
|
whitelistToken: false,
|
|
@@ -192,7 +221,7 @@ class ClearingHouse {
|
|
|
192
221
|
const userPositions = new web3_js_1.Keypair();
|
|
193
222
|
const initializeUserAccountIx = yield this.program.instruction.initializeUser(userAccountNonce, optionalAccounts, {
|
|
194
223
|
accounts: {
|
|
195
|
-
user:
|
|
224
|
+
user: userAccountPublicKey,
|
|
196
225
|
authority: this.wallet.publicKey,
|
|
197
226
|
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
198
227
|
systemProgram: anchor.web3.SystemProgram.programId,
|
|
@@ -201,7 +230,31 @@ class ClearingHouse {
|
|
|
201
230
|
},
|
|
202
231
|
remainingAccounts: remainingAccounts,
|
|
203
232
|
});
|
|
204
|
-
|
|
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
|
+
});
|
|
205
258
|
});
|
|
206
259
|
}
|
|
207
260
|
/**
|
|
@@ -213,7 +266,7 @@ class ClearingHouse {
|
|
|
213
266
|
if (this.userAccountPublicKey) {
|
|
214
267
|
return this.userAccountPublicKey;
|
|
215
268
|
}
|
|
216
|
-
this.userAccountPublicKey = yield
|
|
269
|
+
this.userAccountPublicKey = yield addresses_1.getUserAccountPublicKey(this.program.programId, this.wallet.publicKey);
|
|
217
270
|
return this.userAccountPublicKey;
|
|
218
271
|
});
|
|
219
272
|
}
|
|
@@ -226,6 +279,19 @@ class ClearingHouse {
|
|
|
226
279
|
return this.userAccount;
|
|
227
280
|
});
|
|
228
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
|
+
}
|
|
229
295
|
depositCollateral(amount, collateralAccountPublicKey, userPositionsAccountPublicKey) {
|
|
230
296
|
return __awaiter(this, void 0, void 0, function* () {
|
|
231
297
|
const depositCollateralIx = yield this.getDepositCollateralInstruction(amount, collateralAccountPublicKey, userPositionsAccountPublicKey);
|
|
@@ -264,10 +330,11 @@ class ClearingHouse {
|
|
|
264
330
|
*/
|
|
265
331
|
initializeUserAccountAndDepositCollateral(amount, collateralAccountPublicKey) {
|
|
266
332
|
return __awaiter(this, void 0, void 0, function* () {
|
|
267
|
-
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx,] = yield this.getInitializeUserInstructions();
|
|
333
|
+
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx, initializeUserOrdersAccountIx,] = yield this.getInitializeUserInstructions();
|
|
268
334
|
const depositCollateralIx = yield this.getDepositCollateralInstruction(amount, collateralAccountPublicKey, userPositionsAccount.publicKey);
|
|
269
335
|
const tx = new web3_js_1.Transaction()
|
|
270
336
|
.add(initializeUserAccountIx)
|
|
337
|
+
.add(initializeUserOrdersAccountIx)
|
|
271
338
|
.add(depositCollateralIx);
|
|
272
339
|
const txSig = yield this.program.provider.send(tx, [userPositionsAccount]);
|
|
273
340
|
return [txSig, userAccountPublicKey];
|
|
@@ -276,12 +343,13 @@ class ClearingHouse {
|
|
|
276
343
|
initializeUserAccountForDevnet(mockUSDCFaucet, amount) {
|
|
277
344
|
return __awaiter(this, void 0, void 0, function* () {
|
|
278
345
|
const [associateTokenPublicKey, createAssociatedAccountIx, mintToIx] = yield mockUSDCFaucet.createAssociatedTokenAccountAndMintToInstructions(this.wallet.publicKey, amount);
|
|
279
|
-
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx,] = yield this.getInitializeUserInstructions();
|
|
346
|
+
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx, initializeUserOrdersAccountIx,] = yield this.getInitializeUserInstructions();
|
|
280
347
|
const depositCollateralIx = yield this.getDepositCollateralInstruction(amount, associateTokenPublicKey, userPositionsAccount.publicKey);
|
|
281
348
|
const tx = new web3_js_1.Transaction()
|
|
282
349
|
.add(createAssociatedAccountIx)
|
|
283
350
|
.add(mintToIx)
|
|
284
351
|
.add(initializeUserAccountIx)
|
|
352
|
+
.add(initializeUserOrdersAccountIx)
|
|
285
353
|
.add(depositCollateralIx);
|
|
286
354
|
const txSig = yield this.program.provider.send(tx, [userPositionsAccount]);
|
|
287
355
|
return [txSig, userAccountPublicKey];
|
|
@@ -303,7 +371,7 @@ class ClearingHouse {
|
|
|
303
371
|
}
|
|
304
372
|
withdrawCollateral(amount, collateralAccountPublicKey) {
|
|
305
373
|
return __awaiter(this, void 0, void 0, function* () {
|
|
306
|
-
return this.txSender.send(
|
|
374
|
+
return this.txSender.send(utils_1.wrapInTx(yield this.getWithdrawCollateralIx(amount, collateralAccountPublicKey)), [], this.opts);
|
|
307
375
|
});
|
|
308
376
|
}
|
|
309
377
|
getWithdrawCollateralIx(amount, collateralAccountPublicKey) {
|
|
@@ -332,7 +400,7 @@ class ClearingHouse {
|
|
|
332
400
|
}
|
|
333
401
|
openPosition(direction, amount, marketIndex, limitPrice, discountToken, referrer) {
|
|
334
402
|
return __awaiter(this, void 0, void 0, function* () {
|
|
335
|
-
return yield this.txSender.send(
|
|
403
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getOpenPositionIx(direction, amount, marketIndex, limitPrice, discountToken, referrer)), [], this.opts);
|
|
336
404
|
});
|
|
337
405
|
}
|
|
338
406
|
getOpenPositionIx(direction, amount, marketIndex, limitPrice, discountToken, referrer) {
|
|
@@ -381,6 +449,209 @@ class ClearingHouse {
|
|
|
381
449
|
});
|
|
382
450
|
});
|
|
383
451
|
}
|
|
452
|
+
placeOrder(orderParams, discountToken, referrer) {
|
|
453
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
454
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getPlaceOrderIx(orderParams, discountToken, referrer)), [], this.opts);
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
getPlaceOrderIx(orderParams, discountToken, referrer) {
|
|
458
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
459
|
+
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
460
|
+
const userAccount = yield this.getUserAccount();
|
|
461
|
+
const priceOracle = this.getMarketsAccount().markets[orderParams.marketIndex.toNumber()].amm
|
|
462
|
+
.oracle;
|
|
463
|
+
const remainingAccounts = [];
|
|
464
|
+
if (orderParams.optionalAccounts.discountToken) {
|
|
465
|
+
if (!discountToken) {
|
|
466
|
+
throw Error('Optional accounts specified discount token but no discount token present');
|
|
467
|
+
}
|
|
468
|
+
remainingAccounts.push({
|
|
469
|
+
pubkey: discountToken,
|
|
470
|
+
isWritable: false,
|
|
471
|
+
isSigner: false,
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
if (orderParams.optionalAccounts.referrer) {
|
|
475
|
+
if (!referrer) {
|
|
476
|
+
throw Error('Optional accounts specified referrer but no referrer present');
|
|
477
|
+
}
|
|
478
|
+
remainingAccounts.push({
|
|
479
|
+
pubkey: referrer,
|
|
480
|
+
isWritable: false,
|
|
481
|
+
isSigner: false,
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
const state = this.getStateAccount();
|
|
485
|
+
const orderState = this.getOrderStateAccount();
|
|
486
|
+
return yield this.program.instruction.placeOrder(orderParams, {
|
|
487
|
+
accounts: {
|
|
488
|
+
state: yield this.getStatePublicKey(),
|
|
489
|
+
user: userAccountPublicKey,
|
|
490
|
+
authority: this.wallet.publicKey,
|
|
491
|
+
markets: state.markets,
|
|
492
|
+
userOrders: yield this.getUserOrdersAccountPublicKey(),
|
|
493
|
+
userPositions: userAccount.positions,
|
|
494
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
495
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
496
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
497
|
+
orderHistory: orderState.orderHistory,
|
|
498
|
+
oracle: priceOracle,
|
|
499
|
+
},
|
|
500
|
+
remainingAccounts,
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
cancelOrder(orderId) {
|
|
505
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
506
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getCancelOrderIx(orderId)), [], this.opts);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
getCancelOrderIx(orderId) {
|
|
510
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
511
|
+
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
512
|
+
const userAccount = yield this.getUserAccount();
|
|
513
|
+
const state = this.getStateAccount();
|
|
514
|
+
const orderState = this.getOrderStateAccount();
|
|
515
|
+
return yield this.program.instruction.cancelOrder(orderId, {
|
|
516
|
+
accounts: {
|
|
517
|
+
state: yield this.getStatePublicKey(),
|
|
518
|
+
user: userAccountPublicKey,
|
|
519
|
+
authority: this.wallet.publicKey,
|
|
520
|
+
markets: state.markets,
|
|
521
|
+
userOrders: yield this.getUserOrdersAccountPublicKey(),
|
|
522
|
+
userPositions: userAccount.positions,
|
|
523
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
524
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
525
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
526
|
+
orderHistory: orderState.orderHistory,
|
|
527
|
+
},
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
cancelOrderByUserId(userOrderId) {
|
|
532
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
533
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getCancelOrderByUserIdIx(userOrderId)), [], this.opts);
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
getCancelOrderByUserIdIx(userOrderId) {
|
|
537
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
538
|
+
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
539
|
+
const userAccount = yield this.getUserAccount();
|
|
540
|
+
const state = this.getStateAccount();
|
|
541
|
+
const orderState = this.getOrderStateAccount();
|
|
542
|
+
return yield this.program.instruction.cancelOrderByUserId(userOrderId, {
|
|
543
|
+
accounts: {
|
|
544
|
+
state: yield this.getStatePublicKey(),
|
|
545
|
+
user: userAccountPublicKey,
|
|
546
|
+
authority: this.wallet.publicKey,
|
|
547
|
+
markets: state.markets,
|
|
548
|
+
userOrders: yield this.getUserOrdersAccountPublicKey(),
|
|
549
|
+
userPositions: userAccount.positions,
|
|
550
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
551
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
552
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
553
|
+
orderHistory: orderState.orderHistory,
|
|
554
|
+
},
|
|
555
|
+
});
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
fillOrder(userAccountPublicKey, userOrdersAccountPublicKey, order) {
|
|
559
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
560
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getFillOrderIx(userAccountPublicKey, userOrdersAccountPublicKey, order)), [], this.opts);
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
getFillOrderIx(userAccountPublicKey, userOrdersAccountPublicKey, order) {
|
|
564
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
565
|
+
const fillerPublicKey = yield this.getUserAccountPublicKey();
|
|
566
|
+
const userAccount = yield this.program.account.user.fetch(userAccountPublicKey);
|
|
567
|
+
const marketIndex = order.marketIndex;
|
|
568
|
+
const oracle = this.getMarket(marketIndex).amm.oracle;
|
|
569
|
+
const state = this.getStateAccount();
|
|
570
|
+
const orderState = this.getOrderStateAccount();
|
|
571
|
+
const remainingAccounts = [];
|
|
572
|
+
if (!order.referrer.equals(web3_js_1.PublicKey.default)) {
|
|
573
|
+
remainingAccounts.push({
|
|
574
|
+
pubkey: order.referrer,
|
|
575
|
+
isWritable: true,
|
|
576
|
+
isSigner: false,
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
const orderId = order.orderId;
|
|
580
|
+
return yield this.program.instruction.fillOrder(orderId, {
|
|
581
|
+
accounts: {
|
|
582
|
+
state: yield this.getStatePublicKey(),
|
|
583
|
+
filler: fillerPublicKey,
|
|
584
|
+
user: userAccountPublicKey,
|
|
585
|
+
authority: this.wallet.publicKey,
|
|
586
|
+
markets: state.markets,
|
|
587
|
+
userPositions: userAccount.positions,
|
|
588
|
+
userOrders: userOrdersAccountPublicKey,
|
|
589
|
+
tradeHistory: state.tradeHistory,
|
|
590
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
591
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
592
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
593
|
+
orderHistory: orderState.orderHistory,
|
|
594
|
+
extendedCurveHistory: state.extendedCurveHistory,
|
|
595
|
+
oracle: oracle,
|
|
596
|
+
},
|
|
597
|
+
remainingAccounts,
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
placeAndFillOrder(orderParams, discountToken, referrer) {
|
|
602
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
603
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getPlaceAndFillOrderIx(orderParams, discountToken, referrer)), [], this.opts);
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
getPlaceAndFillOrderIx(orderParams, discountToken, referrer) {
|
|
607
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
608
|
+
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
609
|
+
const userAccount = yield this.getUserAccount();
|
|
610
|
+
const priceOracle = this.getMarketsAccount().markets[orderParams.marketIndex.toNumber()].amm
|
|
611
|
+
.oracle;
|
|
612
|
+
const remainingAccounts = [];
|
|
613
|
+
if (orderParams.optionalAccounts.discountToken) {
|
|
614
|
+
if (!discountToken) {
|
|
615
|
+
throw Error('Optional accounts specified discount token but no discount token present');
|
|
616
|
+
}
|
|
617
|
+
remainingAccounts.push({
|
|
618
|
+
pubkey: discountToken,
|
|
619
|
+
isWritable: false,
|
|
620
|
+
isSigner: false,
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
if (orderParams.optionalAccounts.referrer) {
|
|
624
|
+
if (!referrer) {
|
|
625
|
+
throw Error('Optional accounts specified referrer but no referrer present');
|
|
626
|
+
}
|
|
627
|
+
remainingAccounts.push({
|
|
628
|
+
pubkey: referrer,
|
|
629
|
+
isWritable: true,
|
|
630
|
+
isSigner: false,
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
const state = this.getStateAccount();
|
|
634
|
+
const orderState = this.getOrderStateAccount();
|
|
635
|
+
return yield this.program.instruction.placeAndFillOrder(orderParams, {
|
|
636
|
+
accounts: {
|
|
637
|
+
state: yield this.getStatePublicKey(),
|
|
638
|
+
user: userAccountPublicKey,
|
|
639
|
+
authority: this.wallet.publicKey,
|
|
640
|
+
markets: state.markets,
|
|
641
|
+
userOrders: yield this.getUserOrdersAccountPublicKey(),
|
|
642
|
+
userPositions: userAccount.positions,
|
|
643
|
+
tradeHistory: state.tradeHistory,
|
|
644
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
645
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
646
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
647
|
+
orderHistory: orderState.orderHistory,
|
|
648
|
+
extendedCurveHistory: state.extendedCurveHistory,
|
|
649
|
+
oracle: priceOracle,
|
|
650
|
+
},
|
|
651
|
+
remainingAccounts,
|
|
652
|
+
});
|
|
653
|
+
});
|
|
654
|
+
}
|
|
384
655
|
/**
|
|
385
656
|
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
386
657
|
* @param marketIndex
|
|
@@ -390,7 +661,7 @@ class ClearingHouse {
|
|
|
390
661
|
*/
|
|
391
662
|
closePosition(marketIndex, discountToken, referrer) {
|
|
392
663
|
return __awaiter(this, void 0, void 0, function* () {
|
|
393
|
-
return yield this.txSender.send(
|
|
664
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getClosePositionIx(marketIndex, discountToken, referrer)), [], this.opts);
|
|
394
665
|
});
|
|
395
666
|
}
|
|
396
667
|
getClosePositionIx(marketIndex, discountToken, referrer) {
|
|
@@ -438,7 +709,7 @@ class ClearingHouse {
|
|
|
438
709
|
}
|
|
439
710
|
liquidate(liquidateeUserAccountPublicKey) {
|
|
440
711
|
return __awaiter(this, void 0, void 0, function* () {
|
|
441
|
-
return this.txSender.send(
|
|
712
|
+
return this.txSender.send(utils_1.wrapInTx(yield this.getLiquidateIx(liquidateeUserAccountPublicKey)), [], this.opts);
|
|
442
713
|
});
|
|
443
714
|
}
|
|
444
715
|
getLiquidateIx(liquidateeUserAccountPublicKey) {
|
|
@@ -482,7 +753,7 @@ class ClearingHouse {
|
|
|
482
753
|
}
|
|
483
754
|
updateFundingRate(oracle, marketIndex) {
|
|
484
755
|
return __awaiter(this, void 0, void 0, function* () {
|
|
485
|
-
return this.txSender.send(
|
|
756
|
+
return this.txSender.send(utils_1.wrapInTx(yield this.getUpdateFundingRateIx(oracle, marketIndex)), [], this.opts);
|
|
486
757
|
});
|
|
487
758
|
}
|
|
488
759
|
getUpdateFundingRateIx(oracle, marketIndex) {
|
|
@@ -500,7 +771,7 @@ class ClearingHouse {
|
|
|
500
771
|
}
|
|
501
772
|
settleFundingPayment(userAccount, userPositionsAccount) {
|
|
502
773
|
return __awaiter(this, void 0, void 0, function* () {
|
|
503
|
-
return this.txSender.send(
|
|
774
|
+
return this.txSender.send(utils_1.wrapInTx(yield this.getSettleFundingPaymentIx(userAccount, userPositionsAccount)), [], this.opts);
|
|
504
775
|
});
|
|
505
776
|
}
|
|
506
777
|
getSettleFundingPaymentIx(userAccount, userPositionsAccount) {
|
|
@@ -4,7 +4,7 @@ import BN from 'bn.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 } from '.';
|
|
10
10
|
export declare class ClearingHouseUser {
|
|
@@ -12,8 +12,17 @@ export declare class ClearingHouseUser {
|
|
|
12
12
|
authority: PublicKey;
|
|
13
13
|
accountSubscriber: UserAccountSubscriber;
|
|
14
14
|
userAccountPublicKey?: PublicKey;
|
|
15
|
-
|
|
15
|
+
userOrdersAccountPublicKey?: PublicKey;
|
|
16
|
+
_isSubscribed: boolean;
|
|
16
17
|
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
18
|
+
get isSubscribed(): boolean;
|
|
19
|
+
set isSubscribed(val: boolean);
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated You should use getClearingHouseUser factory method instead
|
|
22
|
+
* @param clearingHouse
|
|
23
|
+
* @param authority
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
17
26
|
static from(clearingHouse: ClearingHouse, authority: PublicKey): ClearingHouseUser;
|
|
18
27
|
constructor(clearingHouse: ClearingHouse, authority: PublicKey, accountSubscriber: UserAccountSubscriber);
|
|
19
28
|
/**
|
|
@@ -28,6 +37,7 @@ export declare class ClearingHouseUser {
|
|
|
28
37
|
unsubscribe(): Promise<void>;
|
|
29
38
|
getUserAccount(): UserAccount;
|
|
30
39
|
getUserPositionsAccount(): UserPositionsAccount;
|
|
40
|
+
getUserOrdersAccount(): UserOrdersAccount;
|
|
31
41
|
/**
|
|
32
42
|
* Gets the user's current position for a given market. If the user has no position returns undefined
|
|
33
43
|
* @param marketIndex
|
|
@@ -35,7 +45,13 @@ export declare class ClearingHouseUser {
|
|
|
35
45
|
*/
|
|
36
46
|
getUserPosition(marketIndex: BN): UserPosition | undefined;
|
|
37
47
|
getEmptyPosition(marketIndex: BN): UserPosition;
|
|
48
|
+
/**
|
|
49
|
+
* @param orderId
|
|
50
|
+
* @returns Order
|
|
51
|
+
*/
|
|
52
|
+
getOrder(orderId: BN): Order | undefined;
|
|
38
53
|
getUserAccountPublicKey(): Promise<PublicKey>;
|
|
54
|
+
getUserOrdersAccountPublicKey(): Promise<PublicKey>;
|
|
39
55
|
exists(): Promise<boolean>;
|
|
40
56
|
/**
|
|
41
57
|
* calculates Buying Power = FC * MAX_LEVERAGE
|
|
@@ -125,21 +141,6 @@ export declare class ClearingHouseUser {
|
|
|
125
141
|
liquidationPriceAfterClose(positionMarketIndex: BN, closeQuoteAmount: BN): BN;
|
|
126
142
|
/**
|
|
127
143
|
* Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
|
|
128
|
-
*
|
|
129
|
-
* To Calculate Max Quote Available:
|
|
130
|
-
*
|
|
131
|
-
* Case 1: SameSide
|
|
132
|
-
* => Remaining quote to get to maxLeverage
|
|
133
|
-
*
|
|
134
|
-
* Case 2: NOT SameSide && currentLeverage <= maxLeverage
|
|
135
|
-
* => Current opposite position x2 + remaining to get to maxLeverage
|
|
136
|
-
*
|
|
137
|
-
* Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
|
|
138
|
-
* => strictly reduce current position size
|
|
139
|
-
*
|
|
140
|
-
* Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
|
|
141
|
-
* => current position + remaining to get to maxLeverage
|
|
142
|
-
*
|
|
143
144
|
* @param marketIndex
|
|
144
145
|
* @param tradeSide
|
|
145
146
|
* @param userMaxLeverageSetting - leverage : Precision TEN_THOUSAND
|
|
@@ -166,5 +167,5 @@ export declare class ClearingHouseUser {
|
|
|
166
167
|
* @returns positionValue : Precision QUOTE_PRECISION
|
|
167
168
|
*/
|
|
168
169
|
private getTotalPositionValueExcludingMarket;
|
|
170
|
+
canFillOrder(order: Order): boolean;
|
|
169
171
|
}
|
|
170
|
-
//# sourceMappingURL=clearingHouseUser.d.ts.map
|