Sell Token Account Deallocation on Settlement - #88
Draft
kaze-cow wants to merge 5 commits into
Draft
Conversation
…t-does-settlement-token-account-deallocation-look
this is maybe crossing a threshold of complexity for the begin_settle function, but it is what it is.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes a sell token account after pulling funds if its empty and the settlement's state account has
close_authorityMotivation
There are various places where a feature like this comes in handy:
ReclaimBufferscommand which we will already have to close token accounts (reclaim buffer account #60), if we have an instruction that allows for the fee reclaimer to place orders on behalf of the settlement account, the settlement could also close its own buffer account while cashing out all tokens using this method. This should naturally work because the settlement account owns its own buffer accounts!Moreover, its easy and safe for a user to provide the permission needed to make this happen! SPL tokens provide a separate role explicitly for
CloseAuthority, which only allows for the supplied account to close the account once it contains 0 balance. So if the user grants close authority to the settlement program, that can be treated as a certain "opt in". The close authority can be granted as part of the same multicall that places the order.Considerations
Who receives the rent?
How we determine this can have a signfiicant impact on what flows we support in the above motivations and how many accounts an order uses.
buy_token_account. In the case that thebuy_token_accountis already funded, it would just become surplus rent. In the case thatbuy_token_accountis WETH, we could hypothetically callSyncNativeto include the amount in the swap output. This technically keeps funds in the user's control, but locked in the destination account until a future time. This method has the benefit of not consuming an additional token account (since thebuy_token_accountis duplicated) and you could hypothetically fund the rent for the new token account using the old one (if its NOT token 2022). Ofc this has problems if you are sending to a differentreceiver, or if you are looking at .owner's wallet.OrderIntent. This requires growing the size of theOrderAccountsubstantially from 200->232 bytes, and the solver would need to specify the target rent recipient as part of theBeginSettle, so this turns out to be substantially more complicated than the other two options above.After some discussion, we elect to do option 3. This is perhaps the most complicated outcome, but its also the one we are probably going to settle on with this feature.
When to check the
sell_account_rent_receiverThis PR has it set up so that the
sell_account_rent_receiveraccount specified by the solver is only checked if the account is to be closed. Otherwise, any account can be specified. Since duplicated accounts do not count towards the maximum transaction accounts limitation in solana, it means that this feature will not negatively impact the maximum orders in a settlement limit unless the sell token account is actually being closed.Out of Scope
Since the user would be setting their token
CloseAuthorityto the settlement account, we should also provide an instruction or similar to recover the close authority back to the owner of the token account. This instruction is not included.This is not "out of scope", but since it was easy, changes were made to add close account handling to the test cli. See test plan below!
Test Plan
[ ] Confirm suitable test/feature coverage.
[ ] (optional) try it with the
test-cli. Follow the test instructions for making a settlement on #52 . As long as the conditions are met, without adding any flags, rent will be returned to the payer account for the order.