@drift-labs/sdk 2.12.0 → 2.13.0-beta.1
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/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +3 -1
- package/lib/driftClient.d.ts +9 -0
- package/lib/driftClient.js +49 -0
- package/lib/examples/loadDlob.d.ts +1 -0
- package/lib/examples/loadDlob.js +54 -0
- package/lib/idl/drift.json +1 -1
- package/lib/math/market.d.ts +5 -0
- package/lib/math/market.js +18 -1
- package/lib/math/oracles.d.ts +2 -2
- package/lib/math/oracles.js +20 -8
- package/lib/math/orders.js +1 -1
- package/lib/math/spotBalance.d.ts +5 -0
- package/lib/math/spotBalance.js +30 -3
- package/lib/math/trade.js +8 -2
- package/lib/user.d.ts +12 -6
- package/lib/user.js +124 -72
- package/package.json +1 -1
- package/src/constants/numericConstants.ts +2 -0
- package/src/driftClient.ts +64 -0
- package/src/examples/loadDlob.ts +82 -0
- package/src/idl/drift.json +1 -1
- package/src/math/market.ts +51 -0
- package/src/math/oracles.ts +30 -16
- package/src/math/orders.ts +2 -2
- package/src/math/spotBalance.ts +51 -3
- package/src/math/trade.ts +10 -2
- package/src/user.ts +267 -138
- package/tests/amm/test.ts +3 -2
- package/src/examples/makeTradeExample.js +0 -157
package/src/math/oracles.ts
CHANGED
|
@@ -6,8 +6,10 @@ import {
|
|
|
6
6
|
PRICE_PRECISION,
|
|
7
7
|
ONE,
|
|
8
8
|
ZERO,
|
|
9
|
+
FIVE_MINUTE,
|
|
10
|
+
ONE_HOUR,
|
|
9
11
|
} from '../constants/numericConstants';
|
|
10
|
-
import { BN, PerpMarketAccount } from '../index';
|
|
12
|
+
import { BN, HistoricalOracleData, PerpMarketAccount } from '../index';
|
|
11
13
|
import { assert } from '../assert/assert';
|
|
12
14
|
|
|
13
15
|
export function oraclePriceBands(
|
|
@@ -68,7 +70,7 @@ export function isOracleTooDivergent(
|
|
|
68
70
|
const sinceLastUpdate = now.sub(
|
|
69
71
|
amm.historicalOracleData.lastOraclePriceTwapTs
|
|
70
72
|
);
|
|
71
|
-
const sinceStart = BN.max(ZERO,
|
|
73
|
+
const sinceStart = BN.max(ZERO, FIVE_MINUTE.sub(sinceLastUpdate));
|
|
72
74
|
const oracleTwap5min = amm.historicalOracleData.lastOraclePriceTwap5Min
|
|
73
75
|
.mul(sinceStart)
|
|
74
76
|
.add(oraclePriceData.price)
|
|
@@ -90,29 +92,36 @@ export function isOracleTooDivergent(
|
|
|
90
92
|
}
|
|
91
93
|
|
|
92
94
|
export function calculateLiveOracleTwap(
|
|
93
|
-
|
|
95
|
+
histOracleData: HistoricalOracleData,
|
|
94
96
|
oraclePriceData: OraclePriceData,
|
|
95
|
-
now: BN
|
|
97
|
+
now: BN,
|
|
98
|
+
period: BN
|
|
96
99
|
): BN {
|
|
100
|
+
let oracleTwap = undefined;
|
|
101
|
+
if (period.eq(ONE_HOUR)) {
|
|
102
|
+
//todo: assumes its 1hr
|
|
103
|
+
// period = amm.fundingPeriod;
|
|
104
|
+
oracleTwap = histOracleData.lastOraclePriceTwap;
|
|
105
|
+
} else if (period.eq(FIVE_MINUTE)) {
|
|
106
|
+
histOracleData.lastOraclePriceTwap5Min;
|
|
107
|
+
} else {
|
|
108
|
+
throw Error('unsupported twap period passed');
|
|
109
|
+
}
|
|
110
|
+
|
|
97
111
|
const sinceLastUpdate = BN.max(
|
|
98
112
|
ONE,
|
|
99
|
-
now.sub(
|
|
113
|
+
now.sub(histOracleData.lastOraclePriceTwapTs)
|
|
100
114
|
);
|
|
101
|
-
const sinceStart = BN.max(ZERO,
|
|
115
|
+
const sinceStart = BN.max(ZERO, period.sub(sinceLastUpdate));
|
|
102
116
|
|
|
103
|
-
const clampRange =
|
|
104
|
-
new BN(3)
|
|
105
|
-
);
|
|
117
|
+
const clampRange = oracleTwap.div(new BN(3));
|
|
106
118
|
|
|
107
119
|
const clampedOraclePrice = BN.min(
|
|
108
|
-
|
|
109
|
-
BN.max(
|
|
110
|
-
oraclePriceData.price,
|
|
111
|
-
amm.historicalOracleData.lastOraclePriceTwap.sub(clampRange)
|
|
112
|
-
)
|
|
120
|
+
oracleTwap.add(clampRange),
|
|
121
|
+
BN.max(oraclePriceData.price, oracleTwap.sub(clampRange))
|
|
113
122
|
);
|
|
114
123
|
|
|
115
|
-
const newOracleTwap =
|
|
124
|
+
const newOracleTwap = oracleTwap
|
|
116
125
|
.mul(sinceStart)
|
|
117
126
|
.add(clampedOraclePrice.mul(sinceLastUpdate))
|
|
118
127
|
.div(sinceStart.add(sinceLastUpdate));
|
|
@@ -131,7 +140,12 @@ export function calculateLiveOracleStd(
|
|
|
131
140
|
);
|
|
132
141
|
const sinceStart = BN.max(ZERO, amm.fundingPeriod.sub(sinceLastUpdate));
|
|
133
142
|
|
|
134
|
-
const liveOracleTwap = calculateLiveOracleTwap(
|
|
143
|
+
const liveOracleTwap = calculateLiveOracleTwap(
|
|
144
|
+
amm.historicalOracleData,
|
|
145
|
+
oraclePriceData,
|
|
146
|
+
now,
|
|
147
|
+
amm.fundingPeriod
|
|
148
|
+
);
|
|
135
149
|
|
|
136
150
|
const priceDeltaVsTwap = oraclePriceData.price.sub(liveOracleTwap).abs();
|
|
137
151
|
|
package/src/math/orders.ts
CHANGED
|
@@ -165,12 +165,12 @@ export function isFillableByVAMM(
|
|
|
165
165
|
): boolean {
|
|
166
166
|
return (
|
|
167
167
|
(isAuctionComplete(order, slot) &&
|
|
168
|
-
|
|
168
|
+
calculateBaseAssetAmountForAmmToFulfill(
|
|
169
169
|
order,
|
|
170
170
|
market,
|
|
171
171
|
oraclePriceData,
|
|
172
172
|
slot
|
|
173
|
-
).
|
|
173
|
+
).gte(market.amm.minOrderSize)) ||
|
|
174
174
|
isOrderExpired(order, ts)
|
|
175
175
|
);
|
|
176
176
|
}
|
package/src/math/spotBalance.ts
CHANGED
|
@@ -67,6 +67,28 @@ export function getSignedTokenAmount(
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
export function getStrictTokenValue(
|
|
71
|
+
tokenAmount: BN,
|
|
72
|
+
spotDecimals: number,
|
|
73
|
+
oraclePriceData: OraclePriceData,
|
|
74
|
+
oraclePriceTwap: BN
|
|
75
|
+
): BN {
|
|
76
|
+
if (tokenAmount.eq(ZERO)) {
|
|
77
|
+
return ZERO;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let price = oraclePriceData.price;
|
|
81
|
+
if (tokenAmount.gt(ZERO)) {
|
|
82
|
+
price = BN.min(oraclePriceData.price, oraclePriceTwap);
|
|
83
|
+
} else {
|
|
84
|
+
price = BN.max(oraclePriceData.price, oraclePriceTwap);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const precisionDecrease = TEN.pow(new BN(spotDecimals));
|
|
88
|
+
|
|
89
|
+
return tokenAmount.mul(price).div(precisionDecrease);
|
|
90
|
+
}
|
|
91
|
+
|
|
70
92
|
export function getTokenValue(
|
|
71
93
|
tokenAmount: BN,
|
|
72
94
|
spotDecimals: number,
|
|
@@ -268,7 +290,14 @@ export function calculateInterestAccumulated(
|
|
|
268
290
|
export function calculateWithdrawLimit(
|
|
269
291
|
spotMarket: SpotMarketAccount,
|
|
270
292
|
now: BN
|
|
271
|
-
): {
|
|
293
|
+
): {
|
|
294
|
+
borrowLimit: BN;
|
|
295
|
+
withdrawLimit: BN;
|
|
296
|
+
minDepositAmount: BN;
|
|
297
|
+
maxBorrowAmount: BN;
|
|
298
|
+
currentDepositAmount;
|
|
299
|
+
currentBorrowAmount;
|
|
300
|
+
} {
|
|
272
301
|
const marketDepositTokenAmount = getTokenAmount(
|
|
273
302
|
spotMarket.depositBalance,
|
|
274
303
|
spotMarket,
|
|
@@ -311,8 +340,27 @@ export function calculateWithdrawLimit(
|
|
|
311
340
|
)
|
|
312
341
|
);
|
|
313
342
|
|
|
343
|
+
let withdrawLimit = BN.max(
|
|
344
|
+
marketDepositTokenAmount.sub(minDepositTokens),
|
|
345
|
+
ZERO
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
let borrowLimit = BN.max(maxBorrowTokens.sub(marketBorrowTokenAmount), ZERO);
|
|
349
|
+
|
|
350
|
+
if (borrowLimit.eq(ZERO)) {
|
|
351
|
+
withdrawLimit = ZERO;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (withdrawLimit.eq(ZERO)) {
|
|
355
|
+
borrowLimit = ZERO;
|
|
356
|
+
}
|
|
357
|
+
|
|
314
358
|
return {
|
|
315
|
-
borrowLimit
|
|
316
|
-
withdrawLimit
|
|
359
|
+
borrowLimit,
|
|
360
|
+
withdrawLimit,
|
|
361
|
+
maxBorrowAmount: maxBorrowTokens,
|
|
362
|
+
minDepositAmount: minDepositTokens,
|
|
363
|
+
currentDepositAmount: marketDepositTokenAmount,
|
|
364
|
+
currentBorrowAmount: marketBorrowTokenAmount,
|
|
317
365
|
};
|
|
318
366
|
}
|
package/src/math/trade.ts
CHANGED
|
@@ -529,7 +529,11 @@ export function calculateEstimatedPerpEntryPrice(
|
|
|
529
529
|
}
|
|
530
530
|
}
|
|
531
531
|
|
|
532
|
-
if (limitOrder
|
|
532
|
+
if (!limitOrder) {
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (usersToSkip.has(limitOrder.userAccount)) {
|
|
533
537
|
continue;
|
|
534
538
|
}
|
|
535
539
|
|
|
@@ -615,7 +619,11 @@ export function calculateEstimatedPerpEntryPrice(
|
|
|
615
619
|
}
|
|
616
620
|
}
|
|
617
621
|
|
|
618
|
-
if (limitOrder
|
|
622
|
+
if (!limitOrder) {
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
if (usersToSkip.has(limitOrder.userAccount)) {
|
|
619
627
|
continue;
|
|
620
628
|
}
|
|
621
629
|
|