Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CloseMarket function to redeem rent from permissioned markets #191

Open
wants to merge 26 commits into
base: master
Choose a base branch
from

Conversation

Henry-E
Copy link

@Henry-E Henry-E commented Nov 3, 2021

Currently a placeholder pull request for adding a close market function.

Still TODO

Questions for @armaniferrante

  • How to check that the events, bid and asks queues are empty before removing rent from them?
  • Do we need a market closed flag to markets so that it's clear the market is shutdown?

Things I will add

  • Add CloseMarkets to the permissioned markets proxy example
  • Add a test to the permissioned markets javascript tests

Plus any other code review and additional checks that need to be added to the function.

The pull request is mainly based on existing examples from how CloseOpenOrders and Prune functions were implemented.

@Henry-E
Copy link
Author

Henry-E commented Dec 2, 2021

Added an anchor test directory that initializes a market and shuts it down again, then checks that the sol is retrieved.

It's not ready for merging yet though. There needs to be more discussion of what checks are needed before the rent is retrieved. Right now though the core functions are there and there's a working test. So fitting in any requested changes ought to be straightforward. We will see though!

Next up

  • Investigate what variables are available on the queue structs to tell whether they are truly empty, similar to the free_slot_bits variable on the open orders struct

Ok, it looks like it's possible to load up request queue and event queue then check their headers to ensure count = 0. For bid and ask queue it might be possible to access their SlabHeaders to check the leaf count, although not sure yet.

Update: Added checks that load the headers for all four queue structs and checks that their counts are empty.

dex/src/state.rs Outdated Show resolved Hide resolved
dex/src/state.rs Outdated Show resolved Hide resolved
@armaniferrante
Copy link
Contributor

How to check that the events, bid and asks queues are empty before removing rent from them?

What the code currently does is correct. The event queue can check the header count and the bid/asks slabs can check the leaf_count.

@Henry-E
Copy link
Author

Henry-E commented Dec 14, 2021

Ok, added the disabled flag. Going to start adding more comprehensive testing. It looks like the permissioned markets set of tests are failing because they're not using solana 1.9.0

Copy link
Author

@Henry-E Henry-E left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finished add close market and a more full set of tests

dex/src/state.rs Outdated Show resolved Hide resolved
dex/src/instruction.rs Outdated Show resolved Hide resolved
dex/src/state.rs Outdated Show resolved Hide resolved
dex/src/state.rs Outdated Show resolved Hide resolved
dex/tests/close-markets/tests/close-markets.ts Outdated Show resolved Hide resolved
dex/tests/close-markets/tests/close-markets.ts Outdated Show resolved Hide resolved
@Henry-E
Copy link
Author

Henry-E commented Dec 15, 2021

@armaniferrante Ran a full set of tests on the new close market functionality.

close_markets doesn't allow markets to be closed while

  • orders are in the bid or ask queue
  • there are uncranked events

And even more importantly

  • Open orders accounts can be settled and closed after the markets has been closed

Copy link
Contributor

@armaniferrante armaniferrante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add the tests to travis CI

**bids_acc.lamports.borrow_mut() = 0;
**asks_acc.lamports.borrow_mut() = 0;

market.account_flags = market.account_flags | (AccountFlag::Disabled as u64);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use Closed instead of Disabled.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serum already has the functionality for Disabled built in from the ability to manually shutdown. I didn't find anything similar for Closed

Copy link
Author

@Henry-E Henry-E Jan 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/project-serum/serum-dex/blob/master/dex/src/state.rs#L1884 - Here's where new order already checks for whether a market is disabled. Is this functionality different from closing? It's possible I haven't fully understood the full extent of what the disable flag affects

dex/tests/close-markets/test-ledger/validator.log Outdated Show resolved Hide resolved
dex/tests/close-markets/test-ledger/faucet-keypair.json Outdated Show resolved Hide resolved
@Henry-E
Copy link
Author

Henry-E commented Jan 7, 2022

Travis CI tests added.

Arguably we still haven't come to a good conclusion on the use of disabled. Serum was already design such that markets could be disabled by some authority in extreme scenarios so we make use of that existing functionality. Only the new_order_v3 function has an explicit market_enabled() check, all the other functions can still be called but will fail if they rely on the existence of any of the queue accounts.

I wanted to keep the actual changes made to the dex as minimal as possible. However, if it's necessary we can re-use the market_enabled check in any of the functions that shouldn't be possible to call post-disabling, e.g. cancel_order, consume events. Any functions involving open orders accounts, e.g. close open orders or settle, should be still be allowed to be called post-disabling, so we wouldn't have to make any changes to them.

@0xBooler
Copy link

0xBooler commented Feb 8, 2022

bump sers @Henry-E @armaniferrante pls help 🥏

@wdstudionft
Copy link

up @Henry-E @armaniferrante
I think this legit solution

@wdstudionft
Copy link

bump

@bigdefidev
Copy link

bigdefidev commented Jul 1, 2022

what's the status of this? Any forthcoming plans to try to include this feature soon?

@gaburipeach
Copy link

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants