Giter VIP home page Giter VIP logo

flashloan-aggregator's Introduction

Flashloan Aggregator

Usage

Pre Requisites

Before running any command, make sure to install dependencies:

$ npm install

Compile

Compile the smart contracts with Hardhat:

$ npm run compile

TypeChain

Compile the smart contracts and generate TypeChain artifacts:

$ npm run typechain

Test

Run tests using interactive CLI

$ npm run test:runner

Run all the tests:

$ npm run test

Deploy

Deploy the contracts to Hardhat Network:

$ npm run deploy

Syntax Highlighting

If you use VSCode, you can enjoy syntax highlighting for your Solidity code via the vscode-solidity extension. The recommended approach to set the compiler version is to add the following fields to your VSCode user settings:

{
  "solidity.compileUsingRemoteVersion": "v0.8.4+commit.c7e474f2",
  "solidity.defaultCompiler": "remote"
}

Where of course v0.8.4+commit.c7e474f2 can be replaced with any other version.

flashloan-aggregator's People

Contributors

kaymasjain avatar piyush14-11 avatar pradyuman-verma avatar shriyatyagii avatar thrilok209 avatar vaibhav20201 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

flashloan-aggregator's Issues

Uniswap v3 route

Need to add uniswap v3 flash support to the FLA. Chains to add on:-

  • Polygon
  • Arbitrum
  • Optimism (Need to make a new folder for this)

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.

  • If its 1, _tokens[0] should be either equal to token0 or token1 from the pool key.
  • If its 2, _tokens[0] and _tokens[1] should be equal to token0 and token1 respectively

Now, 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.

Fluidity route

Start with polygon. We will add it to other chains too.

Minor updates

Contains all the minor updates that we need to implement with the next major update.

  • Change instaFeeBps from 4 decimals to 6 decimals.

Add 2 more routes (Compound & Aave)

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:

  • Flashborrow 500M DAI.
  • Deposit all of them into Compound or Aave.
  • Borrow the flash tokens from Compound or Aave.
  • Send them to the user.
  • User will return.
  • Payback the flash tokens on Compound or Aave.
  • Withdraw 500M DAI.
  • Flashpayback 500M DAI.

We can name Compound as route 3 and Aave as route 4.

Add fee

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 extra security module

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.

Optmizing uniswap route

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.

Follow proper standard

Always keep the first letter of function smaller. Eg:- change CompoundBorrow to compoundBorrow. This is the format that the whole coding industry follows.

Transfer fees to Treasury

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.

Separate folders for different chains

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:

  • Mainnet
  • Polygon
  • Avalanche

Flashloan aggregator

How flashloan works technically?

  • User's contract calls the flashloan contract with tokens, amounts and the encoded data in bytes to execute.
  • Flashloan contract sends the tokens to user's contract and calls a function (in Aave's case that function is executeOperation() with data and fees in the params.
  • User contracts decodes the data and use it in however manner they desire condition by the end of it user needs to return all the assets to the flashloan contract.

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.

Under Test Token Failure

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

  1. "before each" hook for "Should be able to take flashLoan of a single token from AAVE"

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:

FLA's own flashloan route

Add a route in FLA which will give flashloan from the funds stored in itself. This route will offer the lowest gas fee.

Update Flashloan aggregator

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.

  • Set up a new proxy
  • The main flashloan aggregator will have 11 routes.
  • Currently we have 8 and we need to add 2 new routes:
    • Route 8 for Uniswap V3. We have this route on other chains, just need to add on mainnet. Eg for polygon.
    • Route 11 for Maker PSM. Maker PSM allows DAI to USDC swaps at 1:1 ratio. The flow for this will be, if the flashloan token is USDC, take flashloan of DAI from Maker. Swap this DAI from Maker PSM for 1:1 USDC. The user will payback USDC + flashloan fee. Swap this amount from PSM and payback on Maker.

Changes for directly adding new routes

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-

  • Arbitrum
  • Avalanche
  • Fantom
  • Mainnet
  • Optimism

Aave flashloan using lenders' stable coin collateral.

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.

Add more routes to Balancer

On mainnet:-

  • Flashborrow ETH from Balancer.
  • Deposit in Aave/Compound.
  • ... existing MakerDAO thing we are doing.

On Polygon:-

  • Replicate the same thing.

Call cast for DSAs

Add a special function cast(). If the caller address is a DSA account then call the cast function rather than executeOperation.

Optimizing aave route

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.