@drift-labs/sdk 2.97.0-beta.1 → 2.97.0-beta.11
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 +11 -4
- package/VERSION +1 -1
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +5 -1
- package/lib/adminClient.d.ts +6 -0
- package/lib/adminClient.js +53 -0
- package/lib/config.d.ts +1 -1
- package/lib/config.js +5 -3
- package/lib/constants/perpMarkets.js +20 -0
- package/lib/driftClient.d.ts +8 -1
- package/lib/driftClient.js +55 -4
- package/lib/idl/drift.json +248 -3
- package/lib/idl/switchboard_on_demand_30.json +195 -37
- package/lib/math/margin.d.ts +2 -2
- package/lib/math/margin.js +4 -4
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +15 -3
- package/lib/types.d.ts +11 -0
- package/lib/types.js +6 -1
- package/lib/user.d.ts +1 -0
- package/lib/user.js +12 -8
- package/package.json +1 -1
- package/src/addresses/pda.ts +9 -0
- package/src/adminClient.ts +114 -0
- package/src/config.ts +12 -3
- package/src/constants/perpMarkets.ts +20 -0
- package/src/driftClient.ts +132 -11
- package/src/idl/drift.json +248 -3
- package/src/idl/switchboard_on_demand_30.json +195 -37
- package/src/math/margin.ts +8 -4
- package/src/math/market.ts +19 -3
- package/src/types.ts +9 -0
- package/src/user.ts +21 -8
|
@@ -443,10 +443,14 @@
|
|
|
443
443
|
}
|
|
444
444
|
},
|
|
445
445
|
{
|
|
446
|
-
"name": "queue"
|
|
446
|
+
"name": "queue",
|
|
447
|
+
"relations": [
|
|
448
|
+
"oracle"
|
|
449
|
+
]
|
|
447
450
|
},
|
|
448
451
|
{
|
|
449
|
-
"name": "authority"
|
|
452
|
+
"name": "authority",
|
|
453
|
+
"signer": true
|
|
450
454
|
},
|
|
451
455
|
{
|
|
452
456
|
"name": "program_state",
|
|
@@ -503,6 +507,9 @@
|
|
|
503
507
|
},
|
|
504
508
|
{
|
|
505
509
|
"name": "stake_pool"
|
|
510
|
+
},
|
|
511
|
+
{
|
|
512
|
+
"name": "delegation_group"
|
|
506
513
|
}
|
|
507
514
|
],
|
|
508
515
|
"args": [
|
|
@@ -955,10 +962,7 @@
|
|
|
955
962
|
},
|
|
956
963
|
{
|
|
957
964
|
"name": "authority",
|
|
958
|
-
"signer": true
|
|
959
|
-
"relations": [
|
|
960
|
-
"queue"
|
|
961
|
-
]
|
|
965
|
+
"signer": true
|
|
962
966
|
},
|
|
963
967
|
{
|
|
964
968
|
"name": "program_authority"
|
|
@@ -1038,6 +1042,13 @@
|
|
|
1038
1042
|
{
|
|
1039
1043
|
"name": "oracle",
|
|
1040
1044
|
"writable": true
|
|
1045
|
+
},
|
|
1046
|
+
{
|
|
1047
|
+
"name": "authority",
|
|
1048
|
+
"signer": true
|
|
1049
|
+
},
|
|
1050
|
+
{
|
|
1051
|
+
"name": "state"
|
|
1041
1052
|
}
|
|
1042
1053
|
],
|
|
1043
1054
|
"args": [
|
|
@@ -1294,10 +1305,7 @@
|
|
|
1294
1305
|
},
|
|
1295
1306
|
{
|
|
1296
1307
|
"name": "authority",
|
|
1297
|
-
"signer": true
|
|
1298
|
-
"relations": [
|
|
1299
|
-
"queue"
|
|
1300
|
-
]
|
|
1308
|
+
"signer": true
|
|
1301
1309
|
},
|
|
1302
1310
|
{
|
|
1303
1311
|
"name": "program_authority"
|
|
@@ -1336,10 +1344,7 @@
|
|
|
1336
1344
|
},
|
|
1337
1345
|
{
|
|
1338
1346
|
"name": "authority",
|
|
1339
|
-
"signer": true
|
|
1340
|
-
"relations": [
|
|
1341
|
-
"queue"
|
|
1342
|
-
]
|
|
1347
|
+
"signer": true
|
|
1343
1348
|
},
|
|
1344
1349
|
{
|
|
1345
1350
|
"name": "state"
|
|
@@ -1387,6 +1392,13 @@
|
|
|
1387
1392
|
{
|
|
1388
1393
|
"name": "recent_slothashes",
|
|
1389
1394
|
"address": "SysvarS1otHashes111111111111111111111111111"
|
|
1395
|
+
},
|
|
1396
|
+
{
|
|
1397
|
+
"name": "authority",
|
|
1398
|
+
"signer": true,
|
|
1399
|
+
"relations": [
|
|
1400
|
+
"randomness"
|
|
1401
|
+
]
|
|
1390
1402
|
}
|
|
1391
1403
|
],
|
|
1392
1404
|
"args": [
|
|
@@ -1860,6 +1872,19 @@
|
|
|
1860
1872
|
}
|
|
1861
1873
|
],
|
|
1862
1874
|
"events": [
|
|
1875
|
+
{
|
|
1876
|
+
"name": "CostWhitelistEvent",
|
|
1877
|
+
"discriminator": [
|
|
1878
|
+
56,
|
|
1879
|
+
107,
|
|
1880
|
+
191,
|
|
1881
|
+
127,
|
|
1882
|
+
116,
|
|
1883
|
+
6,
|
|
1884
|
+
138,
|
|
1885
|
+
149
|
|
1886
|
+
]
|
|
1887
|
+
},
|
|
1863
1888
|
{
|
|
1864
1889
|
"name": "GarbageCollectionEvent",
|
|
1865
1890
|
"discriminator": [
|
|
@@ -2235,9 +2260,84 @@
|
|
|
2235
2260
|
{
|
|
2236
2261
|
"code": 6047,
|
|
2237
2262
|
"name": "InvalidInstruction"
|
|
2263
|
+
},
|
|
2264
|
+
{
|
|
2265
|
+
"code": 6048,
|
|
2266
|
+
"name": "OracleAlreadyVerified"
|
|
2267
|
+
},
|
|
2268
|
+
{
|
|
2269
|
+
"code": 6049,
|
|
2270
|
+
"name": "GuardianNotVerified"
|
|
2271
|
+
},
|
|
2272
|
+
{
|
|
2273
|
+
"code": 6050,
|
|
2274
|
+
"name": "InvalidConstraint"
|
|
2275
|
+
},
|
|
2276
|
+
{
|
|
2277
|
+
"code": 6051,
|
|
2278
|
+
"name": "InvalidDelegationGroup"
|
|
2279
|
+
},
|
|
2280
|
+
{
|
|
2281
|
+
"code": 6052,
|
|
2282
|
+
"name": "OracleKeyNotFound"
|
|
2283
|
+
},
|
|
2284
|
+
{
|
|
2285
|
+
"code": 6053,
|
|
2286
|
+
"name": "GuardianReregisterAttempt"
|
|
2238
2287
|
}
|
|
2239
2288
|
],
|
|
2240
2289
|
"types": [
|
|
2290
|
+
{
|
|
2291
|
+
"name": "CompactResult",
|
|
2292
|
+
"serialization": "bytemuck",
|
|
2293
|
+
"repr": {
|
|
2294
|
+
"kind": "c"
|
|
2295
|
+
},
|
|
2296
|
+
"type": {
|
|
2297
|
+
"kind": "struct",
|
|
2298
|
+
"fields": [
|
|
2299
|
+
{
|
|
2300
|
+
"name": "std_dev",
|
|
2301
|
+
"docs": [
|
|
2302
|
+
"The standard deviation of the submissions needed for quorom size"
|
|
2303
|
+
],
|
|
2304
|
+
"type": "f32"
|
|
2305
|
+
},
|
|
2306
|
+
{
|
|
2307
|
+
"name": "mean",
|
|
2308
|
+
"docs": [
|
|
2309
|
+
"The mean of the submissions needed for quorom size"
|
|
2310
|
+
],
|
|
2311
|
+
"type": "f32"
|
|
2312
|
+
},
|
|
2313
|
+
{
|
|
2314
|
+
"name": "slot",
|
|
2315
|
+
"docs": [
|
|
2316
|
+
"The slot at which this value was signed."
|
|
2317
|
+
],
|
|
2318
|
+
"type": "u64"
|
|
2319
|
+
}
|
|
2320
|
+
]
|
|
2321
|
+
}
|
|
2322
|
+
},
|
|
2323
|
+
{
|
|
2324
|
+
"name": "CostWhitelistEvent",
|
|
2325
|
+
"type": {
|
|
2326
|
+
"kind": "struct",
|
|
2327
|
+
"fields": [
|
|
2328
|
+
{
|
|
2329
|
+
"name": "feeds",
|
|
2330
|
+
"type": {
|
|
2331
|
+
"vec": "pubkey"
|
|
2332
|
+
}
|
|
2333
|
+
},
|
|
2334
|
+
{
|
|
2335
|
+
"name": "reward",
|
|
2336
|
+
"type": "u32"
|
|
2337
|
+
}
|
|
2338
|
+
]
|
|
2339
|
+
}
|
|
2340
|
+
},
|
|
2241
2341
|
{
|
|
2242
2342
|
"name": "CurrentResult",
|
|
2243
2343
|
"serialization": "bytemuck",
|
|
@@ -2384,7 +2484,7 @@
|
|
|
2384
2484
|
}
|
|
2385
2485
|
},
|
|
2386
2486
|
{
|
|
2387
|
-
"name": "
|
|
2487
|
+
"name": "_reserved1",
|
|
2388
2488
|
"type": "u32"
|
|
2389
2489
|
},
|
|
2390
2490
|
{
|
|
@@ -2437,12 +2537,7 @@
|
|
|
2437
2537
|
"name": "GuardianUnregisterParams",
|
|
2438
2538
|
"type": {
|
|
2439
2539
|
"kind": "struct",
|
|
2440
|
-
"fields": [
|
|
2441
|
-
{
|
|
2442
|
-
"name": "idx",
|
|
2443
|
-
"type": "u32"
|
|
2444
|
-
}
|
|
2445
|
-
]
|
|
2540
|
+
"fields": []
|
|
2446
2541
|
}
|
|
2447
2542
|
},
|
|
2448
2543
|
{
|
|
@@ -2948,7 +3043,7 @@
|
|
|
2948
3043
|
"kind": "struct",
|
|
2949
3044
|
"fields": [
|
|
2950
3045
|
{
|
|
2951
|
-
"name": "
|
|
3046
|
+
"name": "_reserved1",
|
|
2952
3047
|
"type": "u64"
|
|
2953
3048
|
}
|
|
2954
3049
|
]
|
|
@@ -3068,14 +3163,18 @@
|
|
|
3068
3163
|
}
|
|
3069
3164
|
},
|
|
3070
3165
|
{
|
|
3071
|
-
"name": "
|
|
3166
|
+
"name": "padding1",
|
|
3072
3167
|
"type": {
|
|
3073
3168
|
"array": [
|
|
3074
3169
|
"u8",
|
|
3075
|
-
|
|
3170
|
+
2
|
|
3076
3171
|
]
|
|
3077
3172
|
}
|
|
3078
3173
|
},
|
|
3174
|
+
{
|
|
3175
|
+
"name": "historical_result_idx",
|
|
3176
|
+
"type": "u8"
|
|
3177
|
+
},
|
|
3079
3178
|
{
|
|
3080
3179
|
"name": "min_sample_size",
|
|
3081
3180
|
"type": "u8"
|
|
@@ -3089,7 +3188,7 @@
|
|
|
3089
3188
|
"type": "u64"
|
|
3090
3189
|
},
|
|
3091
3190
|
{
|
|
3092
|
-
"name": "
|
|
3191
|
+
"name": "_reserved1",
|
|
3093
3192
|
"type": {
|
|
3094
3193
|
"array": [
|
|
3095
3194
|
"u8",
|
|
@@ -3110,38 +3209,51 @@
|
|
|
3110
3209
|
"type": "u32"
|
|
3111
3210
|
},
|
|
3112
3211
|
{
|
|
3113
|
-
"name": "
|
|
3212
|
+
"name": "padding2",
|
|
3114
3213
|
"type": {
|
|
3115
3214
|
"array": [
|
|
3116
3215
|
"u8",
|
|
3117
|
-
|
|
3216
|
+
12
|
|
3118
3217
|
]
|
|
3119
3218
|
}
|
|
3120
3219
|
},
|
|
3121
3220
|
{
|
|
3122
|
-
"name": "
|
|
3221
|
+
"name": "historical_results",
|
|
3222
|
+
"type": {
|
|
3223
|
+
"array": [
|
|
3224
|
+
{
|
|
3225
|
+
"defined": {
|
|
3226
|
+
"name": "CompactResult"
|
|
3227
|
+
}
|
|
3228
|
+
},
|
|
3229
|
+
32
|
|
3230
|
+
]
|
|
3231
|
+
}
|
|
3232
|
+
},
|
|
3233
|
+
{
|
|
3234
|
+
"name": "_ebuf4",
|
|
3123
3235
|
"type": {
|
|
3124
3236
|
"array": [
|
|
3125
3237
|
"u8",
|
|
3126
|
-
|
|
3238
|
+
8
|
|
3127
3239
|
]
|
|
3128
3240
|
}
|
|
3129
3241
|
},
|
|
3130
3242
|
{
|
|
3131
|
-
"name": "
|
|
3243
|
+
"name": "_ebuf3",
|
|
3132
3244
|
"type": {
|
|
3133
3245
|
"array": [
|
|
3134
3246
|
"u8",
|
|
3135
|
-
|
|
3247
|
+
24
|
|
3136
3248
|
]
|
|
3137
3249
|
}
|
|
3138
3250
|
},
|
|
3139
3251
|
{
|
|
3140
|
-
"name": "
|
|
3252
|
+
"name": "_ebuf2",
|
|
3141
3253
|
"type": {
|
|
3142
3254
|
"array": [
|
|
3143
3255
|
"u8",
|
|
3144
|
-
|
|
3256
|
+
256
|
|
3145
3257
|
]
|
|
3146
3258
|
}
|
|
3147
3259
|
}
|
|
@@ -4237,12 +4349,43 @@
|
|
|
4237
4349
|
"name": "lut_slot",
|
|
4238
4350
|
"type": "u64"
|
|
4239
4351
|
},
|
|
4352
|
+
{
|
|
4353
|
+
"name": "base_reward",
|
|
4354
|
+
"type": "u32"
|
|
4355
|
+
},
|
|
4356
|
+
{
|
|
4357
|
+
"name": "_ebuf6",
|
|
4358
|
+
"type": {
|
|
4359
|
+
"array": [
|
|
4360
|
+
"u8",
|
|
4361
|
+
28
|
|
4362
|
+
]
|
|
4363
|
+
}
|
|
4364
|
+
},
|
|
4365
|
+
{
|
|
4366
|
+
"name": "_ebuf5",
|
|
4367
|
+
"type": {
|
|
4368
|
+
"array": [
|
|
4369
|
+
"u8",
|
|
4370
|
+
32
|
|
4371
|
+
]
|
|
4372
|
+
}
|
|
4373
|
+
},
|
|
4374
|
+
{
|
|
4375
|
+
"name": "_ebuf4",
|
|
4376
|
+
"type": {
|
|
4377
|
+
"array": [
|
|
4378
|
+
"u8",
|
|
4379
|
+
64
|
|
4380
|
+
]
|
|
4381
|
+
}
|
|
4382
|
+
},
|
|
4240
4383
|
{
|
|
4241
4384
|
"name": "_ebuf3",
|
|
4242
4385
|
"type": {
|
|
4243
4386
|
"array": [
|
|
4244
4387
|
"u8",
|
|
4245
|
-
|
|
4388
|
+
128
|
|
4246
4389
|
]
|
|
4247
4390
|
}
|
|
4248
4391
|
},
|
|
@@ -4256,11 +4399,14 @@
|
|
|
4256
4399
|
}
|
|
4257
4400
|
},
|
|
4258
4401
|
{
|
|
4259
|
-
"name": "
|
|
4402
|
+
"name": "cost_whitelist",
|
|
4403
|
+
"docs": [
|
|
4404
|
+
"Cost whitelist by authority"
|
|
4405
|
+
],
|
|
4260
4406
|
"type": {
|
|
4261
4407
|
"array": [
|
|
4262
|
-
"
|
|
4263
|
-
|
|
4408
|
+
"pubkey",
|
|
4409
|
+
32
|
|
4264
4410
|
]
|
|
4265
4411
|
}
|
|
4266
4412
|
}
|
|
@@ -4346,6 +4492,18 @@
|
|
|
4346
4492
|
{
|
|
4347
4493
|
"name": "subsidy_amount",
|
|
4348
4494
|
"type": "u32"
|
|
4495
|
+
},
|
|
4496
|
+
{
|
|
4497
|
+
"name": "base_reward",
|
|
4498
|
+
"type": "u32"
|
|
4499
|
+
},
|
|
4500
|
+
{
|
|
4501
|
+
"name": "add_cost_wl",
|
|
4502
|
+
"type": "pubkey"
|
|
4503
|
+
},
|
|
4504
|
+
{
|
|
4505
|
+
"name": "rm_cost_wl",
|
|
4506
|
+
"type": "pubkey"
|
|
4349
4507
|
}
|
|
4350
4508
|
]
|
|
4351
4509
|
}
|
package/lib/math/margin.d.ts
CHANGED
|
@@ -28,12 +28,12 @@ export declare function calculatePerpLiabilityValue(baseAssetAmount: BN, oracleP
|
|
|
28
28
|
* @param baseSize
|
|
29
29
|
* @returns
|
|
30
30
|
*/
|
|
31
|
-
export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, userMaxMarginRatio?: number): BN;
|
|
31
|
+
export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, userMaxMarginRatio?: number, userHighLeverageMode?: boolean): BN;
|
|
32
32
|
/**
|
|
33
33
|
* Similar to calculatetMarginUSDCRequiredForTrade, but calculates how much of a given collateral is required to cover the margin requirements for a given trade. Basically does the same thing as getMarginUSDCRequiredForTrade but also accounts for asset weight of the selected collateral.
|
|
34
34
|
*
|
|
35
35
|
* Returns collateral required in the precision of the target collateral market.
|
|
36
36
|
*/
|
|
37
|
-
export declare function calculateCollateralDepositRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, collateralIndex: number, userMaxMarginRatio?: number): BN;
|
|
37
|
+
export declare function calculateCollateralDepositRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, collateralIndex: number, userMaxMarginRatio?: number, userHighLeverageMode?: boolean): BN;
|
|
38
38
|
export declare function calculateCollateralValueOfDeposit(driftClient: DriftClient, collateralIndex: number, baseSize: BN): BN;
|
|
39
39
|
export declare function calculateLiquidationPrice(freeCollateral: BN, freeCollateralDelta: BN, oraclePrice: BN): BN;
|
package/lib/math/margin.js
CHANGED
|
@@ -123,11 +123,11 @@ exports.calculatePerpLiabilityValue = calculatePerpLiabilityValue;
|
|
|
123
123
|
* @param baseSize
|
|
124
124
|
* @returns
|
|
125
125
|
*/
|
|
126
|
-
function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio) {
|
|
126
|
+
function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode) {
|
|
127
127
|
const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
|
|
128
128
|
const oracleData = driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex);
|
|
129
129
|
const perpLiabilityValue = calculatePerpLiabilityValue(baseSize, oracleData.price, (0, types_1.isVariant)(targetMarket.contractType, 'prediction'));
|
|
130
|
-
const marginRequired = new anchor_1.BN((0, __1.calculateMarketMarginRatio)(targetMarket, baseSize.abs(), 'Initial', userMaxMarginRatio))
|
|
130
|
+
const marginRequired = new anchor_1.BN((0, __1.calculateMarketMarginRatio)(targetMarket, baseSize.abs(), 'Initial', userMaxMarginRatio, userHighLeverageMode))
|
|
131
131
|
.mul(perpLiabilityValue)
|
|
132
132
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
133
133
|
return marginRequired;
|
|
@@ -138,8 +138,8 @@ exports.calculateMarginUSDCRequiredForTrade = calculateMarginUSDCRequiredForTrad
|
|
|
138
138
|
*
|
|
139
139
|
* Returns collateral required in the precision of the target collateral market.
|
|
140
140
|
*/
|
|
141
|
-
function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio) {
|
|
142
|
-
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio);
|
|
141
|
+
function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio, userHighLeverageMode) {
|
|
142
|
+
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode);
|
|
143
143
|
const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
|
|
144
144
|
const collateralOracleData = driftClient.getOracleDataForSpotMarket(collateralIndex);
|
|
145
145
|
const scaledAssetWeight = (0, __1.calculateScaledInitialAssetWeight)(collateralMarket, collateralOracleData.price);
|
package/lib/math/market.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export declare function calculateAskPrice(market: PerpMarketAccount, oraclePrice
|
|
|
27
27
|
export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market: PerpMarketAccount): PerpMarketAccount;
|
|
28
28
|
export declare function calculateOracleReserveSpread(market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
29
29
|
export declare function calculateOracleSpread(price: BN, oraclePriceData: OraclePriceData): BN;
|
|
30
|
-
export declare function calculateMarketMarginRatio(market: PerpMarketAccount, size: BN, marginCategory: MarginCategory, customMarginRatio?: number): number;
|
|
30
|
+
export declare function calculateMarketMarginRatio(market: PerpMarketAccount, size: BN, marginCategory: MarginCategory, customMarginRatio?: number, userHighLeverageMode?: boolean): number;
|
|
31
31
|
export declare function calculateUnrealizedAssetWeight(market: PerpMarketAccount, quoteSpotMarket: SpotMarketAccount, unrealizedPnl: BN, marginCategory: MarginCategory, oraclePriceData: OraclePriceData): BN;
|
|
32
32
|
export declare function calculateMarketAvailablePNL(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount): BN;
|
|
33
33
|
export declare function calculateMarketMaxAvailableInsurance(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount): BN;
|
package/lib/math/market.js
CHANGED
|
@@ -60,16 +60,28 @@ function calculateOracleSpread(price, oraclePriceData) {
|
|
|
60
60
|
return price.sub(oraclePriceData.price);
|
|
61
61
|
}
|
|
62
62
|
exports.calculateOracleSpread = calculateOracleSpread;
|
|
63
|
-
function calculateMarketMarginRatio(market, size, marginCategory, customMarginRatio = 0) {
|
|
63
|
+
function calculateMarketMarginRatio(market, size, marginCategory, customMarginRatio = 0, userHighLeverageMode = false) {
|
|
64
|
+
let marginRationInitial;
|
|
65
|
+
let marginRatioMaintenance;
|
|
66
|
+
if (userHighLeverageMode &&
|
|
67
|
+
market.highLeverageMarginRatioInitial > 0 &&
|
|
68
|
+
market.highLeverageMarginRatioMaintenance) {
|
|
69
|
+
marginRationInitial = market.highLeverageMarginRatioInitial;
|
|
70
|
+
marginRatioMaintenance = market.highLeverageMarginRatioMaintenance;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
marginRationInitial = market.marginRatioInitial;
|
|
74
|
+
marginRatioMaintenance = market.marginRatioMaintenance;
|
|
75
|
+
}
|
|
64
76
|
let marginRatio;
|
|
65
77
|
switch (marginCategory) {
|
|
66
78
|
case 'Initial': {
|
|
67
79
|
// use lowest leverage between max allowed and optional user custom max
|
|
68
|
-
marginRatio = Math.max((0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(
|
|
80
|
+
marginRatio = Math.max((0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRationInitial), numericConstants_1.MARGIN_PRECISION).toNumber(), customMarginRatio);
|
|
69
81
|
break;
|
|
70
82
|
}
|
|
71
83
|
case 'Maintenance': {
|
|
72
|
-
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(
|
|
84
|
+
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
73
85
|
break;
|
|
74
86
|
}
|
|
75
87
|
}
|
package/lib/types.d.ts
CHANGED
|
@@ -71,6 +71,14 @@ export declare enum UserStatus {
|
|
|
71
71
|
REDUCE_ONLY = 4,
|
|
72
72
|
ADVANCED_LP = 8
|
|
73
73
|
}
|
|
74
|
+
export declare class MarginMode {
|
|
75
|
+
static readonly DEFAULT: {
|
|
76
|
+
default: {};
|
|
77
|
+
};
|
|
78
|
+
static readonly HIGH_LEVERAGE: {
|
|
79
|
+
highLeverage: {};
|
|
80
|
+
};
|
|
81
|
+
}
|
|
74
82
|
export declare class ContractType {
|
|
75
83
|
static readonly PERPETUAL: {
|
|
76
84
|
perpetual: {};
|
|
@@ -753,6 +761,8 @@ export type PerpMarketAccount = {
|
|
|
753
761
|
fuelBoostTaker: number;
|
|
754
762
|
fuelBoostMaker: number;
|
|
755
763
|
fuelBoostPosition: number;
|
|
764
|
+
highLeverageMarginRatioInitial: number;
|
|
765
|
+
highLeverageMarginRatioMaintenance: number;
|
|
756
766
|
};
|
|
757
767
|
export type HistoricalOracleData = {
|
|
758
768
|
lastOraclePrice: BN;
|
|
@@ -1004,6 +1014,7 @@ export type UserAccount = {
|
|
|
1004
1014
|
openAuctions: number;
|
|
1005
1015
|
hasOpenAuction: boolean;
|
|
1006
1016
|
lastFuelBonusUpdateTs: number;
|
|
1017
|
+
marginMode: MarginMode;
|
|
1007
1018
|
};
|
|
1008
1019
|
export type SpotPosition = {
|
|
1009
1020
|
marketIndex: number;
|
package/lib/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SwapReduceOnly = exports.PlaceAndTakeOrderSuccessCondition = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SettlePnlMode = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.UserStatus = exports.InsuranceFundOperation = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.ExchangeStatus = void 0;
|
|
3
|
+
exports.SwapReduceOnly = exports.PlaceAndTakeOrderSuccessCondition = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SettlePnlMode = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.MarginMode = exports.UserStatus = exports.InsuranceFundOperation = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.ExchangeStatus = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
// # Utility Types / Enums / Constants
|
|
6
6
|
var ExchangeStatus;
|
|
@@ -58,6 +58,11 @@ var UserStatus;
|
|
|
58
58
|
UserStatus[UserStatus["REDUCE_ONLY"] = 4] = "REDUCE_ONLY";
|
|
59
59
|
UserStatus[UserStatus["ADVANCED_LP"] = 8] = "ADVANCED_LP";
|
|
60
60
|
})(UserStatus = exports.UserStatus || (exports.UserStatus = {}));
|
|
61
|
+
class MarginMode {
|
|
62
|
+
}
|
|
63
|
+
exports.MarginMode = MarginMode;
|
|
64
|
+
MarginMode.DEFAULT = { default: {} };
|
|
65
|
+
MarginMode.HIGH_LEVERAGE = { highLeverage: {} };
|
|
61
66
|
class ContractType {
|
|
62
67
|
}
|
|
63
68
|
exports.ContractType = ContractType;
|
package/lib/user.d.ts
CHANGED
|
@@ -247,6 +247,7 @@ export declare class User {
|
|
|
247
247
|
isBeingLiquidated(): boolean;
|
|
248
248
|
hasStatus(status: UserStatus): boolean;
|
|
249
249
|
isBankrupt(): boolean;
|
|
250
|
+
isHighLeverageMode(): boolean;
|
|
250
251
|
/**
|
|
251
252
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
252
253
|
* @returns
|
package/lib/user.js
CHANGED
|
@@ -420,7 +420,7 @@ class User {
|
|
|
420
420
|
return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, worstCaseBaseAssetAmount);
|
|
421
421
|
}
|
|
422
422
|
getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, baseAssetAmount) {
|
|
423
|
-
const marginRatio = (0, _1.calculateMarketMarginRatio)(this.driftClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial', this.getUserAccount().maxMarginRatio);
|
|
423
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(this.driftClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial', this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
424
424
|
return freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new _1.BN(marginRatio));
|
|
425
425
|
}
|
|
426
426
|
/**
|
|
@@ -787,7 +787,7 @@ class User {
|
|
|
787
787
|
liabilityValue = (0, _1.calculatePerpLiabilityValue)(baseAssetAmount, valuationPrice, (0, types_1.isVariant)(market.contractType, 'prediction'));
|
|
788
788
|
}
|
|
789
789
|
if (marginCategory) {
|
|
790
|
-
let marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio));
|
|
790
|
+
let marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode()));
|
|
791
791
|
if (liquidationBuffer !== undefined) {
|
|
792
792
|
marginRatio = marginRatio.add(liquidationBuffer);
|
|
793
793
|
}
|
|
@@ -1032,7 +1032,7 @@ class User {
|
|
|
1032
1032
|
.mul(numericConstants_1.PRICE_PRECISION)
|
|
1033
1033
|
.div(marketPrice));
|
|
1034
1034
|
// margin ratio incorporting upper bound on size
|
|
1035
|
-
let marginRatio = (0, _1.calculateMarketMarginRatio)(market, maxSize, marginCategory, this.getUserAccount().maxMarginRatio);
|
|
1035
|
+
let marginRatio = (0, _1.calculateMarketMarginRatio)(market, maxSize, marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
1036
1036
|
// use more fesible size since imf factor activated
|
|
1037
1037
|
let attempts = 0;
|
|
1038
1038
|
while (marginRatio > rawMarginRatio + 1e-4 && attempts < 10) {
|
|
@@ -1043,7 +1043,7 @@ class User {
|
|
|
1043
1043
|
.mul(numericConstants_1.PRICE_PRECISION)
|
|
1044
1044
|
.div(marketPrice));
|
|
1045
1045
|
// margin ratio incorporting more fesible target size
|
|
1046
|
-
marginRatio = (0, _1.calculateMarketMarginRatio)(market, targetSize, marginCategory, this.getUserAccount().maxMarginRatio);
|
|
1046
|
+
marginRatio = (0, _1.calculateMarketMarginRatio)(market, targetSize, marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
1047
1047
|
attempts += 1;
|
|
1048
1048
|
}
|
|
1049
1049
|
// how much more liabilities can be opened w remaining free collateral
|
|
@@ -1133,6 +1133,9 @@ class User {
|
|
|
1133
1133
|
isBankrupt() {
|
|
1134
1134
|
return (this.getUserAccount().status & types_1.UserStatus.BANKRUPT) > 0;
|
|
1135
1135
|
}
|
|
1136
|
+
isHighLeverageMode() {
|
|
1137
|
+
return (0, types_1.isVariant)(this.getUserAccount().marginMode, 'highLeverage');
|
|
1138
|
+
}
|
|
1136
1139
|
/**
|
|
1137
1140
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
1138
1141
|
* @returns
|
|
@@ -1279,7 +1282,7 @@ class User {
|
|
|
1279
1282
|
baseAssetAmount = perpPosition.baseAssetAmount;
|
|
1280
1283
|
liabilityValue = (0, _1.calculatePerpLiabilityValue)(baseAssetAmount, oraclePrice, (0, types_1.isVariant)(market.contractType, 'prediction'));
|
|
1281
1284
|
}
|
|
1282
|
-
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), 'Maintenance');
|
|
1285
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), 'Maintenance', this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
1283
1286
|
return liabilityValue.mul(new _1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION);
|
|
1284
1287
|
};
|
|
1285
1288
|
const freeCollateralConsumptionBefore = calculateMarginRequirement(perpPosition);
|
|
@@ -1295,7 +1298,7 @@ class User {
|
|
|
1295
1298
|
// zero if include orders == false
|
|
1296
1299
|
const orderBaseAssetAmount = baseAssetAmount.sub(perpPosition.baseAssetAmount);
|
|
1297
1300
|
const proposedBaseAssetAmount = baseAssetAmount.add(positionBaseSizeChange);
|
|
1298
|
-
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedBaseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio);
|
|
1301
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedBaseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode());
|
|
1299
1302
|
const marginRatioQuotePrecision = new _1.BN(marginRatio)
|
|
1300
1303
|
.mul(numericConstants_1.QUOTE_PRECISION)
|
|
1301
1304
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
@@ -1372,7 +1375,8 @@ class User {
|
|
|
1372
1375
|
return (0, margin_1.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, this.getUserAccount().maxMarginRatio);
|
|
1373
1376
|
}
|
|
1374
1377
|
getCollateralDepositRequiredForTrade(targetMarketIndex, baseSize, collateralIndex) {
|
|
1375
|
-
return (0, margin_1.calculateCollateralDepositRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, collateralIndex, this.getUserAccount().maxMarginRatio
|
|
1378
|
+
return (0, margin_1.calculateCollateralDepositRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, collateralIndex, this.getUserAccount().maxMarginRatio, false // assume user cant be high leverage if they havent created user account ?
|
|
1379
|
+
);
|
|
1376
1380
|
}
|
|
1377
1381
|
/**
|
|
1378
1382
|
* Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
|
|
@@ -2002,7 +2006,7 @@ class User {
|
|
|
2002
2006
|
this.driftClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
|
|
2003
2007
|
const oraclePrice = _oraclePriceData.price;
|
|
2004
2008
|
const { worstCaseBaseAssetAmount: worstCaseBaseAmount, worstCaseLiabilityValue, } = (0, _1.calculateWorstCasePerpLiabilityValue)(settledLpPosition, perpMarket, oraclePrice);
|
|
2005
|
-
const marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio));
|
|
2009
|
+
const marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode()));
|
|
2006
2010
|
const _quoteOraclePriceData = quoteOraclePriceData ||
|
|
2007
2011
|
this.driftClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
2008
2012
|
let marginRequirement = worstCaseLiabilityValue
|
package/package.json
CHANGED
package/src/addresses/pda.ts
CHANGED
|
@@ -300,3 +300,12 @@ export function getTokenProgramForSpotMarket(
|
|
|
300
300
|
}
|
|
301
301
|
return TOKEN_PROGRAM_ID;
|
|
302
302
|
}
|
|
303
|
+
|
|
304
|
+
export function getHighLeverageModeConfigPublicKey(
|
|
305
|
+
programId: PublicKey
|
|
306
|
+
): PublicKey {
|
|
307
|
+
return PublicKey.findProgramAddressSync(
|
|
308
|
+
[Buffer.from(anchor.utils.bytes.utf8.encode('high_leverage_mode_config'))],
|
|
309
|
+
programId
|
|
310
|
+
)[0];
|
|
311
|
+
}
|