Draft collateral swap script #9
Draft
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.
Motivation
Draft script for collateral swap. I aim to use this code for our experimentation with the collateral swap using flash-loans in Sepolia, with the following goals:
Case covered
The use case I tried to covered is, collateral swap when you have a debt backed by some collateral (
oldCollateral) and you want to keep the debt but swap the collateral for another one (`newCollateral).The script uses the helper from (WIP):
cowprotocol/flash-loan-router#44
Assets involved
In this case, I set up a user with 10 aETHWeth and a debt of 1,000 GHO (I left simple instructions on how you can set a similar state in your account).
The user wants to swap:
These are the relevant addresses:
Relevant contracts
Main logic
oldCollateral, will be used for the sell amountorderHelpercontract based on the order details3.1 Sells
oldUnderlyingfornewUnderlying3.2 Owner and receiver is the helper contract
3.3. Includes the flashloan hint (I still don't have clear how to ask the funds to be delivered to the helper, will need to review that part
3.4 Creates the hook to deploy the helper contract
3.5 Creates the hook to do the collateral swap
3.5 Ask for the quote
* 🚨 currently fails here (keep reading)
*
4.1 Before posting the order, sets the allowance so the
oldCollateralcan be transferred to the helper.* 🚨 We might need to modify the SDK to make it easier to use 1271 orders. When we post the order, we need to include the signature including the order, which should be decodable later
GPv2Order.Data memory _order = abi.decode(_signature, (GPv2Order.Data));. Keep in mind the signature will be simpler in a future implementation, because we don't need all the order data (most of them are already constants in the contract)*
4.2 Post the order
Current appData
The order sets a pre-hook and a post-hook, so these params are a good summary on how the order looks like:
🚨 Not working yet
On top of the issues pointed out in the "main logic" section, there are some small things to review:
NoLiquidity. I ask for the quote, but I get this error because there's no liquidity for WETH-USDC, we could add liquidity in univ2 to make it workHow to test
.envfileEnv file:
Then run:
Open questions
oldCollateralfrom the borrower to the helper.loanReceiver = helperContractquote determines the minReceived --> The minReceive determines the helper address --> The helper address needs to be part of the hook which presumably gets included in the quote in order for the estimation to succeed* [X] We don't see backend dependency issues with indexing the order. The order is born in the orderbook
* [ ] Our understanding of Backend work is:
- pre-interaction mentioned above in the driver
- [good to have] keeping track of the real trader, so we can show it in the explorer
- We can get by by having the mapping in the UI and linking directly to the order. Improble but not launch blocking
* Because we might add a signature verification, we will need to add the trader in the factory
* We can emit an event with the orderUid, and trader (ideally), or with the contract address.
* A service can index the events and link the trader to all their collateral swap orders
* This is not required, so would be just nice to have to make this: simpler, more gas efficient
* Problem: contract needs to be deployed
* we could add info in the hint that we need to deploy (calldata might be a security hole for people frontrunning the settlement, but passing the factory params suffice)
* We agree we can explore this later, is not a must and might require more changes
oldCollateralis not yet in the helper? Technically, the order will contain the flash-loan hint and the approval to move the funds from the settlement to the helper, so the funds can be made available at any time by a solver.Backend requirements
Out of this PR, I would like we comply some expectations for the Orderbook API and driver to make this work. They should be listed here so backend can review them. These items will be know once we answer the questions above.