@drift-labs/sdk 0.2.0-master.38 → 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} +9 -4
- package/lib/{admin.js → adminClient.js} +62 -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} +20 -20
- package/lib/{clearingHouse.js → driftClient.js} +28 -28
- 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} +603 -119
- 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} +53 -45
- 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} +125 -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} +45 -47
- package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +3 -3
- package/src/examples/makeTradeExample.ts +15 -17
- package/src/idl/{clearing_house.json → drift.json} +603 -119
- 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} +59 -53
- 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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.LAMPORTS_EXP = void 0;
|
|
3
|
+
exports.BID_ASK_SPREAD_PRECISION = exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_DIV_PEG = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION_EXP = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_RATE_BUFFER_PRECISION = exports.FUNDING_RATE_PRECISION = exports.PRICE_PRECISION = exports.QUOTE_PRECISION = exports.LIQUIDATION_FEE_PRECISION = exports.SPOT_MARKET_IMF_PRECISION = exports.SPOT_MARKET_IMF_PRECISION_EXP = exports.SPOT_MARKET_BALANCE_PRECISION = exports.SPOT_MARKET_BALANCE_PRECISION_EXP = exports.SPOT_MARKET_WEIGHT_PRECISION = exports.SPOT_MARKET_UTILIZATION_PRECISION = exports.SPOT_MARKET_UTILIZATION_PRECISION_EXP = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP = exports.SPOT_MARKET_RATE_PRECISION = exports.SPOT_MARKET_RATE_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP = exports.PEG_PRECISION_EXP = exports.FUNDING_RATE_PRECISION_EXP = exports.PRICE_PRECISION_EXP = exports.FUNDING_RATE_BUFFER_PRECISION_EXP = exports.QUOTE_PRECISION_EXP = exports.CONCENTRATION_PRECISION = exports.PERCENTAGE_PRECISION = exports.PERCENTAGE_PRECISION_EXP = exports.MAX_LEVERAGE = exports.TEN_MILLION = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.NINE = exports.EIGHT = exports.SEVEN = exports.SIX = exports.FIVE = exports.FOUR = exports.THREE = exports.TWO = exports.ONE = exports.ZERO = void 0;
|
|
4
|
+
exports.LAMPORTS_EXP = exports.LAMPORTS_PRECISION = exports.QUOTE_SPOT_MARKET_INDEX = exports.ONE_YEAR = void 0;
|
|
5
5
|
const web3_js_1 = require("@solana/web3.js");
|
|
6
6
|
const __1 = require("../");
|
|
7
7
|
exports.ZERO = new __1.BN(0);
|
|
@@ -19,6 +19,9 @@ exports.TEN_THOUSAND = new __1.BN(10000);
|
|
|
19
19
|
exports.BN_MAX = new __1.BN(Number.MAX_SAFE_INTEGER);
|
|
20
20
|
exports.TEN_MILLION = exports.TEN_THOUSAND.mul(exports.TEN_THOUSAND);
|
|
21
21
|
exports.MAX_LEVERAGE = new __1.BN(5);
|
|
22
|
+
exports.PERCENTAGE_PRECISION_EXP = new __1.BN(6);
|
|
23
|
+
exports.PERCENTAGE_PRECISION = new __1.BN(10).pow(exports.PERCENTAGE_PRECISION_EXP);
|
|
24
|
+
exports.CONCENTRATION_PRECISION = exports.PERCENTAGE_PRECISION;
|
|
22
25
|
exports.QUOTE_PRECISION_EXP = new __1.BN(6);
|
|
23
26
|
exports.FUNDING_RATE_BUFFER_PRECISION_EXP = new __1.BN(3);
|
|
24
27
|
exports.PRICE_PRECISION_EXP = new __1.BN(6);
|
package/lib/dlob/DLOB.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { NodeList } from './NodeList';
|
|
3
|
-
import { MarketType, BN,
|
|
3
|
+
import { MarketType, BN, DriftClient, Order, SpotMarketAccount, PerpMarketAccount, OraclePriceData, SlotSubscriber, UserMapInterface, MarketTypeStr, StateAccount } from '..';
|
|
4
4
|
import { PublicKey } from '@solana/web3.js';
|
|
5
5
|
import { DLOBNode, TriggerOrderNode } from '..';
|
|
6
6
|
export declare type MarketNodeLists = {
|
|
@@ -77,6 +77,6 @@ export declare class DLOB {
|
|
|
77
77
|
getBestAsk(marketIndex: number, fallbackAsk: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): BN;
|
|
78
78
|
getBestBid(marketIndex: number, fallbackBid: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): BN;
|
|
79
79
|
findNodesToTrigger(marketIndex: number, slot: number, oraclePrice: BN, marketType: MarketType): NodeToTrigger[];
|
|
80
|
-
printTopOfOrderLists(sdkConfig: any,
|
|
80
|
+
printTopOfOrderLists(sdkConfig: any, driftClient: DriftClient, slotSubscriber: SlotSubscriber, marketIndex: number, marketType: MarketType): void;
|
|
81
81
|
}
|
|
82
82
|
export {};
|
package/lib/dlob/DLOB.js
CHANGED
|
@@ -649,11 +649,11 @@ class DLOB {
|
|
|
649
649
|
}
|
|
650
650
|
return nodesToTrigger;
|
|
651
651
|
}
|
|
652
|
-
printTopOfOrderLists(sdkConfig,
|
|
652
|
+
printTopOfOrderLists(sdkConfig, driftClient, slotSubscriber, marketIndex, marketType) {
|
|
653
653
|
if ((0, __1.isVariant)(marketType, 'perp')) {
|
|
654
|
-
const market =
|
|
654
|
+
const market = driftClient.getPerpMarketAccount(marketIndex);
|
|
655
655
|
const slot = slotSubscriber.getSlot();
|
|
656
|
-
const oraclePriceData =
|
|
656
|
+
const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
|
|
657
657
|
const fallbackAsk = (0, __1.calculateAskPrice)(market, oraclePriceData);
|
|
658
658
|
const fallbackBid = (0, __1.calculateBidPrice)(market, oraclePriceData);
|
|
659
659
|
const bestAsk = this.getBestAsk(marketIndex, fallbackAsk, slot, marketType, oraclePriceData);
|
|
@@ -674,7 +674,7 @@ class DLOB {
|
|
|
674
674
|
}
|
|
675
675
|
else if ((0, __1.isVariant)(marketType, 'spot')) {
|
|
676
676
|
const slot = slotSubscriber.getSlot();
|
|
677
|
-
const oraclePriceData =
|
|
677
|
+
const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
|
|
678
678
|
const bestAsk = this.getBestAsk(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
679
679
|
const bestBid = this.getBestBid(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
680
680
|
const mid = bestAsk.add(bestBid).div(new __1.BN(2));
|
|
@@ -7,13 +7,13 @@ import { Connection, PublicKey, TransactionSignature, ConfirmOptions, Transactio
|
|
|
7
7
|
import { TokenFaucet } from './tokenFaucet';
|
|
8
8
|
import { EventEmitter } from 'events';
|
|
9
9
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
10
|
-
import {
|
|
10
|
+
import { DriftClientAccountSubscriber, DriftClientAccountEvents, DataAndSlot } from './accounts/types';
|
|
11
11
|
import { TxSender } from './tx/types';
|
|
12
12
|
import { OraclePriceData } from './oracles/types';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
13
|
+
import { DriftClientConfig } from './driftClientConfig';
|
|
14
|
+
import { User } from './user';
|
|
15
|
+
import { UserSubscriptionConfig } from './userConfig';
|
|
16
|
+
import { UserStats } from './userStats';
|
|
17
17
|
declare type RemainingAccountParams = {
|
|
18
18
|
userAccounts: UserAccount[];
|
|
19
19
|
writablePerpMarketIndexes?: number[];
|
|
@@ -23,21 +23,21 @@ declare type RemainingAccountParams = {
|
|
|
23
23
|
useMarketLastSlotCache?: boolean;
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
|
-
* #
|
|
26
|
+
* # DriftClient
|
|
27
27
|
* 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.
|
|
28
28
|
*/
|
|
29
|
-
export declare class
|
|
29
|
+
export declare class DriftClient {
|
|
30
30
|
connection: Connection;
|
|
31
31
|
wallet: IWallet;
|
|
32
32
|
program: Program;
|
|
33
33
|
provider: AnchorProvider;
|
|
34
34
|
opts?: ConfirmOptions;
|
|
35
|
-
users: Map<number,
|
|
36
|
-
userStats?:
|
|
35
|
+
users: Map<number, User>;
|
|
36
|
+
userStats?: UserStats;
|
|
37
37
|
activeSubAccountId: number;
|
|
38
|
-
userAccountSubscriptionConfig:
|
|
39
|
-
accountSubscriber:
|
|
40
|
-
eventEmitter: StrictEventEmitter<EventEmitter,
|
|
38
|
+
userAccountSubscriptionConfig: UserSubscriptionConfig;
|
|
39
|
+
accountSubscriber: DriftClientAccountSubscriber;
|
|
40
|
+
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
|
|
41
41
|
_isSubscribed: boolean;
|
|
42
42
|
txSender: TxSender;
|
|
43
43
|
perpMarketLastSlotCache: Map<number, number>;
|
|
@@ -45,9 +45,9 @@ export declare class ClearingHouse {
|
|
|
45
45
|
authority: PublicKey;
|
|
46
46
|
get isSubscribed(): boolean;
|
|
47
47
|
set isSubscribed(val: boolean);
|
|
48
|
-
constructor(config:
|
|
49
|
-
createUsers(subAccountIds: number[], accountSubscriptionConfig:
|
|
50
|
-
createUser(subAccountId: number, accountSubscriptionConfig:
|
|
48
|
+
constructor(config: DriftClientConfig);
|
|
49
|
+
createUsers(subAccountIds: number[], accountSubscriptionConfig: UserSubscriptionConfig): void;
|
|
50
|
+
createUser(subAccountId: number, accountSubscriptionConfig: UserSubscriptionConfig): User;
|
|
51
51
|
subscribe(): Promise<boolean>;
|
|
52
52
|
subscribeUsers(): Promise<boolean>[];
|
|
53
53
|
/**
|
|
@@ -69,7 +69,7 @@ export declare class ClearingHouse {
|
|
|
69
69
|
getOraclePriceDataAndSlot(oraclePublicKey: PublicKey): DataAndSlot<OraclePriceData> | undefined;
|
|
70
70
|
getSerumV3FulfillmentConfig(serumMarket: PublicKey): Promise<SerumV3FulfillmentConfigAccount>;
|
|
71
71
|
/**
|
|
72
|
-
* Update the wallet to use for
|
|
72
|
+
* Update the wallet to use for drift transactions and linked user account
|
|
73
73
|
* @param newWallet
|
|
74
74
|
* @param subAccountIds
|
|
75
75
|
* @param activeSubAccountId
|
|
@@ -85,9 +85,9 @@ export declare class ClearingHouse {
|
|
|
85
85
|
updateUserDelegate(delegate: PublicKey, subAccountId?: number): Promise<TransactionSignature>;
|
|
86
86
|
getUserAccountsForDelegate(delegate: PublicKey): Promise<UserAccount[]>;
|
|
87
87
|
deleteUser(subAccountId?: number): Promise<TransactionSignature>;
|
|
88
|
-
getUser(subAccountId?: number):
|
|
89
|
-
getUsers():
|
|
90
|
-
getUserStats():
|
|
88
|
+
getUser(subAccountId?: number): User;
|
|
89
|
+
getUsers(): User[];
|
|
90
|
+
getUserStats(): UserStats;
|
|
91
91
|
userStatsAccountPublicKey: PublicKey;
|
|
92
92
|
getUserStatsAccountPublicKey(): PublicKey;
|
|
93
93
|
getUserAccountPublicKey(): Promise<PublicKey>;
|
|
@@ -217,7 +217,7 @@ export declare class ClearingHouse {
|
|
|
217
217
|
getUpdateFundingRateIx(perpMarketIndex: number, oracle: PublicKey): Promise<TransactionInstruction>;
|
|
218
218
|
settleFundingPayment(userAccountPublicKey: PublicKey): Promise<TransactionSignature>;
|
|
219
219
|
getSettleFundingPaymentIx(userAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
|
|
220
|
-
triggerEvent(eventName: keyof
|
|
220
|
+
triggerEvent(eventName: keyof DriftClientAccountEvents, data?: any): void;
|
|
221
221
|
getOracleDataForPerpMarket(marketIndex: number): OraclePriceData;
|
|
222
222
|
getOracleDataForSpotMarket(marketIndex: number): OraclePriceData;
|
|
223
223
|
initializeInsuranceFundStake(marketIndex: number): Promise<TransactionSignature>;
|
|
@@ -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,
|
|
@@ -1616,7 +1616,7 @@ class ClearingHouse {
|
|
|
1616
1616
|
* @returns
|
|
1617
1617
|
*/
|
|
1618
1618
|
async closePosition(marketIndex, limitPrice) {
|
|
1619
|
-
const userPosition = this.getUser().
|
|
1619
|
+
const userPosition = this.getUser().getPerpPosition(marketIndex);
|
|
1620
1620
|
if (!userPosition) {
|
|
1621
1621
|
throw Error(`No position in market ${marketIndex.toString()}`);
|
|
1622
1622
|
}
|
|
@@ -1841,7 +1841,7 @@ class ClearingHouse {
|
|
|
1841
1841
|
liquidatorStats: liquidatorStatsPublicKey,
|
|
1842
1842
|
spotMarketVault: spotMarket.vault,
|
|
1843
1843
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1844
|
-
|
|
1844
|
+
driftSigner: this.getSignerPublicKey(),
|
|
1845
1845
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1846
1846
|
},
|
|
1847
1847
|
remainingAccounts: remainingAccounts,
|
|
@@ -1870,7 +1870,7 @@ class ClearingHouse {
|
|
|
1870
1870
|
liquidator: liquidatorPublicKey,
|
|
1871
1871
|
spotMarketVault: spotMarket.vault,
|
|
1872
1872
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1873
|
-
|
|
1873
|
+
driftSigner: this.getSignerPublicKey(),
|
|
1874
1874
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1875
1875
|
},
|
|
1876
1876
|
remainingAccounts: remainingAccounts,
|
|
@@ -1963,7 +1963,7 @@ class ClearingHouse {
|
|
|
1963
1963
|
authority: this.wallet.publicKey,
|
|
1964
1964
|
spotMarketVault: spotMarket.vault,
|
|
1965
1965
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
1966
|
-
|
|
1966
|
+
driftSigner: this.getSignerPublicKey(),
|
|
1967
1967
|
userTokenAccount: collateralAccountPublicKey,
|
|
1968
1968
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1969
1969
|
},
|
|
@@ -2026,7 +2026,7 @@ class ClearingHouse {
|
|
|
2026
2026
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
2027
2027
|
authority: this.wallet.publicKey,
|
|
2028
2028
|
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
2029
|
-
|
|
2029
|
+
driftSigner: this.getSignerPublicKey(),
|
|
2030
2030
|
userTokenAccount: collateralAccountPublicKey,
|
|
2031
2031
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
2032
2032
|
},
|
|
@@ -2045,7 +2045,7 @@ class ClearingHouse {
|
|
|
2045
2045
|
state: await this.getStatePublicKey(),
|
|
2046
2046
|
spotMarket: spotMarketAccount.pubkey,
|
|
2047
2047
|
spotMarketVault: spotMarketAccount.vault,
|
|
2048
|
-
|
|
2048
|
+
driftSigner: this.getSignerPublicKey(),
|
|
2049
2049
|
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
2050
2050
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
2051
2051
|
},
|
|
@@ -2070,11 +2070,11 @@ class ClearingHouse {
|
|
|
2070
2070
|
authority: this.wallet.publicKey,
|
|
2071
2071
|
spotMarketVault: spotMarket.vault,
|
|
2072
2072
|
insuranceFundVault: spotMarket.insuranceFund.vault,
|
|
2073
|
-
|
|
2073
|
+
driftSigner: this.getSignerPublicKey(),
|
|
2074
2074
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
2075
2075
|
},
|
|
2076
2076
|
remainingAccounts: remainingAccounts,
|
|
2077
2077
|
});
|
|
2078
2078
|
}
|
|
2079
2079
|
}
|
|
2080
|
-
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();
|