Code Monkey home page Code Monkey logo

aggregated-flashloan's Introduction

Aggregated Flashloans

Overview

This is a working Truffle project you can build upon which draws on flash liquidity from multiple flash lenders within a single transaction. This template uses the Kollateral protocol which handles the aggregation of liquidity from multiple flash-loan-enabled pools such as DyDx and Aave.

Please note this template assumes prior experience with truffle development/deployment, dotenv usage, infura endpoints...etc If you're not familiar I highly recommend undertaking a corresponding tutorial beforehand, as most of the errors you encounter can be resolved with prior study.

Setup

  1. Clone the repo.
git clone https://github.com/fifikobayashi/Aggregated-Flashloan
cd Aggregated-Flashloan
  1. Install the Kollateral solidity library.
npm install @kollateral/contracts
  1. Install dotenv and hdwallet provider so you can hide your private key and Infura IDs within your own .env file.
npm install dotenv
npm install --save truffle-hdwallet-provider
  1. Create a .env file in the root of your project folder.
touch .env

Then edit the .env file and add the following two lines, along with your Infura ID and private key (no quotes).

IMPORTANT Make sure you learn how to use .gitignore to exclude this .env file when publishing your code to github. There have been many incidents, as recently as last week where someone accidentally uploaded their env file containing their private keys onto Github and got their accounts emptied within minutes.

ROPSTEN_PROVIDER=https://ropsten.infura.io/v3/YOUR_INFURA_ID
ROPSTEN_PRTK=YOUR_PRIVATE_KEY

Deployment

  1. Compile the project
truffle compile
  1. Deploy the project. (note: if you're on Node14 please see troubleshooting section below re: callback error)

Initial deployment:

truffle migrate --network ropsten

Subsequent deployments thereafter:

truffle migrate --network ropsten --reset
  1. When deployment completes, take note of the contract address and send some Ether to it so it can cover the aggregation fee plus the native flash loan fees. See Fees below for more info.

Execution

  1. Pop into the ropsten console
truffle console --network ropsten
  1. Execute the aggregated flash loan function by calling the invoke() function while in console mode:
AggregatedFlashloans.deployed().then(function(instance){return instance.invoke()});
  1. A successful execution of this code looks like this in console and like this on Etherscan.
  2. Once you're done playing with this, while still in console mode, you can withdraw the ether from the contract by calling:
AggregatedFlashloans.deployed().then(function(instance){return instance.WithdrawBalance()});

Fees

Kollateral currently charges 6bps on the flash liquidity that is sourced. In addition, you also need to factor in the native fees from each pool you aggregate from, such as Aave's 9bps flash fee and DyDx's 3 wei. If there is not enough funds on the contract to repay the loan + fees then the TX will be reverted.

Trouble shooting

  • If you keep getting a Callback was already called error when deploying contracts via Truffle/Infura/Node14, it's because ganache-cli's internal core is not yet node v14 compatible, so just use 'truffle migrate --network ropsten --skipDryRun'.
  • Failed Tx with ExternalCaller: insufficient ether balance - this means you forgot to send some ether to this contract to cover the aggregation and flash loan fees.
  • Failed Tx with Invoker: not enough liquidity - this means you're asking for too much liquidity than the aggregate pools can handle at this point in time, particularly prevalent in testnets with limited liquidity. Reduce your flash amount.



If you found this useful and would like to send me some gas money:

0xef03254aBC88C81Cb822b5E4DCDf22D55645bCe6

Thanks, @fifikobayashi.

aggregated-flashloan's People

Contributors

fifikobayashi 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

Watchers

 avatar  avatar  avatar

aggregated-flashloan's Issues

how does one compute the amount to be repaid?

     repay(); // ***Step 2: Repay the aggregated flash loan + above fees
    /* Remember to factor in:
        + 6bps in Kollateral aggregation fee
        + 9bps in native Aave flash loan fee
        + 2 Wei in native DyDx floan loan fee
    */

it seems the repay function would need to know how the loan was routed - ie the fraction of tokenAmount sent to aave or dydx? Other question what happens if we repay too much?

you forgot package.json

someone on reddit wanted to know why they couldn't npm install something

it's because you didn't put package.json into the repo (though package-lock is there)

This function is restricted to the contract's owner

I don't really know what the problem is....
But i get the same error everytime

`C:\Users\o\Desktop\Aggregated-Flashloan>truffle migrate --network ropsten --reset

Compiling your contracts...

Everything is up to date, there is nothing to compile.

Migrations dry-run (simulation)

Network name: 'ropsten-fork'
Network id: 3
Block gas limit: 8000029 (0x7a121d)

1_initial_migration.js

Replacing 'AggregatedFlashloans'

block number: 8992950
block timestamp: 1604328692
account: 0x3865FE951c10791a1A04AeC88d1063B158F75b48
balance: 2.547709980852136291
gas used: 755447 (0xb86f7)
gas price: 2 gwei
value sent: 0 ETH
total cost: 0.001510894 ETH

RuntimeError: VM Exception while processing transaction: revert This function is restricted to the contract's owner
at Migration._deploy (C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:97:1)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at Migration._load (C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:55:1)
at Migration.run (C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:171:1)
at Object.runMigrations (C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:150:1)
at Object.runFrom (C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:110:1)
at Object.runAll (C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:114:1)
at Object.run (C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:79:1)
at runMigrations (C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:269:1)
at setupDryRunEnvironmentThenRunMigrations (C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:257:1)
at C:\Users\o\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:220:1
Truffle v5.1.49 (core: 5.1.49)
Node v12.19.0`

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.