Commit Graph

912 Commits

Author SHA1 Message Date
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
Christian Kamm 50c820ddce
Perps: Rename pnl_asset_weight -> overall_asset_weight (#428)
Calling it "pnl" was confusing, because the thing that is weighted isn't
the pnl, but `(weighted base value + quote position)`.
2023-02-01 16:15:45 +01:00
Christian Kamm d3cbb9e2ec
Create a single rust test binary (#425)
Speeds up the linking step, needs way less disk space for test binaries
and executes tests significantly faster.

Test execution went from 35s -> 29s for me and compilation improved a
lot.
2023-01-30 21:09:47 +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
microwavedcola1 f30ebf4ff8
logging + prevention of setting admin to default pk (#423)
* logging + prevention of setting admin to default pk

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-25 15:33:19 +01:00
microwavedcola1 0192728efc
return order id from a perp place order ix (#417)
* return order id from a perp place order ix

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

* fix

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-25 09:03:35 +01:00
microwavedcola1 dae02885c2
log old and new in edit ixs (#418)
* log old and new in edit ixs

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

* fix

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-25 09:02:27 +01:00
Christian Kamm 1224fa4297 Bump program version to v0.5.0 2023-01-24 10:24:50 +01:00
Christian Kamm 4fd8139854
Token deposit: put expensive computation behind gates (#421) 2023-01-24 10:05:37 +01:00
Christian Kamm eb079e1f4c
PerpEditMarket: Fix permission for oracle change (#420) 2023-01-24 10:03:10 +01:00
microwavedcola1 80f4bd8048
security admin can bring markets to reduce only (#394)
* security admin can bring markets to reduce only

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

* fixes from review

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

* fix from review; security admin can only turn on reduce only, security admin can only reduce init asset weight

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

* --wip-- [skip ci]

* 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-23 15:02:48 +01:00
microwavedcola1 64dda20cb5
group level token deposit limit (#415)
* group level token deposit limit

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

* fix

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

* fix

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

* fix

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-23 14:58:03 +01:00
microwavedcola1 f0c797a2e4
zero out perp position and orders for a broken market (#413)
* zero out  perp position and orders for a broken market

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

* fix

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

* client code

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-23 12:50:20 +01:00
Christian Kamm b3aabfadfc
Perp: Clarify has_open_orders use during liquidation (#412) 2023-01-23 10:45:45 +01:00
Christian Kamm 8b4a4c82fb Perp: move Book::new_order() code around 2023-01-20 14:42:35 +01:00
Christian Kamm be37f33946 Perp: Add PerpOpenOrder::is_active_for_market() 2023-01-20 14:42:35 +01:00
Christian Kamm 30ebf173ee Perp: documentation in bookside_iterator.rs 2023-01-20 14:42:35 +01:00
Christian Kamm d65e0794f4 Perp: Documentation in nodes.rs 2023-01-20 14:42:35 +01:00
Christian Kamm 549d9a8108 Perp: Remove EventQueue::empty() - is_empty() exists as well 2023-01-20 14:42:35 +01:00
Christian Kamm 1b4140c078 Perp: Don't allow orders with effective price_lots<1 2023-01-20 14:42:35 +01:00
Christian Kamm e7b51d1dbf
Perp: Simplify consume events (#406) 2023-01-19 13:41:36 +01:00
Christian Kamm a28fd49099 Delete perp_liq_bankruptcy again
Snuck back in in unrelated PR, but was unused
2023-01-19 12:59:24 +01:00
microwavedcola1 44f599ca62
dont process orders which have 0 lots; dont return early, we still want to apply ioc penalty (#404)
* skip iteration if the incoming order has no lots to process

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-19 11:29:00 +01:00
Christian Kamm 1a3d0da34a Perp market edit: Separate out stable price resetting, like for tokens 2023-01-19 09:33:27 +01:00
Christian Kamm 2db617cea5 Perp ix: Mention and reference important constraints 2023-01-19 09:33:27 +01:00
Christian Kamm f16f1a6c92 Perp market field documentation pass 2023-01-19 09:31:59 +01:00
Christian Kamm 0a86257309 Perp: Move best_price/impact_price from Orderbook to BookSide 2023-01-19 09:31:59 +01:00
Christian Kamm 886a6696da
Perp audit: ix constraint fixes (#399) 2023-01-19 09:31:03 +01:00
Christian Kamm ff90c85874
Liquidator: reduce perp base pos if possible (#398)
- send reduce-only ioc order if possible
- settle pnl if needed
- deactivate position
2023-01-18 10:50:23 +01:00
Christian Kamm 71b13efc95 Bump program version to v0.4.0 2023-01-17 15:01:04 +01:00
microwavedcola1 be15f54225
agg pub slot was recent even if price was not updated, switch to last slot which seems more relevant (#393)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-17 14:48:06 +01:00
Christian Kamm 9346c8e546
Perps: track overall realized pnl relating to a position (#392)
This includes trade pnl, funding and fees. Tracking this makes it easier
for uis to display a consistent position overall pnl value that doesn't
decrease by settling.

Co-authored-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-17 14:07:58 +01:00
Christian Kamm ec99376a8f
Perp: Allow setting pnl asset weights (#391)
This replaces the previous distinction between trusted and untrusted
markets, they are equivalent to setting the asset weights to 1 or 0
instead.

This way, we can weigh positive pnl in the trusted case at less than 1
which is more correct from a risk point of view and allows for more
flexibility when it comes to liquidation.

Co-authored-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-16 16:49:09 +01:00
Christian Kamm 15b9b911d1 Bump program version to 0.3.0 2023-01-13 11:38:56 +01:00
Christian Kamm c238ed4f28 Bump version to v0.2.0 2023-01-13 11:25:08 +01:00
Christian Kamm bda8cb9387
v0.1.2 version bump and changelog (#383) 2023-01-13 10:36:47 +01:00
Christian Kamm 42657dcf80
Drop unused HealthCache function, rename ClampedToNum (#382) 2023-01-13 10:21:01 +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 a7a5c740c9
Fix typo (#375)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-12 20:39:46 +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 c5d875e04d
Perp settle limit extension to realized pnl (#359)
Co-authored-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-11 14:32:15 +01:00
microwavedcola1 292cc9a450
bonk (#363)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2023-01-05 20:08:45 +01:00
silas 15784ecd2b
add security policy and on-chain metadata (#361)
* add security-txt macro

* minor fmt fix
2023-01-04 18:13:29 +00:00
microwavedcola1 86c9331647
reduce only order type and mode for tokens and perp markets (#353)
* reduce only order type and mode for tokens and perp markets

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

* Fix from review

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

* Fix from review

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

* Fixes from review

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

* tests

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

* remove token

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

* fixes

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-04 09:24:40 +01:00
Christian Kamm b304ce09ec Perp: Make settle limit robust against settle window size changes 2023-01-03 14:52:04 +01:00
Christian Kamm 7a8f31c611 Perp: settlement applies no loan origination fee
It's likely for perp pnl to oscillate and be settled in positive and
negative direction repeatedly. If the user's USDC balance is <= 0, every
settlement of negative pnl will increase borrows and cause loan
origination fees.

Over time the fees accumulated this way could be significant. Skipping
loan origination fees for borrows created from perp settlement avoids
that.
2022-12-30 12:54:03 +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
microwavedcola1 770b5a8056
pop perp events without processing, when mango account is closed (#351)
* pop perp events without processing, when mango account is closed

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

* fix

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-12-21 10:21:10 +01:00
Christian Kamm 6439791233 Token deposit: limit while being_liquidated
While the flag is true, deposits that don't bring the account health
over the init threshold are forbidden. This makes it impossible for
users to drag out liquidation by continuously depositing tiny amounts.
2022-12-20 14:12:46 +01:00
Christian Kamm 55e8fc390f Liquidation: Ignore dust balances and perp pnl
Liquidation and perp pnl settlement may not bring spot amounts to
exactly zero. Having a small spot amount < 1 native token can otherwise
make the liquidation get stuck and not move one to the next phase.
2022-12-19 12:49:09 +01:00
Christian Kamm 071dc34ccc
Client: Rework max_swap function (#340)
* Client: Rework max_swap function

Determining the amounts at which the health function changes slope
becomes too difficult when serum3 open orders come in.

Instead, generally find the function's maximum and then work with that.

* max_swap: Add test for swapping into a trustless token

* ts port

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
Co-authored-by: microwavedcola1 <microwavedcola@gmail.com>
2022-12-16 16:33:37 +01:00
Christian Kamm 5a38506f04
Liquidator, liqtest and rust client fixes (#336)
Liquidator, liqtest and rust client fixes

- Make rust MangoClient fully async
- Update and improve liquidation test setup scripts
- Update liquidator to use versioned transactions
- Update liquidator to use mango and jupiter account lookup tables
- Use jupiter v4 api to get versioned transactions

Perp liquidation is still not fully good. See discussion aboult perp_liq_pnl_with_token.
2022-12-16 13:10:46 +01:00
Christian Kamm 1c36b1b493 Fix rust test compilation 2022-12-16 13:07:19 +01:00
microwavedcola1 df4a48a558
dont do checks force close flag is used explicitly (#341)
* dont do checks force close flag is used explicitly

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

* comment

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

* comment

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

* Fix ts

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-15 21:10:56 +01:00
microwavedcola1 4169ccb960
enforce min vault to deposits ratio in flash loan and serum3 place order, in addition to token_withdraw (#338)
* refactor

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

* Enforce min vault to deposits ratio in flash loan and serum3 place order

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

* refactor

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

* refactor

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-15 18:27:22 +01:00
microwavedcola1 51cded4965
Mc/health max swap with serum effects (#333)
* rearrange, log before expect

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

* rearrange, log before expect

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

* Fix test

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

* health max swap with serum effects

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

* max swap: Fix with serum reserved amounts

* port rust code to ts

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

* port tests

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

* Fix method call

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

* Fixes from review

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-12-14 09:21:45 +01:00
Christian Kamm 0ed7175341 Upgrade to solana ~1.14.9
- Update anchor submodule
- Fork and submodule switchboard-v2 package to update dep
2022-12-13 17:00:57 +01:00
Christian Kamm e0f50bf0d4
pnl limit: Clean up by adding functions, using it in get-settleable (#329) 2022-12-10 17:56:56 +01:00
Nicholas Clarke e2295b2daa
Add instantaneous interest rates to update index log (#314) 2022-12-09 10:14:06 +01:00
Christian Kamm ec41fe4413 Don't compile the margin-trade fixture all the time 2022-12-08 20:48:44 +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 b66dc7ae80 IDL: Fix missing OrderTreeRoot 2022-12-08 11:22:37 +01:00
microwavedcola1 fa815f1c75 switch to new vanity address
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-12-08 10:23:12 +01:00
microwavedcola1 7d9c3616af
ts client changes (#320)
* cleanup + small sync with program
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

* Fixes from review

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

* Update lock file

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

* Fix tsc errors

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-12-08 10:16:06 +01:00
Christian Kamm 947d9b2b60
Split into perp bids and perp asks (#322)
Separate, smaller accounts that are easy to extend if needed.
2022-12-07 21:03:28 +01:00
Christian Kamm c44dc045cf
Perp: Merge ordertrees for fixed and oracle_pegged (#321)
By sharing the nodes list we're much less likely to grossly
over-allocate space.
2022-12-07 20:54:18 +01:00
Christian Kamm 777c1a7804
Rename bank fields, make weight scale limits editable (#319) 2022-12-06 09:34:02 +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
Christian Kamm a5a015e19f LeafNode: tif field u8 -> u16
Allows for longer TIF durations
2022-12-05 15:28:13 +01:00
Christian Kamm cbaa0098d6 LeafNode: tag field is u8 instead of u32 2022-12-05 15:28:13 +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 fa09c557a1
ts client sync with program (#311)
mostly health related code
2022-12-02 15:48:43 +01:00
Christian Kamm 9bfa574ee6 Doc: note about final health slope in max_swap_source 2022-12-02 14:08:13 +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 512eef96ea Perp pegged orders: Add stub max oracle staleness arg 2022-12-02 08:19:51 +01:00
Christian Kamm 9757f7a509 Fix perp liq computation; doc liq fee in token liq 2022-12-02 08:19:29 +01:00
Christian Kamm 524fe110e3
Health client: Fix max perp trade (#309)
Previously we didn't take the health cap for untrusted perp markets into
account correctly.
2022-12-01 11:26:01 +01:00
Christian Kamm 7bbf045823 Separate health code into multiple files 2022-11-30 13:21:32 +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 f6d189ce29 Test fixes 2022-11-28 11:31:51 +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
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
Christian Kamm f236a0521a
Fast I80F48 division with reduced precision (#300) 2022-11-24 11:59:00 +01:00
Christian Kamm c276353289
Implement a stable_price on banks and perp markets (#303)
It is tracked in the StablePriceModel and updated on
TokenUpdateIndexAndRate and PerpUpdateFunding instructions.

The stable price is used in health computations.
2022-11-24 11:55:22 +01:00
Christian Kamm 65362cb4de AccountRetriever review fixes
Done-with: conj0iner
2022-11-21 20:34:23 +01:00
microwavedcola1 d3661bac7b
mc/oracle peg ts client (#290)
* oracle peg client support

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>

* Fix from review

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-11-21 19:34:41 +01:00
Christian Kamm 6ea7eddcb3
Compute health based on two prices (#298)
Now the oracle price and a "safe" price go into the health computation and
init health uses the min of both for assets and the max for liabs.

This has far-reaching consequences for how serum and perp health need to
be computed because many intermediate values depend on the prices, which
now differ for init and maint health computations.

The biggest change is that the serum3 reservation totals need to be done
for each health call from scratch.

This PR increases the health CU host per serum3 market and perp market
significantly, check the test_health_compute tests.
2022-11-21 15:01:10 +01:00
microwavedcola1 d82608ebee Revert "add support for msrm vault for serum3 discounts (#285)"
This reverts commit 0a4f7150d6.
2022-11-20 15:31:16 +01:00
conj0iner 39ed94aa87
client additions + extra tests + safety (#281)
Co-authored-by: Conj0iner <conj0iner@users.noreply.github.com>
Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-11-18 08:58:56 +01:00
Christian Kamm 3ccfa3241f Perp: Remove misleading LeafNode::price() 2022-11-16 15:53:07 +01:00
riordanp 5d780a86dc
Upgrade to Solana 1.10.35 (#291)
* 1.10.35 upgrade & compatibility

* Patch Anchor

* Fix test compile

* Serum program update

Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-11-16 10:50:40 +00:00
Christian Kamm 1026b2bb85 benchmark improvements 2022-11-16 11:14:46 +01:00
Christian Kamm 2ee152f7ea Oracle staleness: Check when accessing oracle price 2022-11-12 12:12:45 +01:00
Christian Kamm 58f7ff2e0e Add staleness param to OracleConfig
Since Bank can't expand the existing OracleConfig, add a new one to Bank
and (temporarily!) copy over the old value to the new one in
TokenUpdateIndexAndRate.

Add some reserved space to OracleConfig to make this easier in the
future.

Breaks the PerpMarket and PerpPosition layouts
2022-11-12 12:11:09 +01:00
Christian Kamm f5e47c47bd Clarification of price_data for pegged orders 2022-11-11 08:18:53 +01:00
Christian Kamm b6361bad37
PerpUpdateFunding: update automatically on PerpPlaceOrder (#287) 2022-11-10 14:41:20 +01:00
Christian Kamm efec1eb906 Test fixes 2022-11-09 10:51:03 +01:00
microwavedcola1 0a4f7150d6
add support for msrm vault for serum3 discounts (#285)
* add support for msrm vault for serum3 discounts

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

* Fix client

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

* client support

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

* More client code and todos

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

* prettoer

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

* remove mint, not needed

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-11-09 09:59:42 +01:00
microwavedcola1 e47e17d79f
reduce only flag for perp place order (#286)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-11-09 09:59:34 +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
Christian Kamm f2cc869795 Perps: Prepare for settlement token != oracle token
Make sure the setting is available and that the needed accounts are
passed to the perp instructions.
2022-10-06 08:32:58 +02: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
Christian Kamm b906e3dc78 Token deposit: Split into signed and permissionless ix
Token accounts are a limited resource, so allowing other users to make
use of them can cause problems.
2022-10-02 16:06:39 +02:00
microwavedcola1 d1079bb1b9
Mc/ts increase error tolerance for binary search (#258)
* ts: a higher error tolerance is sufficient

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

* Fixes from review

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

* program: fix error size

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-09-30 14:59:12 +02:00
Christian Kamm 7e180c7b3a Perps: Support trusted markets 2022-09-29 15:40:31 +02:00
Christian Kamm 8ba52f46c2
Perp settle pnl: incentive for settler (#252)
This adds three new options to perp markets:
- a flat fee, that is paid for all perp settlements
- given that they exceed the configured min settle amount threshold to
  be eligible for fees
- and a fractional fee, that only applies when the benefiting account
  has low health and gradually becomes bigger as the account slides
  towards liquidation
2022-09-29 12:59:55 +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
Christian Kamm 984695e8d0 liquidator: Call liq_perp_bankruptcy 2022-09-29 07:36:00 +02:00
Christian Kamm 12864e15a6
Merge pull request #245 from blockworks-foundation/ckamm/perp-settle
Liquidator: Settle pnl on perp accounts
2022-09-29 07:35:37 +02:00
Christian Kamm 77abfd0529 Perp liq: Fix base position liquidation amount computation
It was wrong for quote weights != 1.
2022-09-29 07:34:52 +02:00
Christian Kamm 11daf4d0eb client: fetch top settle pnl counterparties 2022-09-28 10:48:41 +02:00
conj0iner a97b40a521
Merge pull request #234 from blockworks-foundation/cj/ioc_fees
Extra fees for IOC orders
2022-09-28 00:05:25 +08: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 f6c9a93ac5 Health: Avoid overflow panic in health ratio for tiny liabs
Spotted by microwavedcola
2022-09-23 12:04:14 +02:00
microwavedcola1 bb6790e678
ts: use price factor in maxSourceForSwap + max perp bid and ask + tests (#237)
* ts: use price factor in maxSourceForSwap
ts: max perp bid and ask
ts: mocha test for max swap

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

* ts: comemnt

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-09-23 11:43:26 +02:00
Conj0iner 55f77ad6c6 Added IOC penalty 2022-09-23 00:55:12 +08:00
microwavedcola1 ae42356b22
program: remove todos for ix which have now been implemented or not relevant (#240)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-09-22 17:43:01 +02:00
Christian Kamm 0040a1477e
Client: Compute max perp trade amount (#231) 2022-09-21 11:45:05 +02:00
Christian Kamm a41a245e24 PerpLiqBankruptcy instruction 2022-09-21 09:46:54 +02:00
conj0iner 1c67b8ed5f
remove base_token_index from perps and include oracles (#224)
Co-authored-by: Conj0iner <conj0iner@users.noreply.github.com>
2022-09-21 09:42:45 +02:00
Christian Kamm ddb68b7991 Test build fix 2022-09-20 16:59:55 +02:00
Christian Kamm 6399e6cb6c Fix PerpMarket data layout breakage
The new fees_settled must be added at the end and the reserved amount
needs to be reduced.
2022-09-20 16:58:07 +02:00
microwavedcola1 b7e79a4663
WIP: ts/perps (#220)
* ts: further fleshing out of perps code

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

* cleanup scripts

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

* Fixes from reviews

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-09-20 12:57:01 +02:00
Christian Kamm 11160ae2fe Health: Pass price to max_swap_source_for_health_ratio
Because it's not correct to think we'll always swap at oracle price.
That was an ok assumption for liquidations, where we'll swap at slightly
better than that, but not for jup.
2022-09-20 10:41:33 +02:00
Christian Kamm a6d25071cd Test: Fix test_alt compile 2022-09-15 19:06:46 +02:00
Christian Kamm 20eb61527b Compile fixups after merge 2022-09-15 09:40:14 +02:00
Christian Kamm 1b3a2b07db Update idl 2022-09-15 09:24:35 +02:00
Christian Kamm 14ca1dac35 Add alt_set / alt_extend instructions
alt_extend will only work in the future, when alt_set can receive lookup
tables where the authority is set to be the mango group pda.
2022-09-15 09:20:08 +02:00
Christian Kamm 50be520e99 Rename: LiqToken* -> TokenLiq* 2022-09-14 16:22:42 +02:00
Christian Kamm f3bdfec6a6
Perp settle pnl: Limit to maint spot health (#225)
Perp settle pnl: Limit to maint spot health

We want to isolate perp markets. As such, it's not acceptable to convert
arbitrary perp losses into spot token borrows.

The safe amount for this conversion is given by the account's
maint health -- with all perp markets ignored.
2022-09-14 13:26:29 +02:00
Christian Kamm 5572242e6d Tests: PerpLiqBaseToken tests 2022-09-10 07:45:27 +02:00
Christian Kamm 45025a1145 Perp tests: Helper for market creation 2022-09-10 07:45:27 +02:00
Christian Kamm bb962c6781 Perp tests: Don't pass derivable accounts explicitly
This makes it impossible to test passing bad accounts for these through
this interface, but I think that's an ok tradeoff.
2022-09-10 07:45:25 +02:00
Christian Kamm 2dfada7377 Tests: initial perp force-cancel test 2022-09-10 07:42:58 +02:00
Christian Kamm 810998a2af Add PerpLiqForceCancelOrders instruction 2022-09-10 07:42:58 +02:00
Christian Kamm 845a32a7c2 Add PerpLiqBasePosition instruction 2022-09-10 07:42:58 +02:00
Conj0iner a6ddf232cb Fixed tests 2022-09-09 18:50:11 +08:00
Conj0iner 3b3286a5b3 Changed max_settle_amount to u64 2022-09-09 18:29:02 +08:00
Christian Kamm 1f26a54399
Perps: introduce explicit position close (#216)
* Perps: fix position lifetime; explicit closing

When an order is placed and the position needs to be created, the
settlement token is marked as in use. The perp position and the in-use
flag are only released with the new perp_close_position instruction.

* Tests: Factor out common floating-point comparisons

* Rename PerpClosePosition -> PerpDeactivatePosition
2022-09-09 10:50:09 +02:00
Christian Kamm 6c32077d1a Perps: Let all base lot changes go through a function
Direct access to base_position_lots and quote_position_native is not
allowed anymore.

Fixes an issue where quote_lots were used instead of quote_native, and
also takes fees into account for the entry price.
2022-09-08 13:12:50 +02:00
Christian Kamm e56ed3776c Perp: Api cleanups
- Clarification of close_order vs close_all_orders
- Remove of get_ prefixes
- Remove find_min/find_max/find_min_max, duplicated with
  min_leaf/max_leaf
2022-09-08 13:11:45 +02:00
Christian Kamm 1d7d5e9a6d Test: simplify by adding a TestKeypair that's Clone+Copy
This means a lot less annoyance because of &'a Keypair or Arc<Keypair>
2022-09-07 14:23:52 +02:00
Christian Kamm 4795286dbd Perps: Account for unsettled funding in health 2022-09-07 11:37:20 +02:00
Christian Kamm c714fb213d Tests: Auto-advance the slot after sending a transaction
This fixes a recurring error where it would look as if a transaction you
send would not be processed at all.
2022-09-07 11:34:47 +02:00
Christian Kamm 3128115dca PerpMarket: Add helper for getting oracle price 2022-09-06 14:26:06 +02:00
Christian Kamm 79a7bdc299 Checked math: support cm!(a += b) 2022-09-06 08:55:27 +02:00
Christian Kamm 3c51449040 Tests: Remove unused imports 2022-09-05 18:40:42 +02:00
Conj0iner c34ee54233 Added perp_settle_fees instruction 2022-09-05 22:18:40 +08:00
Christian Kamm 3fbc3ca32d TokenEdit: remove bank_num arg 2022-09-02 13:55:00 +02:00
Conj0iner 97eed4081d Added perp_settle_pnl instruction 2022-09-02 00:07:57 +08:00
Christian Kamm 511db72f8e Health token adjustment: Deal with odd I80F48 behavior
The identity
  a * b = -((-a) * b)
does not hold for I80F48, probably it's rounding to -inf.

This meant that withdrawing the full native token balance could
fail because the magnitude of -position * price was bigger than
position * price.
2022-09-01 14:38:48 +02:00
Christian Kamm 868c55fef1 Serum index reservation: use a real account
Makes it easier and more future-proof
2022-09-01 12:28:37 +02:00
Christian Kamm c31f11013f Serum deregister: Close index reservation account
Only safe because this is a testing-only instruction
2022-09-01 12:15:00 +02:00
Christian Kamm 1f0cd62846 Don't panic if accounts are shorter than 8 bytes 2022-09-01 12:00:09 +02:00
Christian Kamm 5e0001afb8 Serum: Index reservation account has 8 bytes
otherwise all code needs to be able to deal with mango accounts that are
smaller than 8 bytes in size.
2022-09-01 11:49:33 +02:00
microwavedcola1 b30b2f8870
program: serum3 should work when group has version 1, perps and multiple banks should be moved to version 2 or beyond (#205)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-09-01 09:48:38 +02:00
Christian Kamm d0fef4a586 Test: simplify allow(dead_code) to be per-file 2022-08-30 15:32:59 +02:00
Christian Kamm 8a2d54cce8 Fix some clippy warnings 2022-08-30 13:59:34 +02:00
Christian Kamm 801b68b93c MangoAccount: add unittests 2022-08-30 13:11:47 +02:00
Christian Kamm 77b8e6e8b9 SerumPlaceOrder: Fix vault balance check for base_lot_size
As spotted by microwavedcola
2022-08-30 12:05:07 +02:00
Christian Kamm 1f0138d1de SerumCloseOO: Reduce in_use_count of token positions 2022-08-30 12:05:07 +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 36723792a1 SerumRegister: Block using the same market index twice 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 e1adbf0217 Tests: Fix concurrent log capture 2022-08-30 08:37:29 +02:00
Christian Kamm d6d66402f7 Tests: Helper for create + fund 2022-08-30 08:37:29 +02:00
Christian Kamm 7d508e5df5 MangoAccount: Fixes after audit 2022-08-29 16:13:15 +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
microwavedcola1 453248bbe5
token_deregister: remove redundant token index and add doc (#192)
* token_deregister: remove redundant token index

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

* fix test

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

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-25 19:25:56 +02:00
Christian Kamm dfa8166aee Health: Put common pre/post check logic into functions 2022-08-24 16:39:36 +02:00
Christian Kamm bba27ed6f0 PlacePerpOrder: pre-health computation 2022-08-24 16:07:22 +02:00
Christian Kamm 38b349a401 Rename PerpOpenOrders -> PerpOpenOrder, since it's a single one 2022-08-24 15:22:55 +02:00
Christian Kamm cb0a9e5d2b Tests: allow checking of logged "post_health" 2022-08-24 14:15:01 +02:00
Christian Kamm c8159746e7 Tests: Remove unnecessary argument 2022-08-24 14:15:01 +02:00
Christian Kamm 45f52ae535 Serum3PlaceOrder: compute pre-health 2022-08-24 14:15:01 +02:00
Christian Kamm 40f467024d FlashLoan: Add pre-health check 2022-08-24 14:15:01 +02:00
Christian Kamm 1ebbac7d6f Withdraw: Add pre-health check 2022-08-24 14:15:01 +02:00
Christian Kamm 2d2cef35a0 Add HealthRegion instructions
You can do
- HealthRegionBegin
- ... mango instructions ...
- HealthRegionEnd

and the account health will only be checked at the start and end
instead of for every instruction.
2022-08-24 10:07:45 +02:00
Christian Kamm c9fb4c7133 Tests: Fix program log capture 2022-08-24 10:07:45 +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 ec850b7bd3 Fix size of account_num in idl 2022-08-19 14:52:31 +02:00
Christian Kamm 403226dc28 BeingLiquidated: Deactivate flag on positive health 2022-08-18 13:12:13 +02:00
Christian Kamm a23afed7de Fix computation of utilization time-weighted average 2022-08-17 15:29:11 +02:00
Nicholas Clarke 8a1c58e723
FlashLoan: Add flash_loan_type (#164)
Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-08-17 12:36:55 +02:00
Christian Kamm e8bbfbef83 Bank: fix collection of loan fee
Instead of changing indexed_borrows, change borrow_index!
2022-08-16 16:27:45 +02:00
Christian Kamm 4c65204c19 Liquidator: Fix condition for starting to liquidate 2022-08-16 16:19:04 +02:00
Christian Kamm 421b2d3d19 FlashLoan: Fix validation holes
- Pass the group to Begin and End and ensure it is the same.
- Enforce target token accounts can't be group-owned.
- Now End can consistently derive the number of vaults/token accounts.
2022-08-16 13:10:05 +02:00
Christian Kamm 54222c5b3f liq_token_bankruptcy: don't pass redundant index 2022-08-16 13:08:57 +02:00
Christian Kamm e92561d32e Liquidation: Fix rounding issues in liq_token_bankruptcy
By explicitly dusting trace positive positions.
2022-08-16 13:08:43 +02:00
Christian Kamm 961a81b94f Liquidation: Fix positive dust issues 2022-08-16 13:08:43 +02:00
Christian Kamm af5f622bd8 Liquidator: bankruptcy requires borrows 2022-08-16 12:13:54 +02:00
Christian Kamm c9a5fb5fc1
ScanningAccountRetriever: Don't mut-borrow oracles (#170)
This allows using the same oracle for different banks, like for ETH and
soETH.
2022-08-15 11:32:54 +02:00
Christian Kamm 58105207af Make anchor's lint pass 2022-08-15 10:48:43 +02:00
Christian Kamm 0265b9b0a9 Account: Change type of net_deposts / net_settled
Because f32 doesn't have enough significant digits for large deposits.
2022-08-15 10:48:43 +02:00
Christian Kamm 18857c7b72 Add "client" feature annoations 2022-08-15 10:48:43 +02:00
Christian Kamm 6a77a5c349 tests: fix compilation warnings 2022-08-15 10:48:43 +02:00
Christian Kamm fcecd21cdb health: remove duplicated code 2022-08-15 10:48:43 +02:00
microwavedcola1 1b6ed725be
over explaining the code (#168)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-14 20:45:57 +02:00
Christian Kamm 7a343a9a8d checked_math: support casts and I80F48::from 2022-08-14 15:01:15 +02:00
Christian Kamm d91a676e3d Oracle: Decimal constant cleanup 2022-08-14 15:01:15 +02:00
Christian Kamm 724824d21a Bank: small fixes from audit 2022-08-14 15:01:15 +02:00
Christian Kamm ccba4ee597
Cleanups and bugfixes from the audit (#163)
* AccountExpand: cleanups from audit

* Group instructions: cleanups from audit

* AccountClose: cleanups from audit

* TokenAddBank: Audit fixes, including significant bugs

Must not take collected_fees and bump from the existing bank.

* Instruction comment updates

* GroupEdit: Add version/testing flag changes

Ported from mc/vanity
2022-08-13 19:54:58 +02:00
Christian Kamm 21815612ae
Let all PDA seeds start with a unique prefix (#166)
This is mostly paranoia. Now it's guaranteed that different PDA types
have unique seeds.
2022-08-13 19:30:35 +02:00
Christian Kamm a3067e2448 Avoid custom Debug impls by using derivative crate 2022-08-13 10:18:04 +02:00
microwavedcola1 261c856bb2 reduce max source swap targetError so that we dont error out
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-12 11:08:08 +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
Conj0iner 02c19a6eb4 Optimise entry price calculations 2022-08-11 22:58:47 +08:00
Christian Kamm ac58081430 Audit: Fix some account constraints 2022-08-11 13:30:04 +02:00
Christian Kamm e8479e2e7c liq_token_with_token: Correctly adjust health cache for dusting
Previously the health cache adjustments didn't take into account that
positions between 0 and 1 native token would be dusted to 0.

This makes a difference for valuable tokens with few decimals. For
example 1 BTC-native token is currently worth 500k SOL-native tokens.
That means 0.9 BTC-native would easily be enough collateral for a 10k
SOL-native borrow -- but if the 0.9 get dusted, then the whole account
is bankrupt instead.
2022-08-10 16:24:48 +02:00
Christian Kamm 7c72a1956a Withdraw: Allow withdrawing all funds with allow_borrow=false
We should also add the ability to withdraw as much as possible without
borrowing and without specifying the exact amount, for convenience.
2022-08-09 11:56:08 +02:00
microwavedcola1 5516ecb132 remove unused param in context
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-08 21:11:15 +02:00
microwavedcola1 d594286266
reserve for ALTs (#152)
* reserve for ALTs

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

* add explicit padding

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

* fix from review

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

* fix from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-08 18:50:59 +02:00
microwavedcola1 b8bf3a1c40 reorder mango account fields in idl to match on chain order according to MangoAccountFixed
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-08 17:04:54 +02:00
microwavedcola1 ca790c9ef3
add prioritization for token rate update ix in keeper (#153)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-08 16:31:59 +02:00
microwavedcola1 4f72985f2d
also allow editing group insurance fund (#149)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-07 14:16:23 +02:00
microwavedcola1 e06736660a
remove account size, make mango account feature position counts explicit (#148)
* remove account size from program, make position counts explicit

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

* cargo-fix

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-07 14:16:06 +02:00
conj0iner ff19940ee7
Merge pull request #140 from blockworks-foundation/cj/avgentryprice
Added avg entry + break even price
2022-08-07 12:20:33 +08:00
Christian Kamm dded844693 Update dynamic interest and default token parameters 2022-08-06 21:06:34 +02:00
Conj0iner dc4a770835 Merge remote-tracking branch 'origin/dev' into cj/avgentryprice 2022-08-06 22:32:59 +08:00
microwavedcola1 6f5f53c1d8 update comment
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-06 13:36:21 +02:00
microwavedcola1 59d28c97a1
fix name and error message check in flash loan (#145)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-06 12:59:13 +02:00
Christian Kamm 2880a2ec88 Instruction introspection: don't hardcode discriminators
Patch anchor to allow access to the generated discriminators.
2022-08-05 23:24:17 +02:00
Nicholas Clarke e4a073c83b
Clarkeni/group logging (#142)
* Add group logging and updateIndexAndRate anchor test
* Bugfix on health account ordering
2022-08-05 10:11:44 -07:00
microwavedcola1 dd22ca2ad6 format
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-05 16:17:29 +02:00
Conj0iner 977e85dcf0 Added avg entry + break even price 2022-08-04 22:53:14 +08:00
microwavedcola1 331bb7ebf0 refactor,format
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-04 12:13:36 +02:00
microwavedcola1 dd8086d8eb format
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-04 12:04:08 +02:00
microwavedcola1 ca6b857a43 format
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-04 12:03:06 +02:00
microwavedcola1 d34dea961c format
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-04 12:01:23 +02:00
microwavedcola1 a546f6fb3e format
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-04 11:58:46 +02:00
microwavedcola1 f134f0257a format
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-04 11:54:19 +02:00
microwavedcola1 7953e123cc add simple logging
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-04 09:25:19 +02:00
Christian Kamm 22080fdcc1 TokenRegister: Fix anchor seed derivation
It likes names constant, but not constant literals.
2022-08-04 08:26:18 +02:00
microwavedcola1 b62c0cd9be fix
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-03 15:45:10 +02:00
microwavedcola1 9d117c84ac fix from review
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-03 15:45:10 +02:00
microwavedcola1 505cbd48aa fixes from review
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-03 15:45:10 +02:00
microwavedcola1 59dd3aa66e insurance fund for trustless vs not trustful
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-03 15:45:10 +02:00
microwavedcola1 e31ad9ce34 token register trustless
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-03 15:45:10 +02:00
microwavedcola1 9c10101166 remove traces of address lookup tables aka transactions v2
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-02 09:39:08 +02:00
microwavedcola1 374c0eb136 mc/clippy (#135)
* clippy pass 1

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

* clippy pass 2

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-02 09:07:10 +02:00
microwavedcola1 a9a03b97ac
mc/registration time (#132)
* registration time

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

* fix from review

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

* fix from review

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

* mc/u32 numbers - consistent numbers in pdas (#133)

* consistent numbers in pdas

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

* fix

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-01 18:46:45 +02:00
microwavedcola1 5c3b2c1189
mc/kill flash loan 1 & 2 and rename flash loan 3 to flash loan (#131)
* remove flash loan 1 & 2

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

* rename flash loan 3 to flash loan

* fix test

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-01 16:55:17 +02:00
microwavedcola1 c516e45d08
mc/review reserved on accounts (#130)
* review and extend reserved on accounts

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

* revert changes to node size, stack gets smashed in rust unit tests

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

* max possible

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

* max

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

* Fixes from review

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

* header version

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

* Fix from review

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

* fix

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

* fixes from review

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

* fix from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-01 16:53:30 +02:00
microwavedcola1 e8e774a1a4
mc/feature gating (#129)
* feature gating

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

* Fixes from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-08-01 12:56:29 +02:00
microwavedcola1 91d59c1918 close insurance vault also
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-31 09:57:56 +02:00
microwavedcola1 45fd36642e
prepare group for ownership transfer to governance (#125)
* prepare group for ownership transfer to governance

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

* Fixes from reviews

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

* fix from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-31 09:28:16 +02:00
microwavedcola1 c88297f42e
mc/ts and keeper improvements (#127)
* ts and keeper improvements

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

* remove stray heroku experiments

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

* Fixes from reviews

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-31 09:25:11 +02:00
microwavedcola1 dfa268dfd6
fix small things to make ci work again (#126)
* checkout submodules on ci

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

* change to https, so that ci can clone

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

* fix clippy

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

* redundant clone

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-30 09:49:56 +02:00
microwavedcola1 f2bfb951fd
anchor as submodule (#121)
* anchor as submodule
* Use anchor submodule as dependency

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-07-27 08:52:41 +02:00
microwavedcola1 2fb92abad8
Mc/ts upgrade (#122)
* upgrade anchor fork, supports anchor idl close now

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

* upgrade ts deps

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-26 17:14:07 +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 46b6bce14b IDL: Remove NodeRef and NodeRefMut enums
Anchor cannot load idls that include enums with tuple variants.
2022-07-22 08:35:30 +02:00
Christian Kamm ea85824ccb Remove code copied from solana-address-lookup-table-program
This removes the ProgramInstruction enum from the IDL, which was unused
and could break clients.
2022-07-21 06:49:04 +02:00
microwavedcola1 39686d3d0f CU of test has increased, accomodate only for this test
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-20 15:51:10 +02:00
microwavedcola1 3eae2cdc34 upgrade anchor to latest, upgrade solana dependencies to match what latest anchor uses
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-20 15:51:10 +02:00
Christian Kamm f8d4b2443c Liq: Don't log common+expected simulation errors 2022-07-20 09:47:37 +02:00
Christian Kamm 2bff4bba3d Drop being_liquidated after bankruptcy resolves 2022-07-20 09:47:37 +02:00
Christian Kamm b5d3e2164c Make Serum3Info and PerpInfo pub again
Accidentally reverted in bad merge
2022-07-20 09:47:37 +02:00
Christian Kamm a82bab4ed7 Better error when vault doesn't have enough funds
The spl_token transfer cpi instruction just says "error code: 1", which
is too opaque.
2022-07-19 13:43:05 +02:00
Christian Kamm 348d8cfcd8 Liq/Client: Various improvements
- Abstract away account fetching, so it can be done via RPC or from a
  websocket stream (or a geyser plugin) that populates a ChainData
  instance.

- Separate out information about tokens, markets into MangoGroupContext.

- Separate all gPA calls into functions in a new file

- The liquidator re-fetches critical accounts via RPC before
  liquidation. Unfortunately the websocket stream seems slower :/

- Don't re-implement health account derivation in the liquidator.
  Instead reuse the existing code from the client.

- More smaller stuff.
2022-07-18 18:18:57 +02:00
Christian Kamm b44f628c01 LiqTokenWithToken: More error contexts 2022-07-18 18:18:57 +02:00
Christian Kamm 5231ee7f98 Token edit: Fix bug where mintinfo.oracle goes out of sync 2022-07-18 18:18:57 +02:00
Christian Kamm 88a292e656 Health: Add max_swap_for_source function 2022-07-15 10:09:36 +02:00
microwavedcola1 bc4c57911a
Health from health components in ts/client (#104) 2022-07-13 19:18:55 +02:00
Christian Kamm 86a84396f5 Health: cleanup and new functions for ratio and assets/liabs 2022-07-12 09:47:23 +02:00
Christian Kamm 6f758ff25b clippy 2022-07-12 09:46:17 +02:00
Christian Kamm b77af54f15 Fix tests 2022-07-11 22:31:00 +02:00
microwavedcola1 ef7d2862da
wip: dynamic rates (#98)
* dynamic rates

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

* fmt

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-11 17:08:32 +02:00
Christian Kamm f132f30874 Better error messages 2022-07-08 12:48:43 +02:00
Christian Kamm 469e7fd704 Use get() instead of get_mut() where possible 2022-07-08 09:10:08 +02:00
Christian Kamm 6c7245adaa Serum3 liq force cancel orders: Deactivate inactive token positions 2022-07-07 13:22:25 +02:00
Christian Kamm 693333b71e Serum3 place/settle: deactivate inactive token positions 2022-07-07 13:16:23 +02:00
Christian Kamm 9f90c3982f Fix serum3 loan origination fee withdrawal
Paying the loan origination fee must be like a withdrawal and can't be
charged as a borrow if the user has deposits.
2022-07-07 12:33:21 +02:00
Christian Kamm 45da68c6ac LiqTokenWithToken: Close inactive token positions 2022-07-07 12:24:48 +02:00
Christian Kamm 31831454b4 Return raw position index from get_mut() 2022-07-07 12:01:18 +02:00
microwavedcola1 0ff269c38c Merge branch 'main' into dev 2022-07-06 17:47:45 +02:00
microwavedcola1 ab3abc69e7 Fix flash loan3 - fetching of bank from retriever
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-06 17:41:08 +02:00
microwavedcola1 b0f1585541 Merge branch 'main' into dev 2022-07-06 17:11:20 +02:00
microwavedcola1 8a1e9136a6 better error msg
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-06 17:11:12 +02:00
Christian Kamm ffd83a7a05
Rename instructions to follow naming pattern (#97) 2022-07-06 14:51:15 +02:00
Nicholas Clarke 348fbc1cb9
Track cumulative net deposits (deposits - withdraws) (#91)
Track cumulative net deposits (deposits - withdraws) using prices at the time of the deposit and withdraw.
This is used for calculating overall pnl (across all tokens).
I want to store UI amount * UI price = (native amount / base decimals) * (oracle price * base decimals / quote decimals) => native amount * oracle price / quote decimals.
I have used f32 here to reduce the space required on the mango account - we don't need so much precision for this as it's purely a display value.
I've also included a field for net_settled - this will be used for perp pnl but is not implemented yet (as perp settling instructions are not ready).

Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-07-06 12:07:30 +02:00
Christian Kamm de86b69e1a Client/program interface changes
- rearrange structs to have gPA data in front and add comments about the
  offsets being relevant
- add insuranceMint to group creation in client
- drop quoteTokenIndex storage on PerpMarket
- fixes to editAccount in example1-user and client
2022-07-06 11:34:07 +02:00
Christian Kamm 35517bdb75 Review fixes 2022-07-06 11:34:04 +02:00
Christian Kamm b25f6d5f4f Insurance fund 2022-07-06 09:07:50 +02:00
Christian Kamm 35ca4d31b3 liq_token_bankruptcy instruction for socialized loss 2022-07-06 09:07:49 +02:00
Nicholas Clarke 30fc7def77
Add oracle price to update_index logging. (#89)
* Add oracle price to update_index logging.

* Update IDL with changes to update_index instruction and logging.
2022-07-05 11:59:44 -07:00
microwavedcola1 d74cc78a84
edit tokens, perp markets, mango accounts, allow delegate to perform certain operations (#94)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-05 19:31:47 +02:00
microwavedcola1 dff3f7cd8c client functions via program simulation
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-04 14:11:38 +02:00
microwavedcola1 39284c5705
Flash loan 3 minimal example (#90)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-04 12:09:33 +02:00
microwavedcola1 6a99eb893b Fix types
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-01 14:31:47 +02:00
Christian Kamm 30a221047c Bank: rename indexed total deposits and schedule for removal 2022-07-01 14:08:28 +02:00
microwavedcola1 e8cb27610e
close mango account without checks when testing flag is set on the group, workaround weird issues when all dust cant be withdraw (#92)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-07-01 13:38:50 +02:00
Christian Kamm 6d05306859 Improve FlashLoan3 docs 2022-06-30 18:45:17 +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
microwavedcola1 1c02ccd21d
Transfer dust from bank vaults to a dust account, without this we wont be able to close bank vaults (#88) 2022-06-29 09:11:14 +02:00
Christian Kamm 86d635f5d0 FlashLoan3: Variant that transfers directly
FlashLoan2 requires the user to set up transfers to and from the Mango
vault accounts. This version directly provides the loaned funds into a
target token account and repays everything that exceeds the initial
token account balance at the end.
2022-06-28 15:32:26 +02:00
Christian Kamm d786a672f1 FlashLoan2: API with Begin and End instructions
For FlashLoan users had to pass the target cpi programs, accounts and
data to the FlashLoan instruction itself.

The new API allows existing instructions to be used unchanged, they
just need to be bracketed by FlashLoan2Begin and FlashLoan2End.
2022-06-28 15:30:13 +02:00
Christian Kamm 3eedba67b1 Fix serum3_cancel_order
accidental removal of mut in 20fc9d557a
2022-06-27 11:38:55 +02:00
microwavedcola1 9fc8a5a56a
multiple banks (#82)
* multiple banks

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

* Fixes from review

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

* test for closing multiple banks for a registered token

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

* fix deregister_token

* update idl

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

* Fixes from review

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

Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-06-27 11:27:17 +02:00
Christian Kamm 20fc9d557a Satisfy anchor's lint by adding CHECK: comments 2022-06-24 12:36:35 +02:00
microwavedcola1 7a28a3bfb0 improvements to flash loan
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-23 16:41:24 +02:00
microwavedcola1 54c0306ae7 Fix flash loan
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-23 13:10:15 +02:00
Christian Kamm 0bd5b8fdf3 Rename margin_trade.rs -> flash_loan.rs 2022-06-23 10:23:52 +02:00
Christian Kamm c8ebc1f611 Rename MarginTrade -> FlashLoan 2022-06-23 10:23:52 +02:00
Christian Kamm d8a6a29e70 Renames: propagate to idl and ts 2022-06-23 10:04:54 +02:00
Christian Kamm 648b462352 Rename MangoAccount components 2022-06-23 10:04:54 +02:00
Christian Kamm 1143b38d3c Health review comments 2022-06-22 16:52:11 +02:00
Christian Kamm ba79995c01 Health: order-independent serum3 health
Now all the reserved funds in serum3 open orders accounts are added into
each possible token at the same time. Then the worse case from applying
the reserved funds to either quote or base is selected.

This is reasonably cheap to compute, leads to unchanged results when no
markets share (non USDC) base or quote tokens, but can underestimate
the "true" health value when markets do.

An additional advantage is that HealthCache is now indepenent of serum
open orders and can compute the init and maint health when the user has
active OpenOrders accounts.
2022-06-22 16:52:11 +02:00
Christian Kamm d4cec8dfa4 Bank: Fix bug when when depositing to exactly 0 2022-06-22 16:51:16 +02:00
Christian Kamm aeb94642ce fix clippy warnings 2022-06-21 13:10:37 +02:00
Christian Kamm 5dd186ac13 Health: fix unittests 2022-06-21 13:02:47 +02:00
Christian Kamm 6f41efad0f Health: helper for finding token info by index 2022-06-21 13:02:47 +02:00
microwavedcola1 b0fd4c2a61 Fix warnings
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-21 13:02:30 +02:00
microwavedcola1 86c8817e0b
serum3 loan origination fees (#69)
* serum3 loan origination fees

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

* Fix tests
Fix 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>
2022-06-21 11:45:38 +02:00
Christian Kamm cb0010b2b5 Health: never allow positive contributions from perps 2022-06-21 11:07:13 +02:00
Christian Kamm be98f6524a Orderbook: Avoid usize fields in data 2022-06-21 11:05:46 +02:00
Christian Kamm 6ff4ff5910 Add a ComputeHealth instruction
This way the client can ask for simulation to determine an account's
health.
2022-06-21 11:05:32 +02:00
Christian Kamm 712bece0b2 Remove stray log message from health computation 2022-06-20 18:21:26 +02:00
microwavedcola1 470292024e
Filter out bad switchboard prices (#70)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-20 18:03:16 +02:00
microwavedcola1 5b78293c5c minor refactoring
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-19 18:32:04 +02:00
microwavedcola1 9462be7f0b Fix margin trade interface
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-18 17:14:20 +02:00
microwavedcola1 4864562ff3
margin trade, support multi cpi (#68)
* margin trade, support multi cpi

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

* Fix from review

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

* todo

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-18 16:43:45 +02:00
microwavedcola1 0f34851c64
switchboard support (#67)
* switchboard support

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

* Fix from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-18 16:38:46 +02:00
microwavedcola1 95c06c2bfc
Mc/liquidator (#64)
* Copy over and integrate liquidatable-accounts-feed (doesn't compile)

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

* Liquidator

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

* Adapt to refactoring

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

* remove dead code

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

* Use copies of AccountSharedData

* Implement AccountReader for AccountSharedData

* use LoadZeroCopy in liquidator

* Fix from review

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

Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-06-18 16:31:28 +02:00
microwavedcola1 de481a4008 todo
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-10 08:42:26 +02:00
microwavedcola1 740ff0c09e
Close various things (#65)
* close bank, vaults, mint infos, stub oracles, serum OO (doesnt work, throws https://github.com/project-serum/serum-dex/blob/master/dex/src/error.rs\#L88), close serum market

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

* serum oo closing example in ts

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

* Fix from code review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-09 18:27:31 +02:00
Christian Kamm 40023fcef1 Health: Make FixedOrderAccountRetriever more flexible 2022-06-08 14:38:49 +02:00
Christian Kamm b3568b7f3e
Make health/oracle functions usable from clients (#63)
* Make health/oracle functions usable from clients

By creating a shared trait that is implementable for AccountInfo and
AccountSharedData.

* Health: fixup tests
2022-06-08 13:43:12 +02:00
Christian Kamm a14f37362d margin_trade: forbid direct self-calls 2022-06-07 13:25:30 +02:00
Christian Kamm 685fd3122f Perps: Limit overall health contribution from perp markets
We don't want to allow borrowing against unsettled pnl in perp markets.

This makes it safer to create perp markets for untrustworthy assets.
2022-06-06 12:58:08 +02:00
microwavedcola1 1b4063cd0d remove misleading example
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-03 08:29:35 +02:00
microwavedcola1 47bccfeec8 Fix oracle price decimal usage
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-02 19:16:30 +02:00
microwavedcola1 a20d04b6ff
Fix oracle price read by using decimals from pyth (#62)
* Fix oracle price read by using decimals from pyth

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

* fix

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
2022-06-02 10:36:04 +02:00
microwavedcola1 383489ae34 Merge branch 'mc/liquidator-bot' into dev 2022-05-30 17:08:16 +02:00
Christian Kamm 48cb1f0b88 Compute health in perp_place_order 2022-05-29 20:50:21 +02:00