Last active
March 30, 2024 04:09
-
-
Save shobhitic/8c0f2929898b625baa00770ed5274cee to your computer and use it in GitHub Desktop.
Simple Flashloan Tutorial - https://www.youtube.com/watch?v=2lY3sbjQBwk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Right click on the script name and hit "Run" to execute | |
const { expect } = require("chai"); | |
const { ethers } = require("hardhat"); | |
// Mainnet DAI Address | |
const DAI = "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063"; | |
// Random user's address that happens to have a lot of DAI on Polygon Mainnet | |
const DAI_WHALE = "0xdfD74E3752c187c4BA899756238C76cbEEfa954B"; | |
// Mainnet Pool contract address | |
const POOL_ADDRESS_PROVIDER = "0xa97684ead0e402dc232d5a977953df7ecbab3cdb"; | |
describe("Storage", function () { | |
it("test initial value", async function () { | |
const SimpleFlashLoan = await ethers.getContractFactory("SimpleFlashLoan"); | |
let simpleFlashLoan = SimpleFlashLoan.attach("0x45C823850AeB23E2ed11c9105d5f666C0BD539d1") | |
const token = await ethers.getContractAt("IERC20", DAI); | |
const BALANCE_AMOUNT_DAI = ethers.utils.parseEther("2000"); | |
const tx = await simpleFlashLoan.createFlashLoan(DAI, ethers.utils.parseEther("1000")); // Borrow 1000 DAI in a Flash Loan with no upfront collateral | |
await tx.wait(); | |
const remainingBalance = await token.balanceOf(simpleFlashLoan.address); // Check the balance of DAI in the Flash Loan contract afterwards | |
console.log(`remaining balance: ${remainingBalance}`) | |
expect(remainingBalance.lt(BALANCE_AMOUNT_DAI)).to.be.true; | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.4; | |
import "@openzeppelin/contracts/utils/math/SafeMath.sol"; | |
import "@aave/core-v3/contracts/flashloan/base/FlashLoanSimpleReceiverBase.sol"; | |
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
contract SimpleFlashLoan is FlashLoanSimpleReceiverBase { | |
using SafeMath for uint; | |
event Log(address asset, uint val); | |
constructor(IPoolAddressesProvider provider) FlashLoanSimpleReceiverBase(provider) {} | |
function createFlashLoan(address asset, uint amount) external { | |
address receiver = address(this); | |
bytes memory params = ""; | |
uint16 referralCode = 0; | |
emit Log(asset, IERC20(asset).balanceOf(address(this))); | |
POOL.flashLoanSimple( | |
receiver, | |
asset, | |
amount, | |
params, | |
referralCode | |
); | |
} | |
function executeOperation( | |
address asset, | |
uint256 amount, | |
uint256 premium, | |
address initiator, | |
bytes calldata params | |
) external returns (bool){ | |
// run arbitrage or liquidations here | |
// abi.decode(params) to decode params | |
emit Log(asset, IERC20(asset).balanceOf(address(this))); | |
uint amountOwing = amount.add(premium); | |
IERC20(asset).approve(address(POOL), amountOwing); | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npx hardhat init | |
$ npx hardhat node --fork https://polygon-rpc.com/ # replace https://polygon-rpc.com/ with YOUR_RPC_URL | |
$ npx hardhat console --network local # to connect with node via JS | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this transfer DAI to your account | |
const DAI = "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063"; | |
// Random user's address that happens to have a lot of DAI on Polygon Mainnet | |
const DAI_WHALE = "0x25E53Fe97360906cb990417cf0292a25DcF06075"; | |
const token = await ethers.getContractAt("IERC20", DAI); | |
const BALANCE_AMOUNT_DAI = ethers.utils.parseEther("2000"); | |
await hre.network.provider.request({ | |
method: "hardhat_impersonateAccount", | |
params: [DAI_WHALE], | |
}); | |
const signer = await ethers.getSigner(DAI_WHALE); | |
await token.connect(signer).transfer("ENTER ADDRESS HERE", BALANCE_AMOUNT_DAI); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment