-
Notifications
You must be signed in to change notification settings - Fork 7
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 flash loans #7
Comments
took a stab at adding this. This is still untested since we want to try using the snapshots. Best way for that might actually be a testing crate that generates various snapshots depending on the state required but that's out of scope for this issue. The tldr is that:
I like this approach vs executing the transfer + invocation in the The real main question here is how to handle the receiver contract, i.e the contract called by blend when executing the flash loan. Right now, it's |
Awesome! I agree that For now, I would use It might be worth it to implement #11 alongside this and/or before extensive testing is written. This way, the |
After further discussion, it was deemed that #11 will not be implemented, which means it does not make sense to implement flash borrow as a request, given we will not have the ability to pass a contract ID into the request type. For now, it appears to make the most sense to do the following interface: pub struct FlashBorrow {
pub contract: Address,
pub asset: Address,
pub amount: i128
}
fn flash_borrow(
e: Env,
from: Address,
flashBorrow: FlashBorrow
requests: Vec<Request>,
) -> Positions; in this case, it is assumed that I am also OK leaving |
Hey @mootz12 I understand the reasoning about adding the new However, there are some elements that obscure my comprehension. Let me list the cases that come to my mind and the questions that pop up: Vanilla flash loan
If we want this outcome where Flash loan as part of a bundleUseful for collateral and debt swaps or liquidations, mostly in conjunction with a swapper. Collateral Swap
FIll Liquidation Auction
We can see in those cases that the flash loan is the first action. Does it mean you suggest we handle first the flash loan in Do I understand properly the context? Can you please rectify my comprehension if necessary and provide more specs and details about the desired outcomes. |
Hey @brozorec! The implementation will be slightly different than erc3156 flash loans in that, specifically, there is no expectation that the loan is repaid in full, rather that that account taking on the loan enough collateral to maintain the loan. Thus, the requests are used to process the repayment of the loan (or, whatever a user might want to do post a flash loan). Vanilla Flash loanIt would functionally work the same, but here is a concrete example of the implementation with Blend, lets say a user wants to take a flash loan out for 10k USDC to fill some arb opportunity, and the user wants to repay the full amount after.
Flash loan as part of a bundleCollateral SwapI'm not sure this would actually work in the existing implementation, at least easily in an atomic transaction, given no re-entrancy. IIRC something like this would not be possible on Aave in an atomic transaction either? Assuming the flash loan new_debt cannot result in Fill Liquidation AuctionFor a more concrete example, lets imagine there is
The big win here actually comes from #4, since with Let me know if that makes sense! Could you share some more info about the collateral swap use case? |
Noted, thank you @mootz12 I'll start working on that and will come back if more questions pop up.
As an example, the collateral swap is useful when the user has an open debt position with BTC as collateral and USDC as debt. They don't dispose the borrowed amount at the given moment (because they deployed it in high-yield farm). The user deems that XLM has a higher upward potential now and they want to swap their pledged BTC for XLM w/o closing the position. The logic for a debt swap will be similar but this time, the reason to swap the borrowed asset (say USDC) for another one (say USDT) could be that the interest rate of USDC is now much higher than USDT and the user wishes to optimize their interest cost. Btw there's another case for flash loans that might be applicable to Blend. It's building up and unwinding levered positions. For example, a user wants to leverage XLM; assuming 1 XLM = $0.5 and a target loan-to-value of 65%, they can achieve this by:
Their final position is → Supply: 4690 and Borrow: 1345 USDC. The user got ~2x leverage and they can keep looping but it's way more practical to accomplish this with a flash loan:
Using a flash loan to unwind a levered position is even more practical.
Yes, you're right it's difficult to make it work with the current implementation. Though, it is possible on Aave with the help o periphery contracts and the possibility to act on-behalf. Check here for more details. |
Yep, you can also enter and exit a levered position in a single transaction with Hopefully, as flash loan contracts get written, common implementations (swap USDC for XLM, or vice versa) can be shared by users, allowing easy/safe methods to do this. Interestingly, with And just to clarify @brozorec, you are going to attempt to tackle #4? If you want to reach me quicker, you should join the Blend discord, and DM me there. |
Add flash loan capabilities to Blend.
Technical Notes
Add a new action type to Blend called "FlashBorrow"
-> FlashBorrow will immediately send the sepecified amount of tokens, then invoke "processFlashLoan" on the user. This will fail if the user is not a contract.
The usage of flash loans would be as follows:
Deploy a flash loan strategy. (TODO - determine if interface is good)
Consider -> USDCXLMArbContract.processFlashLoan(from: Address, asset: Address, amount: i128) that takes in USDC to do some USDC<->XLM arbs and ends up with USDC.
The contract would need to return the USDC to from, or panic if some condition is not met
Invoke the the Blend pool with the following request stack, such that from is the user conducting the flash loan (IE - the one that actually takes on the liabilities)
Requests:
-> SupplyCollateral 50,000 XLM (user -> from)
-> FlashBorrow 100,000 USDC (user -> contract)
-> Repay 95,000 USDC (user -> from)
After the requests, the health factor for from is checked. Assuming 50k XLM is enough collateral to cover a 5k USDC loan, the transaction would complete successfully.
One benefit here is that flash loan strategies can be deployed and used by anyone.
The text was updated successfully, but these errors were encountered: