Commit Graph

50 Commits

Author SHA1 Message Date
Christian Kamm a7aaaff07e
allow skipping banks and oracles in fixed-order health account list (#891)
The following instructions now allow skipping banks and oracles if health
and the token position balance is not negative:
- token_withdraw
- token_deposit
- serum3_place_order
- perp_place_order
- flash_loan

They also allow skipping oracles that are stale.
2024-03-04 15:49:14 +01: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 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
microwavedcola1 d51c3ee85d
release 19.1 -> deploy + serum3 open orders estimation ts patch (#719)
* 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.

(cherry picked from commit 2adc0339dc)

* Changelog, version bump for program v0.19.1

* ts: ts patch for the PR

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

---------

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
Co-authored-by: Christian Kamm <mail@ckamm.de>
2023-09-16 11:04:38 +02:00
Christian Kamm 4e45653c95 Audit fixes (#613)
* TokenForceCloseBorrows: Respect reduce-only flag
* ForceCancelOrders: Readability of early-out condition

(cherry picked from commit b5c9f6d039)
2023-06-16 13:35:24 +02:00
Nicholas Clarke 23761ef28c
Add price to WithdrawLoanLog (#603) 2023-06-14 09:41:54 +02:00
microwavedcola1 497012042c
Force close for serum3 market (#551)
* force close for serum3 market

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

* Fixes from review

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

* always require admin

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>

* Fixes from review

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

* Fixes from review

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

* format

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

* Fixes from review

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

* update client

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-04-20 10:19:23 +02:00
Christian Kamm b7dd8e0663
Fee buyback: Respect USDC oracle (#504) 2023-03-20 11:18:11 +01:00
Christian Kamm 252210d194
Serum settle funds V2: fees can go to users (#484)
Co-authored-by: microwavedcola1 <microwavedcola@gmail.com>
2023-03-03 14:04:45 +01:00
Christian Kamm df4a7b0474
Fees buyback: Credit users for ui part of serum fees (#481)
At least for markets with USDC quote currency.
2023-02-28 09:48:15 +01:00
Christian Kamm f75e3ee656
Serum: Track referrer rebates as Mango platform fees (#469)
And start sharing code between serum3 settle and force cancel
2023-02-22 12:04:21 +01:00
Christian Kamm 8916c28b99
License: Mark instructions GPLv3 and everything else MIT (#454)
To do that, split up the Accounts objects and the instruction
implementations.

GPL code is only used when the "enable-gpl" feature is enabled. That
means compiling the program or running tests need explicit feature
activation now.
2023-02-15 08:42:07 +01:00
Christian Kamm f6abd9579d
Perp cancel all: Don't error when orders are filled/expired (#453) 2023-02-14 10:44:51 +01:00
Christian Kamm 1f66bef88f
Change liquidation end target to a new, third health type (#447)
Due to the safety features in v4, the init health can differ from maint
health a lot more than it used to in v3. This is because of stable-price
adjusted oracle prices used in init health, and the weight scaling based
on total deposits and borrows used in init health.

The effect is that once an account becomes liquidatable, it could be
liquidated a lot until it reaches init>=0.

The original idea of liquidating until init>=0 was just to provide some
buffer, such that liquidated accounts wouldn't immediately become
liquidatable again.

This patch decouples the buffer idea explicit from init health by
creating a new LiquidationEnd health type. Liquidation proceeds until
the LiquidationEnd health becomes positive.

Co-authored-by: microwavedcola1 <89031858+microwavedcola1@users.noreply.github.com>
2023-02-10 09:00:36 +01:00
Nicholas Clarke 4c93a2740a
Add market index to Serum3OpenOrdersBalanceLog (#429) 2023-02-02 10:01:19 +01:00
microwavedcola1 0012907ee3
Mc/ix toggler (#419)
* add gating to individual ixs

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>

* 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-01-26 20:27:39 +01:00
Christian Kamm 5227daa0b8
Allow serum/perp order force cancelling on frozen accounts (#374) 2023-01-13 08:13:15 +01:00
microwavedcola1 7c69197505
mango account freeze (#372)
* mango account freeze

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

* format

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-01-12 13:08:10 +01:00
microwavedcola1 5ef04d6d08
group halt (#370)
* group halt

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

* Fixes from review

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

* format

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-12 09:12:55 +01:00
Christian Kamm 31bd72e84a Drop AccountLoaderDynamic
This gives us better compatibility with released anchor versions.

Instead of using AccountLoaderDynamic<MangoAccount>, we now use
a standard AccountLoader<MangoAccountFixed>. This will generally work
(except for load_init(), which is dangerous).
A new trait, MangoAccountLoader, provides load_full(), load_full_mut()
etc on the AccountLoader<MangoAccountFixed> to create accessor structs
that can read and write to the dynamic part of the mango account data.
2022-12-29 17:40:08 +01:00
Christian Kamm a29a736ba2 Move src/state/health/ -> src/health/ 2022-12-08 20:48:44 +01:00
Christian Kamm c8f1f3c821
Net borrow limits: Limit in quote, not native (#312)
* Net borrow limits: Limit in quote, not native
* make perp settle limit disableable
2022-12-02 12:24:11 +01:00
Christian Kamm cf34a5b4b7 Health: Add soft deposit and borrow limits 2022-12-02 08:20:22 +01:00
Nicholas Clarke 1320451e1a
Add serum open orders balance logging. Remove price from token and perp balances. (#236) 2022-09-23 10:42:43 -07:00
Christian Kamm 801b68b93c MangoAccount: add unittests 2022-08-30 13:11:47 +02:00
Christian Kamm 8e30870689 SerumLiqForceClose: Proper health/liq checking 2022-08-30 12:05:07 +02:00
Christian Kamm e0437305ee SerumPlaceOrder: Only pass the payer bank/vault 2022-08-30 12:05:07 +02:00
Christian Kamm dc4acd0dd7 Serum: loan origination fee, don't auto-settle, fix vault check
- Loan origination fees: The previous approach of tracking the reserved
  amount did not work because OutEvents will also reduce the reserved
  amount. This means we can't know if it was an OutEvent-cancel or an
  order execution that caused the reduction.

  Instead, we now track the amount of borrows that was made (without
  applying origination fees) in place order. Whenever we try to settle
  and the amount of tokens on the oo account is less than the potential
  borrows, we can be certain that the borrow has actualized.

- Place order is no longer automatically followed by a settle.

  This can reduce compute use when people want to place multiple orders
  in sequence. Now they can use the HealthRegion instructions to place
  their orders, settle once at the end, and then have health checked.

- Vault check: Place order previously rejected valid orders because it
  didn't consider that there could be free tokens on the oo account.

- Tests: Some infrastructure for less verbose serum testing.
2022-08-30 08:38:13 +02:00
Christian Kamm d63c5bfc76 Serum: Don't attempt deactivating token positions after serum actions
While a serum open orders is active, the base and quote token positions
for it are locked to active. It's pointless to check if they need to be
deactivated after place/settle etc.
2022-08-26 11:45:54 +02:00
Christian Kamm 1a95a84ed4 Serum: Comments on account constraints 2022-08-26 11:45:54 +02:00
Christian Kamm 45f52ae535 Serum3PlaceOrder: compute pre-health 2022-08-24 14:15:01 +02:00
Nicholas Clarke 704dfcaa27
Clarkeni/loan fee logging (#180)
Logging for loan origination fees and token bankruptcy
2022-08-19 18:50:54 -07:00
Christian Kamm 09fc5f716b Renames in MangoAccount
- Accessors in MangoAccountValue
- PerpPositions -> PerpPosition
2022-08-19 14:58:20 +02:00
Christian Kamm 92a37f23ed Remove is_bankrupt
Instead, check for any liquidatable assets in liq_token_bankruptcy.

Bankrupt accounts may use the same operations as any other
negative-health account.
2022-08-11 22:57:53 +02:00
microwavedcola1 0b2e1e6e6c
Mc/realloc 4 (#119)
Realloc + dynamic mango account

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-07-25 16:07:53 +02:00
Christian Kamm 6c7245adaa Serum3 liq force cancel orders: Deactivate inactive token positions 2022-07-07 13:22:25 +02:00
Christian Kamm 31831454b4 Return raw position index from get_mut() 2022-07-07 12:01:18 +02:00
Christian Kamm 35ca4d31b3 liq_token_bankruptcy instruction for socialized loss 2022-07-06 09:07:49 +02:00
Nicholas Clarke f8da1f6a40
Clarkeni/logging (#81)
* Add logging

* Added new_fixed_order_account_retriever to allow us to more easily access oracle prices outside of health calculations for logging purposes

* Add token balances logging to token and token liquidation and add logging to margin trade

* rust format

* fix clippy errors

* Address PR requested changes

* fix flash_loan

* Recalculate raw_token_index in token withdraw to account for position becoming inactive

* Fix retrieving oracle for logging

in get_mut_or_create(), return the raw index into the account's token
positions as well as the index into active positions only. The latter
index is useful for indexing into banks in the health account list.

* Add logging flash_loan2 and flash_loan3

* Refactoring flash loan logging

Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-06-30 14:35:05 +02:00
Christian Kamm 20fc9d557a Satisfy anchor's lint by adding CHECK: comments 2022-06-24 12:36:35 +02:00
Christian Kamm 8a8b36e345 Fold loan origination fee handling into Bank 2022-05-18 22:53:03 +02:00
Christian Kamm cbc3cc7bbd MangoAccount: Rename how tokens/serum3/perps data is stored
token_account_map -> tokens
serum3_account_map -> serum3
perp -> perps
2022-04-02 13:51:26 +02:00
Christian Kamm 9cf4f1a748 Rearrange MangoAccount to avoid padding bytes 2022-03-31 14:01:08 +02:00
Christian Kamm babbff6bec Add is_bankrupt check to most instructions 2022-03-30 12:24:49 +02:00
Christian Kamm 69426d6d96 liq: Introduce HealthCache to get init/maint health after liquidation
The concept only works if all serum3 open orders have been fully
cancelled and settled.
2022-03-29 09:18:13 +02:00
Christian Kamm 82819720e0 liq token: progress
Change bank.deposit()/withdraw() to work with I80F48 values so we don't
need to round during liquidation.
2022-03-27 14:46:51 +02:00
microwavedcola1 5919a5a1df Merge branch 'main' into mc/clippy 2022-03-23 09:53:50 +01:00
microwavedcola1 f135ca4d9b fix clippy warnings
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-03-23 09:33:51 +01:00
Christian Kamm bfcb672116 Health: allow account order to be fixed, or to scan for accounts
The latter is important when we want to compute health for two different
accounts. Then the remainingAccounts would hold the union of the
necessary accounts, and each health check would need to look for the
accounts that it needs.

This patch al so reduces the compute needs for fixed-layout health
checks significantly (Deposit for an account with 8 tokens and 7 markets
takes 65k instead of 70k cu)
2022-03-22 14:13:07 +01:00
Christian Kamm 34d14ef267 Serum: serum3_liq_force_cancel_orders instruction
Still needs a test.
2022-03-21 17:39:52 +01:00