@drift-labs/sdk 0.1.19-master.1 → 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 +31 -0
- package/lib/accounts/bulkAccountLoader.js +177 -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 +136 -0
- package/lib/accounts/types.d.ts +38 -3
- package/lib/accounts/utils.d.ts +1 -0
- package/lib/accounts/utils.js +7 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -3
- package/lib/accounts/webSocketAccountSubscriber.js +43 -12
- 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 +10 -4
- package/lib/admin.js +54 -17
- package/lib/assert/assert.d.ts +0 -1
- package/lib/clearingHouse.d.ts +39 -4
- package/lib/clearingHouse.js +334 -23
- package/lib/clearingHouseUser.d.ts +26 -20
- package/lib/clearingHouseUser.js +149 -118
- package/lib/config.d.ts +0 -1
- package/lib/constants/markets.d.ts +2 -2
- package/lib/constants/markets.js +28 -15
- package/lib/constants/numericConstants.d.ts +4 -2
- package/lib/constants/numericConstants.js +16 -17
- 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 +1066 -39
- 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 +1 -2
- 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 +2 -2
- package/lib/math/insuranceFund.js +3 -6
- 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 +32 -0
- package/lib/math/position.d.ts +6 -3
- package/lib/math/position.js +21 -10
- package/lib/math/trade.d.ts +0 -1
- package/lib/math/trade.js +16 -16
- package/lib/math/utils.d.ts +2 -2
- package/lib/math/utils.js +3 -3
- package/lib/mockUSDCFaucet.d.ts +2 -2
- 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/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 +147 -3
- 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 +11 -3
- package/src/accounts/bulkAccountLoader.ts +215 -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 +194 -0
- package/src/accounts/types.ts +48 -1
- package/src/accounts/utils.ts +3 -0
- package/src/accounts/webSocketAccountSubscriber.ts +67 -17
- 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 +92 -24
- package/src/clearingHouse.ts +455 -22
- package/src/clearingHouseUser.ts +190 -108
- package/src/constants/markets.ts +17 -1
- package/src/constants/numericConstants.ts +3 -1
- package/src/examples/makeTradeExample.ts +4 -1
- package/src/factory/clearingHouse.ts +125 -0
- package/src/factory/clearingHouseUser.ts +73 -0
- package/src/idl/clearing_house.json +1066 -39
- package/src/index.ts +11 -2
- package/src/math/amm.ts +47 -14
- package/src/math/conversion.ts +1 -1
- package/src/math/insuranceFund.ts +1 -1
- package/src/math/market.ts +28 -2
- package/src/math/orders.ts +44 -0
- package/src/math/position.ts +24 -4
- package/src/math/utils.ts +1 -1
- package/src/mockUSDCFaucet.ts +1 -1
- package/src/orderParams.ts +151 -0
- package/src/orders.ts +236 -0
- package/src/token/index.ts +37 -0
- package/src/types.ts +130 -2
- 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,29 @@ 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
|
+
}
|
|
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
|
+
}
|
|
229
305
|
depositCollateral(amount, collateralAccountPublicKey, userPositionsAccountPublicKey) {
|
|
230
306
|
return __awaiter(this, void 0, void 0, function* () {
|
|
231
307
|
const depositCollateralIx = yield this.getDepositCollateralInstruction(amount, collateralAccountPublicKey, userPositionsAccountPublicKey);
|
|
@@ -264,10 +340,11 @@ class ClearingHouse {
|
|
|
264
340
|
*/
|
|
265
341
|
initializeUserAccountAndDepositCollateral(amount, collateralAccountPublicKey) {
|
|
266
342
|
return __awaiter(this, void 0, void 0, function* () {
|
|
267
|
-
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx,] = yield this.getInitializeUserInstructions();
|
|
343
|
+
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx, initializeUserOrdersAccountIx,] = yield this.getInitializeUserInstructions();
|
|
268
344
|
const depositCollateralIx = yield this.getDepositCollateralInstruction(amount, collateralAccountPublicKey, userPositionsAccount.publicKey);
|
|
269
345
|
const tx = new web3_js_1.Transaction()
|
|
270
346
|
.add(initializeUserAccountIx)
|
|
347
|
+
.add(initializeUserOrdersAccountIx)
|
|
271
348
|
.add(depositCollateralIx);
|
|
272
349
|
const txSig = yield this.program.provider.send(tx, [userPositionsAccount]);
|
|
273
350
|
return [txSig, userAccountPublicKey];
|
|
@@ -276,12 +353,13 @@ class ClearingHouse {
|
|
|
276
353
|
initializeUserAccountForDevnet(mockUSDCFaucet, amount) {
|
|
277
354
|
return __awaiter(this, void 0, void 0, function* () {
|
|
278
355
|
const [associateTokenPublicKey, createAssociatedAccountIx, mintToIx] = yield mockUSDCFaucet.createAssociatedTokenAccountAndMintToInstructions(this.wallet.publicKey, amount);
|
|
279
|
-
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx,] = yield this.getInitializeUserInstructions();
|
|
356
|
+
const [userPositionsAccount, userAccountPublicKey, initializeUserAccountIx, initializeUserOrdersAccountIx,] = yield this.getInitializeUserInstructions();
|
|
280
357
|
const depositCollateralIx = yield this.getDepositCollateralInstruction(amount, associateTokenPublicKey, userPositionsAccount.publicKey);
|
|
281
358
|
const tx = new web3_js_1.Transaction()
|
|
282
359
|
.add(createAssociatedAccountIx)
|
|
283
360
|
.add(mintToIx)
|
|
284
361
|
.add(initializeUserAccountIx)
|
|
362
|
+
.add(initializeUserOrdersAccountIx)
|
|
285
363
|
.add(depositCollateralIx);
|
|
286
364
|
const txSig = yield this.program.provider.send(tx, [userPositionsAccount]);
|
|
287
365
|
return [txSig, userAccountPublicKey];
|
|
@@ -303,7 +381,7 @@ class ClearingHouse {
|
|
|
303
381
|
}
|
|
304
382
|
withdrawCollateral(amount, collateralAccountPublicKey) {
|
|
305
383
|
return __awaiter(this, void 0, void 0, function* () {
|
|
306
|
-
return this.txSender.send(
|
|
384
|
+
return this.txSender.send(utils_1.wrapInTx(yield this.getWithdrawCollateralIx(amount, collateralAccountPublicKey)), [], this.opts);
|
|
307
385
|
});
|
|
308
386
|
}
|
|
309
387
|
getWithdrawCollateralIx(amount, collateralAccountPublicKey) {
|
|
@@ -332,7 +410,7 @@ class ClearingHouse {
|
|
|
332
410
|
}
|
|
333
411
|
openPosition(direction, amount, marketIndex, limitPrice, discountToken, referrer) {
|
|
334
412
|
return __awaiter(this, void 0, void 0, function* () {
|
|
335
|
-
return yield this.txSender.send(
|
|
413
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getOpenPositionIx(direction, amount, marketIndex, limitPrice, discountToken, referrer)), [], this.opts);
|
|
336
414
|
});
|
|
337
415
|
}
|
|
338
416
|
getOpenPositionIx(direction, amount, marketIndex, limitPrice, discountToken, referrer) {
|
|
@@ -381,6 +459,239 @@ class ClearingHouse {
|
|
|
381
459
|
});
|
|
382
460
|
});
|
|
383
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
|
+
}
|
|
384
695
|
/**
|
|
385
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.
|
|
386
697
|
* @param marketIndex
|
|
@@ -390,7 +701,7 @@ class ClearingHouse {
|
|
|
390
701
|
*/
|
|
391
702
|
closePosition(marketIndex, discountToken, referrer) {
|
|
392
703
|
return __awaiter(this, void 0, void 0, function* () {
|
|
393
|
-
return yield this.txSender.send(
|
|
704
|
+
return yield this.txSender.send(utils_1.wrapInTx(yield this.getClosePositionIx(marketIndex, discountToken, referrer)), [], this.opts);
|
|
394
705
|
});
|
|
395
706
|
}
|
|
396
707
|
getClosePositionIx(marketIndex, discountToken, referrer) {
|
|
@@ -438,7 +749,7 @@ class ClearingHouse {
|
|
|
438
749
|
}
|
|
439
750
|
liquidate(liquidateeUserAccountPublicKey) {
|
|
440
751
|
return __awaiter(this, void 0, void 0, function* () {
|
|
441
|
-
return this.txSender.send(
|
|
752
|
+
return this.txSender.send(utils_1.wrapInTx(yield this.getLiquidateIx(liquidateeUserAccountPublicKey)), [], this.opts);
|
|
442
753
|
});
|
|
443
754
|
}
|
|
444
755
|
getLiquidateIx(liquidateeUserAccountPublicKey) {
|
|
@@ -482,7 +793,7 @@ class ClearingHouse {
|
|
|
482
793
|
}
|
|
483
794
|
updateFundingRate(oracle, marketIndex) {
|
|
484
795
|
return __awaiter(this, void 0, void 0, function* () {
|
|
485
|
-
return this.txSender.send(
|
|
796
|
+
return this.txSender.send(utils_1.wrapInTx(yield this.getUpdateFundingRateIx(oracle, marketIndex)), [], this.opts);
|
|
486
797
|
});
|
|
487
798
|
}
|
|
488
799
|
getUpdateFundingRateIx(oracle, marketIndex) {
|
|
@@ -500,7 +811,7 @@ class ClearingHouse {
|
|
|
500
811
|
}
|
|
501
812
|
settleFundingPayment(userAccount, userPositionsAccount) {
|
|
502
813
|
return __awaiter(this, void 0, void 0, function* () {
|
|
503
|
-
return this.txSender.send(
|
|
814
|
+
return this.txSender.send(utils_1.wrapInTx(yield this.getSettleFundingPaymentIx(userAccount, userPositionsAccount)), [], this.opts);
|
|
504
815
|
});
|
|
505
816
|
}
|
|
506
817
|
getSettleFundingPaymentIx(userAccount, userPositionsAccount) {
|
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="bn.js" />
|
|
2
3
|
import { PublicKey } from '@solana/web3.js';
|
|
3
|
-
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
|
-
import { PositionDirection } from '.';
|
|
9
|
+
import { PositionDirection, BN } from '.';
|
|
10
10
|
export declare class ClearingHouseUser {
|
|
11
11
|
clearingHouse: ClearingHouse;
|
|
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 | undefined;
|
|
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,18 @@ 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;
|
|
53
|
+
/**
|
|
54
|
+
* @param userOrderId
|
|
55
|
+
* @returns Order
|
|
56
|
+
*/
|
|
57
|
+
getOrderByUserOrderId(userOrderId: number): Order | undefined;
|
|
38
58
|
getUserAccountPublicKey(): Promise<PublicKey>;
|
|
59
|
+
getUserOrdersAccountPublicKey(): Promise<PublicKey>;
|
|
39
60
|
exists(): Promise<boolean>;
|
|
40
61
|
/**
|
|
41
62
|
* calculates Buying Power = FC * MAX_LEVERAGE
|
|
@@ -125,21 +146,6 @@ export declare class ClearingHouseUser {
|
|
|
125
146
|
liquidationPriceAfterClose(positionMarketIndex: BN, closeQuoteAmount: BN): BN;
|
|
126
147
|
/**
|
|
127
148
|
* 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
149
|
* @param marketIndex
|
|
144
150
|
* @param tradeSide
|
|
145
151
|
* @param userMaxLeverageSetting - leverage : Precision TEN_THOUSAND
|
|
@@ -166,5 +172,5 @@ export declare class ClearingHouseUser {
|
|
|
166
172
|
* @returns positionValue : Precision QUOTE_PRECISION
|
|
167
173
|
*/
|
|
168
174
|
private getTotalPositionValueExcludingMarket;
|
|
175
|
+
canFillOrder(order: Order): boolean;
|
|
169
176
|
}
|
|
170
|
-
//# sourceMappingURL=clearingHouseUser.d.ts.map
|