GithubHelp home page GithubHelp logo

kulaneian / dydx-aave-curve Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kayaba-attribution/dydx-aave-curve

0.0 0.0 0.0 134 KB

Example of a DyDx Flashloan on a mainet fork to get ~$65,000,000 USDC and USDT with 0% swapping fees, interact with AAVE, CurveFi, Uniswap and pay back the loans

JavaScript 6.72% Solidity 93.28%

dydx-aave-curve's Introduction

Freelancer Project. DyDx FlashLoan + AAVE + CURVEFI + UNISWAP

This project includes the commented and logged contract for ease of understanding and learning on how to perform a DyDx loan, and interact with various protocols.

Initial approach took a loan of WETH and used UniSwap to get USDC and USDT, this method was unrelaiable and fee expensive.

The direct borrow of USDC and USDT was also considered but DyDx only has USDC avaliable for flashloan and only ~$8,000,000

Final and implemented method consist on a series of deposits and WETH collateralized stablecoin borrows on the AAVE protocol the get USDT and USDC virtually at no 0% fee cost.

The result of this approach at block 13506500 is ~$33,440,000 in USDC and ~$33,440,000 in USDT.

With Curve.fi Swaps disabled the loan can be paid, only paying gas fees; with the swaps enabled, the loan is not payable as Curve takes a fee on each swap resulting in a loss of ~22,000 USD.

How To Run this Project

Clone the repo or unzip it, then open the folder on VsCode and run the following in the terminal:

// Intall dependencies
npm install --save-dev hardhat @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers

// Compile the contracts
npx hardhat compile

// On another terminal fork the ETH mainet
npx hardhat node --fork https://speedy-nodes-nyc.moralis.io/your-key/eth/mainnet/archive --fork-block-number 13506500

// (runs the test contract that logs on every step with CurveFi swaps disabled)
npx hardhat test --network localhost 

// (runs the no comment contract with CurveFi swaps enabled)
npx hardhat run scripts/deploy.js --network localhost

Both dydx.sol and FlashLoanNoComments.sol have the explanation of their workings inside.

Please make all the tests in the forked enviroment before jumping into the mainet.

Thank you!

Logs with Curve.fi Swaps Enabled:

  console.log:
    Logic Start ;)
    [DyDx] WETH FlashLoan: 20000 ( FLASHLOAN WETH )
    [CONTRACT] WETH:  20000
    --- Borrow USDC and USDT from AAVE ---
    (essentialy swap our assets to stablecoins without paying Uniswap fees)
    [AAVE] Approve lending pool to use our WETH
    [AAVE] Deposit all WETH - 2 wei:
    [CONTRACT] WETH ( in wei ): 2
    [CONTRACT] aWETH ( in ether ): 20000
    We can borrow up to 80% of the collateral
    [AAVE] Borrow USDT with 4/10 of the WETH...
    [CONTRACT] USDT:  33440000
    [AAVE] Borrow USDC with 4/10 of the WETH...
    [CONTRACT] USDC:  33440000
    ---- Protocol Interactions ----
    ############# AAVE ##############
    [AAVE] Deposit USDC and get aUSDC
    [CONTRACT] USDC:  0
    [CONTRACT] aUSDC:  33440000
    [AAVE] Withdraw all aUSDC:
    [CONTRACT] aUSDC:  0
    [CONTRACT] USDC:  33440000
    ########### AAVE END ############
    ############# CURVE ##############
    [CURVE] Approve USDT
    [CURVE] Approve USDC
    [CONTRACT] USDT: 33440000
    [CONTRACT] USDC: 33440000
    [CURVE] Swap USDT to USDC
    [CONTRACT] USDT: 66852991
    [CONTRACT] USDC: 0
    [CURVE] Swap USDC to USDT
    [CONTRACT] USDT: 33440000
    [CONTRACT] USDC: 33413254
    ############ CURVE END ############
    ---- Repay USDC and USDT loans ----
    [AAVE] Approve USDT
    [AAVE] Approve USDC
    [AAVE] Repay the USDC and USDT

  Error: VM Exception while processing transaction: reverted with reason string 'NOT ENOUGH USDC TO PAY 
FOR AAVE LOAN'

Logs for Curve.fi swaps disabled:

  console.log:
    Logic Start ;)
    [DyDx] WETH FlashLoan: 20000 ( FLASHLOAN WETH )
    [CONTRACT] WETH:  20000
    --- Borrow USDC and USDT from AAVE ---
    (essentialy swap our assets to stablecoins without paying Uniswap fees)
    [AAVE] Approve lending pool to use our WETH
    [AAVE] Deposit all WETH - 2 wei:
    [CONTRACT] WETH ( in wei ): 2
    [CONTRACT] aWETH ( in ether ): 20000
    We can borrow up to 80% of the collateral
    [AAVE] Borrow USDT with 4/10 of the WETH...
    [CONTRACT] USDT:  33440000
    [AAVE] Borrow USDC with 4/10 of the WETH...
    [CONTRACT] USDC:  33440000
    ---- Protocol Interactions ----
    ############# AAVE ##############
    [AAVE] Deposit USDC and get aUSDC
    [CONTRACT] USDC:  0
    [CONTRACT] aUSDC:  33440000
    [AAVE] Withdraw all aUSDC:
    [CONTRACT] aUSDC:  0
    [CONTRACT] USDC:  33440000
    ########### AAVE END ############
    ############# CURVE ##############
    [CURVE] Approve USDT
    [CURVE] Approve USDC
    [CONTRACT] USDT: 33440000
    [CONTRACT] USDC: 33440000
    [CURVE] Swap USDT to USDC
    [CONTRACT] USDT: 33440000
    [CONTRACT] USDC: 33440000
    [CURVE] Swap USDC to USDT
    [CONTRACT] USDT: 33440000
    [CONTRACT] USDC: 33440000
    ############ CURVE END ############
    ---- Repay USDC and USDT loans ----
    [AAVE] Approve USDT
    [AAVE] Approve USDC
    [AAVE] Repay the USDC and USDT
    [CONTRACT] USDC ( after payment ):  0
    [CONTRACT] USDT ( after payment ):  0
    [AAVE] Withdraw all aWETH:  0 aWETH
    [CONTRACT] aWETH:  0
    [CONTRACT] WETH:  20000
    DyDx Loan Paid !!!

dydx-aave-curve's People

Contributors

kayaba-attribution avatar

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.