@drift-labs/sdk 2.86.0-beta.0 → 2.86.0-beta.10
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/VERSION +1 -1
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +24 -16
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +25 -32
- package/lib/adminClient.d.ts +4 -0
- package/lib/adminClient.js +36 -0
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +3 -1
- package/lib/constants/perpMarkets.js +32 -0
- package/lib/constants/spotMarkets.js +12 -1
- package/lib/idl/drift.json +246 -4
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/fuel.d.ts +5 -0
- package/lib/math/fuel.js +45 -0
- package/lib/types.d.ts +14 -0
- package/lib/user.d.ts +7 -0
- package/lib/user.js +48 -0
- package/package.json +4 -1
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +38 -23
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +55 -38
- package/src/adminClient.ts +96 -0
- package/src/constants/numericConstants.ts +3 -0
- package/src/constants/perpMarkets.ts +34 -0
- package/src/constants/spotMarkets.ts +14 -1
- package/src/idl/drift.json +246 -4
- package/src/index.ts +1 -0
- package/src/math/fuel.ts +56 -0
- package/src/types.ts +19 -0
- package/src/user.ts +120 -0
package/lib/idl/drift.json
CHANGED
|
@@ -2244,6 +2244,42 @@
|
|
|
2244
2244
|
],
|
|
2245
2245
|
"args": []
|
|
2246
2246
|
},
|
|
2247
|
+
{
|
|
2248
|
+
"name": "updateUserGovTokenInsuranceStake",
|
|
2249
|
+
"accounts": [
|
|
2250
|
+
{
|
|
2251
|
+
"name": "state",
|
|
2252
|
+
"isMut": false,
|
|
2253
|
+
"isSigner": false
|
|
2254
|
+
},
|
|
2255
|
+
{
|
|
2256
|
+
"name": "spotMarket",
|
|
2257
|
+
"isMut": false,
|
|
2258
|
+
"isSigner": false
|
|
2259
|
+
},
|
|
2260
|
+
{
|
|
2261
|
+
"name": "insuranceFundStake",
|
|
2262
|
+
"isMut": true,
|
|
2263
|
+
"isSigner": false
|
|
2264
|
+
},
|
|
2265
|
+
{
|
|
2266
|
+
"name": "userStats",
|
|
2267
|
+
"isMut": true,
|
|
2268
|
+
"isSigner": false
|
|
2269
|
+
},
|
|
2270
|
+
{
|
|
2271
|
+
"name": "authority",
|
|
2272
|
+
"isMut": false,
|
|
2273
|
+
"isSigner": true
|
|
2274
|
+
},
|
|
2275
|
+
{
|
|
2276
|
+
"name": "insuranceFundVault",
|
|
2277
|
+
"isMut": true,
|
|
2278
|
+
"isSigner": false
|
|
2279
|
+
}
|
|
2280
|
+
],
|
|
2281
|
+
"args": []
|
|
2282
|
+
},
|
|
2247
2283
|
{
|
|
2248
2284
|
"name": "initializeInsuranceFundStake",
|
|
2249
2285
|
"accounts": [
|
|
@@ -5079,6 +5115,92 @@
|
|
|
5079
5115
|
}
|
|
5080
5116
|
]
|
|
5081
5117
|
},
|
|
5118
|
+
{
|
|
5119
|
+
"name": "updatePerpMarketFuel",
|
|
5120
|
+
"accounts": [
|
|
5121
|
+
{
|
|
5122
|
+
"name": "admin",
|
|
5123
|
+
"isMut": false,
|
|
5124
|
+
"isSigner": true
|
|
5125
|
+
},
|
|
5126
|
+
{
|
|
5127
|
+
"name": "state",
|
|
5128
|
+
"isMut": false,
|
|
5129
|
+
"isSigner": false
|
|
5130
|
+
},
|
|
5131
|
+
{
|
|
5132
|
+
"name": "perpMarket",
|
|
5133
|
+
"isMut": true,
|
|
5134
|
+
"isSigner": false
|
|
5135
|
+
}
|
|
5136
|
+
],
|
|
5137
|
+
"args": [
|
|
5138
|
+
{
|
|
5139
|
+
"name": "fuelBoostTaker",
|
|
5140
|
+
"type": {
|
|
5141
|
+
"option": "u8"
|
|
5142
|
+
}
|
|
5143
|
+
},
|
|
5144
|
+
{
|
|
5145
|
+
"name": "fuelBoostMaker",
|
|
5146
|
+
"type": {
|
|
5147
|
+
"option": "u8"
|
|
5148
|
+
}
|
|
5149
|
+
},
|
|
5150
|
+
{
|
|
5151
|
+
"name": "fuelBoostPosition",
|
|
5152
|
+
"type": {
|
|
5153
|
+
"option": "u8"
|
|
5154
|
+
}
|
|
5155
|
+
}
|
|
5156
|
+
]
|
|
5157
|
+
},
|
|
5158
|
+
{
|
|
5159
|
+
"name": "updateSpotMarketFuel",
|
|
5160
|
+
"accounts": [
|
|
5161
|
+
{
|
|
5162
|
+
"name": "admin",
|
|
5163
|
+
"isMut": false,
|
|
5164
|
+
"isSigner": true
|
|
5165
|
+
},
|
|
5166
|
+
{
|
|
5167
|
+
"name": "state",
|
|
5168
|
+
"isMut": false,
|
|
5169
|
+
"isSigner": false
|
|
5170
|
+
},
|
|
5171
|
+
{
|
|
5172
|
+
"name": "spotMarket",
|
|
5173
|
+
"isMut": true,
|
|
5174
|
+
"isSigner": false
|
|
5175
|
+
}
|
|
5176
|
+
],
|
|
5177
|
+
"args": [
|
|
5178
|
+
{
|
|
5179
|
+
"name": "fuelBoostDeposits",
|
|
5180
|
+
"type": {
|
|
5181
|
+
"option": "u8"
|
|
5182
|
+
}
|
|
5183
|
+
},
|
|
5184
|
+
{
|
|
5185
|
+
"name": "fuelBoostBorrows",
|
|
5186
|
+
"type": {
|
|
5187
|
+
"option": "u8"
|
|
5188
|
+
}
|
|
5189
|
+
},
|
|
5190
|
+
{
|
|
5191
|
+
"name": "fuelBoostTaker",
|
|
5192
|
+
"type": {
|
|
5193
|
+
"option": "u8"
|
|
5194
|
+
}
|
|
5195
|
+
},
|
|
5196
|
+
{
|
|
5197
|
+
"name": "fuelBoostMaker",
|
|
5198
|
+
"type": {
|
|
5199
|
+
"option": "u8"
|
|
5200
|
+
}
|
|
5201
|
+
}
|
|
5202
|
+
]
|
|
5203
|
+
},
|
|
5082
5204
|
{
|
|
5083
5205
|
"name": "updateAdmin",
|
|
5084
5206
|
"accounts": [
|
|
@@ -5918,12 +6040,36 @@
|
|
|
5918
6040
|
],
|
|
5919
6041
|
"type": "i16"
|
|
5920
6042
|
},
|
|
6043
|
+
{
|
|
6044
|
+
"name": "fuelBoostPosition",
|
|
6045
|
+
"docs": [
|
|
6046
|
+
"fuel multiplier for perp funding",
|
|
6047
|
+
"precision: 10"
|
|
6048
|
+
],
|
|
6049
|
+
"type": "u8"
|
|
6050
|
+
},
|
|
6051
|
+
{
|
|
6052
|
+
"name": "fuelBoostTaker",
|
|
6053
|
+
"docs": [
|
|
6054
|
+
"fuel multiplier for perp taker",
|
|
6055
|
+
"precision: 10"
|
|
6056
|
+
],
|
|
6057
|
+
"type": "u8"
|
|
6058
|
+
},
|
|
6059
|
+
{
|
|
6060
|
+
"name": "fuelBoostMaker",
|
|
6061
|
+
"docs": [
|
|
6062
|
+
"fuel multiplier for perp maker",
|
|
6063
|
+
"precision: 10"
|
|
6064
|
+
],
|
|
6065
|
+
"type": "u8"
|
|
6066
|
+
},
|
|
5921
6067
|
{
|
|
5922
6068
|
"name": "padding",
|
|
5923
6069
|
"type": {
|
|
5924
6070
|
"array": [
|
|
5925
6071
|
"u8",
|
|
5926
|
-
|
|
6072
|
+
43
|
|
5927
6073
|
]
|
|
5928
6074
|
}
|
|
5929
6075
|
}
|
|
@@ -6376,12 +6522,44 @@
|
|
|
6376
6522
|
],
|
|
6377
6523
|
"type": "u8"
|
|
6378
6524
|
},
|
|
6525
|
+
{
|
|
6526
|
+
"name": "fuelBoostDeposits",
|
|
6527
|
+
"docs": [
|
|
6528
|
+
"fuel multiplier for spot deposits",
|
|
6529
|
+
"precision: 10"
|
|
6530
|
+
],
|
|
6531
|
+
"type": "u8"
|
|
6532
|
+
},
|
|
6533
|
+
{
|
|
6534
|
+
"name": "fuelBoostBorrows",
|
|
6535
|
+
"docs": [
|
|
6536
|
+
"fuel multiplier for spot borrows",
|
|
6537
|
+
"precision: 10"
|
|
6538
|
+
],
|
|
6539
|
+
"type": "u8"
|
|
6540
|
+
},
|
|
6541
|
+
{
|
|
6542
|
+
"name": "fuelBoostTaker",
|
|
6543
|
+
"docs": [
|
|
6544
|
+
"fuel multiplier for spot taker",
|
|
6545
|
+
"precision: 10"
|
|
6546
|
+
],
|
|
6547
|
+
"type": "u8"
|
|
6548
|
+
},
|
|
6549
|
+
{
|
|
6550
|
+
"name": "fuelBoostMaker",
|
|
6551
|
+
"docs": [
|
|
6552
|
+
"fuel multiplier for spot maker",
|
|
6553
|
+
"precision: 10"
|
|
6554
|
+
],
|
|
6555
|
+
"type": "u8"
|
|
6556
|
+
},
|
|
6379
6557
|
{
|
|
6380
6558
|
"name": "padding",
|
|
6381
6559
|
"type": {
|
|
6382
6560
|
"array": [
|
|
6383
6561
|
"u8",
|
|
6384
|
-
|
|
6562
|
+
43
|
|
6385
6563
|
]
|
|
6386
6564
|
}
|
|
6387
6565
|
}
|
|
@@ -6729,12 +6907,25 @@
|
|
|
6729
6907
|
],
|
|
6730
6908
|
"type": "bool"
|
|
6731
6909
|
},
|
|
6910
|
+
{
|
|
6911
|
+
"name": "padding1",
|
|
6912
|
+
"type": {
|
|
6913
|
+
"array": [
|
|
6914
|
+
"u8",
|
|
6915
|
+
5
|
|
6916
|
+
]
|
|
6917
|
+
}
|
|
6918
|
+
},
|
|
6919
|
+
{
|
|
6920
|
+
"name": "lastFuelBonusUpdateTs",
|
|
6921
|
+
"type": "i64"
|
|
6922
|
+
},
|
|
6732
6923
|
{
|
|
6733
6924
|
"name": "padding",
|
|
6734
6925
|
"type": {
|
|
6735
6926
|
"array": [
|
|
6736
6927
|
"u8",
|
|
6737
|
-
|
|
6928
|
+
8
|
|
6738
6929
|
]
|
|
6739
6930
|
}
|
|
6740
6931
|
}
|
|
@@ -6855,12 +7046,63 @@
|
|
|
6855
7046
|
"name": "disableUpdatePerpBidAskTwap",
|
|
6856
7047
|
"type": "bool"
|
|
6857
7048
|
},
|
|
7049
|
+
{
|
|
7050
|
+
"name": "padding1",
|
|
7051
|
+
"type": {
|
|
7052
|
+
"array": [
|
|
7053
|
+
"u8",
|
|
7054
|
+
6
|
|
7055
|
+
]
|
|
7056
|
+
}
|
|
7057
|
+
},
|
|
7058
|
+
{
|
|
7059
|
+
"name": "fuelDeposits",
|
|
7060
|
+
"docs": [
|
|
7061
|
+
"sub account id for spot deposit, borrow fuel tracking"
|
|
7062
|
+
],
|
|
7063
|
+
"type": "u32"
|
|
7064
|
+
},
|
|
7065
|
+
{
|
|
7066
|
+
"name": "fuelBorrows",
|
|
7067
|
+
"docs": [
|
|
7068
|
+
"accumulate fuel bonus for epoch"
|
|
7069
|
+
],
|
|
7070
|
+
"type": "u32"
|
|
7071
|
+
},
|
|
7072
|
+
{
|
|
7073
|
+
"name": "fuelPositions",
|
|
7074
|
+
"docs": [
|
|
7075
|
+
"accumulated fuel for perp open interest"
|
|
7076
|
+
],
|
|
7077
|
+
"type": "u32"
|
|
7078
|
+
},
|
|
7079
|
+
{
|
|
7080
|
+
"name": "fuelTaker",
|
|
7081
|
+
"docs": [
|
|
7082
|
+
"accumulate fuel bonus for epoch"
|
|
7083
|
+
],
|
|
7084
|
+
"type": "u32"
|
|
7085
|
+
},
|
|
7086
|
+
{
|
|
7087
|
+
"name": "fuelMaker",
|
|
7088
|
+
"docs": [
|
|
7089
|
+
"accumulate fuel bonus for epoch"
|
|
7090
|
+
],
|
|
7091
|
+
"type": "u32"
|
|
7092
|
+
},
|
|
7093
|
+
{
|
|
7094
|
+
"name": "ifStakedGovTokenAmount",
|
|
7095
|
+
"docs": [
|
|
7096
|
+
"The amount of tokens staked in the governance spot markets if"
|
|
7097
|
+
],
|
|
7098
|
+
"type": "u64"
|
|
7099
|
+
},
|
|
6858
7100
|
{
|
|
6859
7101
|
"name": "padding",
|
|
6860
7102
|
"type": {
|
|
6861
7103
|
"array": [
|
|
6862
7104
|
"u8",
|
|
6863
|
-
|
|
7105
|
+
16
|
|
6864
7106
|
]
|
|
6865
7107
|
}
|
|
6866
7108
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -64,6 +64,7 @@ export * from './slot/SlotSubscriber';
|
|
|
64
64
|
export * from './wallet';
|
|
65
65
|
export * from './types';
|
|
66
66
|
export * from './math/utils';
|
|
67
|
+
export * from './math/fuel';
|
|
67
68
|
export * from './config';
|
|
68
69
|
export * from './constants/numericConstants';
|
|
69
70
|
export * from './serum/serumSubscriber';
|
package/lib/index.js
CHANGED
|
@@ -87,6 +87,7 @@ __exportStar(require("./slot/SlotSubscriber"), exports);
|
|
|
87
87
|
__exportStar(require("./wallet"), exports);
|
|
88
88
|
__exportStar(require("./types"), exports);
|
|
89
89
|
__exportStar(require("./math/utils"), exports);
|
|
90
|
+
__exportStar(require("./math/fuel"), exports);
|
|
90
91
|
__exportStar(require("./config"), exports);
|
|
91
92
|
__exportStar(require("./constants/numericConstants"), exports);
|
|
92
93
|
__exportStar(require("./serum/serumSubscriber"), exports);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { BN } from '@coral-xyz/anchor';
|
|
3
|
+
import { SpotMarketAccount, PerpMarketAccount } from '..';
|
|
4
|
+
export declare function calculateSpotFuelBonus(spotMarket: SpotMarketAccount, signedTokenValue: BN, fuelBonusNumerator: BN): BN;
|
|
5
|
+
export declare function calculatePerpFuelBonus(perpMarket: PerpMarketAccount, baseAssetValue: BN, fuelBonusNumerator: BN): BN;
|
package/lib/math/fuel.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculatePerpFuelBonus = exports.calculateSpotFuelBonus = void 0;
|
|
4
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
5
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
+
function calculateSpotFuelBonus(spotMarket, signedTokenValue, fuelBonusNumerator) {
|
|
7
|
+
let result;
|
|
8
|
+
if (signedTokenValue.abs().lt(new anchor_1.BN(1))) {
|
|
9
|
+
result = numericConstants_1.ZERO;
|
|
10
|
+
}
|
|
11
|
+
else if (signedTokenValue.gt(new anchor_1.BN(0))) {
|
|
12
|
+
result = signedTokenValue
|
|
13
|
+
.abs()
|
|
14
|
+
.mul(fuelBonusNumerator)
|
|
15
|
+
.mul(new anchor_1.BN(spotMarket.fuelBoostDeposits))
|
|
16
|
+
.div(numericConstants_1.FUEL_WINDOW)
|
|
17
|
+
.div(numericConstants_1.QUOTE_PRECISION.div(new anchor_1.BN(10)));
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
result = signedTokenValue
|
|
21
|
+
.abs()
|
|
22
|
+
.mul(fuelBonusNumerator)
|
|
23
|
+
.mul(new anchor_1.BN(spotMarket.fuelBoostBorrows))
|
|
24
|
+
.div(numericConstants_1.FUEL_WINDOW)
|
|
25
|
+
.div(numericConstants_1.QUOTE_PRECISION.div(new anchor_1.BN(10)));
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
exports.calculateSpotFuelBonus = calculateSpotFuelBonus;
|
|
30
|
+
function calculatePerpFuelBonus(perpMarket, baseAssetValue, fuelBonusNumerator) {
|
|
31
|
+
let result;
|
|
32
|
+
if (baseAssetValue.abs().lte(numericConstants_1.QUOTE_PRECISION)) {
|
|
33
|
+
result = new anchor_1.BN(0);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
result = baseAssetValue
|
|
37
|
+
.abs()
|
|
38
|
+
.mul(fuelBonusNumerator)
|
|
39
|
+
.mul(new anchor_1.BN(perpMarket.fuelBoostPosition))
|
|
40
|
+
.div(numericConstants_1.FUEL_WINDOW)
|
|
41
|
+
.div(numericConstants_1.QUOTE_PRECISION.div(new anchor_1.BN(10)));
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
exports.calculatePerpFuelBonus = calculatePerpFuelBonus;
|
package/lib/types.d.ts
CHANGED
|
@@ -731,6 +731,9 @@ export type PerpMarketAccount = {
|
|
|
731
731
|
quoteSpotMarketIndex: number;
|
|
732
732
|
feeAdjustment: number;
|
|
733
733
|
pausedOperations: number;
|
|
734
|
+
fuelBoostPosition: number;
|
|
735
|
+
fuelBoostMaker: number;
|
|
736
|
+
fuelBoostTaker: number;
|
|
734
737
|
};
|
|
735
738
|
export type HistoricalOracleData = {
|
|
736
739
|
lastOraclePrice: BN;
|
|
@@ -812,6 +815,10 @@ export type SpotMarketAccount = {
|
|
|
812
815
|
ifPausedOperations: number;
|
|
813
816
|
maxTokenBorrowsFraction: number;
|
|
814
817
|
minBorrowRate: number;
|
|
818
|
+
fuelBoostDeposits: number;
|
|
819
|
+
fuelBoostBorrows: number;
|
|
820
|
+
fuelBoostMaker: number;
|
|
821
|
+
fuelBoostTaker: number;
|
|
815
822
|
};
|
|
816
823
|
export type PoolBalance = {
|
|
817
824
|
scaledBalance: BN;
|
|
@@ -939,6 +946,12 @@ export type UserStatsAccount = {
|
|
|
939
946
|
isReferrer: boolean;
|
|
940
947
|
authority: PublicKey;
|
|
941
948
|
ifStakedQuoteAssetAmount: BN;
|
|
949
|
+
fuelDeposits: number;
|
|
950
|
+
fuelBorrows: number;
|
|
951
|
+
fuelPositions: number;
|
|
952
|
+
fuelTaker: number;
|
|
953
|
+
fuelMaker: number;
|
|
954
|
+
ifStakedGovTokenAmount: BN;
|
|
942
955
|
};
|
|
943
956
|
export type UserAccount = {
|
|
944
957
|
authority: PublicKey;
|
|
@@ -967,6 +980,7 @@ export type UserAccount = {
|
|
|
967
980
|
hasOpenOrder: boolean;
|
|
968
981
|
openAuctions: number;
|
|
969
982
|
hasOpenAuction: boolean;
|
|
983
|
+
lastFuelBonusUpdateTs: BN;
|
|
970
984
|
};
|
|
971
985
|
export type SpotPosition = {
|
|
972
986
|
marketIndex: number;
|
package/lib/user.d.ts
CHANGED
|
@@ -136,6 +136,13 @@ export declare class User {
|
|
|
136
136
|
* @returns : Precision QUOTE_PRECISION
|
|
137
137
|
*/
|
|
138
138
|
getUnrealizedFundingPNL(marketIndex?: number): BN;
|
|
139
|
+
getFuelBonus(now: BN, includeSettled?: boolean, includeUnsettled?: boolean): {
|
|
140
|
+
depositFuel: any;
|
|
141
|
+
borrowFuel: any;
|
|
142
|
+
positionFuel: any;
|
|
143
|
+
takerFuel: any;
|
|
144
|
+
makerFuel: any;
|
|
145
|
+
};
|
|
139
146
|
getSpotMarketAssetAndLiabilityValue(marketIndex?: number, marginCategory?: MarginCategory, liquidationBuffer?: BN, includeOpenOrders?: boolean, strict?: boolean, now?: BN): {
|
|
140
147
|
totalAssetValue: BN;
|
|
141
148
|
totalLiabilityValue: BN;
|
package/lib/user.js
CHANGED
|
@@ -14,6 +14,7 @@ const spotPosition_1 = require("./math/spotPosition");
|
|
|
14
14
|
const oracles_1 = require("./math/oracles");
|
|
15
15
|
const tiers_1 = require("./math/tiers");
|
|
16
16
|
const strictOraclePrice_1 = require("./oracles/strictOraclePrice");
|
|
17
|
+
const fuel_1 = require("./math/fuel");
|
|
17
18
|
class User {
|
|
18
19
|
get isSubscribed() {
|
|
19
20
|
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
@@ -539,6 +540,53 @@ class User {
|
|
|
539
540
|
return pnl.add((0, _1.calculatePositionFundingPNL)(market, perpPosition));
|
|
540
541
|
}, numericConstants_1.ZERO);
|
|
541
542
|
}
|
|
543
|
+
getFuelBonus(now, includeSettled = true, includeUnsettled = true) {
|
|
544
|
+
const userAccount = this.getUserAccount();
|
|
545
|
+
const result = {
|
|
546
|
+
takerFuel: numericConstants_1.ZERO,
|
|
547
|
+
makerFuel: numericConstants_1.ZERO,
|
|
548
|
+
depositFuel: numericConstants_1.ZERO,
|
|
549
|
+
borrowFuel: numericConstants_1.ZERO,
|
|
550
|
+
positionFuel: numericConstants_1.ZERO,
|
|
551
|
+
};
|
|
552
|
+
if (includeSettled) {
|
|
553
|
+
const userStats = this.driftClient
|
|
554
|
+
.getUserStats()
|
|
555
|
+
.getAccount();
|
|
556
|
+
result.takerFuel = result.takerFuel.add(new _1.BN(userStats.fuelTaker));
|
|
557
|
+
result.makerFuel = result.makerFuel.add(new _1.BN(userStats.fuelMaker));
|
|
558
|
+
result.depositFuel = result.depositFuel.add(new _1.BN(userStats.fuelDeposits));
|
|
559
|
+
result.borrowFuel = result.borrowFuel.add(new _1.BN(userStats.fuelBorrows));
|
|
560
|
+
result.positionFuel = result.positionFuel.add(new _1.BN(userStats.fuelPositions));
|
|
561
|
+
}
|
|
562
|
+
if (includeUnsettled) {
|
|
563
|
+
const fuelBonusNumerator = _1.BN.max(now.sub(_1.BN.max(userAccount.lastFuelBonusUpdateTs, numericConstants_1.FUEL_START_TS)), numericConstants_1.ZERO);
|
|
564
|
+
if (fuelBonusNumerator.gt(numericConstants_1.ZERO)) {
|
|
565
|
+
for (const spotPosition of this.getActiveSpotPositions()) {
|
|
566
|
+
const spotMarketAccount = this.driftClient.getSpotMarketAccount(spotPosition.marketIndex);
|
|
567
|
+
const tokenAmount = this.getTokenAmount(spotPosition.marketIndex);
|
|
568
|
+
const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
|
|
569
|
+
const twap5min = (0, oracles_1.calculateLiveOracleTwap)(spotMarketAccount.historicalOracleData, oraclePriceData, now, numericConstants_1.FIVE_MINUTE // 5MIN
|
|
570
|
+
);
|
|
571
|
+
const strictOraclePrice = new strictOraclePrice_1.StrictOraclePrice(oraclePriceData.price, twap5min);
|
|
572
|
+
const signedTokenValue = (0, _1.getStrictTokenValue)(tokenAmount, spotMarketAccount.decimals, strictOraclePrice);
|
|
573
|
+
if (signedTokenValue.gt(numericConstants_1.ZERO)) {
|
|
574
|
+
result.depositFuel = result.depositFuel.add((0, fuel_1.calculateSpotFuelBonus)(spotMarketAccount, signedTokenValue, fuelBonusNumerator));
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
result.borrowFuel = result.borrowFuel.add((0, fuel_1.calculateSpotFuelBonus)(spotMarketAccount, signedTokenValue, fuelBonusNumerator));
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
for (const perpPosition of this.getActivePerpPositions()) {
|
|
581
|
+
const oraclePriceData = this.getOracleDataForPerpMarket(perpPosition.marketIndex);
|
|
582
|
+
const perpMarketAccount = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
583
|
+
const baseAssetValue = this.getPerpPositionValue(perpPosition.marketIndex, oraclePriceData, false);
|
|
584
|
+
result.positionFuel = result.positionFuel.add((0, fuel_1.calculatePerpFuelBonus)(perpMarketAccount, baseAssetValue, fuelBonusNumerator));
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return result;
|
|
589
|
+
}
|
|
542
590
|
getSpotMarketAssetAndLiabilityValue(marketIndex, marginCategory, liquidationBuffer, includeOpenOrders, strict = false, now) {
|
|
543
591
|
now = now || new _1.BN(new Date().getTime() / 1000);
|
|
544
592
|
let netQuoteValue = numericConstants_1.ZERO;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drift-labs/sdk",
|
|
3
|
-
"version": "2.86.0-beta.
|
|
3
|
+
"version": "2.86.0-beta.10",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"author": "crispheaney",
|
|
@@ -73,5 +73,8 @@
|
|
|
73
73
|
"description": "SDK for Drift Protocol",
|
|
74
74
|
"engines": {
|
|
75
75
|
"node": ">=18"
|
|
76
|
+
},
|
|
77
|
+
"resolutions": {
|
|
78
|
+
"@solana/errors": "2.0.0-preview.4"
|
|
76
79
|
}
|
|
77
80
|
}
|
|
@@ -101,7 +101,7 @@ export class PollingDriftClientAccountSubscriber
|
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
await this.updateAccountsToPoll();
|
|
104
|
-
|
|
104
|
+
this.updateOraclesToPoll();
|
|
105
105
|
await this.addToAccountLoader();
|
|
106
106
|
|
|
107
107
|
let subscriptionSucceeded = false;
|
|
@@ -116,8 +116,7 @@ export class PollingDriftClientAccountSubscriber
|
|
|
116
116
|
this.eventEmitter.emit('update');
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
await this.setPerpOracleMap();
|
|
120
|
-
await this.setSpotOracleMap();
|
|
119
|
+
await Promise.all([this.setPerpOracleMap(), this.setSpotOracleMap()]);
|
|
121
120
|
|
|
122
121
|
this.isSubscribing = false;
|
|
123
122
|
this.isSubscribed = subscriptionSucceeded;
|
|
@@ -141,14 +140,18 @@ export class PollingDriftClientAccountSubscriber
|
|
|
141
140
|
eventType: 'stateAccountUpdate',
|
|
142
141
|
});
|
|
143
142
|
|
|
144
|
-
await
|
|
145
|
-
|
|
143
|
+
await Promise.all([
|
|
144
|
+
this.updatePerpMarketAccountsToPoll(),
|
|
145
|
+
this.updateSpotMarketAccountsToPoll(),
|
|
146
|
+
]);
|
|
146
147
|
}
|
|
147
148
|
|
|
148
149
|
async updatePerpMarketAccountsToPoll(): Promise<boolean> {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
await Promise.all(
|
|
151
|
+
this.perpMarketIndexes.map((marketIndex) => {
|
|
152
|
+
return this.addPerpMarketAccountToPoll(marketIndex);
|
|
153
|
+
})
|
|
154
|
+
);
|
|
152
155
|
return true;
|
|
153
156
|
}
|
|
154
157
|
|
|
@@ -169,9 +172,11 @@ export class PollingDriftClientAccountSubscriber
|
|
|
169
172
|
}
|
|
170
173
|
|
|
171
174
|
async updateSpotMarketAccountsToPoll(): Promise<boolean> {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
+
await Promise.all(
|
|
176
|
+
this.spotMarketIndexes.map(async (marketIndex) => {
|
|
177
|
+
await this.addSpotMarketAccountToPoll(marketIndex);
|
|
178
|
+
})
|
|
179
|
+
);
|
|
175
180
|
|
|
176
181
|
return true;
|
|
177
182
|
}
|
|
@@ -209,16 +214,19 @@ export class PollingDriftClientAccountSubscriber
|
|
|
209
214
|
|
|
210
215
|
return true;
|
|
211
216
|
}
|
|
212
|
-
|
|
213
217
|
async addToAccountLoader(): Promise<void> {
|
|
218
|
+
const accountPromises = [];
|
|
214
219
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
215
|
-
|
|
220
|
+
accountPromises.push(this.addAccountToAccountLoader(accountToPoll));
|
|
216
221
|
}
|
|
217
222
|
|
|
223
|
+
const oraclePromises = [];
|
|
218
224
|
for (const [_, oracleToPoll] of this.oraclesToPoll) {
|
|
219
|
-
|
|
225
|
+
oraclePromises.push(this.addOracleToAccountLoader(oracleToPoll));
|
|
220
226
|
}
|
|
221
227
|
|
|
228
|
+
await Promise.all([...accountPromises, ...oraclePromises]);
|
|
229
|
+
|
|
222
230
|
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
223
231
|
this.eventEmitter.emit('error', error);
|
|
224
232
|
});
|
|
@@ -446,37 +454,44 @@ export class PollingDriftClientAccountSubscriber
|
|
|
446
454
|
}
|
|
447
455
|
console.log(`Pausing to find oracle ${oracle} failed`);
|
|
448
456
|
}
|
|
449
|
-
|
|
450
457
|
async setPerpOracleMap() {
|
|
451
458
|
const perpMarkets = this.getMarketAccountsAndSlots();
|
|
459
|
+
const oraclePromises = [];
|
|
452
460
|
for (const perpMarket of perpMarkets) {
|
|
453
461
|
const perpMarketAccount = perpMarket.data;
|
|
454
462
|
const perpMarketIndex = perpMarketAccount.marketIndex;
|
|
455
463
|
const oracle = perpMarketAccount.amm.oracle;
|
|
456
464
|
if (!this.oracles.has(oracle.toBase58())) {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
465
|
+
oraclePromises.push(
|
|
466
|
+
this.addOracle({
|
|
467
|
+
publicKey: oracle,
|
|
468
|
+
source: perpMarketAccount.amm.oracleSource,
|
|
469
|
+
})
|
|
470
|
+
);
|
|
461
471
|
}
|
|
462
472
|
this.perpOracleMap.set(perpMarketIndex, oracle);
|
|
463
473
|
}
|
|
474
|
+
await Promise.all(oraclePromises);
|
|
464
475
|
}
|
|
465
476
|
|
|
466
477
|
async setSpotOracleMap() {
|
|
467
478
|
const spotMarkets = this.getSpotMarketAccountsAndSlots();
|
|
479
|
+
const oraclePromises = [];
|
|
468
480
|
for (const spotMarket of spotMarkets) {
|
|
469
481
|
const spotMarketAccount = spotMarket.data;
|
|
470
482
|
const spotMarketIndex = spotMarketAccount.marketIndex;
|
|
471
483
|
const oracle = spotMarketAccount.oracle;
|
|
472
484
|
if (!this.oracles.has(oracle.toBase58())) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
485
|
+
oraclePromises.push(
|
|
486
|
+
this.addOracle({
|
|
487
|
+
publicKey: oracle,
|
|
488
|
+
source: spotMarketAccount.oracleSource,
|
|
489
|
+
})
|
|
490
|
+
);
|
|
477
491
|
}
|
|
478
492
|
this.spotOracleMap.set(spotMarketIndex, oracle);
|
|
479
493
|
}
|
|
494
|
+
await Promise.all(oraclePromises);
|
|
480
495
|
}
|
|
481
496
|
|
|
482
497
|
assertIsSubscribed(): void {
|