Commit Graph

912 Commits

Author SHA1 Message Date
Christian Kamm 40b6b49680 Test: Max borrow with init asset weight scaling 2024-01-22 12:56:37 +01:00
Christian Kamm 8383109f0d
Add a token-token platform liquidation fee (#849)
The liqor liquidation fee and platform liquidation fee for the asset and
liab token are both payed by the liqee.

The platform liquidation fee is added to the Bank's
collected_fees_native and tracked in collected_liquidation_fees.
2024-01-19 16:34:55 +01:00
Christian Kamm 18729cf04c
Configurable interest rate for 0% utilization (#848) 2024-01-15 12:45:00 +01:00
Christian Kamm 53bc52efe7
Update openbook v1 fast listing default parameter (#841) 2024-01-09 15:14:52 +01:00
Christian Kamm ce0713070a Merge remote-tracking branch 'origin/deploy' into dev 2024-01-09 12:31:20 +01:00
Christian Kamm 857dcb397f
Allow fast listing admin to register openbook v1 markets (#839) 2024-01-08 14:18:43 +01:00
Lou-Kamades 9ce6b67831
Add an Orca oracle type (#813)
* deps: add whirlpool crate

* allow oracle to read price from Orca CLMM

* test: add test for raw orca CLMM price

* require the USD/USDC oracle when using a CLMM oracle

* test: add CLMM oracle tests

* use KeyedAccountReader instead of AccountInfoRef for fallback fetching functions

* calculate price for inverted orca pools

* ensure that Orca fallback oracles have USDC side

* remove unused Whirlpool impl

* clmm prices have correct decimals and pyth update slot

* manually deserialize the orca Whirlpool

* refactor: use OracleAccountInfos when checking oracle price

* properly handle inverted clmm prices

* update rs client with OracleAccountInfos struct

* refactor: simplify OracleAccountInfos impl

* add clmm oracle integration test

* use OracleAccountInfos::from_reader in account_retriever

* CLM oracles inherit deviation from supplementary quote oracle

* review fixes

* use f64 division for clmm sqrt price

* standardize fixed to f64 conversion

* review fixes
2024-01-04 11:29:54 -06:00
Britt Cyr 797e866f04
Add a type to the perp cancel order instructions when not found (#833) 2024-01-04 16:00:54 +01:00
Christian Kamm 34aebf01f2
oracle: don't force logging in OracleState functions (#828)
The msg!() caused writes to stdout in the liquidator etc in codepaths
that just wanted to check if the oracle was usable.
2024-01-03 08:37:35 +01:00
Christian Kamm f10fc01388
Fix idl generation, update it (#834) 2024-01-03 08:36:19 +01:00
Christian Kamm 30e639fe84 Bank: require borrows <= deposits
- require borrows <= deposits, even if vaults have more tokens
- change min_vault_to_deposits_ratio to be about max utilization

Reviewed and audited as part of the v0.21.1 release, original
commit was 5c2f857112
2024-01-02 11:05:58 +01:00
Christian Kamm 0e3b52b0ef Clamp utilization at 100%
To avoid excessive interest rates or interest curve moves.

Reviewed and audited as part of the v0.21.1 release, original
commit was 3914e48c24
2024-01-02 11:05:22 +01:00
Christian Kamm 79ddbf21f1
Liquidation: Use asset and liab liq fee (#825) 2023-12-28 10:46:43 +01:00
Maximilian Schneider e83052a036
helpers for estimating order sizes (#823) 2023-12-27 09:56:54 +01:00
Christian Kamm 428e71e07f
Allow delegates to withdraw small token amounts (#820)
- if they're withdrawing to the owner's ata
- if they're closing the token position
- if the amount is small
2023-12-20 11:15:47 +01:00
Christian Kamm b5614e98c3
test: Add interest rate test (#818) 2023-12-20 11:15:29 +01:00
Christian Kamm 7655a87404
Perp: Always allow users to free perp open order slots (#817)
Previously, freeing would be impossible if a canceling fill or out event
was already in-flight - then the order would no longer be on the
orderbook.

Now, FillEvent and OutEvent store the order id and can check if the open
order slot on the account has been reused already. That allows canceling
orders to always free up the user slot immediately.
2023-12-20 11:15:10 +01:00
Christian Kamm 86334020e2
Use custom allocator for the program (#801)
To allow making use of extended heap sizes.
2023-12-20 11:10:18 +01:00
Christian Kamm 27b1ceebe8 Accounts: Expect only the v3 MangoAccounts now (#783) 2023-12-19 10:08:46 +01:00
Christian Kamm 64d6c8c3c4
Add doc comments for new fields (#819) 2023-12-18 17:41:17 +01:00
Christian Kamm 99cb3d4f4c liquidator: Pick better asset/liab for liq_token 2023-12-15 11:40:25 +01:00
Christian Kamm 05f03f4ed8
test: Add one for migrating interest rates for v0.21 (#816) 2023-12-12 11:28:37 +01:00
Christian Kamm d6f46bec70 Fix program tests after cherry-picks 2023-12-07 13:23:18 +01:00
Christian Kamm 509afb01b6 adjust token_register_trustless params (#810)
- since there's no borrowing or asset weight, don't put staleness or
  confidence restrictions on the oracle
- disable the tcs fees, they are not enabled anywhere yet

(cherry picked from commit 5f8f717430)
2023-12-06 16:48:00 +01:00
Christian Kamm 5f8f717430
adjust token_register_trustless params (#810)
- since there's no borrowing or asset weight, don't put staleness or
  confidence restrictions on the oracle
- disable the tcs fees, they are not enabled anywhere yet
2023-12-06 16:46:57 +01:00
Christian Kamm db4e486643 Bump program version to v0.21.0 and update idl 2023-12-05 15:57:42 +01:00
Christian Kamm e7bfa4e03e Deposit limits (#806)
- limit deposits (via deposit, flash loan, tcs)
- limit potential deposits via openbook settle
  by restricting placable orders via potential_serum_tokens
- introduce Serum3PlaceOrderV2 for this purpose
- account for new limits in liquidator, max_swap

(cherry picked from commit 42e31ae859)
2023-12-05 15:57:26 +01:00
Christian Kamm f533d65a58 Openbook token tracking and price bands (#805)
- track min bid, max ask
- track maximal token outflow from oo
- add serum3_place_order_v2 with mutable receiver bank
- placing openbook orders is restricted to a certain distance from the
  oracle

(cherry picked from commit 81501837a9)
2023-12-05 15:48:57 +01:00
Christian Kamm 42e31ae859
Deposit limits (#806)
- limit deposits (via deposit, flash loan, tcs)
- limit potential deposits via openbook settle
  by restricting placable orders via potential_serum_tokens
- introduce Serum3PlaceOrderV2 for this purpose
- account for new limits in liquidator, max_swap
2023-12-05 15:43:38 +01:00
Christian Kamm 81501837a9
Openbook token tracking and price bands (#805)
- track min bid, max ask
- track maximal token outflow from oo
- add serum3_place_order_v2 with mutable receiver bank
- placing openbook orders is restricted to a certain distance from the
  oracle
2023-12-05 15:39:24 +01:00
Christian Kamm e2c9cf2de0 tests: drop extra debug output 2023-12-05 11:16:09 +01:00
Christian Kamm ce71c67a88 Bump program version to v0.22 2023-12-05 11:15:50 +01:00
Christian Kamm 99790a01e4 revert trustless token registration params to conservative defaults (#804)
This mostly reverts commit 33bc278a2c.

(cherry picked from commit 361ba23df1)
2023-12-05 10:14:48 +01:00
Christian Kamm 04f0f55aa5 Disable TokenAddBank instruction (#803)
It was unused for a long time and it should be reevaluated before being
used.

(cherry picked from commit f03944b5dd)
2023-12-05 10:14:36 +01:00
Christian Kamm 361ba23df1
revert trustless token registration params to conservative defaults (#804)
This mostly reverts commit 33bc278a2c.
2023-12-05 10:13:55 +01:00
Christian Kamm f03944b5dd
Disable TokenAddBank instruction (#803)
It was unused for a long time and it should be reevaluated before being
used.
2023-12-05 10:12:50 +01:00
Lou-Kamades 838df8cd7c
Merge pull request #790 from blockworks-foundation/lou/fallback-oracle
Add fallback oracles
2023-12-01 12:59:38 -06:00
Lou-Kamades 25aa422e2d also log primary oracle when fallback errors 2023-12-01 12:44:35 -06:00
Britt Cyr 68944747e0
Add serum3 cancel by client order id instruction (#798) 2023-12-01 13:36:29 +01:00
Lou-Kamades ab9d3c37b4 check serum OO headers in ScanningAccountRetriever 2023-11-30 11:03:24 -06:00
Lou-Kamades b19678f874 use a Vec for fallbacks in ScannedBanksAndOracles 2023-11-29 01:03:48 -06:00
Lou-Kamades 59001b3631 reduce CU cost of fallback oracles 2023-11-28 22:55:19 -06:00
Christian Kamm e9198577f1
deposit: remove debug output (#799) 2023-11-28 14:59:40 +01:00
Christian Kamm 6d80568022
Accounts: Expect only the v3 MangoAccounts now (#783) 2023-11-27 09:30:37 +01:00
microwavedcola1 236aee99d8
program: optimize cu usage in token deposit, by computing health only… (#786)
* program: optimize cu usage in token deposit, by computing health only if user is being liquidated

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

* Fixes from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

* Fixes from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

---------

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-11-23 08:59:33 +01:00
Lou-Kamades 2b0a0e3a3e add fallback CU and serum tests 2023-11-22 16:40:48 -06:00
Lou-Kamades aed2afbde4 ensure fallbacks are only used for stale or unconfident oracles 2023-11-22 11:42:32 -06:00
Lou-Kamades a0a47af6a4 ScanningAccountRetriever uses fallback oracles 2023-11-22 11:35:26 -06:00
Lou-Kamades 20261bf734 test: add fallback oracle withdrawal test 2023-11-22 11:35:26 -06:00
Lou-Kamades d4017e6038 FixedOrderAccountRetriever uses fallback oracles 2023-11-22 11:35:26 -06:00
Lou-Kamades c22db3be0c test: stubOracle is not a PDA (allow multiple with same mint) 2023-11-22 11:35:26 -06:00
Lou-Kamades 6288f2dd7d add fallback oracles to bank and mintinfo 2023-11-22 11:35:26 -06:00
Christian Kamm eceef44a96
Log emissions: Serialize to buffer on stack instead of using heap (#787)
Emitting many events could previously cause heap exhaustion.
2023-11-22 16:00:47 +01:00
Christian Kamm eeb172e44e
Bank: Close zero position when withdrawing 0 (#793) 2023-11-22 15:54:38 +01:00
Christian Kamm 57a9835aa8
Reduce HealthCache heap use (#785)
Growing these vectors is a waste, rough measurements say this saves
around 1k bytes for a 5 tokens and 4 serum account.
2023-11-20 10:15:39 +01:00
Christian Kamm 0ac930eb8d tests: fix build 2023-11-16 16:44:18 +01:00
Christian Kamm 2d1bf023c6
TokenRegister: Add arg for group insurance fund flag (#782) 2023-11-16 08:16:18 +01:00
Christian Kamm 93d85c3f71
Bank: allow maint weights to change over time (#780)
- token_edit can set it up to gradually scale to new target values
- security admin can abort an ongoing change via token_edit
- all health computations are now time dependent and get the weight
  based on it
- when the change is done, the keeper "cleans up" and moves the new
  values into the default fields
2023-11-14 14:52:04 +01:00
Christian Kamm 12d74789ef
cli: save-snapshot command (#773) 2023-11-08 10:27:55 +01:00
microwavedcola1 5ca5d8f78c
program: even delegate can create token position (#775)
* program: even delegate can create token position

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

* Fixes from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

---------

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-11-08 09:06:43 +01:00
Christian Kamm c49efb2213
Change conditions for perp settle incentive (#771)
- Only ever give an incentive when pnl is at least 1% of the position
  value. That way large positions (like $100k in SOL-PERP) don't get
  settled on 0.1% price fluctuations. The price now needs to change by
  1% for settlement to occur.
- For low health incentives, cap the percentual incentive at 2x the flat
  settle fee. We want to give the settler incentive to use these first,
  but the settler doesn't take on risk, so the reward doesn't need to be
  large.
2023-11-07 12:01:02 +01:00
Christian Kamm f690514638
rust client: ix cu limits based on health compute cost (#768)
Many instructions now return PreparedInstructions instead of a direct
Instruction or Vec<Instruction>. That way they can keep track of the
expected cu cost of the instructions for the compute limit instruction
that gets added once all instructions are made.
2023-11-03 11:20:37 +01:00
Christian Kamm 941f945999
fast listing: disable flash loan swap fee (#770) 2023-11-02 10:40:43 +01:00
Christian Kamm 26549ffd92
Bank: track deposits in serum oo accounts (#769) 2023-11-02 10:40:31 +01:00
Christian Kamm 5d08f4ae0f tcs trigger: consistent maker fee computation 2023-10-30 17:22:24 +01:00
Christian Kamm c5cbdd927f tcs start: defensive assert on sold <= max_sell 2023-10-30 13:43:15 +01:00
Christian Kamm a81d0918cc tcs start: use same naming convention as in other ix
liqee/liqor over account/caller
2023-10-30 13:41:19 +01:00
Christian Kamm a24e43c8a7 tcs trigger: remove dead code
deactivation of liqee token positions is impossible because they're in
use until the tcs is closed
2023-10-30 13:30:08 +01:00
Christian Kamm 15072be644 tcs trigger: simplify min buy check for expired 2023-10-30 13:25:53 +01:00
microwavedcola1 33bc278a2c
program: update trustless token registration interest rates and leverage and support borrowing (#760) 2023-10-19 10:21:11 +02:00
Christian Kamm 381ef7401d
Group: Limit fast-listing tokens per week (#756)
To avoid potential abuse of fast listing while even when the dao is
unresponsive.
2023-10-17 13:14:49 +02:00
Christian Kamm 3b28856692
Bank: store interest curve scale and target util separately (#755)
Allows for better configurability independent of the previous MAX_RATE
concept
2023-10-17 13:14:07 +02:00
Christian Kamm 8110dd1566
FlashLoan: Replace deposit fee by "swap fee" (#754)
Which applies to the in token amount of swaps only.

Charging a deposit fee on flash loans was a bad idea:
- It incentivizes flash loan users to make the deposit a separate
  instruction, defeating the purpose.
- For swaps, it makes traders pay a loan origination fee in in-token and
  a deposit fee in out-token, leading to more complex bookkeeping and ui
  display.

Instead, charge a fee on the in-token for all flash loans explicitly
marked as swaps only.
2023-10-13 09:02:23 +02:00
Christian Kamm c354f55423
v0.20.0 minor fixes from self-audit (#751)
* tcs::is_startable only true for premium auctions
* individual tcs premium auction / linear auction ix gates
* drop unnecessary I80F48::from(...)
* minor: adjust confusing wording in v2 mango account format doc
* idl update
* ts: prettier
2023-10-11 15:19:53 +02:00
Christian Kamm dfde7ec43f
serum health: use lower-precision division (#750)
This saves around 1/3rd of the compute for active serum markets.
2023-10-11 15:19:24 +02:00
Christian Kamm 20eb02af01
program-test improvements: performance, cu test output, warnings (#749)
* tests: Upgrade tx log capturing

Instead of overriding the system logger to intercept the logs, we can
now ask solana to return the logs of a tx execution directly.

This speeds up tests a lot because we don't need to hold a global lock
on tx execution anymore!

* tests: Improve token, serum, perp cu test
* benchmark: add a few more operations
2023-10-11 15:18:23 +02:00
Christian Kamm 817bf9bbc6
Tcs: linear and premium auctions (#717) 2023-10-07 21:27:19 +02:00
Christian Kamm 81fd7951dd
FlashLoanSwapBegin: small tx-size FlashLoanBegin for swaps (#744) 2023-10-06 16:18:59 +02:00
Christian Kamm fd997e2e2c Cargo: Remove explicit workspace-inheritance feature use
solana-release cargo version was updated
2023-10-05 11:05:12 +02:00
Steve f625284593
Update to solana 1.16.14 and anchor 0.28.0 (#718)
- Change fixed to be a git dependency (no more submodules!)
- Upgrade fixed to a version compatible with borsh 0.10
- Upgrade openbook-v2 dependency (for anchor compat)
- Move services from mango-feeds repo into bin/
- Update mango-feeds-connector

Co-authored-by: Christian Kamm <mail@ckamm.de>
Co-authored-by: Riordan Panayides <riordan@panayid.es>
2023-10-05 10:56:45 +02:00
Christian Kamm 1afcff5dc2
Changelog and version bump for v0.20.0 (#739) 2023-10-04 09:18:57 +02:00
microwavedcola1 2df4df048c
program: be more relaxed with max rate (#734)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-10-03 21:13:50 +02:00
Christian Kamm ea7fae4687 Changelog, version bump for program v0.19.1 2023-10-02 19:54:09 +02:00
Christian Kamm 1d241de75a
FlashLoan: Allow jup v6 for delegates (#737) 2023-10-02 19:40:43 +02:00
microwavedcola1 39ed3a3fb5
program: if position is not negative then continue ahead with withdrawal, also deactivate if position is 0 (#736)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-10-02 09:11:44 +02:00
Christian Kamm ac38ccc687
Drop HealthCache from idl, disable ComputeAccountData ix (#723)
It's used for tests, we don't want people depending on it.
2023-09-19 10:01:44 +02:00
Christian Kamm 2c4009eab1
Token deposit: Require valid oracle for new token positions (#722)
This avoids making an account accidentally or intentionally unusable
(except for deposits/withdraws) by depositing a token with a stale
oracle.
2023-09-19 09:08:32 +02:00
Christian Kamm 2adc0339dc
Serum3 open orders: Fix health overestimation (#716)
When bids or asks crossed the oracle price, the serum3 health would be
overestimated before.

The health code has no access to the open order quantites or prices and
used to assume all orders are at oracle price.

Now we track an account's max bid and min ask in each market and use that
as a worst-case price. The tracking isn't perfect for technical reasons
(compute cost, no notifications on fill) but produces an upper bound on
bids (lower bound on asks) that is sufficient to make health not
overestimate.

The tracked price is reset every time the serum3 open orders on a book
side are completely cleared.
2023-09-13 09:35:10 +02:00
Christian Kamm ba6bd96784
Allow account shrinking and migration (#692)
- The AccountExpand instruction can now shrink accounts by reducing
  the number of token/perp/serum/tcs/perp oo slots.
- A new AccountSizeMigration instruction can permissionlessly shrink
  accounts that are too large and migrate them to the v3 layout.
2023-09-04 09:30:57 +02:00
Christian Kamm c07978fb68
max_swap: Fix swapping between zero weight tokens (#699)
Co-authored-by: microwavedcola1 <microwavedcola@gmail.com>
2023-08-31 14:01:18 +02:00
Christian Kamm 03378bb808
FlashLoan: Log the approved amount (#696)
Previously only the part of the approved amount that was a loan got
logged, missing some interesting information.
2023-08-30 10:57:41 +02:00
Christian Kamm 020a978270
Account expansion: Transfer more lamports only if needed (#694)
Result of audit feedback
2023-08-29 10:03:24 +02:00
Christian Kamm 1d6ce550f3
FlashLoan: fee fixes (#693)
- Rename the new "swap fee" to "deposit fee" and let it apply to all
  deposits, not just for Swap-type flash loans.
- But don't apply it to withdrawals (effectively giving rebates!)

Result of audit feedback
2023-08-29 10:03:07 +02:00
Christian Kamm 6e2363c86f
Improve CU benchmarks (#690) 2023-08-24 16:33:08 +02:00
Christian Kamm f63163d737
Account limits: Prefer 4 perp + 4 serum (#689) 2023-08-21 16:26:49 +02:00
Christian Kamm a151ebcf11
Account size: restict more, but only increases (#688)
* Account size: Don't fail on unrelated resize

If the account was previously resized to larger than is allowed now,
don't fail unrelated resizes.

* Further reduce account size limits

Out of caution and future-proofing. Can always raise again.

Perp settle pnl needs 6 accounts plus 2 health account lists that could
be nearly fully disjoint.
2023-08-21 14:34:52 +02:00
Christian Kamm 34a875d968
Restrict account growth (#686)
Because of the 64-accounts-per-tx limit. Making a single account use
more than half of these could be problematic.
2023-08-19 08:20:40 +02:00
Christian Kamm 6fd158ff6c
account_create_v2: fix bad account size (#685) 2023-08-19 08:17:03 +02:00
Christian Kamm 3480b62945
Changelog and version bump for program v0.19.0 (#683) 2023-08-18 15:38:28 +02:00
Christian Kamm 6e9878e31d
Tcs trigger: Avoid building a health cache for expired tcs (#682) 2023-08-18 15:37:59 +02:00