instadapp / flashloan-aggregator Goto Github PK
View Code? Open in Web Editor NEWFlashloan Aggregator on all major chains.
Home Page: https://docs.instadapp.io/flashloan/flashloanAggregator
License: MIT License
Flashloan Aggregator on all major chains.
Home Page: https://docs.instadapp.io/flashloan/flashloanAggregator
License: MIT License
How flashloan works technically?
data
in bytes to execute.executeOperation()
with data and fees in the params.Aave flashloan docs.
Look at the readme here
This is our current flashloan. Not very much standardised at the moment.
Build an upgradable Flashloan aggregator contract.
It'll have a function like:
function initiateFlashLoan(
address[] memory tokens_,
uint256[] memory amounts_,
uint256 route_,
bytes calldata data
) external isDSA isWhitelisted(data) {
// code
}
route
will help to decide from which flashloan provider do we need to flash borrow.
On mainnet:-
On Polygon:-
Before calling flashloan, hash the entire data in keccak bytes32 and store it in storage variable and when it calls back at the time verify the data back again and make it bytes(0)
This will allow us to add any flashloan provider without any security concerns.
Lenders will lend there tokens (USDC for now) to our contract, which we will supply to aave.
We will use the collateral to borrow, give flashloan and payback (aave flashloan at 0% fee).
Lenders will get the aave's apy, along with some cut in the fee whenever this route is used.
We need to update the flashloan aggregator from scratch. Current code has a lot of redundant code that has caused the contract size to increase a lot and it's hard to add new routes to it.
We need to merge all the common code together and try to make logics as modular and reusable as possible. All the core logics and storage variable updates should remain on the core contract. Rest everything can be moved to helpers or separate implementation.
How can we flash borrow Compound and Aave in other ways? We can deposit collateral into these protocols and borrow against them.
MakerDAO provides free flashloan access of 500M DAI. So what we do in our flashloan aggregator is:
We can name Compound as route 3 and Aave as route 4.
Add the addNewRoute
and fallback
functions so that we can add a new route directly via calling addNewRoute
on the main contract instead of re-deploying the main contract.
Additional function to add -
addNewRoutesAndEnable
, updateRouteImplementations
, changeRouteStatus
, getEnabledRoutes
Chains-
Currently we get the funds first in the FLA, then transfer them to the user. If we add the recipient as the caller, then the funds will directly go to the caller. This will save some gas.
There wont be any extra fee taken by FLA for whitelisted contracts.
Contains all the minor updates that we need to implement with the next major update.
Networks to add-in:
Helpful links:
https://docs.balancer.fi/concepts/features/flash-loans
https://dev.balancer.fi/references/contracts/contract-addresses [Vaults has the flashloan function]
https://etherscan.io/address/0xBA12222222228d8Ba445958a75a0704d566BF2C8#writeContract
Add a special function cast(). If the caller address is a DSA account then call the cast function rather than executeOperation.
We're going to have flashloan aggregators on each chain and they will differ according to the flashloan source. Eg:- Aave in on 3 chains but maker is only on 1 chain so separate out things according to different chains.
Chains to start with:
Need to implement this
A fee variable. Just like Aave flashloan. 1 = 1 BPS = 0.01% fee. 9 = 9 BPS = 0.09% fee.
The fee will be net. Eg:-
Let's say we set the fee at 5 BPS. If the flashloan fee is 2 BPS then we will only charge 3 BPS so net equals 5 BPS.
If the flashloan fee is already >=5 BPS. Eg:- Aave has 9 BPS then we won't charge anything in that case.
Add cream finance route (route 8)
Checkout the docs here.
Chains to add the route to:-
Always keep the first letter of function smaller. Eg:- change CompoundBorrow
to compoundBorrow
. This is the format that the whole coding industry follows.
Need to add uniswap v3 flash support to the FLA. Chains to add on:-
Checkout the docs here.
In order to take flashloan using a uniswap v3 pool, we need the call the flash function of the pool. So, we will be needing the pool address. We kept this extra param for future use, which we will be using for this integration. We will be encoding the pool key in this param while calling the uniswap route from the frontend. On the contracts side, we need the decode and get the pool key. Pool key is a struct which contains token0 address, token1 address and fee tier of the pool.
In the route handler we will check that the length of the _tokens
param should be 1 or 2.
_tokens[0]
should be either equal to token0
or token1
from the pool key._tokens[0]
and _tokens[1]
should be equal to token0
and token1
respectivelyNow, we have to calculate the pool address from the pool key in the route handler (using the uniswap v3 lib, or we can simply define our own function for it) and call the flash function of the pool, by passing the respective amounts.
In the callback, we will get the amounts, send the to the user, expect them to be returned with fee, and return it to the pool.
Add function to transfer fees to Treasury. It'll accept addresses[] tokens
& uint[] amounts
as params. If amounts
is type(uint).max
then transfer max balanceOf
.
Dont harcode the fees for pools on contract level, rather take them as params
Add Euler route on Mainnet
Currently we get the funds first in the FLA, then transfer them to the user. If we add the receiverAddress
as the caller, then the funds will directly go to the caller. This will save some gas.
Add FLA support on Fantom with the following routes -
Add a route in FLA which will give flashloan from the funds stored in itself. This route will offer the lowest gas fee.
also getting error here ๐ can you help fix?
$ npm run test:runner
[email protected] test:runner
hardhat run scripts/run-tests.ts
No need to generate any newer typings.
? What chain do you want to run tests on? mainnet
? Which file's tests do you want to run? all
No need to generate any newer typings.
FlashLoan
Single token
Resolver
1,2,3,4,5,6,7,9,5,5,5,5,5,5
โ Should be able to return routes info (15391ms)
3,4,5,6,7,5
2,3,4,5,6,7,5
โ Should be able to return the best route for flashloan (31816ms)
1,2,3,4,5,6,7,9,5,5,5,5,5,5,3,4,5,6,7,5
โ Should be able to return all the data for flashloan (800ms)
3 passing (56s)
1 failing
FlashLoan
"before each" hook for "Should be able to take flashLoan of a single token from AAVE":
Error: VM Exception while processing transaction: reverted with reason string 'Address: low-level delegate call failed'
Thanks for any help:
Start with polygon. We will add it to other chains too.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.