@drift-labs/sdk 0.1.34-master.2 → 0.1.34-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/math/funding.d.ts +4 -4
- package/lib/math/funding.js +24 -20
- package/package.json +1 -1
- package/src/math/funding.ts +34 -29
package/lib/math/funding.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
9
9
|
* @param periodAdjustment
|
|
10
10
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
11
11
|
*/
|
|
12
|
-
export declare function calculateAllEstimatedFundingRate(market: Market, oraclePriceData
|
|
12
|
+
export declare function calculateAllEstimatedFundingRate(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN, BN]>;
|
|
13
13
|
/**
|
|
14
14
|
*
|
|
15
15
|
* @param market
|
|
@@ -18,7 +18,7 @@ export declare function calculateAllEstimatedFundingRate(market: Market, oracleP
|
|
|
18
18
|
* @param estimationMethod
|
|
19
19
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
20
20
|
*/
|
|
21
|
-
export declare function calculateEstimatedFundingRate(market: Market, oraclePriceData
|
|
21
|
+
export declare function calculateEstimatedFundingRate(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN, estimationMethod?: 'interpolated' | 'lowerbound' | 'capped'): Promise<BN>;
|
|
22
22
|
/**
|
|
23
23
|
*
|
|
24
24
|
* @param market
|
|
@@ -26,7 +26,7 @@ export declare function calculateEstimatedFundingRate(market: Market, oraclePric
|
|
|
26
26
|
* @param periodAdjustment
|
|
27
27
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
28
28
|
*/
|
|
29
|
-
export declare function calculateLongShortFundingRate(market: Market, oraclePriceData
|
|
29
|
+
export declare function calculateLongShortFundingRate(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN]>;
|
|
30
30
|
/**
|
|
31
31
|
*
|
|
32
32
|
* @param market
|
|
@@ -34,7 +34,7 @@ export declare function calculateLongShortFundingRate(market: Market, oraclePric
|
|
|
34
34
|
* @param periodAdjustment
|
|
35
35
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
36
36
|
*/
|
|
37
|
-
export declare function calculateLongShortFundingRateAndLiveTwaps(market: Market, oraclePriceData
|
|
37
|
+
export declare function calculateLongShortFundingRateAndLiveTwaps(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN]>;
|
|
38
38
|
/**
|
|
39
39
|
*
|
|
40
40
|
* @param market
|
package/lib/math/funding.js
CHANGED
|
@@ -51,30 +51,34 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
|
|
|
51
51
|
const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
52
52
|
const oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
|
|
53
53
|
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
54
|
-
const oracleTwapTimeSinceLastUpdate = anchor_1.BN.
|
|
55
|
-
const oraclePrice = oraclePriceData.price;
|
|
54
|
+
const oracleTwapTimeSinceLastUpdate = anchor_1.BN.min(secondsInHour, anchor_1.BN.max(numericConstants_1.ZERO, secondsInHour.sub(timeSinceLastOracleTwapUpdate)));
|
|
56
55
|
let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
56
|
+
// if passing live oracle data, improve predicted calc estimate
|
|
57
|
+
if (oraclePriceData) {
|
|
58
|
+
const oraclePrice = oraclePriceData.price;
|
|
59
|
+
const oracleLiveVsTwap = oraclePrice
|
|
60
|
+
.sub(lastOracleTwapWithMantissa)
|
|
61
|
+
.abs()
|
|
62
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
63
|
+
.mul(new anchor_1.BN(100))
|
|
64
|
+
.div(lastOracleTwapWithMantissa);
|
|
65
|
+
// verify pyth live input is within 10% of last twap for live update
|
|
66
|
+
if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
|
|
67
|
+
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
68
|
+
.mul(lastOracleTwapWithMantissa)
|
|
69
|
+
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
70
|
+
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
|
|
74
|
+
.mul(lastOracleTwapWithMantissa)
|
|
75
|
+
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
76
|
+
.div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
|
|
77
|
+
const twapSpread = lastMarkTwapWithMantissa.sub(shrunkLastOracleTwapwithMantissa);
|
|
74
78
|
const twapSpreadPct = twapSpread
|
|
75
79
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
76
80
|
.mul(new anchor_1.BN(100))
|
|
77
|
-
.div(
|
|
81
|
+
.div(shrunkLastOracleTwapwithMantissa);
|
|
78
82
|
const lowerboundEst = twapSpreadPct
|
|
79
83
|
.mul(payFreq)
|
|
80
84
|
.mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
|
package/package.json
CHANGED
package/src/math/funding.ts
CHANGED
|
@@ -18,7 +18,7 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
18
18
|
*/
|
|
19
19
|
export async function calculateAllEstimatedFundingRate(
|
|
20
20
|
market: Market,
|
|
21
|
-
oraclePriceData
|
|
21
|
+
oraclePriceData?: OraclePriceData,
|
|
22
22
|
periodAdjustment: BN = new BN(1)
|
|
23
23
|
): Promise<[BN, BN, BN, BN, BN]> {
|
|
24
24
|
// periodAdjustment
|
|
@@ -65,40 +65,45 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
65
65
|
);
|
|
66
66
|
|
|
67
67
|
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
68
|
-
const oracleTwapTimeSinceLastUpdate = BN.
|
|
68
|
+
const oracleTwapTimeSinceLastUpdate = BN.min(
|
|
69
69
|
secondsInHour,
|
|
70
|
-
secondsInHour.sub(timeSinceLastOracleTwapUpdate)
|
|
70
|
+
BN.max(ZERO, secondsInHour.sub(timeSinceLastOracleTwapUpdate))
|
|
71
71
|
);
|
|
72
|
-
|
|
73
|
-
const oraclePrice = oraclePriceData.price;
|
|
74
72
|
let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
|
|
75
73
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
74
|
+
// if passing live oracle data, improve predicted calc estimate
|
|
75
|
+
if (oraclePriceData) {
|
|
76
|
+
const oraclePrice = oraclePriceData.price;
|
|
77
|
+
|
|
78
|
+
const oracleLiveVsTwap = oraclePrice
|
|
79
|
+
.sub(lastOracleTwapWithMantissa)
|
|
80
|
+
.abs()
|
|
81
|
+
.mul(MARK_PRICE_PRECISION)
|
|
82
|
+
.mul(new BN(100))
|
|
83
|
+
.div(lastOracleTwapWithMantissa);
|
|
84
|
+
|
|
85
|
+
// verify pyth live input is within 10% of last twap for live update
|
|
86
|
+
if (oracleLiveVsTwap.lte(MARK_PRICE_PRECISION.mul(new BN(10)))) {
|
|
87
|
+
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
88
|
+
.mul(lastOracleTwapWithMantissa)
|
|
89
|
+
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
90
|
+
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
|
|
91
|
+
}
|
|
94
92
|
}
|
|
95
93
|
|
|
96
|
-
const
|
|
94
|
+
const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
|
|
95
|
+
.mul(lastOracleTwapWithMantissa)
|
|
96
|
+
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
97
|
+
.div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
|
|
98
|
+
|
|
99
|
+
const twapSpread = lastMarkTwapWithMantissa.sub(
|
|
100
|
+
shrunkLastOracleTwapwithMantissa
|
|
101
|
+
);
|
|
97
102
|
|
|
98
103
|
const twapSpreadPct = twapSpread
|
|
99
104
|
.mul(MARK_PRICE_PRECISION)
|
|
100
105
|
.mul(new BN(100))
|
|
101
|
-
.div(
|
|
106
|
+
.div(shrunkLastOracleTwapwithMantissa);
|
|
102
107
|
|
|
103
108
|
const lowerboundEst = twapSpreadPct
|
|
104
109
|
.mul(payFreq)
|
|
@@ -197,9 +202,9 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
197
202
|
*/
|
|
198
203
|
export async function calculateEstimatedFundingRate(
|
|
199
204
|
market: Market,
|
|
200
|
-
oraclePriceData
|
|
205
|
+
oraclePriceData?: OraclePriceData,
|
|
201
206
|
periodAdjustment: BN = new BN(1),
|
|
202
|
-
estimationMethod
|
|
207
|
+
estimationMethod?: 'interpolated' | 'lowerbound' | 'capped'
|
|
203
208
|
): Promise<BN> {
|
|
204
209
|
const [_1, _2, lowerboundEst, cappedAltEst, interpEst] =
|
|
205
210
|
await calculateAllEstimatedFundingRate(
|
|
@@ -227,7 +232,7 @@ export async function calculateEstimatedFundingRate(
|
|
|
227
232
|
*/
|
|
228
233
|
export async function calculateLongShortFundingRate(
|
|
229
234
|
market: Market,
|
|
230
|
-
oraclePriceData
|
|
235
|
+
oraclePriceData?: OraclePriceData,
|
|
231
236
|
periodAdjustment: BN = new BN(1)
|
|
232
237
|
): Promise<[BN, BN]> {
|
|
233
238
|
const [_1, _2, _, cappedAltEst, interpEst] =
|
|
@@ -255,7 +260,7 @@ export async function calculateLongShortFundingRate(
|
|
|
255
260
|
*/
|
|
256
261
|
export async function calculateLongShortFundingRateAndLiveTwaps(
|
|
257
262
|
market: Market,
|
|
258
|
-
oraclePriceData
|
|
263
|
+
oraclePriceData?: OraclePriceData,
|
|
259
264
|
periodAdjustment: BN = new BN(1)
|
|
260
265
|
): Promise<[BN, BN, BN, BN]> {
|
|
261
266
|
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] =
|