Code Monkey home page Code Monkey logo

dydx-flashloan's Introduction

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)

dydx-flashloan's People

Contributors

aboudoc avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

dydx-flashloan's Issues

.env file ignored

thanks for sharing this project. but .env file is missing because the .gitignore including .env. so I can't run this project directly.

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.