Table of Contents
npm init -y
npm install --save-dev hardhat
npx hardhat
npm i @openzeppelin/contracts@^4.3.2
npm install --save-dev dotenv
If you need testnet funds, use the Alchemy testnet faucet.
This project shows how to swap, add and remove liquidity
On DyDx the fees that you have to pay for flashloan is 2 wei
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
- Initiate a
solo
contract at the address ofSOLO
- 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 byDydxFlashloanBase.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()
- Withdraw: using
- We also need
accountInfos[]
that we will get by calling_getAccountInfo()
- Once we created these 3 actions in
operations[]
, we submit it over to thesolo
contract by callingoperate()
and passing in the 3 actions andaccountInfos[]
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 therepayAmount
- 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
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
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
This contract assumes that token0 and token1 both have same decimals
Consider Uniswap trading fee = 0.3%
- Flashloan test
- More reading
- Arbitrage using dydx Flashloan
See the open issues for a full list of proposed features (and known issues).
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!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Reda Aboutika - @twitter - reda.aboutika@gmail.com
Project Link: https://github.com/Aboudoc/dydx-Flashloan.git