@drift-labs/sdk 0.2.0-master.37 → 0.2.0-master.39
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/README.md +14 -14
- package/lib/accounts/bulkUserStatsSubscription.d.ts +2 -2
- package/lib/accounts/bulkUserSubscription.d.ts +2 -2
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +3 -8
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +6 -13
- package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
- package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
- package/lib/accounts/types.d.ts +4 -4
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.d.ts → webSocketDriftClientAccountSubscriber.d.ts} +3 -3
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +4 -4
- package/lib/addresses/pda.d.ts +3 -3
- package/lib/addresses/pda.js +10 -10
- package/lib/{admin.d.ts → adminClient.d.ts} +12 -4
- package/lib/{admin.js → adminClient.js} +89 -15
- package/lib/config.js +1 -1
- package/lib/constants/numericConstants.d.ts +3 -0
- package/lib/constants/numericConstants.js +5 -2
- package/lib/dlob/DLOB.d.ts +2 -2
- package/lib/dlob/DLOB.js +4 -4
- package/lib/{clearingHouse.d.ts → driftClient.d.ts} +21 -20
- package/lib/{clearingHouse.js → driftClient.js} +48 -37
- package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +3 -3
- package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
- package/lib/examples/makeTradeExample.js +13 -13
- package/lib/idl/{clearing_house.json → drift.json} +717 -143
- package/lib/index.d.ts +10 -10
- package/lib/index.js +10 -10
- package/lib/math/orders.d.ts +4 -4
- package/lib/math/orders.js +3 -3
- package/lib/serum/serumFulfillmentConfigMap.d.ts +3 -3
- package/lib/serum/serumFulfillmentConfigMap.js +3 -3
- package/lib/types.d.ts +38 -2
- package/lib/types.js +2 -1
- package/lib/{clearingHouseUser.d.ts → user.d.ts} +16 -10
- package/lib/{clearingHouseUser.js → user.js} +54 -46
- package/lib/userConfig.d.ts +14 -0
- package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
- package/lib/userMap/userMap.d.ts +17 -17
- package/lib/userMap/userMap.js +15 -15
- package/lib/userMap/userStatsMap.d.ts +7 -7
- package/lib/userMap/userStatsMap.js +10 -10
- package/lib/{clearingHouseUserStats.d.ts → userStats.d.ts} +5 -5
- package/lib/{clearingHouseUserStats.js → userStats.js} +8 -8
- package/lib/userStatsConfig.d.ts +14 -0
- package/lib/{clearingHouseUserStatsConfig.js → userStatsConfig.js} +0 -0
- package/package.json +1 -1
- package/src/accounts/bulkUserStatsSubscription.ts +2 -2
- package/src/accounts/bulkUserSubscription.ts +2 -2
- package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +11 -25
- package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
- package/src/accounts/types.ts +4 -4
- package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +7 -7
- package/src/addresses/pda.ts +6 -8
- package/src/{admin.ts → adminClient.ts} +185 -18
- package/src/config.ts +1 -1
- package/src/constants/numericConstants.ts +4 -0
- package/src/dlob/DLOB.ts +5 -5
- package/src/{clearingHouse.ts → driftClient.ts} +85 -68
- package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +3 -3
- package/src/examples/makeTradeExample.ts +15 -17
- package/src/idl/{clearing_house.json → drift.json} +717 -143
- package/src/index.ts +10 -10
- package/src/math/orders.ts +7 -13
- package/src/serum/serumFulfillmentConfigMap.ts +5 -5
- package/src/types.ts +41 -2
- package/src/{clearingHouseUser.ts → user.ts} +60 -54
- package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
- package/src/userMap/userMap.ts +29 -29
- package/src/userMap/userStatsMap.ts +21 -23
- package/src/{clearingHouseUserStats.ts → userStats.ts} +10 -10
- package/src/userStatsConfig.ts +18 -0
- package/tests/dlob/helpers.ts +55 -13
- package/lib/clearingHouseUserConfig.d.ts +0 -14
- package/lib/clearingHouseUserStatsConfig.d.ts +0 -14
- package/src/clearingHouseUserStatsConfig.ts +0 -18
|
@@ -26,13 +26,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.
|
|
29
|
+
exports.DriftClient = void 0;
|
|
30
30
|
const anchor_1 = require("@project-serum/anchor");
|
|
31
31
|
const bs58_1 = __importDefault(require("bs58"));
|
|
32
32
|
const spl_token_1 = require("@solana/spl-token");
|
|
33
33
|
const types_1 = require("./types");
|
|
34
34
|
const anchor = __importStar(require("@project-serum/anchor"));
|
|
35
|
-
const
|
|
35
|
+
const drift_json_1 = __importDefault(require("./idl/drift.json"));
|
|
36
36
|
const web3_js_1 = require("@solana/web3.js");
|
|
37
37
|
const pda_1 = require("./addresses/pda");
|
|
38
38
|
const utils_1 = require("./tx/utils");
|
|
@@ -40,19 +40,19 @@ const numericConstants_1 = require("./constants/numericConstants");
|
|
|
40
40
|
const position_1 = require("./math/position");
|
|
41
41
|
const spotBalance_1 = require("./math/spotBalance");
|
|
42
42
|
const userName_1 = require("./userName");
|
|
43
|
-
const
|
|
44
|
-
const
|
|
43
|
+
const pollingDriftClientAccountSubscriber_1 = require("./accounts/pollingDriftClientAccountSubscriber");
|
|
44
|
+
const webSocketDriftClientAccountSubscriber_1 = require("./accounts/webSocketDriftClientAccountSubscriber");
|
|
45
45
|
const retryTxSender_1 = require("./tx/retryTxSender");
|
|
46
|
-
const
|
|
46
|
+
const user_1 = require("./user");
|
|
47
47
|
const config_1 = require("./config");
|
|
48
48
|
const spotMarkets_1 = require("./constants/spotMarkets");
|
|
49
|
-
const
|
|
49
|
+
const userStats_1 = require("./userStats");
|
|
50
50
|
const spotPosition_1 = require("./math/spotPosition");
|
|
51
51
|
/**
|
|
52
|
-
* #
|
|
52
|
+
* # DriftClient
|
|
53
53
|
* 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.
|
|
54
54
|
*/
|
|
55
|
-
class
|
|
55
|
+
class DriftClient {
|
|
56
56
|
constructor(config) {
|
|
57
57
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
58
58
|
this.users = new Map();
|
|
@@ -63,7 +63,7 @@ class ClearingHouse {
|
|
|
63
63
|
this.wallet = config.wallet;
|
|
64
64
|
this.opts = config.opts || anchor_1.AnchorProvider.defaultOptions();
|
|
65
65
|
this.provider = new anchor_1.AnchorProvider(config.connection, config.wallet, this.opts);
|
|
66
|
-
this.program = new anchor_1.Program(
|
|
66
|
+
this.program = new anchor_1.Program(drift_json_1.default, config.programID, this.provider);
|
|
67
67
|
this.authority = (_a = config.authority) !== null && _a !== void 0 ? _a : this.wallet.publicKey;
|
|
68
68
|
const subAccountIds = (_b = config.subAccountIds) !== null && _b !== void 0 ? _b : [0];
|
|
69
69
|
this.activeSubAccountId = (_c = config.activeSubAccountId) !== null && _c !== void 0 ? _c : subAccountIds[0];
|
|
@@ -78,8 +78,8 @@ class ClearingHouse {
|
|
|
78
78
|
};
|
|
79
79
|
this.createUsers(subAccountIds, this.userAccountSubscriptionConfig);
|
|
80
80
|
if (config.userStats) {
|
|
81
|
-
this.userStats = new
|
|
82
|
-
|
|
81
|
+
this.userStats = new userStats_1.UserStats({
|
|
82
|
+
driftClient: this,
|
|
83
83
|
userStatsAccountPublicKey: (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.authority),
|
|
84
84
|
accountSubscription: this.userAccountSubscriptionConfig,
|
|
85
85
|
});
|
|
@@ -98,10 +98,10 @@ class ClearingHouse {
|
|
|
98
98
|
oracleInfos = oracleInfos ? oracleInfos : envOralceInfos;
|
|
99
99
|
}
|
|
100
100
|
if (((_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.type) === 'polling') {
|
|
101
|
-
this.accountSubscriber = new
|
|
101
|
+
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, perpMarketIndexes !== null && perpMarketIndexes !== void 0 ? perpMarketIndexes : [], spotMarketIndexes !== null && spotMarketIndexes !== void 0 ? spotMarketIndexes : [], oracleInfos !== null && oracleInfos !== void 0 ? oracleInfos : []);
|
|
102
102
|
}
|
|
103
103
|
else {
|
|
104
|
-
this.accountSubscriber = new
|
|
104
|
+
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_f = config.perpMarketIndexes) !== null && _f !== void 0 ? _f : [], (_g = config.spotMarketIndexes) !== null && _g !== void 0 ? _g : [], (_h = config.oracleInfos) !== null && _h !== void 0 ? _h : []);
|
|
105
105
|
}
|
|
106
106
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
107
107
|
this.txSender = new retryTxSender_1.RetryTxSender(this.provider, (_j = config.txSenderConfig) === null || _j === void 0 ? void 0 : _j.timeout, (_k = config.txSenderConfig) === null || _k === void 0 ? void 0 : _k.retrySleep, (_l = config.txSenderConfig) === null || _l === void 0 ? void 0 : _l.additionalConnections);
|
|
@@ -120,8 +120,8 @@ class ClearingHouse {
|
|
|
120
120
|
}
|
|
121
121
|
createUser(subAccountId, accountSubscriptionConfig) {
|
|
122
122
|
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.authority, subAccountId);
|
|
123
|
-
return new
|
|
124
|
-
|
|
123
|
+
return new user_1.User({
|
|
124
|
+
driftClient: this,
|
|
125
125
|
userAccountPublicKey,
|
|
126
126
|
accountSubscription: accountSubscriptionConfig,
|
|
127
127
|
});
|
|
@@ -164,14 +164,14 @@ class ClearingHouse {
|
|
|
164
164
|
if (this.statePublicKey) {
|
|
165
165
|
return this.statePublicKey;
|
|
166
166
|
}
|
|
167
|
-
this.statePublicKey = await (0, pda_1.
|
|
167
|
+
this.statePublicKey = await (0, pda_1.getDriftStateAccountPublicKey)(this.program.programId);
|
|
168
168
|
return this.statePublicKey;
|
|
169
169
|
}
|
|
170
170
|
getSignerPublicKey() {
|
|
171
171
|
if (this.signerPublicKey) {
|
|
172
172
|
return this.signerPublicKey;
|
|
173
173
|
}
|
|
174
|
-
this.signerPublicKey = (0, pda_1.
|
|
174
|
+
this.signerPublicKey = (0, pda_1.getDriftSignerPublicKey)(this.program.programId);
|
|
175
175
|
return this.signerPublicKey;
|
|
176
176
|
}
|
|
177
177
|
getStateAccount() {
|
|
@@ -205,14 +205,14 @@ class ClearingHouse {
|
|
|
205
205
|
return (await this.program.account.serumV3FulfillmentConfig.fetch(address));
|
|
206
206
|
}
|
|
207
207
|
/**
|
|
208
|
-
* Update the wallet to use for
|
|
208
|
+
* Update the wallet to use for drift transactions and linked user account
|
|
209
209
|
* @param newWallet
|
|
210
210
|
* @param subAccountIds
|
|
211
211
|
* @param activeSubAccountId
|
|
212
212
|
*/
|
|
213
213
|
async updateWallet(newWallet, subAccountIds = [0], activeSubAccountId = 0) {
|
|
214
214
|
const newProvider = new anchor_1.AnchorProvider(this.connection, newWallet, this.opts);
|
|
215
|
-
const newProgram = new anchor_1.Program(
|
|
215
|
+
const newProgram = new anchor_1.Program(drift_json_1.default, this.program.programId, newProvider);
|
|
216
216
|
// Update provider for txSender with new wallet details
|
|
217
217
|
this.txSender.provider = newProvider;
|
|
218
218
|
this.wallet = newWallet;
|
|
@@ -779,7 +779,7 @@ class ClearingHouse {
|
|
|
779
779
|
state: await this.getStatePublicKey(),
|
|
780
780
|
spotMarket: spotMarketAccount.pubkey,
|
|
781
781
|
spotMarketVault: spotMarketAccount.vault,
|
|
782
|
-
|
|
782
|
+
driftSigner: this.getSignerPublicKey(),
|
|
783
783
|
user: userAccountPublicKey,
|
|
784
784
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
785
785
|
userTokenAccount: userTokenAccount,
|
|
@@ -1271,9 +1271,7 @@ class ClearingHouse {
|
|
|
1271
1271
|
}
|
|
1272
1272
|
const orderId = order.orderId;
|
|
1273
1273
|
const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
|
|
1274
|
-
|
|
1275
|
-
this.addSerumRemainingAccounts(marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1276
|
-
}
|
|
1274
|
+
this.addSpotFulfillmentAccounts(marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1277
1275
|
return await this.program.instruction.fillSpotOrder(orderId, fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null, makerOrderId, {
|
|
1278
1276
|
accounts: {
|
|
1279
1277
|
state: await this.getStatePublicKey(),
|
|
@@ -1286,6 +1284,23 @@ class ClearingHouse {
|
|
|
1286
1284
|
remainingAccounts,
|
|
1287
1285
|
});
|
|
1288
1286
|
}
|
|
1287
|
+
addSpotFulfillmentAccounts(marketIndex, remainingAccounts, fulfillmentConfig) {
|
|
1288
|
+
if (fulfillmentConfig) {
|
|
1289
|
+
this.addSerumRemainingAccounts(marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1290
|
+
}
|
|
1291
|
+
else {
|
|
1292
|
+
remainingAccounts.push({
|
|
1293
|
+
pubkey: this.getSpotMarketAccount(marketIndex).vault,
|
|
1294
|
+
isWritable: false,
|
|
1295
|
+
isSigner: false,
|
|
1296
|
+
});
|
|
1297
|
+
remainingAccounts.push({
|
|
1298
|
+
pubkey: this.getQuoteSpotMarketAccount().vault,
|
|
1299
|
+
isWritable: false,
|
|
1300
|
+
isSigner: false,
|
|
1301
|
+
});
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1289
1304
|
addSerumRemainingAccounts(marketIndex, remainingAccounts, fulfillmentConfig) {
|
|
1290
1305
|
remainingAccounts.push({
|
|
1291
1306
|
pubkey: fulfillmentConfig.pubkey,
|
|
@@ -1540,9 +1555,7 @@ class ClearingHouse {
|
|
|
1540
1555
|
isSigner: false,
|
|
1541
1556
|
});
|
|
1542
1557
|
}
|
|
1543
|
-
|
|
1544
|
-
this.addSerumRemainingAccounts(orderParams.marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1545
|
-
}
|
|
1558
|
+
this.addSpotFulfillmentAccounts(orderParams.marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1546
1559
|
return await this.program.instruction.placeAndTakeSpotOrder(orderParams, fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null, makerOrderId, {
|
|
1547
1560
|
accounts: {
|
|
1548
1561
|
state: await this.getStatePublicKey(),
|
|
@@ -1583,9 +1596,7 @@ class ClearingHouse {
|
|
|
1583
1596
|
isSigner: false,
|
|
1584
1597
|
});
|
|
1585
1598
|
}
|
|
1586
|
-
|
|
1587
|
-
this.addSerumRemainingAccounts(orderParams.marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1588
|
-
}
|
|
1599
|
+
this.addSpotFulfillmentAccounts(orderParams.marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1589
1600
|
const takerOrderId = takerInfo.order.orderId;
|
|
1590
1601
|
return await this.program.instruction.placeAndMakeSpotOrder(orderParams, takerOrderId, fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null, {
|
|
1591
1602
|
accounts: {
|
|
@@ -1605,7 +1616,7 @@ class ClearingHouse {
|
|
|
1605
1616
|
* @returns
|
|
1606
1617
|
*/
|
|
1607
1618
|
async closePosition(marketIndex, limitPrice) {
|
|
1608
|
-
const userPosition = this.getUser().
|
|
1619
|
+
const userPosition = this.getUser().getPerpPosition(marketIndex);
|
|
1609
1620
|
if (!userPosition) {
|
|
1610
1621
|
throw Error(`No position in market ${marketIndex.toString()}`);
|
|
1611
1622
|
}
|
|
@@ -1830,7 +1841,7 @@ class ClearingHouse {
|
|
|
1830
1841
|
liquidatorStats: liquidatorStatsPublicKey,
|
|
1831
1842
|
spotMarketVault: spotMarket.vault,
|
|
1832
1843
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1833
|
-
|
|
1844
|
+
driftSigner: this.getSignerPublicKey(),
|
|
1834
1845
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1835
1846
|
},
|
|
1836
1847
|
remainingAccounts: remainingAccounts,
|
|
@@ -1859,7 +1870,7 @@ class ClearingHouse {
|
|
|
1859
1870
|
liquidator: liquidatorPublicKey,
|
|
1860
1871
|
spotMarketVault: spotMarket.vault,
|
|
1861
1872
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1862
|
-
|
|
1873
|
+
driftSigner: this.getSignerPublicKey(),
|
|
1863
1874
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1864
1875
|
},
|
|
1865
1876
|
remainingAccounts: remainingAccounts,
|
|
@@ -1952,7 +1963,7 @@ class ClearingHouse {
|
|
|
1952
1963
|
authority: this.wallet.publicKey,
|
|
1953
1964
|
spotMarketVault: spotMarket.vault,
|
|
1954
1965
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1955
|
-
|
|
1966
|
+
driftSigner: this.getSignerPublicKey(),
|
|
1956
1967
|
userTokenAccount: collateralAccountPublicKey,
|
|
1957
1968
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1958
1969
|
},
|
|
@@ -2015,7 +2026,7 @@ class ClearingHouse {
|
|
|
2015
2026
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
2016
2027
|
authority: this.wallet.publicKey,
|
|
2017
2028
|
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
2018
|
-
|
|
2029
|
+
driftSigner: this.getSignerPublicKey(),
|
|
2019
2030
|
userTokenAccount: collateralAccountPublicKey,
|
|
2020
2031
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
2021
2032
|
},
|
|
@@ -2034,7 +2045,7 @@ class ClearingHouse {
|
|
|
2034
2045
|
state: await this.getStatePublicKey(),
|
|
2035
2046
|
spotMarket: spotMarketAccount.pubkey,
|
|
2036
2047
|
spotMarketVault: spotMarketAccount.vault,
|
|
2037
|
-
|
|
2048
|
+
driftSigner: this.getSignerPublicKey(),
|
|
2038
2049
|
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
2039
2050
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
2040
2051
|
},
|
|
@@ -2059,11 +2070,11 @@ class ClearingHouse {
|
|
|
2059
2070
|
authority: this.wallet.publicKey,
|
|
2060
2071
|
spotMarketVault: spotMarket.vault,
|
|
2061
2072
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
2062
|
-
|
|
2073
|
+
driftSigner: this.getSignerPublicKey(),
|
|
2063
2074
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
2064
2075
|
},
|
|
2065
2076
|
remainingAccounts: remainingAccounts,
|
|
2066
2077
|
});
|
|
2067
2078
|
}
|
|
2068
2079
|
}
|
|
2069
|
-
exports.
|
|
2080
|
+
exports.DriftClient = DriftClient;
|
|
@@ -3,11 +3,11 @@ import { IWallet } from './types';
|
|
|
3
3
|
import { OracleInfo } from './oracles/types';
|
|
4
4
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
5
5
|
import { DriftEnv } from './config';
|
|
6
|
-
export declare type
|
|
6
|
+
export declare type DriftClientConfig = {
|
|
7
7
|
connection: Connection;
|
|
8
8
|
wallet: IWallet;
|
|
9
9
|
programID: PublicKey;
|
|
10
|
-
accountSubscription?:
|
|
10
|
+
accountSubscription?: DriftClientSubscriptionConfig;
|
|
11
11
|
opts?: ConfirmOptions;
|
|
12
12
|
txSenderConfig?: TxSenderConfig;
|
|
13
13
|
subAccountIds?: number[];
|
|
@@ -19,7 +19,7 @@ export declare type ClearingHouseConfig = {
|
|
|
19
19
|
userStats?: boolean;
|
|
20
20
|
authority?: PublicKey;
|
|
21
21
|
};
|
|
22
|
-
declare type
|
|
22
|
+
declare type DriftClientSubscriptionConfig = {
|
|
23
23
|
type: 'websocket';
|
|
24
24
|
} | {
|
|
25
25
|
type: 'polling';
|
|
File without changes
|
|
@@ -30,12 +30,12 @@ const main = async () => {
|
|
|
30
30
|
// Misc. other things to set up
|
|
31
31
|
const usdcTokenAddress = await (0, exports.getTokenAddress)(sdkConfig.USDC_MINT_ADDRESS, wallet.publicKey.toString());
|
|
32
32
|
// Set up the Drift Clearing House
|
|
33
|
-
const
|
|
33
|
+
const driftPublicKey = new web3_js_1.PublicKey(sdkConfig.CLEARING_HOUSE_PROGRAM_ID);
|
|
34
34
|
const bulkAccountLoader = new __2.BulkAccountLoader(connection, 'confirmed', 1000);
|
|
35
|
-
const
|
|
35
|
+
const driftClient = new __2.DriftClient({
|
|
36
36
|
connection,
|
|
37
37
|
wallet: provider.wallet,
|
|
38
|
-
programID:
|
|
38
|
+
programID: driftPublicKey,
|
|
39
39
|
perpMarketIndexes: __2.PerpMarkets[cluster].map((market) => market.marketIndex),
|
|
40
40
|
spotMarketIndexes: spotMarkets_1.SpotMarkets[cluster].map((spotMarket) => spotMarket.marketIndex),
|
|
41
41
|
accountSubscription: {
|
|
@@ -43,11 +43,11 @@ const main = async () => {
|
|
|
43
43
|
accountLoader: bulkAccountLoader,
|
|
44
44
|
},
|
|
45
45
|
});
|
|
46
|
-
await
|
|
46
|
+
await driftClient.subscribe();
|
|
47
47
|
// Set up Clearing House user client
|
|
48
|
-
const user = new __2.
|
|
49
|
-
|
|
50
|
-
userAccountPublicKey: await
|
|
48
|
+
const user = new __2.User({
|
|
49
|
+
driftClient: driftClient,
|
|
50
|
+
userAccountPublicKey: await driftClient.getUserAccountPublicKey(),
|
|
51
51
|
accountSubscription: {
|
|
52
52
|
type: 'polling',
|
|
53
53
|
accountLoader: bulkAccountLoader,
|
|
@@ -58,26 +58,26 @@ const main = async () => {
|
|
|
58
58
|
if (!userAccountExists) {
|
|
59
59
|
//// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
|
|
60
60
|
const depositAmount = new anchor_1.BN(10000).mul(__2.QUOTE_PRECISION);
|
|
61
|
-
await
|
|
61
|
+
await driftClient.initializeUserAccountAndDepositCollateral(depositAmount, await (0, exports.getTokenAddress)(usdcTokenAddress.toString(), wallet.publicKey.toString()), spotMarkets_1.SpotMarkets['devnet'][0].marketIndex);
|
|
62
62
|
}
|
|
63
63
|
await user.subscribe();
|
|
64
64
|
// Get current price
|
|
65
65
|
const solMarketInfo = sdkConfig.PERP_MARKETS.find((market) => market.baseAssetSymbol === 'SOL');
|
|
66
|
-
const currentMarketPrice = (0, __2.calculateReservePrice)(
|
|
66
|
+
const currentMarketPrice = (0, __2.calculateReservePrice)(driftClient.getPerpMarketAccount(solMarketInfo.marketIndex), undefined);
|
|
67
67
|
const formattedPrice = (0, __2.convertToNumber)(currentMarketPrice, __2.PRICE_PRECISION);
|
|
68
68
|
console.log(`Current Market Price is $${formattedPrice}`);
|
|
69
69
|
// Estimate the slippage for a $5000 LONG trade
|
|
70
|
-
const solMarketAccount =
|
|
70
|
+
const solMarketAccount = driftClient.getPerpMarketAccount(solMarketInfo.marketIndex);
|
|
71
71
|
const longAmount = new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION);
|
|
72
72
|
const slippage = (0, __2.convertToNumber)((0, __2.calculateTradeSlippage)(__2.PositionDirection.LONG, longAmount, solMarketAccount, 'quote', undefined)[0], __2.PRICE_PRECISION);
|
|
73
73
|
console.log(`Slippage for a $5000 LONG on the SOL market would be $${slippage}`);
|
|
74
74
|
// Make a $5000 LONG trade
|
|
75
|
-
await
|
|
75
|
+
await driftClient.openPosition(__2.PositionDirection.LONG, longAmount, solMarketInfo.marketIndex);
|
|
76
76
|
console.log(`LONGED $5000 SOL`);
|
|
77
77
|
// Reduce the position by $2000
|
|
78
78
|
const reduceAmount = new anchor_1.BN(2000).mul(__2.QUOTE_PRECISION);
|
|
79
|
-
await
|
|
79
|
+
await driftClient.openPosition(__2.PositionDirection.SHORT, reduceAmount, solMarketInfo.marketIndex);
|
|
80
80
|
// Close the rest of the position
|
|
81
|
-
await
|
|
81
|
+
await driftClient.closePosition(solMarketInfo.marketIndex);
|
|
82
82
|
};
|
|
83
83
|
main();
|