Skip to content

Aboudoc/dydx-Flashloan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DyDx Solo Margin - flashloan

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

Solo Margin

dydx Flashloan
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Learn more about dydx
  5. DyDxSoloMargin contract
  6. initiateFlashloan Function
  7. callFunction Function
  8. Test
  9. Forking mainnet
  10. Note
  11. Roadmap
  12. Contributing
  13. License
  14. Contact
  15. Acknowledgments

Setup the project

npm init -y
npm install --save-dev hardhat
npx hardhat
npm i @openzeppelin/contracts@^4.3.2
npm install --save-dev dotenv

Built With

  • Hardhat
  • Ethers

(back to top)

Usage

If you need testnet funds, use the Alchemy testnet faucet.

This project shows how to swap, add and remove liquidity

(back to top)

Learn more about dydx

On DyDx the fees that you have to pay for flashloan is 2 wei

(back to top)

DyDxSoloMargin contract

First thing to do, set the address of the contract to call flashloan on dydx (SOLO)

Next, set up some state variable, event and struct: flashUser, Log, MyCustomData

(back to top)

initiateFlashloan Function

  • Initiate a solo contract at the address of SOLO
  • We need the marketId Market ID's: 0 WETH; 1 SAI, 2 USDC, 3 DAI
  • Calculate the amount that we will need to repay by using _getRepaymentAmountInternal provided by DydxFlashloanBase.sol
  • Approve the SOLO contract to spend the repayment amount
  • The way you request a flashloan on DyDx is by creating 3 actions:
    • Withdraw: using _getWithdrawAction()
    • Call callFunction(): using _getCallAction()
    • Deposit: back using getDepositAction()
  • We also need accountInfos[] that we will get by calling _getAccountInfo()
  • Once we created these 3 actions in operations[], we submit it over to the solo contract by calling operate() and passing in the 3 actions and accountInfos[]

(back to top)

callFunction Function

dydx will callback this function

  • We only want the solo contract to be able to call this function, require it to restrict access
  • Check that the initiator of the flashloan is this contract by requiring it
  • Decode the data passed from the input
  • From MyCustomData decoded get back the repayAmount
  • Exctract also bal to make sure we ahve enough amount to repay back the flashloan
  • Write custom code: Arbitrage. For the example we just set flashUser state variable and Log bal, repay and profit variables

(back to top)

Test

We will borrow 1 000 000 USDC

To make sure we have enough amount to pay back flashloan, we will fund the contract with 2 000 000 USDC and check the balance of SOLO contract

npx hardhat test test/flashloan.test.js

(back to top)

Forking mainnet

hardhat.config.js

  networks: {
        hardhat: {
          forking: {
            url: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
       },
     },
  }

Note: Replace the ${} component of the URL with your personal Alchemy API key.

npx hardhat test testunlock-accounts.test.js

(back to top)

Note

This contract assumes that token0 and token1 both have same decimals

Consider Uniswap trading fee = 0.3%

Further reading

(back to top)

Sources

(back to top)

Roadmap

  • Flashloan test
  • More reading
  • Arbitrage using dydx Flashloan

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Reda Aboutika - @twitter - [email protected]

Project Link: https://github.com/Aboudoc/dydx-Flashloan.git

(back to top)

Acknowledgments

(back to top)

About

How to request a flashloan on dydx ⚡

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published