@drift-labs/sdk 2.0.13 → 2.0.15
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 +3 -3
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +0 -3
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +0 -3
- package/lib/adminClient.d.ts +2 -0
- package/lib/adminClient.js +16 -0
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +3 -1
- package/lib/dlob/DLOB.d.ts +8 -8
- package/lib/dlob/DLOB.js +227 -246
- package/lib/driftClient.d.ts +19 -0
- package/lib/driftClient.js +32 -0
- package/lib/idl/drift.json +65 -7
- package/lib/math/amm.d.ts +3 -2
- package/lib/math/amm.js +61 -27
- package/lib/math/oracles.d.ts +2 -0
- package/lib/math/oracles.js +23 -1
- package/lib/math/orders.d.ts +1 -2
- package/lib/math/orders.js +4 -20
- package/lib/math/utils.d.ts +1 -0
- package/lib/math/utils.js +5 -1
- package/lib/types.d.ts +6 -2
- package/lib/user.js +10 -6
- package/package.json +1 -1
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +0 -4
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +0 -4
- package/src/adminClient.ts +28 -0
- package/src/constants/numericConstants.ts +3 -0
- package/src/dlob/DLOB.ts +444 -345
- package/src/driftClient.ts +41 -0
- package/src/idl/drift.json +65 -7
- package/src/math/amm.ts +126 -42
- package/src/math/oracles.ts +52 -0
- package/src/math/orders.ts +5 -22
- package/src/math/utils.ts +4 -0
- package/src/types.ts +6 -2
- package/src/user.ts +17 -6
- package/tests/dlob/helpers.ts +4 -2
- package/tests/dlob/test.ts +547 -63
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
<img height="120x" src="https://uploads-ssl.webflow.com/611580035ad59b20437eb024/616f97a42f5637c4517d0193_Logo%20(1)%20(1).png" />
|
|
3
3
|
|
|
4
|
-
<h1 style="margin-top:20px;">Drift Protocol
|
|
4
|
+
<h1 style="margin-top:20px;">Drift Protocol v2</h1>
|
|
5
5
|
|
|
6
6
|
<p>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@drift-labs/sdk"><img alt="SDK npm package" src="https://img.shields.io/npm/v/@drift-labs/sdk" /></a>
|
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
</p>
|
|
12
12
|
</div>
|
|
13
13
|
|
|
14
|
-
# Drift Protocol
|
|
14
|
+
# Drift Protocol v2
|
|
15
15
|
|
|
16
16
|
This repository provides open source access to Drift's Typescript SDK, Solana Programs, and more.
|
|
17
17
|
|
|
18
18
|
# SDK Guide
|
|
19
19
|
|
|
20
|
-
The technical documentation for the SDK can be found [here](https://drift-labs.github.io/protocol-
|
|
20
|
+
The technical documentation for the SDK can be found [here](https://drift-labs.github.io/protocol-v2/sdk/), and you can visit Drift's general purpose documentation [here](https://docs.drift.trade/sdk-documentation).
|
|
21
21
|
|
|
22
22
|
## Installation
|
|
23
23
|
|
|
@@ -201,9 +201,6 @@ class PollingDriftClientAccountSubscriber {
|
|
|
201
201
|
return false;
|
|
202
202
|
}
|
|
203
203
|
async unsubscribe() {
|
|
204
|
-
if (!this.isSubscribed) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
204
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
208
205
|
this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
|
|
209
206
|
}
|
|
@@ -83,9 +83,6 @@ class PollingUserStatsAccountSubscriber {
|
|
|
83
83
|
return success;
|
|
84
84
|
}
|
|
85
85
|
async unsubscribe() {
|
|
86
|
-
if (!this.isSubscribed) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
86
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
90
87
|
this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
|
|
91
88
|
}
|
package/lib/adminClient.d.ts
CHANGED
|
@@ -27,6 +27,8 @@ export declare class AdminClient extends DriftClient {
|
|
|
27
27
|
updatePerpMarketMaxSpread(perpMarketIndex: number, maxSpread: number): Promise<TransactionSignature>;
|
|
28
28
|
updatePerpFeeStructure(feeStructure: FeeStructure): Promise<TransactionSignature>;
|
|
29
29
|
updateSpotFeeStructure(feeStructure: FeeStructure): Promise<TransactionSignature>;
|
|
30
|
+
updateInitialPctToLiquidate(initialPctToLiquidate: number): Promise<TransactionSignature>;
|
|
31
|
+
updateLiquidationDuration(liquidationDuration: number): Promise<TransactionSignature>;
|
|
30
32
|
updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
|
|
31
33
|
updateStateSettlementDuration(settlementDuration: number): Promise<TransactionSignature>;
|
|
32
34
|
updateWithdrawGuardThreshold(spotMarketIndex: number, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
|
package/lib/adminClient.js
CHANGED
|
@@ -324,6 +324,22 @@ class AdminClient extends driftClient_1.DriftClient {
|
|
|
324
324
|
},
|
|
325
325
|
});
|
|
326
326
|
}
|
|
327
|
+
async updateInitialPctToLiquidate(initialPctToLiquidate) {
|
|
328
|
+
return await this.program.rpc.updateInitialPctToLiquidate(initialPctToLiquidate, {
|
|
329
|
+
accounts: {
|
|
330
|
+
admin: this.wallet.publicKey,
|
|
331
|
+
state: await this.getStatePublicKey(),
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
async updateLiquidationDuration(liquidationDuration) {
|
|
336
|
+
return await this.program.rpc.updateLiquidationDuration(liquidationDuration, {
|
|
337
|
+
accounts: {
|
|
338
|
+
admin: this.wallet.publicKey,
|
|
339
|
+
state: await this.getStatePublicKey(),
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
}
|
|
327
343
|
async updateOracleGuardRails(oracleGuardRails) {
|
|
328
344
|
return await this.program.rpc.updateOracleGuardRails(oracleGuardRails, {
|
|
329
345
|
accounts: {
|
|
@@ -50,7 +50,9 @@ export declare const PRICE_TO_QUOTE_PRECISION: BN;
|
|
|
50
50
|
export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: BN;
|
|
51
51
|
export declare const MARGIN_PRECISION: BN;
|
|
52
52
|
export declare const BID_ASK_SPREAD_PRECISION: BN;
|
|
53
|
+
export declare const LIQUIDATION_PCT_PRECISION: BN;
|
|
53
54
|
export declare const ONE_YEAR: BN;
|
|
54
55
|
export declare const QUOTE_SPOT_MARKET_INDEX = 0;
|
|
55
56
|
export declare const LAMPORTS_PRECISION: BN;
|
|
56
57
|
export declare const LAMPORTS_EXP: BN;
|
|
58
|
+
export declare const OPEN_ORDER_MARGIN_REQUIREMENT: BN;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
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;
|
|
4
|
+
exports.OPEN_ORDER_MARGIN_REQUIREMENT = exports.LAMPORTS_EXP = exports.LAMPORTS_PRECISION = exports.QUOTE_SPOT_MARKET_INDEX = exports.ONE_YEAR = exports.LIQUIDATION_PCT_PRECISION = 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);
|
|
@@ -54,7 +54,9 @@ exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_PRECISION.div(exports.QUOTE_PRE
|
|
|
54
54
|
exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.mul(exports.PEG_PRECISION).div(exports.QUOTE_PRECISION); // 10^9
|
|
55
55
|
exports.MARGIN_PRECISION = exports.TEN_THOUSAND;
|
|
56
56
|
exports.BID_ASK_SPREAD_PRECISION = new __1.BN(1000000); // 10^6
|
|
57
|
+
exports.LIQUIDATION_PCT_PRECISION = exports.TEN_THOUSAND;
|
|
57
58
|
exports.ONE_YEAR = new __1.BN(31536000);
|
|
58
59
|
exports.QUOTE_SPOT_MARKET_INDEX = 0;
|
|
59
60
|
exports.LAMPORTS_PRECISION = new __1.BN(web3_js_1.LAMPORTS_PER_SOL);
|
|
60
61
|
exports.LAMPORTS_EXP = new __1.BN(Math.log10(web3_js_1.LAMPORTS_PER_SOL));
|
|
62
|
+
exports.OPEN_ORDER_MARGIN_REQUIREMENT = exports.QUOTE_PRECISION.div(new __1.BN(100));
|
package/lib/dlob/DLOB.d.ts
CHANGED
|
@@ -29,7 +29,6 @@ export declare type NodeToFill = {
|
|
|
29
29
|
export declare type NodeToTrigger = {
|
|
30
30
|
node: TriggerOrderNode;
|
|
31
31
|
};
|
|
32
|
-
declare type Side = 'ask' | 'bid' | 'both' | 'nocross';
|
|
33
32
|
export declare class DLOB {
|
|
34
33
|
openOrders: Map<MarketTypeStr, Set<string>>;
|
|
35
34
|
orderLists: Map<MarketTypeStr, Map<number, MarketNodeLists>>;
|
|
@@ -57,22 +56,23 @@ export declare class DLOB {
|
|
|
57
56
|
trigger(order: Order, userAccount: PublicKey, onTrigger?: OrderBookCallback): void;
|
|
58
57
|
getListForOrder(order: Order): NodeList<any> | undefined;
|
|
59
58
|
findNodesToFill(marketIndex: number, fallbackBid: BN | undefined, fallbackAsk: BN | undefined, slot: number, ts: number, marketType: MarketType, oraclePriceData: OraclePriceData): NodeToFill[];
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
findLimitOrderNodesToFill(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, isAmmPaused: boolean, fallbackAsk: BN | undefined, fallbackBid: BN | undefined): NodeToFill[];
|
|
60
|
+
findMarketNodesToFill(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, isAmmPaused: boolean, fallbackAsk: BN | undefined, fallbackBid?: BN | undefined): NodeToFill[];
|
|
61
|
+
findMarketNodesCrossingLimitNodes(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, takerNodeGenerator: Generator<DLOBNode>, makerNodeGeneratorFn: (marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData) => Generator<DLOBNode>, doesCross: (takerPrice: BN | undefined, makerPrice: BN) => boolean): NodeToFill[];
|
|
62
|
+
findNodesCrossingFallbackLiquidity(marketType: MarketType, slot: number, oraclePriceData: OraclePriceData, nodeGenerator: Generator<DLOBNode>, fallbackPrice: BN, doesCross: (nodePrice: BN | undefined, fallbackPrice: BN) => boolean): NodeToFill[];
|
|
62
63
|
findExpiredNodesToFill(marketIndex: number, ts: number, marketType: MarketType): NodeToFill[];
|
|
63
64
|
findJitAuctionNodesToFill(marketIndex: number, slot: number, marketType: MarketType): NodeToFill[];
|
|
64
65
|
getMarketBids(marketIndex: number, marketType: MarketType): Generator<DLOBNode>;
|
|
65
66
|
getMarketAsks(marketIndex: number, marketType: MarketType): Generator<DLOBNode>;
|
|
67
|
+
private getBestNode;
|
|
68
|
+
getLimitAsks(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
69
|
+
getLimitBids(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
66
70
|
getAsks(marketIndex: number, fallbackAsk: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
67
71
|
getBids(marketIndex: number, fallbackBid: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
68
|
-
|
|
69
|
-
crossingNodes: NodeToFill[];
|
|
70
|
-
exhaustedSide: Side;
|
|
71
|
-
};
|
|
72
|
+
findCrossingLimitOrders(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): NodeToFill[];
|
|
72
73
|
determineMakerAndTaker(askNode: DLOBNode, bidNode: DLOBNode): {
|
|
73
74
|
takerNode: DLOBNode;
|
|
74
75
|
makerNode: DLOBNode;
|
|
75
|
-
makerSide: Side;
|
|
76
76
|
};
|
|
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;
|