@velocity-exchange/sdk 0.6.1 → 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/blockhashSubscriber/BlockhashSubscriber.d.ts +19 -15
- package/lib/browser/blockhashSubscriber/BlockhashSubscriber.js +31 -22
- package/lib/browser/constants/numericConstants.d.ts +2 -0
- package/lib/browser/constants/numericConstants.js +3 -1
- 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/amm.d.ts +8 -8
- package/lib/browser/math/funding.d.ts +2 -2
- package/lib/browser/math/liquidation.d.ts +37 -2
- package/lib/browser/math/liquidation.js +44 -3
- package/lib/browser/math/market.d.ts +5 -4
- package/lib/browser/math/market.js +21 -14
- package/lib/browser/math/oracles.d.ts +3 -3
- package/lib/browser/math/orders.d.ts +1 -1
- package/lib/browser/math/spotBalance.d.ts +13 -0
- package/lib/browser/math/spotBalance.js +43 -2
- package/lib/browser/testClient.js +12 -1
- package/lib/browser/tx/txHandler.d.ts +7 -1
- package/lib/browser/tx/txHandler.js +7 -3
- package/lib/browser/types.d.ts +9 -0
- package/lib/browser/user.d.ts +2 -2
- package/lib/browser/userMap/userMap.d.ts +1 -0
- package/lib/browser/userMap/userMap.js +3 -7
- 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/blockhashSubscriber/BlockhashSubscriber.d.ts +19 -15
- package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts.map +1 -1
- package/lib/node/blockhashSubscriber/BlockhashSubscriber.js +31 -22
- package/lib/node/constants/numericConstants.d.ts +2 -0
- package/lib/node/constants/numericConstants.d.ts.map +1 -1
- package/lib/node/constants/numericConstants.js +3 -1
- 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/amm.d.ts +8 -8
- package/lib/node/math/amm.d.ts.map +1 -1
- package/lib/node/math/funding.d.ts +2 -2
- package/lib/node/math/funding.d.ts.map +1 -1
- 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/market.d.ts +5 -4
- package/lib/node/math/market.d.ts.map +1 -1
- package/lib/node/math/market.js +21 -14
- package/lib/node/math/oracles.d.ts +3 -3
- package/lib/node/math/oracles.d.ts.map +1 -1
- package/lib/node/math/orders.d.ts +1 -1
- package/lib/node/math/orders.d.ts.map +1 -1
- 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/testClient.d.ts.map +1 -1
- package/lib/node/testClient.js +12 -1
- package/lib/node/tx/txHandler.d.ts +7 -1
- package/lib/node/tx/txHandler.d.ts.map +1 -1
- package/lib/node/tx/txHandler.js +7 -3
- package/lib/node/types.d.ts +9 -0
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/user.d.ts +2 -2
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/userMap/userMap.d.ts +1 -0
- package/lib/node/userMap/userMap.d.ts.map +1 -1
- package/lib/node/userMap/userMap.js +3 -7
- 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
|
}
|
|
@@ -29,7 +29,7 @@ export declare function calculatePegFromTargetPrice(targetPrice: BN, baseAssetRe
|
|
|
29
29
|
* available to spend, QUOTE_PRECISION (1e6); `checkLowerBound` tells the caller whether it
|
|
30
30
|
* must still verify the repeg doesn't push `totalFeeMinusDistributions` negative.
|
|
31
31
|
*/
|
|
32
|
-
export declare function calculateOptimalPegAndBudget(amm: AMM, mmOraclePriceData: MMOraclePriceData): [BN, BN, BN, boolean];
|
|
32
|
+
export declare function calculateOptimalPegAndBudget(amm: AMM, mmOraclePriceData: Pick<MMOraclePriceData, 'price'>): [BN, BN, BN, boolean];
|
|
33
33
|
/**
|
|
34
34
|
* Determines the full curve update (repeg cost, K scale factor, new peg) for `amm` against
|
|
35
35
|
* the current oracle price, mirroring the "use full budget peg" fallback path of `adjust_amm`
|
|
@@ -46,7 +46,7 @@ export declare function calculateOptimalPegAndBudget(amm: AMM, mmOraclePriceData
|
|
|
46
46
|
* `calculateUpdatedAMM` whether it must still verify affordability against
|
|
47
47
|
* `totalFeeMinusDistributions`.
|
|
48
48
|
*/
|
|
49
|
-
export declare function calculateNewAmm(amm: AMM, mmOraclePriceData: MMOraclePriceData): [BN, BN, BN, BN, boolean];
|
|
49
|
+
export declare function calculateNewAmm(amm: AMM, mmOraclePriceData: Pick<MMOraclePriceData, 'price'>): [BN, BN, BN, BN, boolean];
|
|
50
50
|
/**
|
|
51
51
|
* Returns a copy of `amm` with its curve (peg, reserves, sqrtK) repegged/updated to the
|
|
52
52
|
* current oracle price, or `amm` unchanged if no update applies. Mirrors the program's
|
|
@@ -63,7 +63,7 @@ export declare function calculateNewAmm(amm: AMM, mmOraclePriceData: MMOraclePri
|
|
|
63
63
|
* @param mmOraclePriceData Current MM oracle price data; omit to skip the update entirely.
|
|
64
64
|
* @returns Updated `AMM` (new object), or the original `amm` reference if no update applies or the affordability gate rejects it.
|
|
65
65
|
*/
|
|
66
|
-
export declare function calculateUpdatedAMM(amm: AMM, mmOraclePriceData?: MMOraclePriceData): AMM;
|
|
66
|
+
export declare function calculateUpdatedAMM(amm: AMM, mmOraclePriceData?: Pick<MMOraclePriceData, 'price'>): AMM;
|
|
67
67
|
/**
|
|
68
68
|
* Repegs `amm` to the current oracle price (`calculateUpdatedAMM`) and returns the
|
|
69
69
|
* one-sided spread reserves (bid reserves for `short`, ask reserves for `long`) plus the
|
|
@@ -76,7 +76,7 @@ export declare function calculateUpdatedAMM(amm: AMM, mmOraclePriceData?: MMOrac
|
|
|
76
76
|
* @param latestSlot Current slot, forwarded for reference-price-offset smoothing.
|
|
77
77
|
* @returns `baseAssetReserve`/`quoteAssetReserve` for the requested side (AMM_RESERVE_PRECISION, 1e9), and the post-update `sqrtK`/`newPeg` (AMM_RESERVE_PRECISION 1e9 / PEG_PRECISION 1e6).
|
|
78
78
|
*/
|
|
79
|
-
export declare function calculateUpdatedAMMSpreadReserves(amm: AMM, marketStats: MarketStats, direction: PositionDirection, mmOraclePriceData?: MMOraclePriceData, latestSlot?: BN): {
|
|
79
|
+
export declare function calculateUpdatedAMMSpreadReserves(amm: AMM, marketStats: MarketStats, direction: PositionDirection, mmOraclePriceData?: Pick<MMOraclePriceData, 'price' | 'confidence'>, latestSlot?: BN): {
|
|
80
80
|
baseAssetReserve: BN;
|
|
81
81
|
quoteAssetReserve: BN;
|
|
82
82
|
sqrtK: BN;
|
|
@@ -92,7 +92,7 @@ export declare function calculateUpdatedAMMSpreadReserves(amm: AMM, marketStats:
|
|
|
92
92
|
* @param latestSlot Current slot, forwarded for reference-price-offset smoothing.
|
|
93
93
|
* @returns `[bidPrice, askPrice]`, both PRICE_PRECISION (1e6).
|
|
94
94
|
*/
|
|
95
|
-
export declare function calculateBidAskPrice(amm: AMM, marketStats: MarketStats, mmOraclePriceData?: MMOraclePriceData, withUpdate?: boolean, latestSlot?: BN): [BN, BN];
|
|
95
|
+
export declare function calculateBidAskPrice(amm: AMM, marketStats: MarketStats, mmOraclePriceData?: Pick<MMOraclePriceData, 'price' | 'confidence'>, withUpdate?: boolean, latestSlot?: BN): [BN, BN];
|
|
96
96
|
/**
|
|
97
97
|
* Computes the constant-product price implied by a pair of AMM reserves and a peg multiplier:
|
|
98
98
|
* `quoteAssetReserves * pegMultiplier / baseAssetReserves`, converted to `PRICE_PRECISION`.
|
|
@@ -338,7 +338,7 @@ export declare function calculateSpreadBN(baseSpread: number, lastOracleReserveP
|
|
|
338
338
|
* @throws if `oraclePriceData` is omitted while `baseSpread` and `curveUpdateIntensity` are both nonzero.
|
|
339
339
|
* @returns `[longSpread, shortSpread]`, both BID_ASK_SPREAD_PRECISION (1e6) fraction-of-price units.
|
|
340
340
|
*/
|
|
341
|
-
export declare function calculateSpread(amm: AMM, marketStats: MarketStats, oraclePriceData?: OraclePriceData, now?: BN, reservePrice?: BN): [number, number];
|
|
341
|
+
export declare function calculateSpread(amm: AMM, marketStats: MarketStats, oraclePriceData?: Pick<OraclePriceData, 'price' | 'confidence'>, now?: BN, reservePrice?: BN): [number, number];
|
|
342
342
|
/**
|
|
343
343
|
* Computes the AMM's one-sided bid and ask reserves — the reserves a long (ask side) or short
|
|
344
344
|
* (bid side) trade would actually execute against — by combining `calculateSpread`'s
|
|
@@ -357,7 +357,7 @@ export declare function calculateSpread(amm: AMM, marketStats: MarketStats, orac
|
|
|
357
357
|
* @param latestSlot Current slot; required for reference-price-offset smoothing to take effect (treated as 0 slots elapsed if omitted).
|
|
358
358
|
* @returns `[bidReserves, askReserves]`, each `{ baseAssetReserve, quoteAssetReserve }` in AMM_RESERVE_PRECISION (1e9).
|
|
359
359
|
*/
|
|
360
|
-
export declare function calculateSpreadReserves(amm: AMM, marketStats: MarketStats, mmOraclePriceData?: MMOraclePriceData, now?: BN, latestSlot?: BN): {
|
|
360
|
+
export declare function calculateSpreadReserves(amm: AMM, marketStats: MarketStats, mmOraclePriceData?: Pick<MMOraclePriceData, 'price' | 'confidence'>, now?: BN, latestSlot?: BN): {
|
|
361
361
|
baseAssetReserve: BN;
|
|
362
362
|
quoteAssetReserve: BN;
|
|
363
363
|
}[];
|
|
@@ -407,7 +407,7 @@ export declare function calculateTerminalPrice(market: PerpMarketAccount): BN;
|
|
|
407
407
|
* @param now Current unix timestamp (seconds), forwarded to `calculateSpreadReserves`.
|
|
408
408
|
* @returns `[baseAssetAmount, direction]`: `baseAssetAmount` is AMM_RESERVE_PRECISION (1e9), zero if the trade size would round to nothing.
|
|
409
409
|
*/
|
|
410
|
-
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, marketStats: MarketStats, limit_price: BN, direction: PositionDirection, mmOraclePriceData: MMOraclePriceData, now?: BN): [BN, PositionDirection];
|
|
410
|
+
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, marketStats: MarketStats, limit_price: BN, direction: PositionDirection, mmOraclePriceData: Pick<MMOraclePriceData, 'price' | 'confidence'>, now?: BN): [BN, PositionDirection];
|
|
411
411
|
/**
|
|
412
412
|
* Converts a quote-asset AMM reserve amount into the actual quote asset amount swapped
|
|
413
413
|
* (applying the peg multiplier), rounding by 1 in the direction that favors the AMM when
|
|
@@ -26,7 +26,7 @@ import { MMOraclePriceData, OraclePriceData } from '../oracles/types';
|
|
|
26
26
|
* straight-line interpolation; `lowerboundEst` further scales the spread down by the
|
|
27
27
|
* fraction of the current funding period that has elapsed since the last update.
|
|
28
28
|
*/
|
|
29
|
-
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, mmOraclePriceData?: MMOraclePriceData, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN, BN];
|
|
29
|
+
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, mmOraclePriceData?: Pick<MMOraclePriceData, 'price' | 'confidence'>, oraclePriceData?: Pick<OraclePriceData, 'price'>, markPrice?: BN, now?: BN): [BN, BN, BN, BN, BN];
|
|
30
30
|
/**
|
|
31
31
|
* Calculates estimated funding rates in human-readable form (plain JS `number` percentages,
|
|
32
32
|
* not BN), including a plain-English summary sentence. Values pass through floating-point
|
|
@@ -68,7 +68,7 @@ export declare function calculateLongShortFundingRate(market: PerpMarketAccount,
|
|
|
68
68
|
* @param now Current unix timestamp (seconds); defaults to wall-clock time.
|
|
69
69
|
* @returns `[markTwapLive, oracleTwapLive, longFundingRateEst, shortFundingRateEst]` — TWAPs in PRICE_PRECISION (1e6), rate estimates in the same scale as `calculateAllEstimatedFundingRate`.
|
|
70
70
|
*/
|
|
71
|
-
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, mmOraclePriceData?: MMOraclePriceData, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN];
|
|
71
|
+
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, mmOraclePriceData?: MMOraclePriceData, oraclePriceData?: Pick<OraclePriceData, 'price'>, markPrice?: BN, now?: BN): [BN, BN, BN, BN];
|
|
72
72
|
/**
|
|
73
73
|
* Estimated quote pool available to top up the smaller side's funding payment shortfall:
|
|
74
74
|
* one-third of the AMM's own retained equity (`totalFeeMinusDistributions`), floored at
|
|
@@ -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,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) {
|
|
@@ -141,10 +141,11 @@ export declare function calculateNetUserPnlImbalance(perpMarket: PerpMarketAccou
|
|
|
141
141
|
/**
|
|
142
142
|
* Calculates the price used to evaluate trigger (stop/take-profit) orders for a perp market,
|
|
143
143
|
* mirroring the Rust `get_trigger_price`. When `useMedianPrice` is true, the trigger price is the
|
|
144
|
-
* median of three candidates — the last fill price (or oracle price if there's been no fill
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* oracle price (tier A/B: 20bps,
|
|
144
|
+
* median of three candidates — the last fill price (or oracle price if there's been no fill or the
|
|
145
|
+
* last fill is older than `TRIGGER_PRICE_LAST_FILL_MAX_AGE`), the oracle price adjusted by the
|
|
146
|
+
* implied funding basis, and the oracle price adjusted by the 5min mark/oracle TWAP basis — then
|
|
147
|
+
* clamped to within a contract-tier-dependent band around the raw oracle price (tier A/B: 20bps,
|
|
148
|
+
* tier C: 100bps, others: 250bps) via `clampTriggerPrice`. This
|
|
148
149
|
* resists a single manipulated print (last fill or a momentary oracle/mark divergence) from
|
|
149
150
|
* triggering orders it shouldn't. When `useMedianPrice` is false, the raw oracle price is used
|
|
150
151
|
* directly with no smoothing.
|