Fujicracy / fuji-v2

Cross-chain money market aggregator
https://fuji-v2-frontend.vercel.app
15 stars 10 forks source link

Balancer flashloan provider #257

Closed pedrovalido closed 1 year ago

pedrovalido commented 1 year ago

Integrating Balancer as Flashloan provider in every chain we currently have. Turns out they have it deployed on mainnet, arbitrum, optimism, polygon and even gnosis! Btw, big plus their fee being 0 cost which means after this PR, we will have a flasher on every chain with 0 cost :) I took advantage of the tests done previously for FlasherAaveV3 (which include a super simple flashloan and a rebalance with Rebalancer Manager which is the real use case scenario)

0xdcota commented 1 year ago

@pedrovalido I will review this tomorrow.

Only a non-PR related comment, I believe balancer at some point introduced a small fee on eth-mainnet for their flashloans? This was not the case anymore? Just double check. It has been months last time I checked.

pedrovalido commented 1 year ago

@pedrovalido I will review this tomorrow.

Only a non-PR related comment, I believe balancer at some point introduced a small fee on eth-mainnet for their flashloans? This was not the case anymore? Just double check. It has been months last time I checked.

After some digging, I found out their fees are currently zero, otherwise the tests would fail. They have an if statement in the flashloan function saying if feeAmount has not been paid, throw an error (error is not being thrown). With this if statement I went searching for where this "feeAmounts" is being defined. Turns out there's an admin function where they can change the fee amount

Screenshot 2023-01-18 at 08 55 20

This aligns with what they say in the docs

Screenshot 2023-01-18 at 09 00 36

"At deployment, flash loan fees were set to zero, and as of this writing they have not been activated by governance." So, they're currently 0 because they haven't been activated but can be in the future.

Thank you for noticing this @DaigaroCota . I will make the necessary changes

pedrovalido commented 1 year ago

From the Balancer's vault we can see the flasloan fee percentage is returned by the IProtocolFeesCollector's function getFlashLoanFeePercentage(). Both that function and the IProtocolFeesCollector interface have been added. Using them in the computeFlashloanFee inside Fuji's FlasherBalancer it would be simple (and slightly incorrect) to just return fee * feePercentage (feePercentage being the value returned by the function mentioned above). From balancer's calculation of the fee to be paid

Screenshot 2023-01-18 at 22 07 28

They are using FixedPoint.mulUp function:

Screenshot 2023-01-18 at 22 08 56