Commit Graph

216 Commits

Author SHA1 Message Date
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 b5d3e2164c Make Serum3Info and PerpInfo pub again
Accidentally reverted in bad merge
2022-07-20 09:47:37 +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 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
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 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 31831454b4 Return raw position index from get_mut() 2022-07-07 12:01:18 +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
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
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
Christian Kamm 30a221047c Bank: rename indexed total deposits and schedule for removal 2022-07-01 14:08:28 +02:00
Nicholas Clarke f8da1f6a40
Clarkeni/logging (#81)
* Add logging

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

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

* rust format

* fix clippy errors

* Address PR requested changes

* fix flash_loan

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

* Fix retrieving oracle for logging

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

* Add logging flash_loan2 and flash_loan3

* Refactoring flash loan logging

Co-authored-by: Christian Kamm <mail@ckamm.de>
2022-06-30 14:35:05 +02:00
Christian Kamm 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
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
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 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 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 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