Commit Graph

34 Commits

Author SHA1 Message Date
Christian Kamm d9a9c7d664
liquidation: Add flag to disable asset liquidation (#867)
This can be used to remove the oracle dependency for tokens with zero maint asset
weight that are not borrowable.
2024-02-07 12:52:01 +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 79ddbf21f1
Liquidation: Use asset and liab liq fee (#825) 2023-12-28 10:46:43 +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
Nicholas Clarke 23761ef28c
Add price to WithdrawLoanLog (#603) 2023-06-14 09:41:54 +02:00
Nicholas Clarke defbd8c2b1
Add loan amount to WithdrawLoanLog. Add withdraw loan logging to toke… (#599)
* Add loan amount to WithdrawLoanLog. Add withdraw loan logging to token_force_close_borrows_with_token

* Address review comments.
2023-06-11 15:37:33 -07:00
Britt Cyr 2ea8f73ad1
Fix typo in token_liq_with_token comment (#594) 2023-05-25 12:35:22 +02:00
Christian Kamm 5fc7aa1092
Configurable perp settle token (#550)
This changes perp market margining to no longer assume all pnl is in USD
while settlement is in USDC. Instead, a configurable settle token is used for
pnl and settlement, defaulting to USDC. 

There is no difference while the USDC price is forced to $1 and the init and liab
weights are 1. But with this patch, it becomes possible to change that.

For now it is not recommended to use a token other than USDC or USDT (or
another USD targeting stable token) for perp settlement.

The patch also updates all insurance vault use to be aware that the insurance
fund is not in USD but in USDC and apply the USDC price before payouts.
To do this, the previous PerpLiqNegativePnlOrBankruptcy was replaced by
a new PerpLiqNegativePnlOrBankruptcyV2 instruction.

Co-authored-by: microwavedcola1 <89031858+microwavedcola1@users.noreply.github.com>
2023-05-17 15:50:05 +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
microwavedcola1 2dca3003df
Don't incentivise using asset tokens with high liquidation fee during liquidation (#536)
* Don't incentivise using non-usdc coins to liquidate tokens

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

* fix tests

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

* fix tests

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

* review: add back test comment

---------

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
Co-authored-by: Christian Kamm <mail@ckamm.de>
2023-04-13 12:43:31 +02:00
Christian Kamm da2a7f4e0c
Net borrow limit: Separate out tracking and checking (#534)
That way it's easier to be specific about where the limit should be
checked.
2023-04-13 08:56:33 +02:00
Christian Kamm 5c7a2e3e10
Use the overflow-checks=true equivalent with the fixed crate (#476)
* Vendor `fixed` crate to have checked math in release mode
* remove all cm!()
* drop superfluous parens
* drop use of checked_math crate
* manual removal of redundant checked_* functions
2023-02-24 11:56:33 +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 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
Christian Kamm 2cb4da8b58
Liquidation: Fix amounts when stable price != oracle price (#440)
Previously liquidation would overestimate the amount required, because
it used the oracle price for computing the health gain from
liquidation.

Now it uses the correct (stable price adjusted) price for figuring out
the amount of liquidation required, while still executing at
fee-adjusted oracle price.
2023-02-08 09:05:17 +01:00
Christian Kamm bac15698e6
Add explicit self-liquidation checks (#427)
Just for safety, the other conditions and borrowing pattern would
already make it impossible.
2023-02-01 16:15:58 +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 b3aabfadfc
Perp: Clarify has_open_orders use during liquidation (#412) 2023-01-23 10:45:45 +01:00
Christian Kamm 5227daa0b8
Allow serum/perp order force cancelling on frozen accounts (#374) 2023-01-13 08:13:15 +01:00
microwavedcola1 b4b7085e67
fix clippy (#373)
* fix clippy

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

* Fixes from review

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

* add clippy whitelist trait

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-12 14:26:55 +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 93d33edb74
Perp: liq with token instruction (#368)
The new instruction allows the liqor to take over negative pnl (limited
by liqee settle health and settle limits) before applying the bankruptcy
logic.
2023-01-12 09:07:13 +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
Christian Kamm 9757f7a509 Fix perp liq computation; doc liq fee in token liq 2022-12-02 08:19:29 +01:00
microwavedcola1 1732a5aff4
net borrow limits (#301)
* net borrow limits

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

* fix client

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

* Fixes from review

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

* Fix tests

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-11-25 13:45:17 +01:00
Nicholas Clarke 96344d7de1
Clarkeni/perp logging (#265)
* Perp instruction logging
* Onchain funding calculation
* perp_spot_transfers field on perp positions
* Logging on register token, perp market
* Additional fields on update token index logging
* maker and taker volume fields on perp position
2022-10-07 12:12:55 -07:00
Nicholas Clarke 01a958cd22
Clarkeni/onchain interest (#244)
* Onchain interest calculation
* Fix to TokenBalanceLog for token_liq_bankruptcy (was previously using liqee liab position for liqor liab position).
* Log cumulative interest when token position is deactivated.
2022-09-28 23:04:33 -07: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 50be520e99 Rename: LiqToken* -> TokenLiq* 2022-09-14 16:22:42 +02:00