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

Implement flashloans #16

Closed
MerlinEgalite opened this issue Jul 3, 2023 · 7 comments · Fixed by #168
Closed

Implement flashloans #16

MerlinEgalite opened this issue Jul 3, 2023 · 7 comments · Fixed by #168
Assignees

Comments

@MerlinEgalite
Copy link
Contributor

No description provided.

@makcandrov
Copy link
Contributor

Did we already discuss whether we should be ERC3156 compliant or not? Imo this ERC is not really relevant and is a burden for the contract that provides flash loans (it adds 2 useless functions) as well as for contracts that use the flash loans. Additionally, among the major flashloan providers, only Maker is ERC3156 compliant, while Balancer, Aave & Uniswap aren't, so it's not unusual to not be compliant

@MerlinEgalite
Copy link
Contributor Author

@Rubilmax I think you did a small benchmark about what would be the best solution no?

@Rubilmax
Copy link
Collaborator

Rubilmax commented Jul 12, 2023

From https://github.com/morpho-labs/blue/tree/poc/rubilmax#flash-loans:

I opted to enable flash loans following a modification of ERC3156: ERC3156FlashBorrower.onFlashLoan now returns bytes data which is passed as return data of ERC3156FlashLender.flashLoan, so that flash borrowers don't have to use storage to keep data.

Contango also suggested to have some way of defining which function the flash lender should call back from the flash borrower, but it opens up a vulnerability: anyone could be able to make the flash lender call any function from any contract, including ERC20.transfer. So there must be some way of whitelisting these: either use msg.sender as the flash borrower or require flash borrowers to permissionlessly register themselves upfront.

I find both solutions unappealing, because I don't think having the possibility to pass the callback as argument to ERC3156FlashLender.flashLoan has a lot of benefits: it would save a little gas compared to having a function dispatcher inside ERC3156FlashBorrower.onFlashLoan. If we anyway want this feature, I'd go for the permissionless upfront flash borrower registration.

My solution is already not compliant to EIP3156. I don't care whether we chose to implement maxFlashLoan and flashFee from EIP3156, which I believe @makcandrov considers useless.

Other references:

image image

@MerlinEgalite
Copy link
Contributor Author

What would be the data returned by the onLiquidation function?

@Rubilmax
Copy link
Collaborator

What would be the data returned by the onLiquidation function?

Do you mean the onFlashLoan callback?

Generic answer:

ERC3156FlashBorrower.onFlashLoan now returns bytes data which is passed as return data of ERC3156FlashLender.flashLoan, so that flash borrowers don't have to use storage to keep data.

It's essentially mimicking transient storage, via return data.

Practical usecase:

  1. Liquidator calls ERC3156FlashLender.flashLoan
  2. ERC3156FlashLender calls ERC3156FlashBorrower.onFlashLoan that returns returnData
  3. Liquidator gets returnData as return values of ERC3156FlashLender.flashLoan
  4. Liquidator can decode returnData to get information they would have stored to storage otherwise (how much did they swap? how much should they transfer? how much can they tip to block.coinbase?)

@MathisGD
Copy link
Contributor

For reference

@Rubilmax
Copy link
Collaborator

Some more information on Contango's usecase:

image

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 a pull request may close this issue.

4 participants