@drift-labs/sdk 2.97.0-beta.2 → 2.97.0-beta.21
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 +43 -2
- package/lib/constants/spotMarkets.js +2 -2
- package/lib/decode/user.js +10 -0
- package/lib/driftClient.d.ts +10 -1
- package/lib/driftClient.js +80 -1
- package/lib/idl/drift.json +267 -6
- package/lib/idl/switchboard_on_demand_30.json +195 -37
- package/lib/math/margin.d.ts +3 -3
- package/lib/math/margin.js +10 -10
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +15 -3
- package/lib/types.d.ts +21 -1
- package/lib/types.js +11 -1
- package/lib/user.d.ts +2 -1
- package/lib/user.js +14 -10
- 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 +44 -2
- package/src/constants/spotMarkets.ts +2 -2
- package/src/decode/user.ts +11 -1
- package/src/driftClient.ts +169 -1
- package/src/idl/drift.json +267 -6
- package/src/idl/switchboard_on_demand_30.json +195 -37
- package/src/math/margin.ts +20 -12
- package/src/math/market.ts +19 -3
- package/src/types.ts +21 -1
- package/src/user.ts +26 -10
|
@@ -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
|
@@ -21,19 +21,19 @@ export declare function calculateWorstCasePerpLiabilityValue(perpPosition: PerpP
|
|
|
21
21
|
worstCaseBaseAssetAmount: BN;
|
|
22
22
|
worstCaseLiabilityValue: BN;
|
|
23
23
|
};
|
|
24
|
-
export declare function calculatePerpLiabilityValue(baseAssetAmount: BN,
|
|
24
|
+
export declare function calculatePerpLiabilityValue(baseAssetAmount: BN, price: BN, isPredictionMarket: boolean): BN;
|
|
25
25
|
/**
|
|
26
26
|
* Calculates the margin required to open a trade, in quote amount. Only accounts for the trade size as a scalar value, does not account for the trade direction or current open positions and whether the trade would _actually_ be risk-increasing and use any extra collateral.
|
|
27
27
|
* @param targetMarketIndex
|
|
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, entryPrice?: BN): 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, estEntryPrice?: BN): 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
|
@@ -100,20 +100,20 @@ function calculateWorstCasePerpLiabilityValue(perpPosition, perpMarket, oraclePr
|
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
exports.calculateWorstCasePerpLiabilityValue = calculateWorstCasePerpLiabilityValue;
|
|
103
|
-
function calculatePerpLiabilityValue(baseAssetAmount,
|
|
103
|
+
function calculatePerpLiabilityValue(baseAssetAmount, price, isPredictionMarket) {
|
|
104
104
|
if (isPredictionMarket) {
|
|
105
105
|
if (baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
106
|
-
return baseAssetAmount.mul(
|
|
106
|
+
return baseAssetAmount.mul(price).div(numericConstants_1.BASE_PRECISION);
|
|
107
107
|
}
|
|
108
108
|
else {
|
|
109
109
|
return baseAssetAmount
|
|
110
110
|
.abs()
|
|
111
|
-
.mul(numericConstants_1.MAX_PREDICTION_PRICE.sub(
|
|
111
|
+
.mul(numericConstants_1.MAX_PREDICTION_PRICE.sub(price))
|
|
112
112
|
.div(numericConstants_1.BASE_PRECISION);
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
else {
|
|
116
|
-
return baseAssetAmount.abs().mul(
|
|
116
|
+
return baseAssetAmount.abs().mul(price).div(numericConstants_1.BASE_PRECISION);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
exports.calculatePerpLiabilityValue = calculatePerpLiabilityValue;
|
|
@@ -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, entryPrice) {
|
|
127
127
|
const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
|
|
128
|
-
const
|
|
129
|
-
const perpLiabilityValue = calculatePerpLiabilityValue(baseSize,
|
|
130
|
-
const marginRequired = new anchor_1.BN((0, __1.calculateMarketMarginRatio)(targetMarket, baseSize.abs(), 'Initial', userMaxMarginRatio))
|
|
128
|
+
const price = entryPrice !== null && entryPrice !== void 0 ? entryPrice : driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex).price;
|
|
129
|
+
const perpLiabilityValue = calculatePerpLiabilityValue(baseSize, price, (0, types_1.isVariant)(targetMarket.contractType, 'prediction'));
|
|
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, estEntryPrice) {
|
|
142
|
+
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode, estEntryPrice);
|
|
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;
|
|
@@ -964,7 +974,7 @@ export type UserStatsAccount = {
|
|
|
964
974
|
current_epoch_referrer_reward: BN;
|
|
965
975
|
};
|
|
966
976
|
referrer: PublicKey;
|
|
967
|
-
|
|
977
|
+
referrerStatus: boolean;
|
|
968
978
|
authority: PublicKey;
|
|
969
979
|
ifStakedQuoteAssetAmount: BN;
|
|
970
980
|
lastFuelIfBonusUpdateTs: number;
|
|
@@ -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;
|
|
@@ -1127,6 +1138,10 @@ export type ReferrerInfo = {
|
|
|
1127
1138
|
referrer: PublicKey;
|
|
1128
1139
|
referrerStats: PublicKey;
|
|
1129
1140
|
};
|
|
1141
|
+
export declare enum ReferrerStatus {
|
|
1142
|
+
IsReferrer = 1,
|
|
1143
|
+
IsReferred = 2
|
|
1144
|
+
}
|
|
1130
1145
|
export declare enum PlaceAndTakeOrderSuccessCondition {
|
|
1131
1146
|
PartialFill = 1,
|
|
1132
1147
|
FullFill = 2
|
|
@@ -1308,4 +1323,9 @@ export type SignedTxData = {
|
|
|
1308
1323
|
lastValidBlockHeight?: number;
|
|
1309
1324
|
blockHash: string;
|
|
1310
1325
|
};
|
|
1326
|
+
export type HighLeverageModeConfig = {
|
|
1327
|
+
maxUsers: number;
|
|
1328
|
+
currentUsers: number;
|
|
1329
|
+
reduceOnly: boolean;
|
|
1330
|
+
};
|
|
1311
1331
|
export {};
|
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.ReferrerStatus = 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;
|
|
@@ -325,6 +330,11 @@ exports.DefaultOrderParams = {
|
|
|
325
330
|
auctionStartPrice: null,
|
|
326
331
|
auctionEndPrice: null,
|
|
327
332
|
};
|
|
333
|
+
var ReferrerStatus;
|
|
334
|
+
(function (ReferrerStatus) {
|
|
335
|
+
ReferrerStatus[ReferrerStatus["IsReferrer"] = 1] = "IsReferrer";
|
|
336
|
+
ReferrerStatus[ReferrerStatus["IsReferred"] = 2] = "IsReferred";
|
|
337
|
+
})(ReferrerStatus = exports.ReferrerStatus || (exports.ReferrerStatus = {}));
|
|
328
338
|
var PlaceAndTakeOrderSuccessCondition;
|
|
329
339
|
(function (PlaceAndTakeOrderSuccessCondition) {
|
|
330
340
|
PlaceAndTakeOrderSuccessCondition[PlaceAndTakeOrderSuccessCondition["PartialFill"] = 1] = "PartialFill";
|
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
|
|
@@ -279,7 +280,7 @@ export declare class User {
|
|
|
279
280
|
* @returns : Precision PRICE_PRECISION
|
|
280
281
|
*/
|
|
281
282
|
liquidationPriceAfterClose(positionMarketIndex: number, closeQuoteAmount: BN, estimatedEntryPrice?: BN): BN;
|
|
282
|
-
getMarginUSDCRequiredForTrade(targetMarketIndex: number, baseSize: BN): BN;
|
|
283
|
+
getMarginUSDCRequiredForTrade(targetMarketIndex: number, baseSize: BN, estEntryPrice?: BN): BN;
|
|
283
284
|
getCollateralDepositRequiredForTrade(targetMarketIndex: number, baseSize: BN, collateralIndex: number): BN;
|
|
284
285
|
/**
|
|
285
286
|
* Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
|