@drift-labs/sdk 0.1.34-master.1 → 0.1.34-master.4
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/idl/clearing_house.json +8 -4
- package/lib/math/funding.d.ts +4 -4
- package/lib/math/funding.js +25 -20
- package/lib/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/idl/clearing_house.json +8 -4
- package/src/math/funding.ts +38 -29
- package/src/types.ts +1 -0
|
@@ -2538,7 +2538,11 @@
|
|
|
2538
2538
|
},
|
|
2539
2539
|
{
|
|
2540
2540
|
"name": "totalFeePaid",
|
|
2541
|
-
"type": "
|
|
2541
|
+
"type": "u64"
|
|
2542
|
+
},
|
|
2543
|
+
{
|
|
2544
|
+
"name": "totalFeeRebate",
|
|
2545
|
+
"type": "u64"
|
|
2542
2546
|
},
|
|
2543
2547
|
{
|
|
2544
2548
|
"name": "totalTokenDiscount",
|
|
@@ -3369,7 +3373,7 @@
|
|
|
3369
3373
|
},
|
|
3370
3374
|
{
|
|
3371
3375
|
"name": "fee",
|
|
3372
|
-
"type": "
|
|
3376
|
+
"type": "i128"
|
|
3373
3377
|
},
|
|
3374
3378
|
{
|
|
3375
3379
|
"name": "fillerReward",
|
|
@@ -3620,7 +3624,7 @@
|
|
|
3620
3624
|
},
|
|
3621
3625
|
{
|
|
3622
3626
|
"name": "fee",
|
|
3623
|
-
"type": "
|
|
3627
|
+
"type": "i128"
|
|
3624
3628
|
},
|
|
3625
3629
|
{
|
|
3626
3630
|
"name": "quoteAssetAmountSurplus",
|
|
@@ -3772,7 +3776,7 @@
|
|
|
3772
3776
|
},
|
|
3773
3777
|
{
|
|
3774
3778
|
"name": "fee",
|
|
3775
|
-
"type": "
|
|
3779
|
+
"type": "i128"
|
|
3776
3780
|
},
|
|
3777
3781
|
{
|
|
3778
3782
|
"name": "direction",
|
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
|
@@ -28,6 +28,7 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
|
|
|
28
28
|
// 24 * 365.25: annualized
|
|
29
29
|
const secondsInHour = new anchor_1.BN(3600);
|
|
30
30
|
const hoursInDay = new anchor_1.BN(24);
|
|
31
|
+
const ONE = new anchor_1.BN(1);
|
|
31
32
|
if (!market.initialized) {
|
|
32
33
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
33
34
|
}
|
|
@@ -51,30 +52,34 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
|
|
|
51
52
|
const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
52
53
|
const oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
|
|
53
54
|
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
54
|
-
const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, secondsInHour.sub(timeSinceLastOracleTwapUpdate));
|
|
55
|
-
const oraclePrice = oraclePriceData.price;
|
|
55
|
+
const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(ONE, anchor_1.BN.min(secondsInHour, anchor_1.BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))));
|
|
56
56
|
let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
57
|
+
// if passing live oracle data, improve predicted calc estimate
|
|
58
|
+
if (oraclePriceData) {
|
|
59
|
+
const oraclePrice = oraclePriceData.price;
|
|
60
|
+
const oracleLiveVsTwap = oraclePrice
|
|
61
|
+
.sub(lastOracleTwapWithMantissa)
|
|
62
|
+
.abs()
|
|
63
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
64
|
+
.mul(new anchor_1.BN(100))
|
|
65
|
+
.div(lastOracleTwapWithMantissa);
|
|
66
|
+
// verify pyth live input is within 10% of last twap for live update
|
|
67
|
+
if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
|
|
68
|
+
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
69
|
+
.mul(lastOracleTwapWithMantissa)
|
|
70
|
+
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
71
|
+
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
|
|
75
|
+
.mul(lastOracleTwapWithMantissa)
|
|
76
|
+
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
77
|
+
.div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
|
|
78
|
+
const twapSpread = lastMarkTwapWithMantissa.sub(shrunkLastOracleTwapwithMantissa);
|
|
74
79
|
const twapSpreadPct = twapSpread
|
|
75
80
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
76
81
|
.mul(new anchor_1.BN(100))
|
|
77
|
-
.div(
|
|
82
|
+
.div(shrunkLastOracleTwapwithMantissa);
|
|
78
83
|
const lowerboundEst = twapSpreadPct
|
|
79
84
|
.mul(payFreq)
|
|
80
85
|
.mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
|
package/lib/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -2538,7 +2538,11 @@
|
|
|
2538
2538
|
},
|
|
2539
2539
|
{
|
|
2540
2540
|
"name": "totalFeePaid",
|
|
2541
|
-
"type": "
|
|
2541
|
+
"type": "u64"
|
|
2542
|
+
},
|
|
2543
|
+
{
|
|
2544
|
+
"name": "totalFeeRebate",
|
|
2545
|
+
"type": "u64"
|
|
2542
2546
|
},
|
|
2543
2547
|
{
|
|
2544
2548
|
"name": "totalTokenDiscount",
|
|
@@ -3369,7 +3373,7 @@
|
|
|
3369
3373
|
},
|
|
3370
3374
|
{
|
|
3371
3375
|
"name": "fee",
|
|
3372
|
-
"type": "
|
|
3376
|
+
"type": "i128"
|
|
3373
3377
|
},
|
|
3374
3378
|
{
|
|
3375
3379
|
"name": "fillerReward",
|
|
@@ -3620,7 +3624,7 @@
|
|
|
3620
3624
|
},
|
|
3621
3625
|
{
|
|
3622
3626
|
"name": "fee",
|
|
3623
|
-
"type": "
|
|
3627
|
+
"type": "i128"
|
|
3624
3628
|
},
|
|
3625
3629
|
{
|
|
3626
3630
|
"name": "quoteAssetAmountSurplus",
|
|
@@ -3772,7 +3776,7 @@
|
|
|
3772
3776
|
},
|
|
3773
3777
|
{
|
|
3774
3778
|
"name": "fee",
|
|
3775
|
-
"type": "
|
|
3779
|
+
"type": "i128"
|
|
3776
3780
|
},
|
|
3777
3781
|
{
|
|
3778
3782
|
"name": "direction",
|
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
|
|
@@ -27,6 +27,7 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
27
27
|
// 24 * 365.25: annualized
|
|
28
28
|
const secondsInHour = new BN(3600);
|
|
29
29
|
const hoursInDay = new BN(24);
|
|
30
|
+
const ONE = new BN(1);
|
|
30
31
|
|
|
31
32
|
if (!market.initialized) {
|
|
32
33
|
return [ZERO, ZERO, ZERO, ZERO, ZERO];
|
|
@@ -66,39 +67,47 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
66
67
|
|
|
67
68
|
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
68
69
|
const oracleTwapTimeSinceLastUpdate = BN.max(
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
ONE,
|
|
71
|
+
BN.min(
|
|
72
|
+
secondsInHour,
|
|
73
|
+
BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))
|
|
74
|
+
)
|
|
71
75
|
);
|
|
72
|
-
|
|
73
|
-
const oraclePrice = oraclePriceData.price;
|
|
74
76
|
let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
|
|
75
77
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
78
|
+
// if passing live oracle data, improve predicted calc estimate
|
|
79
|
+
if (oraclePriceData) {
|
|
80
|
+
const oraclePrice = oraclePriceData.price;
|
|
81
|
+
|
|
82
|
+
const oracleLiveVsTwap = oraclePrice
|
|
83
|
+
.sub(lastOracleTwapWithMantissa)
|
|
84
|
+
.abs()
|
|
85
|
+
.mul(MARK_PRICE_PRECISION)
|
|
86
|
+
.mul(new BN(100))
|
|
87
|
+
.div(lastOracleTwapWithMantissa);
|
|
88
|
+
|
|
89
|
+
// verify pyth live input is within 10% of last twap for live update
|
|
90
|
+
if (oracleLiveVsTwap.lte(MARK_PRICE_PRECISION.mul(new BN(10)))) {
|
|
91
|
+
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
92
|
+
.mul(lastOracleTwapWithMantissa)
|
|
93
|
+
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
94
|
+
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
|
|
95
|
+
}
|
|
94
96
|
}
|
|
95
97
|
|
|
96
|
-
const
|
|
98
|
+
const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
|
|
99
|
+
.mul(lastOracleTwapWithMantissa)
|
|
100
|
+
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
101
|
+
.div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
|
|
102
|
+
|
|
103
|
+
const twapSpread = lastMarkTwapWithMantissa.sub(
|
|
104
|
+
shrunkLastOracleTwapwithMantissa
|
|
105
|
+
);
|
|
97
106
|
|
|
98
107
|
const twapSpreadPct = twapSpread
|
|
99
108
|
.mul(MARK_PRICE_PRECISION)
|
|
100
109
|
.mul(new BN(100))
|
|
101
|
-
.div(
|
|
110
|
+
.div(shrunkLastOracleTwapwithMantissa);
|
|
102
111
|
|
|
103
112
|
const lowerboundEst = twapSpreadPct
|
|
104
113
|
.mul(payFreq)
|
|
@@ -197,9 +206,9 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
197
206
|
*/
|
|
198
207
|
export async function calculateEstimatedFundingRate(
|
|
199
208
|
market: Market,
|
|
200
|
-
oraclePriceData
|
|
209
|
+
oraclePriceData?: OraclePriceData,
|
|
201
210
|
periodAdjustment: BN = new BN(1),
|
|
202
|
-
estimationMethod
|
|
211
|
+
estimationMethod?: 'interpolated' | 'lowerbound' | 'capped'
|
|
203
212
|
): Promise<BN> {
|
|
204
213
|
const [_1, _2, lowerboundEst, cappedAltEst, interpEst] =
|
|
205
214
|
await calculateAllEstimatedFundingRate(
|
|
@@ -227,7 +236,7 @@ export async function calculateEstimatedFundingRate(
|
|
|
227
236
|
*/
|
|
228
237
|
export async function calculateLongShortFundingRate(
|
|
229
238
|
market: Market,
|
|
230
|
-
oraclePriceData
|
|
239
|
+
oraclePriceData?: OraclePriceData,
|
|
231
240
|
periodAdjustment: BN = new BN(1)
|
|
232
241
|
): Promise<[BN, BN]> {
|
|
233
242
|
const [_1, _2, _, cappedAltEst, interpEst] =
|
|
@@ -255,7 +264,7 @@ export async function calculateLongShortFundingRate(
|
|
|
255
264
|
*/
|
|
256
265
|
export async function calculateLongShortFundingRateAndLiveTwaps(
|
|
257
266
|
market: Market,
|
|
258
|
-
oraclePriceData
|
|
267
|
+
oraclePriceData?: OraclePriceData,
|
|
259
268
|
periodAdjustment: BN = new BN(1)
|
|
260
269
|
): Promise<[BN, BN, BN, BN]> {
|
|
261
270
|
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] =
|