Commit Graph

145 Commits

Author SHA1 Message Date
Christian Kamm 4f5ec41d7a
tests: Check mango account backwards compatibility (#878) 2024-02-14 10:00:09 +01:00
Christian Kamm e57dcdc2a9
Add collateral fees (#868)
- New permissionless instruction to regularly charge collateral fees
- Bank and group configuration to set rate and interval
- Keeper addition to call the instruction
2024-02-13 12:39:28 +01:00
Serge Farny 08a5ee8f53
rust client: Perp - Auto close account when needed (#875)
* perp: auto close perp market account when needing to open a new one with no slot available

* rust_client: do not send health accounts when deactivating a perp position (not needed on program side)

* rust_client: add perp place order command
2024-02-09 11:19:52 +01:00
Christian Kamm ae5907ba3a
fix perp settle limit materialization (#865)
Previously, we tried to keep track of "other" and "trade" realized pnl.
An issue occured when a perp base position went to zero: the way we
computed the trade pnl included potential non-trade unsettled pnl.

That caused follow-up trouble because the value could change sign and
reset the settle limit for trade pnl.

This change aims to simplify in some ways:
- explicitly talk about oneshot-settleable pnl (fees, funding,
  liquidation) and recurring-settleable pnl (materialization of settle
  limit derived from the stable value of the base position when reducing
  the base position)
- instead of directly tracking realized settleable amounts (which
  doesn't really work), just decrease the recurring settleable amount
  when it exceeds the remaining unsettled pnl
- get rid of the directionality to avoid bugs of that kind
- stop tracking unsettled-realized trade pnl (it was wrong before, and
  no client uses it) - we already track position-lifetime realized trade
  pnl
2024-02-01 11:23:45 +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 6d80568022
Accounts: Expect only the v3 MangoAccounts now (#783) 2023-11-27 09:30:37 +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 12d74789ef
cli: save-snapshot command (#773) 2023-11-08 10:27:55 +01: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 817bf9bbc6
Tcs: linear and premium auctions (#717) 2023-10-07 21:27:19 +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 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
microwavedcola1 8d924a8b9b
Delegate with expiry (#663)
* delegate with expiry

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

* update idl

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-08-09 12:05:16 +02:00
Christian Kamm 4f810edebc
Allow token withdraws/deposits even with stale oracles (#646) 2023-08-07 16:15:45 +02:00
Christian Kamm 5dd5c507f2
Tcs: reserve token positions while tcs is open (#647)
- Update in_use_count to u16
- Pass banks to TCS cancel instruction
- Increase the in_use_count when a tcs is created and decrease on
  trigger/cancel
2023-07-17 13:14:53 +02:00
Christian Kamm 35064d4ab0
Token conditional swaps (#604)
Users can request token swaps to happen when the oracle price
is within a price band. Once the price is right, an executor can
trigger the swap. The executors are rewarded with a premium
over the oracle price.

This allows limit and stop loss orders on arbitrary spot pairs.

The PR comes with basic ts support and adjustments to the liquidator,
to execute available token conditional swaps.

Co-authored-by: microwavedcola1 <microwavedcola@gmail.com>
2023-07-03 14:09:11 +02:00
Christian Kamm 57e1d981ac
Health: Allow actions while init health does not decrease (#592)
Instead of requiring a strict increase when init health < 0.

This allows users to still place reducing limit orders on the spot and
perp orderbooks as long as these orders keep the health unchanged.
2023-06-15 10:44:11 +02:00
Christian Kamm 66c6926419
Audit fixes for the fee buyback feature (#608)
* reduce_buyback_fees_accrued: forbid reducing more than accrued
* Buyback fees: Expire accrued during event processing
2023-06-15 10:43:31 +02:00
Christian Kamm 5a05e9cb2e
Update anchor to v0.27.0, stop using submodule (#582) 2023-05-12 13:54:53 +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 8559d3ef07
Perp funding: Fix logging in update funding + deactivate pos (#528) 2023-03-31 10:58:04 +02:00
Christian Kamm e4d46c3c5b
Buyback fees: Add staggered expiry (#478) 2023-02-27 16:36:27 +01:00
microwavedcola1 d88d44b34a
buyback perp fees with mngo at a discount (#464)
buyback perp fees with mngo

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-02-25 20:34:16 +01: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 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 5328003183
Perp: Reorganize fill events, add client order id to LeafNodes (#426)
- don't store internal order ids: unneeded
- provide the maker client order id for user correlation
- store fees as f32 to waste fewer bytes
- new FillLogV2
2023-02-02 09:15:06 +01:00
Christian Kamm be37f33946 Perp: Add PerpOpenOrder::is_active_for_market() 2023-01-20 14:42:35 +01:00
microwavedcola1 3ed416a97f
Fix type (#381)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-13 10:20: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 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
Christian Kamm c5d875e04d
Perp settle limit extension to realized pnl (#359)
Co-authored-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-11 14:32:15 +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 57e4510b08 Remove mango-macro 2022-12-08 20:48:44 +01:00
Christian Kamm a29a736ba2 Move src/state/health/ -> src/health/ 2022-12-08 20:48:44 +01:00
Christian Kamm 450007d34b
Perp: Drop client order id from LeafNodes (#323)
This saves 8 bytes and only the FillEvents, FillLogs were populated from
the data.
2022-12-08 19:55:32 +01:00
Christian Kamm 375b2b3fb3
Use bytemuck::Pod derives to check for accidental padding (#317) 2022-12-06 09:25:43 +01:00
Christian Kamm e0c403dcb9
Client: consider borrow limits in max-swap (#316) 2022-12-06 09:25:24 +01:00
microwavedcola1 25312bc398
struct layout reorg + reserved pass (#315)
* reorder structs + review reserved space + split const assets into individual fields for correct checking
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>
2022-12-05 15:23:20 +01:00
microwavedcola1 d64d9285f4
limit settleable pnl (#295)
Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-11-30 13:20:19 +01:00
Christian Kamm 502f0767a8
Adjust avg entry price / break even price computation (#304)
- Don't return result as I80F48, f64 is more honest
- Allow access to avg entry price per base lot without division
- Reset break even price when position changes sign
- Fix occasionally-failing fast division test
- Fix sign of break even price -- it can now be negative if the
  position has broken even.
2022-11-25 09:59:52 +01:00
microwavedcola1 e8ba511c45
mc/flash loan for delegate (#271)
* flash loan for delegates

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

* Fix rust client

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

* prettier

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-11-09 09:35:13 +01:00
Christian Kamm 5731ce8faa
Perp oracle peg feature (#264)
This introduces the ability to use oracle peg orders on perp markets.

This PR has significant non-backwards compatible changes, for example all
order trees are now in a single account instead of separate.
2022-11-08 15:27:56 +01:00
microwavedcola1 cae10bcdae Merge branch 'dev' 2022-11-02 11:52:43 +00:00
microwavedcola1 a66177a77e
ts: match client class definitions 1-1 to program state struct definitions (#268)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-10-11 09:34:02 +02: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
microwavedcola1 5b549fb59f
program: fix bug where expansion would not move positions whose size remained same (#262)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-10-03 13:06:04 +02: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