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 flash loans #7

Open
mootz12 opened this issue Dec 3, 2024 · 2 comments
Open

Add flash loans #7

mootz12 opened this issue Dec 3, 2024 · 2 comments
Labels
enhancement New feature or request pool This issue impacts the pool

Comments

@mootz12
Copy link
Contributor

mootz12 commented Dec 3, 2024

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:

  1. 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

  2. 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.

@mootz12 mootz12 added enhancement New feature or request pool This issue impacts the pool labels Dec 3, 2024
@heytdep
Copy link

heytdep commented Dec 9, 2024

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:

  • A new FlashBorrow was added.
  • A new flash_borrow field was added to Actions.
  • FlashBorrow is almost equivalent to Borrow, the only difference is that instead of adding a pool transfer for the borrow, it's adding it to flash_borrow, which is handled before all the other actions, allowing the flash loan executor to execute the ideally profit-earning strategy before the transfers to unwind the hf (either supply collateral or repay) are carried.
  • the currently used interface is the xycloans standard inspired by erc3156 (https://github.com/xycloo/xycloans/tree/main/moderc3156)

I like this approach vs executing the transfer + invocation in the FlashBorrow action code block because it's more similar to the current design of the blend codebase and it's likely better for deriving the flash loan contract without changing the structure of requests.

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 from but this doesn't allow for receiver contracts to be cross-user (which will also probs require a change in the current receiver interface) and requires the receiver contract to be the one holding the actual positions. I wouldn't want to add a new parameter to the submission but neither spender or to seem better than from, maybe spender?

@mootz12
Copy link
Contributor Author

mootz12 commented Dec 9, 2024

Awesome!

I agree that from would not work as it is expected that flash loans contract will exist that should not hold positions in a pool.

For now, I would use to, given to is the destination of the tokens being sent.

It might be worth it to implement #11 alongside this and/or before extensive testing is written. This way, the FlashBorrow action will have a user (or something similar) and asset parameter that can specify both the asset being flash loaned and the flash loan contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pool This issue impacts the pool
Projects
None yet
Development

No branches or pull requests

2 participants