@velocity-exchange/sdk 0.7.0 → 0.8.0
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/browser/adminClient.d.ts +24 -4
- package/lib/browser/adminClient.js +62 -12
- package/lib/browser/core/VelocityCore.d.ts +2 -0
- package/lib/browser/core/VelocityCore.js +1 -0
- package/lib/browser/core/instructions/trigger.d.ts +4 -0
- package/lib/browser/core/instructions/trigger.js +4 -0
- package/lib/browser/idl/velocity.d.ts +223 -33
- package/lib/browser/idl/velocity.json +224 -34
- package/lib/browser/math/liquidation.d.ts +37 -2
- package/lib/browser/math/liquidation.js +44 -3
- package/lib/browser/math/spotBalance.d.ts +13 -0
- package/lib/browser/math/spotBalance.js +43 -2
- package/lib/browser/types.d.ts +9 -0
- package/lib/browser/velocityClient.d.ts +32 -20
- package/lib/browser/velocityClient.js +48 -23
- package/lib/node/adminClient.d.ts +24 -4
- package/lib/node/adminClient.d.ts.map +1 -1
- package/lib/node/adminClient.js +62 -12
- package/lib/node/core/VelocityCore.d.ts +2 -0
- package/lib/node/core/VelocityCore.d.ts.map +1 -1
- package/lib/node/core/VelocityCore.js +1 -0
- package/lib/node/core/instructions/trigger.d.ts +4 -0
- package/lib/node/core/instructions/trigger.d.ts.map +1 -1
- package/lib/node/core/instructions/trigger.js +4 -0
- package/lib/node/idl/velocity.d.ts +223 -33
- package/lib/node/idl/velocity.d.ts.map +1 -1
- package/lib/node/idl/velocity.json +224 -34
- package/lib/node/math/liquidation.d.ts +37 -2
- package/lib/node/math/liquidation.d.ts.map +1 -1
- package/lib/node/math/liquidation.js +44 -3
- package/lib/node/math/spotBalance.d.ts +13 -0
- package/lib/node/math/spotBalance.d.ts.map +1 -1
- package/lib/node/math/spotBalance.js +43 -2
- package/lib/node/types.d.ts +9 -0
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/velocityClient.d.ts +32 -20
- package/lib/node/velocityClient.d.ts.map +1 -1
- package/lib/node/velocityClient.js +48 -23
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"address": "vELoC1audYbSYVRXn1vPaV8Axoa9oU6BYmNGZZBDZ1P",
|
|
3
3
|
"metadata": {
|
|
4
4
|
"name": "velocity",
|
|
5
|
-
"version": "2.
|
|
5
|
+
"version": "2.164.0",
|
|
6
6
|
"spec": "0.1.0",
|
|
7
7
|
"description": "Created with Anchor"
|
|
8
8
|
},
|
|
@@ -4420,6 +4420,9 @@
|
|
|
4420
4420
|
"name": "liquidator",
|
|
4421
4421
|
"writable": true
|
|
4422
4422
|
},
|
|
4423
|
+
{
|
|
4424
|
+
"name": "liquidator_stats"
|
|
4425
|
+
},
|
|
4423
4426
|
{
|
|
4424
4427
|
"name": "user",
|
|
4425
4428
|
"writable": true
|
|
@@ -6283,6 +6286,9 @@
|
|
|
6283
6286
|
52
|
|
6284
6287
|
],
|
|
6285
6288
|
"accounts": [
|
|
6289
|
+
{
|
|
6290
|
+
"name": "state"
|
|
6291
|
+
},
|
|
6286
6292
|
{
|
|
6287
6293
|
"name": "spot_market",
|
|
6288
6294
|
"writable": true,
|
|
@@ -6327,6 +6333,40 @@
|
|
|
6327
6333
|
"user_stats"
|
|
6328
6334
|
]
|
|
6329
6335
|
},
|
|
6336
|
+
{
|
|
6337
|
+
"name": "spot_market_vault",
|
|
6338
|
+
"writable": true,
|
|
6339
|
+
"pda": {
|
|
6340
|
+
"seeds": [
|
|
6341
|
+
{
|
|
6342
|
+
"kind": "const",
|
|
6343
|
+
"value": [
|
|
6344
|
+
115,
|
|
6345
|
+
112,
|
|
6346
|
+
111,
|
|
6347
|
+
116,
|
|
6348
|
+
95,
|
|
6349
|
+
109,
|
|
6350
|
+
97,
|
|
6351
|
+
114,
|
|
6352
|
+
107,
|
|
6353
|
+
101,
|
|
6354
|
+
116,
|
|
6355
|
+
95,
|
|
6356
|
+
118,
|
|
6357
|
+
97,
|
|
6358
|
+
117,
|
|
6359
|
+
108,
|
|
6360
|
+
116
|
|
6361
|
+
]
|
|
6362
|
+
},
|
|
6363
|
+
{
|
|
6364
|
+
"kind": "arg",
|
|
6365
|
+
"path": "market_index"
|
|
6366
|
+
}
|
|
6367
|
+
]
|
|
6368
|
+
}
|
|
6369
|
+
},
|
|
6330
6370
|
{
|
|
6331
6371
|
"name": "insurance_fund_vault",
|
|
6332
6372
|
"writable": true,
|
|
@@ -6363,6 +6403,12 @@
|
|
|
6363
6403
|
}
|
|
6364
6404
|
]
|
|
6365
6405
|
}
|
|
6406
|
+
},
|
|
6407
|
+
{
|
|
6408
|
+
"name": "velocity_signer"
|
|
6409
|
+
},
|
|
6410
|
+
{
|
|
6411
|
+
"name": "token_program"
|
|
6366
6412
|
}
|
|
6367
6413
|
],
|
|
6368
6414
|
"args": [
|
|
@@ -6537,13 +6583,7 @@
|
|
|
6537
6583
|
}
|
|
6538
6584
|
},
|
|
6539
6585
|
{
|
|
6540
|
-
"name": "authority"
|
|
6541
|
-
"relations": [
|
|
6542
|
-
"user"
|
|
6543
|
-
]
|
|
6544
|
-
},
|
|
6545
|
-
{
|
|
6546
|
-
"name": "user"
|
|
6586
|
+
"name": "authority"
|
|
6547
6587
|
},
|
|
6548
6588
|
{
|
|
6549
6589
|
"name": "payer",
|
|
@@ -8238,6 +8278,9 @@
|
|
|
8238
8278
|
{
|
|
8239
8279
|
"name": "user",
|
|
8240
8280
|
"writable": true
|
|
8281
|
+
},
|
|
8282
|
+
{
|
|
8283
|
+
"name": "user_stats"
|
|
8241
8284
|
}
|
|
8242
8285
|
],
|
|
8243
8286
|
"args": [
|
|
@@ -11431,6 +11474,42 @@
|
|
|
11431
11474
|
],
|
|
11432
11475
|
"args": []
|
|
11433
11476
|
},
|
|
11477
|
+
{
|
|
11478
|
+
"name": "update_spot_market_deposit_cap",
|
|
11479
|
+
"discriminator": [
|
|
11480
|
+
76,
|
|
11481
|
+
21,
|
|
11482
|
+
179,
|
|
11483
|
+
154,
|
|
11484
|
+
28,
|
|
11485
|
+
161,
|
|
11486
|
+
174,
|
|
11487
|
+
107
|
|
11488
|
+
],
|
|
11489
|
+
"accounts": [
|
|
11490
|
+
{
|
|
11491
|
+
"name": "admin",
|
|
11492
|
+
"signer": true
|
|
11493
|
+
},
|
|
11494
|
+
{
|
|
11495
|
+
"name": "state"
|
|
11496
|
+
},
|
|
11497
|
+
{
|
|
11498
|
+
"name": "spot_market",
|
|
11499
|
+
"writable": true
|
|
11500
|
+
}
|
|
11501
|
+
],
|
|
11502
|
+
"args": [
|
|
11503
|
+
{
|
|
11504
|
+
"name": "deposit_guard_threshold",
|
|
11505
|
+
"type": "u64"
|
|
11506
|
+
},
|
|
11507
|
+
{
|
|
11508
|
+
"name": "max_deposit_bps_per_day",
|
|
11509
|
+
"type": "u16"
|
|
11510
|
+
}
|
|
11511
|
+
]
|
|
11512
|
+
},
|
|
11434
11513
|
{
|
|
11435
11514
|
"name": "update_spot_market_expiry",
|
|
11436
11515
|
"discriminator": [
|
|
@@ -12070,6 +12149,38 @@
|
|
|
12070
12149
|
}
|
|
12071
12150
|
]
|
|
12072
12151
|
},
|
|
12152
|
+
{
|
|
12153
|
+
"name": "update_spot_market_withdraw_circuit_breaker",
|
|
12154
|
+
"discriminator": [
|
|
12155
|
+
2,
|
|
12156
|
+
97,
|
|
12157
|
+
135,
|
|
12158
|
+
97,
|
|
12159
|
+
117,
|
|
12160
|
+
169,
|
|
12161
|
+
65,
|
|
12162
|
+
223
|
|
12163
|
+
],
|
|
12164
|
+
"accounts": [
|
|
12165
|
+
{
|
|
12166
|
+
"name": "admin",
|
|
12167
|
+
"signer": true
|
|
12168
|
+
},
|
|
12169
|
+
{
|
|
12170
|
+
"name": "state"
|
|
12171
|
+
},
|
|
12172
|
+
{
|
|
12173
|
+
"name": "spot_market",
|
|
12174
|
+
"writable": true
|
|
12175
|
+
}
|
|
12176
|
+
],
|
|
12177
|
+
"args": [
|
|
12178
|
+
{
|
|
12179
|
+
"name": "withdraw_circuit_breaker_bps",
|
|
12180
|
+
"type": "u16"
|
|
12181
|
+
}
|
|
12182
|
+
]
|
|
12183
|
+
},
|
|
12073
12184
|
{
|
|
12074
12185
|
"name": "update_state_max_initialize_user_fee",
|
|
12075
12186
|
"discriminator": [
|
|
@@ -16157,6 +16268,31 @@
|
|
|
16157
16268
|
"code": 6359,
|
|
16158
16269
|
"name": "InvalidEquityFloorTransfer",
|
|
16159
16270
|
"msg": "Invalid equity floor transfer between subaccounts"
|
|
16271
|
+
},
|
|
16272
|
+
{
|
|
16273
|
+
"code": 6360,
|
|
16274
|
+
"name": "IFDepositMintsZeroShares",
|
|
16275
|
+
"msg": "Insurance fund deposit would mint zero shares"
|
|
16276
|
+
},
|
|
16277
|
+
{
|
|
16278
|
+
"code": 6361,
|
|
16279
|
+
"name": "LiquidationWorsensAccountHealth",
|
|
16280
|
+
"msg": "Liquidation would worsen the account's margin shortage"
|
|
16281
|
+
},
|
|
16282
|
+
{
|
|
16283
|
+
"code": 6362,
|
|
16284
|
+
"name": "PerpBankruptcyMustPrecedeSpot",
|
|
16285
|
+
"msg": "Perp bankruptcies must be resolved before spot bankruptcies"
|
|
16286
|
+
},
|
|
16287
|
+
{
|
|
16288
|
+
"code": 6363,
|
|
16289
|
+
"name": "InvalidRevenueShareRecipient",
|
|
16290
|
+
"msg": "Revenue share recipient user must be sub_account_id 0"
|
|
16291
|
+
},
|
|
16292
|
+
{
|
|
16293
|
+
"code": 6364,
|
|
16294
|
+
"name": "DailyDepositLimit",
|
|
16295
|
+
"msg": "Spot market daily deposit limit hit"
|
|
16160
16296
|
}
|
|
16161
16297
|
],
|
|
16162
16298
|
"types": [
|
|
@@ -21204,19 +21340,24 @@
|
|
|
21204
21340
|
}
|
|
21205
21341
|
},
|
|
21206
21342
|
{
|
|
21207
|
-
"name": "
|
|
21343
|
+
"name": "pending_revenue_share",
|
|
21208
21344
|
"docs": [
|
|
21209
|
-
"
|
|
21210
|
-
"
|
|
21211
|
-
"
|
|
21212
|
-
"
|
|
21345
|
+
"Aggregate accrued builder/referrer revenue-share owed out of this",
|
|
21346
|
+
"market's `pnl_pool` but not yet paid: incremented as builder and",
|
|
21347
|
+
"referrer fees accrue on fills (mirrors the per-order",
|
|
21348
|
+
"`RevenueShareOrder.fees_accrued` writes) and decremented as",
|
|
21349
|
+
"`sweep_completed_revenue_share_for_market` pays them. The",
|
|
21350
|
+
"permissionless fee sweep reserves it (like `max(net_user_pnl, 0)` and",
|
|
21351
|
+
"the floored IF tranche) so a protocol-fee drain can't move the tokens",
|
|
21352
|
+
"backing already-owed revenue share out of the pnl pool and leave those",
|
|
21353
|
+
"claims temporarily unpayable. precision: QUOTE_PRECISION.",
|
|
21354
|
+
"",
|
|
21355
|
+
"Occupies the 8 bytes Rust naturally inserts to 16-align AMM's leading",
|
|
21356
|
+
"u128 (formerly explicit `_padding_align_amm`): a u64 at the same",
|
|
21357
|
+
"8-aligned offset keeps every downstream byte offset and the total size",
|
|
21358
|
+
"unchanged, so legacy accounts read 0 (nothing owed) until fees accrue."
|
|
21213
21359
|
],
|
|
21214
|
-
"type":
|
|
21215
|
-
"array": [
|
|
21216
|
-
"u8",
|
|
21217
|
-
8
|
|
21218
|
-
]
|
|
21219
|
-
}
|
|
21360
|
+
"type": "u64"
|
|
21220
21361
|
},
|
|
21221
21362
|
{
|
|
21222
21363
|
"name": "amm",
|
|
@@ -22998,15 +23139,44 @@
|
|
|
22998
23139
|
{
|
|
22999
23140
|
"name": "_padding_align_pfp",
|
|
23000
23141
|
"docs": [
|
|
23001
|
-
"
|
|
23002
|
-
"
|
|
23142
|
+
"Explicit filler carved from the alignment gap before `protocol_fee_pool`",
|
|
23143
|
+
"(which must stay at struct offset 752 so host/SBF layouts agree and the",
|
|
23144
|
+
"borsh/IDL packed offset matches). The gap is 13 bytes; the three",
|
|
23145
|
+
"configurable-limit fields below plus this 1-byte filler fill it exactly,",
|
|
23146
|
+
"so `protocol_fee_pool` and every field after it keep their offsets and the",
|
|
23147
|
+
"account size is unchanged. Reads 0 on markets created before these fields",
|
|
23148
|
+
"existed. Every byte is explicit so no implicit `#[repr(C)]` pad desyncs",
|
|
23149
|
+
"off-chain borsh decoders. Do not reorder or resize."
|
|
23003
23150
|
],
|
|
23004
|
-
"type":
|
|
23005
|
-
|
|
23006
|
-
|
|
23007
|
-
|
|
23008
|
-
|
|
23009
|
-
|
|
23151
|
+
"type": "u8"
|
|
23152
|
+
},
|
|
23153
|
+
{
|
|
23154
|
+
"name": "withdraw_circuit_breaker_bps",
|
|
23155
|
+
"docs": [
|
|
23156
|
+
"Daily withdraw circuit-breaker size: the max fraction of the 24h deposit",
|
|
23157
|
+
"TWAP that may be withdrawn per 24h window. `0` is treated as the default",
|
|
23158
|
+
"(2500 bps = 25%) so markets created before this field existed keep prior",
|
|
23159
|
+
"behavior. precision: basis points (10_000 = 100%)"
|
|
23160
|
+
],
|
|
23161
|
+
"type": "u16"
|
|
23162
|
+
},
|
|
23163
|
+
{
|
|
23164
|
+
"name": "max_deposit_bps_per_day",
|
|
23165
|
+
"docs": [
|
|
23166
|
+
"Daily deposit rate limit: the max fraction above the 24h deposit TWAP that",
|
|
23167
|
+
"resulting deposits may reach per 24h window. Disabled when `0`.",
|
|
23168
|
+
"precision: basis points (10_000 = 100%)"
|
|
23169
|
+
],
|
|
23170
|
+
"type": "u16"
|
|
23171
|
+
},
|
|
23172
|
+
{
|
|
23173
|
+
"name": "deposit_guard_threshold",
|
|
23174
|
+
"docs": [
|
|
23175
|
+
"No deposit rate limit when resulting deposits are below this threshold.",
|
|
23176
|
+
"Mirrors `withdraw_guard_threshold` on the deposit side.",
|
|
23177
|
+
"precision: token mint precision"
|
|
23178
|
+
],
|
|
23179
|
+
"type": "u64"
|
|
23010
23180
|
},
|
|
23011
23181
|
{
|
|
23012
23182
|
"name": "protocol_fee_pool",
|
|
@@ -23042,13 +23212,33 @@
|
|
|
23042
23212
|
"type": "u32"
|
|
23043
23213
|
},
|
|
23044
23214
|
{
|
|
23045
|
-
"name": "
|
|
23046
|
-
"
|
|
23047
|
-
"
|
|
23048
|
-
|
|
23049
|
-
|
|
23050
|
-
|
|
23051
|
-
|
|
23215
|
+
"name": "if_last_settle_vault_amount",
|
|
23216
|
+
"docs": [
|
|
23217
|
+
"Donation-proof accounted balance of the insurance-fund vault. It is moved",
|
|
23218
|
+
"by the same signed delta as the real SPL vault on *every* instruction that",
|
|
23219
|
+
"moves the vault, so it stays a faithful shadow of the vault minus raw",
|
|
23220
|
+
"donations. Inflows grow it: staker deposits (`add_insurance_fund_stake`)",
|
|
23221
|
+
"and settled revenue (`settle_revenue_to_insurance_fund`). Outflows/draws",
|
|
23222
|
+
"shrink it (saturating at 0): staker withdrawals",
|
|
23223
|
+
"(`remove_insurance_fund_stake`) and every IF draw that covers a loss —",
|
|
23224
|
+
"`resolve_perp_pnl_deficit`, `resolve_perp_bankruptcy`,",
|
|
23225
|
+
"`resolve_spot_bankruptcy`. The one movement deliberately *excluded* is a",
|
|
23226
|
+
"raw SPL transfer straight into the vault: it runs no instruction, so it",
|
|
23227
|
+
"never enters this balance — that is exactly the donation the shadow must",
|
|
23228
|
+
"not see. Consumed by the per-period revenue-settle APR cap in",
|
|
23229
|
+
"`settle_revenue_to_insurance_fund`, sized off `min(live_if_vault, this)`,",
|
|
23230
|
+
"so a donation spiked into the live vault right before a settle cannot",
|
|
23231
|
+
"inflate the cap while legitimate stakes and real settled revenue (which",
|
|
23232
|
+
"this balance tracks) still do. (The unstake-cancel share forfeiture is",
|
|
23233
|
+
"donation-proofed differently — by withdraw-and-restake at the active share",
|
|
23234
|
+
"price — and does *not* read this field.) Repurposed from trailing padding —",
|
|
23235
|
+
"layout/size unchanged; `0` means \"uninitialized\" (existing account",
|
|
23236
|
+
"pre-upgrade, or an accounted balance legitimately drained to empty — an",
|
|
23237
|
+
"empty IF vault has no user shares, so this is safe), and is seeded from the",
|
|
23238
|
+
"live balance on the next add/settle and treated as \"fall back to live\" by",
|
|
23239
|
+
"the consumers."
|
|
23240
|
+
],
|
|
23241
|
+
"type": "u64"
|
|
23052
23242
|
}
|
|
23053
23243
|
]
|
|
23054
23244
|
}
|
|
@@ -85,6 +85,37 @@ export declare function calculatePerpIfFee(marginShortage: BN, userBaseAssetAmou
|
|
|
85
85
|
* `liabilityLiquidationMultiplier` is zero.
|
|
86
86
|
*/
|
|
87
87
|
export declare function calculateSpotIfFee(marginShortage: BN, tokenAmount: BN, assetWeight: number, assetLiquidationMultiplier: number, liabilityWeight: number, liabilityLiquidationMultiplier: number, liabilityDecimals: number, liabilityPrice: BN, maxIfFee: number): number;
|
|
88
|
+
/**
|
|
89
|
+
* User-protective price at which a collateral (deposit) asset is seized when its oracle is
|
|
90
|
+
* margin-invalid (`StaleForMargin`/`TooUncertain`) but still acceptable for
|
|
91
|
+
* `VelocityAction::Liquidate`, mirroring `calculate_user_protective_asset_price` in
|
|
92
|
+
* `programs/velocity/src/math/liquidation.rs`. The program sizes the asset-conversion leg
|
|
93
|
+
* of spot and pnl-vs-spot liquidations (and the swap-liquidation worst-case price) with this
|
|
94
|
+
* instead of the raw oracle price whenever the deposit oracle fails the margin-calc validity
|
|
95
|
+
* gate — a stale or uncertain oracle can make an account liquidatable but cannot cheapen its
|
|
96
|
+
* collateral. Pass the result as `assetPrice` to `calculateAssetTransferForLiabilityTransfer`
|
|
97
|
+
* to predict on-chain transfer amounts in that case.
|
|
98
|
+
* @param oraclePrice Raw oracle price of the asset, PRICE_PRECISION (1e6).
|
|
99
|
+
* @param oracleConfidence Oracle confidence interval, PRICE_PRECISION (1e6).
|
|
100
|
+
* @param lastOraclePriceTwap5Min The asset spot market's `historicalOracleData.lastOraclePriceTwap5Min`, PRICE_PRECISION (1e6).
|
|
101
|
+
* @returns `max(oraclePrice, lastOraclePriceTwap5Min, oraclePrice + oracleConfidence)`, PRICE_PRECISION (1e6).
|
|
102
|
+
*/
|
|
103
|
+
export declare function calculateUserProtectiveAssetPrice(oraclePrice: BN, oracleConfidence: BN, lastOraclePriceTwap5Min: BN): BN;
|
|
104
|
+
/**
|
|
105
|
+
* Liability-side counterpart of `calculateUserProtectiveAssetPrice`, mirroring
|
|
106
|
+
* `calculate_user_protective_liability_price` in
|
|
107
|
+
* `programs/velocity/src/math/liquidation.rs`. When the borrow (liability) oracle is
|
|
108
|
+
* margin-invalid, the program prices the repayment leg of spot and pnl-vs-spot liquidations
|
|
109
|
+
* (and the swap-liquidation worst-case price) at this instead of the raw oracle price, so an
|
|
110
|
+
* inflated stale/uncertain debt price cannot cheapen the collateral (or pnl) exchanged for
|
|
111
|
+
* it. Pass the result as `liabilityPrice` to `calculateAssetTransferForLiabilityTransfer` to
|
|
112
|
+
* predict on-chain transfer amounts in that case.
|
|
113
|
+
* @param oraclePrice Raw oracle price of the liability, PRICE_PRECISION (1e6).
|
|
114
|
+
* @param oracleConfidence Oracle confidence interval, PRICE_PRECISION (1e6).
|
|
115
|
+
* @param lastOraclePriceTwap5Min The liability spot market's `historicalOracleData.lastOraclePriceTwap5Min`, PRICE_PRECISION (1e6).
|
|
116
|
+
* @returns `min(oraclePrice, lastOraclePriceTwap5Min, oraclePrice - oracleConfidence)`, floored at 1, PRICE_PRECISION (1e6).
|
|
117
|
+
*/
|
|
118
|
+
export declare function calculateUserProtectiveLiabilityPrice(oraclePrice: BN, oracleConfidence: BN, lastOraclePriceTwap5Min: BN): BN;
|
|
88
119
|
/**
|
|
89
120
|
* Calculates how much of a liquidated user's collateral asset a liquidator receives in
|
|
90
121
|
* exchange for repaying `liabilityAmount` of a liability, mirroring
|
|
@@ -97,11 +128,15 @@ export declare function calculateSpotIfFee(marginShortage: BN, tokenAmount: BN,
|
|
|
97
128
|
* @param assetAmount User's available balance of the asset being transferred, asset spot market's own token precision.
|
|
98
129
|
* @param assetLiquidationMultiplier Liquidation-time discount multiplier on the asset side, LIQUIDATION_FEE_PRECISION (1e6).
|
|
99
130
|
* @param assetDecimals Asset spot market's token decimals.
|
|
100
|
-
* @param assetPrice Oracle price of the asset, PRICE_PRECISION (1e6).
|
|
131
|
+
* @param assetPrice Oracle price of the asset, PRICE_PRECISION (1e6). When the asset oracle
|
|
132
|
+
* is margin-invalid, the program prices the seizure protectively — pass
|
|
133
|
+
* `calculateUserProtectiveAssetPrice(...)` instead of the raw oracle price to match.
|
|
101
134
|
* @param liabilityAmount Liability amount being repaid, liability spot market's own token precision.
|
|
102
135
|
* @param liabilityLiquidationMultiplier Liquidation-time premium multiplier on the liability side, LIQUIDATION_FEE_PRECISION (1e6).
|
|
103
136
|
* @param liabilityDecimals Liability spot market's token decimals.
|
|
104
|
-
* @param liabilityPrice Oracle price of the liability asset, PRICE_PRECISION (1e6).
|
|
137
|
+
* @param liabilityPrice Oracle price of the liability asset, PRICE_PRECISION (1e6). When the
|
|
138
|
+
* liability oracle is margin-invalid, the program prices the repayment protectively — pass
|
|
139
|
+
* `calculateUserProtectiveLiabilityPrice(...)` instead of the raw oracle price to match.
|
|
105
140
|
* @returns Asset amount to transfer to the liquidator, asset spot market's own token precision (floored at 1).
|
|
106
141
|
*/
|
|
107
142
|
export declare function calculateAssetTransferForLiabilityTransfer(assetAmount: BN, assetLiquidationMultiplier: number, assetDecimals: number, assetPrice: BN, liabilityAmount: BN, liabilityLiquidationMultiplier: number, liabilityDecimals: number, liabilityPrice: BN): BN | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"liquidation.d.ts","sourceRoot":"","sources":["../../../src/math/liquidation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,sBAAsB,CAAC;AAe1C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,6CAA6C,CAC5D,cAAc,EAAE,EAAE,EAClB,WAAW,EAAE,MAAM,EACnB,cAAc,EAAE,MAAM,EACtB,gBAAgB,EAAE,MAAM,EACxB,WAAW,EAAE,EAAE,EACf,gBAAgB,EAAE,EAAE,GAClB,EAAE,GAAG,SAAS,CAqBhB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,+CAA+C,CAC9D,cAAc,EAAE,EAAE,EAClB,WAAW,EAAE,MAAM,EACnB,0BAA0B,EAAE,MAAM,EAClC,eAAe,EAAE,MAAM,EACvB,8BAA8B,EAAE,MAAM,EACtC,iBAAiB,EAAE,MAAM,EACzB,cAAc,EAAE,EAAE,EAClB,gBAAgB,EAAE,MAAM,GACtB,EAAE,GAAG,SAAS,CA8ChB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kBAAkB,CACjC,cAAc,EAAE,EAAE,EAClB,mBAAmB,EAAE,EAAE,EACvB,WAAW,EAAE,MAAM,EACnB,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,EAAE,EACf,gBAAgB,EAAE,EAAE,EACpB,mBAAmB,EAAE,MAAM,GACzB,MAAM,CAgCR;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,kBAAkB,CACjC,cAAc,EAAE,EAAE,EAClB,WAAW,EAAE,EAAE,EACf,WAAW,EAAE,MAAM,EACnB,0BAA0B,EAAE,MAAM,EAClC,eAAe,EAAE,MAAM,EACvB,8BAA8B,EAAE,MAAM,EACtC,iBAAiB,EAAE,MAAM,EACzB,cAAc,EAAE,EAAE,EAClB,QAAQ,EAAE,MAAM,GACd,MAAM,CAuCR;AAED
|
|
1
|
+
{"version":3,"file":"liquidation.d.ts","sourceRoot":"","sources":["../../../src/math/liquidation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,sBAAsB,CAAC;AAe1C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,6CAA6C,CAC5D,cAAc,EAAE,EAAE,EAClB,WAAW,EAAE,MAAM,EACnB,cAAc,EAAE,MAAM,EACtB,gBAAgB,EAAE,MAAM,EACxB,WAAW,EAAE,EAAE,EACf,gBAAgB,EAAE,EAAE,GAClB,EAAE,GAAG,SAAS,CAqBhB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,+CAA+C,CAC9D,cAAc,EAAE,EAAE,EAClB,WAAW,EAAE,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C,iBAAiB,EAAE,MAAM,EACzB,cAAc,EAAE,EAAE,GAChB,EAAE,GAAG,SAAS,CAkDhB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,0BAA0B,CACzC,kBAAkB,EAAE,EAAE,EACtB,0BAA0B,EAAE,EAAE,EAC9B,cAAc,EAAE,EAAE,EAClB,IAAI,EAAE,EAAE,EACR,qBAAqB,EAAE,EAAE,EACzB,mBAAmB,EAAE,EAAE,EACvB,kBAAkB,UAAQ,GACxB,EAAE,CA+BJ;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAChC,sCAAsC,EAAE,EAAE,EAC1C,0BAA0B,EAAE,EAAE,GAC5B,EAAE,CAIJ"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMarginShortage = exports.calculateMaxPctToLiquidate = exports.calculateAssetTransferForLiabilityTransfer = exports.calculateSpotIfFee = exports.calculatePerpIfFee = exports.calculateLiabilityTransferToCoverMarginShortage = exports.calculateBaseAssetAmountToCoverMarginShortage = void 0;
|
|
3
|
+
exports.getMarginShortage = exports.calculateMaxPctToLiquidate = exports.calculateAssetTransferForLiabilityTransfer = exports.calculateUserProtectiveLiabilityPrice = exports.calculateUserProtectiveAssetPrice = exports.calculateSpotIfFee = exports.calculatePerpIfFee = exports.calculateLiabilityTransferToCoverMarginShortage = exports.calculateBaseAssetAmountToCoverMarginShortage = void 0;
|
|
4
4
|
const anchor_1 = require("../isomorphic/anchor");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
/**
|
|
@@ -188,6 +188,43 @@ function calculateSpotIfFee(marginShortage, tokenAmount, assetWeight, assetLiqui
|
|
|
188
188
|
return anchor_1.BN.min(new anchor_1.BN(maxIfFee), impliedIfFee).toNumber();
|
|
189
189
|
}
|
|
190
190
|
exports.calculateSpotIfFee = calculateSpotIfFee;
|
|
191
|
+
/**
|
|
192
|
+
* User-protective price at which a collateral (deposit) asset is seized when its oracle is
|
|
193
|
+
* margin-invalid (`StaleForMargin`/`TooUncertain`) but still acceptable for
|
|
194
|
+
* `VelocityAction::Liquidate`, mirroring `calculate_user_protective_asset_price` in
|
|
195
|
+
* `programs/velocity/src/math/liquidation.rs`. The program sizes the asset-conversion leg
|
|
196
|
+
* of spot and pnl-vs-spot liquidations (and the swap-liquidation worst-case price) with this
|
|
197
|
+
* instead of the raw oracle price whenever the deposit oracle fails the margin-calc validity
|
|
198
|
+
* gate — a stale or uncertain oracle can make an account liquidatable but cannot cheapen its
|
|
199
|
+
* collateral. Pass the result as `assetPrice` to `calculateAssetTransferForLiabilityTransfer`
|
|
200
|
+
* to predict on-chain transfer amounts in that case.
|
|
201
|
+
* @param oraclePrice Raw oracle price of the asset, PRICE_PRECISION (1e6).
|
|
202
|
+
* @param oracleConfidence Oracle confidence interval, PRICE_PRECISION (1e6).
|
|
203
|
+
* @param lastOraclePriceTwap5Min The asset spot market's `historicalOracleData.lastOraclePriceTwap5Min`, PRICE_PRECISION (1e6).
|
|
204
|
+
* @returns `max(oraclePrice, lastOraclePriceTwap5Min, oraclePrice + oracleConfidence)`, PRICE_PRECISION (1e6).
|
|
205
|
+
*/
|
|
206
|
+
function calculateUserProtectiveAssetPrice(oraclePrice, oracleConfidence, lastOraclePriceTwap5Min) {
|
|
207
|
+
return anchor_1.BN.max(anchor_1.BN.max(oraclePrice, lastOraclePriceTwap5Min), oraclePrice.add(oracleConfidence));
|
|
208
|
+
}
|
|
209
|
+
exports.calculateUserProtectiveAssetPrice = calculateUserProtectiveAssetPrice;
|
|
210
|
+
/**
|
|
211
|
+
* Liability-side counterpart of `calculateUserProtectiveAssetPrice`, mirroring
|
|
212
|
+
* `calculate_user_protective_liability_price` in
|
|
213
|
+
* `programs/velocity/src/math/liquidation.rs`. When the borrow (liability) oracle is
|
|
214
|
+
* margin-invalid, the program prices the repayment leg of spot and pnl-vs-spot liquidations
|
|
215
|
+
* (and the swap-liquidation worst-case price) at this instead of the raw oracle price, so an
|
|
216
|
+
* inflated stale/uncertain debt price cannot cheapen the collateral (or pnl) exchanged for
|
|
217
|
+
* it. Pass the result as `liabilityPrice` to `calculateAssetTransferForLiabilityTransfer` to
|
|
218
|
+
* predict on-chain transfer amounts in that case.
|
|
219
|
+
* @param oraclePrice Raw oracle price of the liability, PRICE_PRECISION (1e6).
|
|
220
|
+
* @param oracleConfidence Oracle confidence interval, PRICE_PRECISION (1e6).
|
|
221
|
+
* @param lastOraclePriceTwap5Min The liability spot market's `historicalOracleData.lastOraclePriceTwap5Min`, PRICE_PRECISION (1e6).
|
|
222
|
+
* @returns `min(oraclePrice, lastOraclePriceTwap5Min, oraclePrice - oracleConfidence)`, floored at 1, PRICE_PRECISION (1e6).
|
|
223
|
+
*/
|
|
224
|
+
function calculateUserProtectiveLiabilityPrice(oraclePrice, oracleConfidence, lastOraclePriceTwap5Min) {
|
|
225
|
+
return anchor_1.BN.max(anchor_1.BN.min(anchor_1.BN.min(oraclePrice, lastOraclePriceTwap5Min), oraclePrice.sub(oracleConfidence)), numericConstants_1.ONE);
|
|
226
|
+
}
|
|
227
|
+
exports.calculateUserProtectiveLiabilityPrice = calculateUserProtectiveLiabilityPrice;
|
|
191
228
|
/**
|
|
192
229
|
* Calculates how much of a liquidated user's collateral asset a liquidator receives in
|
|
193
230
|
* exchange for repaying `liabilityAmount` of a liability, mirroring
|
|
@@ -200,11 +237,15 @@ exports.calculateSpotIfFee = calculateSpotIfFee;
|
|
|
200
237
|
* @param assetAmount User's available balance of the asset being transferred, asset spot market's own token precision.
|
|
201
238
|
* @param assetLiquidationMultiplier Liquidation-time discount multiplier on the asset side, LIQUIDATION_FEE_PRECISION (1e6).
|
|
202
239
|
* @param assetDecimals Asset spot market's token decimals.
|
|
203
|
-
* @param assetPrice Oracle price of the asset, PRICE_PRECISION (1e6).
|
|
240
|
+
* @param assetPrice Oracle price of the asset, PRICE_PRECISION (1e6). When the asset oracle
|
|
241
|
+
* is margin-invalid, the program prices the seizure protectively — pass
|
|
242
|
+
* `calculateUserProtectiveAssetPrice(...)` instead of the raw oracle price to match.
|
|
204
243
|
* @param liabilityAmount Liability amount being repaid, liability spot market's own token precision.
|
|
205
244
|
* @param liabilityLiquidationMultiplier Liquidation-time premium multiplier on the liability side, LIQUIDATION_FEE_PRECISION (1e6).
|
|
206
245
|
* @param liabilityDecimals Liability spot market's token decimals.
|
|
207
|
-
* @param liabilityPrice Oracle price of the liability asset, PRICE_PRECISION (1e6).
|
|
246
|
+
* @param liabilityPrice Oracle price of the liability asset, PRICE_PRECISION (1e6). When the
|
|
247
|
+
* liability oracle is margin-invalid, the program prices the repayment protectively — pass
|
|
248
|
+
* `calculateUserProtectiveLiabilityPrice(...)` instead of the raw oracle price to match.
|
|
208
249
|
* @returns Asset amount to transfer to the liquidator, asset spot market's own token precision (floored at 1).
|
|
209
250
|
*/
|
|
210
251
|
function calculateAssetTransferForLiabilityTransfer(assetAmount, assetLiquidationMultiplier, assetDecimals, assetPrice, liabilityAmount, liabilityLiquidationMultiplier, liabilityDecimals, liabilityPrice) {
|
|
@@ -241,6 +241,19 @@ export declare function calculateWithdrawLimit(spotMarket: SpotMarketAccount, no
|
|
|
241
241
|
currentDepositAmount: BN;
|
|
242
242
|
currentBorrowAmount: BN;
|
|
243
243
|
};
|
|
244
|
+
/**
|
|
245
|
+
* Mirror of the program's `calculate_max_deposit_token_amount`. Returns the max
|
|
246
|
+
* resulting deposit token amount permitted by the daily deposit cap: growth up
|
|
247
|
+
* to `maxDepositBpsPerDay` above the 24h deposit TWAP, but never below the
|
|
248
|
+
* deposit guard threshold. Returns null when the cap is disabled (pct == 0).
|
|
249
|
+
*/
|
|
250
|
+
export declare function calculateMaxDepositTokenAmount(depositTokenTwap: BN, depositGuardThreshold: BN, maxDepositBpsPerDay: number): BN | null;
|
|
251
|
+
/**
|
|
252
|
+
* Mirror of the program's `check_deposit_limits`. Returns true if the market's
|
|
253
|
+
* current deposit level is within the daily deposit cap (always true when the
|
|
254
|
+
* cap is disabled).
|
|
255
|
+
*/
|
|
256
|
+
export declare function checkDepositLimits(spotMarket: SpotMarketAccount): boolean;
|
|
244
257
|
/**
|
|
245
258
|
* Calculates the margin-weighted value of a spot deposit, mirroring the asset-side of the
|
|
246
259
|
* program's collateral valuation (`get_strict_token_value` + `get_asset_weight`). Uses the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spotBalance.d.ts","sourceRoot":"","sources":["../../../src/math/spotBalance.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,iBAAiB,EACjB,eAAe,EAEf,cAAc,EACd,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,EAAE,EAAE,MAAM,sBAAsB,CAAC;AAgB1C,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"spotBalance.d.ts","sourceRoot":"","sources":["../../../src/math/spotBalance.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,iBAAiB,EACjB,eAAe,EAEf,cAAc,EACd,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,EAAE,EAAE,MAAM,sBAAsB,CAAC;AAgB1C,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAMnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAcjE;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CACzB,WAAW,EAAE,EAAE,EACf,UAAU,EAAE,iBAAiB,EAC7B,WAAW,EAAE,eAAe,EAC5B,OAAO,CAAC,EAAE,OAAO,GACf,EAAE,CAeJ;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC7B,aAAa,EAAE,EAAE,EACjB,UAAU,EAAE,iBAAiB,EAC7B,WAAW,EAAE,eAAe,GAC1B,EAAE,CAYJ;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CACnC,WAAW,EAAE,EAAE,EACf,WAAW,EAAE,eAAe,GAC1B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B,iBAAiB,CAChC,WAAW,EAAE,EAAE,EACf,iBAAiB,EAAE,iBAAiB,EACpC,iBAAiB,EAAE,iBAAiB,EACpC,cAAc,EAAE,MAAM,EACtB,cAAc,CAAC,EAAE,cAAc,GAC7B,EAAE,CA8BJ;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,qBAAqB,CACpC,WAAW,EAAE,EAAE,EACf,iBAAiB,EAAE,iBAAiB,EACpC,iBAAiB,EAAE,iBAAiB,EACpC,cAAc,EAAE,MAAM,EACtB,cAAc,CAAC,EAAE,cAAc,EAC/B,iBAAiB,CAAC,EAAE,EAAE,GACpB,EAAE,CAkCJ"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSpotLiabilityValue = exports.getSpotAssetValue = exports.calculateWithdrawLimit = exports.calculateTokenUtilizationLimits = exports.calculateInterestAccumulated = exports.calculateBorrowRate = exports.calculateDepositRate = exports.calculateInterestRate = exports.calculateSpotMarketBorrowCapacity = exports.calculateUtilization = exports.calculateLiabilityWeight = exports.calculateScaledInitialAssetWeight = exports.calculateAssetWeight = exports.getTokenValue = exports.getStrictTokenValue = exports.getSignedTokenAmount = exports.getTokenAmount = exports.getBalance = void 0;
|
|
3
|
+
exports.getSpotLiabilityValue = exports.getSpotAssetValue = exports.checkDepositLimits = exports.calculateMaxDepositTokenAmount = exports.calculateWithdrawLimit = exports.calculateTokenUtilizationLimits = exports.calculateInterestAccumulated = exports.calculateBorrowRate = exports.calculateDepositRate = exports.calculateInterestRate = exports.calculateSpotMarketBorrowCapacity = exports.calculateUtilization = exports.calculateLiabilityWeight = exports.calculateScaledInitialAssetWeight = exports.calculateAssetWeight = exports.getTokenValue = exports.getStrictTokenValue = exports.getSignedTokenAmount = exports.getTokenAmount = exports.getBalance = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const anchor_1 = require("../isomorphic/anchor");
|
|
6
6
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
@@ -571,7 +571,14 @@ function calculateWithdrawLimit(spotMarket, now) {
|
|
|
571
571
|
else {
|
|
572
572
|
maxBorrowTokensTwap = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(lesserDepositAmount.div(new anchor_1.BN(2)), borrowTokenTwapLive.add(lesserDepositAmount.div(new anchor_1.BN(3)))), lesserDepositAmount.sub(lesserDepositAmount.div(new anchor_1.BN(20))))); // isolated pools between 50-95% utilization with friction on twap in 33% increments
|
|
573
573
|
}
|
|
574
|
-
|
|
574
|
+
// 0 is treated as the default 25% (2500 bps) so markets created before the
|
|
575
|
+
// field existed keep prior behavior (mirrors calculate_min_deposit_token_amount).
|
|
576
|
+
// withdrawCircuitBreakerBps is in basis points (BPS_PRECISION = 10_000 = 100%).
|
|
577
|
+
const breakerPct = spotMarket.withdrawCircuitBreakerBps === 0
|
|
578
|
+
? numericConstants_2.BPS_PRECISION.divn(4)
|
|
579
|
+
: new anchor_1.BN(spotMarket.withdrawCircuitBreakerBps);
|
|
580
|
+
const maxDrop = depositTokenTwapLive.mul(breakerPct).div(numericConstants_2.BPS_PRECISION);
|
|
581
|
+
const minDepositTokensTwap = depositTokenTwapLive.sub(anchor_1.BN.max(maxDrop, anchor_1.BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)));
|
|
575
582
|
const { minDepositTokensForUtilization, maxBorrowTokensForUtilization } = calculateTokenUtilizationLimits(marketDepositTokenAmount, marketBorrowTokenAmount, spotMarket);
|
|
576
583
|
const minDepositTokens = anchor_1.BN.max(minDepositTokensForUtilization, minDepositTokensTwap);
|
|
577
584
|
let maxBorrowTokens = anchor_1.BN.min(maxBorrowTokensForUtilization, maxBorrowTokensTwap);
|
|
@@ -599,6 +606,40 @@ function calculateWithdrawLimit(spotMarket, now) {
|
|
|
599
606
|
};
|
|
600
607
|
}
|
|
601
608
|
exports.calculateWithdrawLimit = calculateWithdrawLimit;
|
|
609
|
+
/**
|
|
610
|
+
* Mirror of the program's `calculate_max_deposit_token_amount`. Returns the max
|
|
611
|
+
* resulting deposit token amount permitted by the daily deposit cap: growth up
|
|
612
|
+
* to `maxDepositBpsPerDay` above the 24h deposit TWAP, but never below the
|
|
613
|
+
* deposit guard threshold. Returns null when the cap is disabled (pct == 0).
|
|
614
|
+
*/
|
|
615
|
+
function calculateMaxDepositTokenAmount(depositTokenTwap, depositGuardThreshold, maxDepositBpsPerDay) {
|
|
616
|
+
if (maxDepositBpsPerDay === 0) {
|
|
617
|
+
return null; // disabled
|
|
618
|
+
}
|
|
619
|
+
// maxDepositBpsPerDay is in basis points (BPS_PRECISION = 10_000 = 100%).
|
|
620
|
+
const maxIncrease = depositTokenTwap
|
|
621
|
+
.mul(new anchor_1.BN(maxDepositBpsPerDay))
|
|
622
|
+
.div(numericConstants_2.BPS_PRECISION);
|
|
623
|
+
return anchor_1.BN.max(depositTokenTwap.add(maxIncrease), depositGuardThreshold);
|
|
624
|
+
}
|
|
625
|
+
exports.calculateMaxDepositTokenAmount = calculateMaxDepositTokenAmount;
|
|
626
|
+
/**
|
|
627
|
+
* Mirror of the program's `check_deposit_limits`. Returns true if the market's
|
|
628
|
+
* current deposit level is within the daily deposit cap (always true when the
|
|
629
|
+
* cap is disabled).
|
|
630
|
+
*/
|
|
631
|
+
function checkDepositLimits(spotMarket) {
|
|
632
|
+
if (spotMarket.maxDepositBpsPerDay === 0) {
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
const depositTokenAmount = getTokenAmount(spotMarket.depositBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
636
|
+
const maxDepositToken = calculateMaxDepositTokenAmount(spotMarket.depositTokenTwap, spotMarket.depositGuardThreshold, spotMarket.maxDepositBpsPerDay);
|
|
637
|
+
if (maxDepositToken === null) {
|
|
638
|
+
return true;
|
|
639
|
+
}
|
|
640
|
+
return depositTokenAmount.lte(maxDepositToken);
|
|
641
|
+
}
|
|
642
|
+
exports.checkDepositLimits = checkDepositLimits;
|
|
602
643
|
/**
|
|
603
644
|
* Calculates the margin-weighted value of a spot deposit, mirroring the asset-side of the
|
|
604
645
|
* program's collateral valuation (`get_strict_token_value` + `get_asset_weight`). Uses the
|
package/lib/node/types.d.ts
CHANGED
|
@@ -1207,6 +1207,8 @@ export type PerpMarketAccount = {
|
|
|
1207
1207
|
feePoolBufferTarget: BN;
|
|
1208
1208
|
/** PERCENTAGE_PRECISION (1e6 = 100%); fraction of OI notional (at the oracle TWAP) the sweep leaves behind in `feeLedger.pendingIfFee` as a standing bankruptcy first-loss tranche; 0 disables */
|
|
1209
1209
|
bankruptcyIfFloorPct: number;
|
|
1210
|
+
/** QUOTE_PRECISION (1e6); aggregate builder/referrer revenue share accrued but not yet paid out of this market's pnl pool. The fee sweep reserves it (like `max(net_user_pnl, 0)` and the floored IF tranche) so a protocol-fee drain can't leave accrued revenue-share claims temporarily unpayable */
|
|
1211
|
+
pendingRevenueShare: BN;
|
|
1210
1212
|
/** MARGIN_PRECISION (1e4); scales margin ratio up for large positions */
|
|
1211
1213
|
imfFactor: number;
|
|
1212
1214
|
/** MARGIN_PRECISION (1e4); discounts positive-unrealized-pnl asset weight for large positions */
|
|
@@ -1357,6 +1359,10 @@ export type SpotMarketAccount = {
|
|
|
1357
1359
|
protocolLiquidationFee: number;
|
|
1358
1360
|
/** IF_FACTOR_PRECISION (1e6); protocol's carveout of lending deposit-interest gains */
|
|
1359
1361
|
protocolFeeFactor: number;
|
|
1362
|
+
/** token mint precision; IF vault balance recorded at the last revenue settle, used as a
|
|
1363
|
+
* donation-proof base for the per-period revenue-settle APR cap (see `settle_revenue_to_insurance_fund`);
|
|
1364
|
+
* `0` = uninitialized (pre-upgrade accounts, seeded on first settle) */
|
|
1365
|
+
ifLastSettleVaultAmount: BN;
|
|
1360
1366
|
/** token mint decimals; token-mint precision throughout this account is 10^decimals */
|
|
1361
1367
|
decimals: number;
|
|
1362
1368
|
/** SPOT_UTILIZATION_PRECISION (1e6) */
|
|
@@ -1439,6 +1445,9 @@ export type SpotMarketAccount = {
|
|
|
1439
1445
|
poolId: number;
|
|
1440
1446
|
/** -100 to 100; percentage adjustment applied to the base fee rate */
|
|
1441
1447
|
feeAdjustment: number;
|
|
1448
|
+
depositGuardThreshold: BN;
|
|
1449
|
+
withdrawCircuitBreakerBps: number;
|
|
1450
|
+
maxDepositBpsPerDay: number;
|
|
1442
1451
|
};
|
|
1443
1452
|
/** A scaled token balance inside a market's internal pools (pnl pool, protocol fee pool, revenue pool, spot fee pool, AMM fee pool). Multiply `scaledBalance` (SPOT_BALANCE_PRECISION, 1e9) by the referenced spot market's `cumulativeDepositInterest`/`cumulativeBorrowInterest` to get the token amount. */
|
|
1444
1453
|
export type PoolBalance = {
|