Skip to content

Instantly share code, notes, and snippets.

@Mobey-eth
Created January 23, 2023 21:26
Show Gist options
  • Save Mobey-eth/5963fb719f028286b5f4983c4605eee2 to your computer and use it in GitHub Desktop.
Save Mobey-eth/5963fb719f028286b5f4983c4605eee2 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import { FlashLoanReceiverBase } from "./FlashLoanReceiverBase.sol";
import { ILendingPool, ILendingPoolAddressesProvider, IERC20 } from "./Interfaces.sol";
import { SafeMath } from "./Libraries.sol";
/**
!!!
Never keep funds permanently on your FlashLoanReceiverBase contract as they could be
exposed to a 'griefing' attack, where the stored funds are used by an attacker.
!!!
*/
contract MyV2FlashLoan is FlashLoanReceiverBase {
using SafeMath for uint256;
constructor(ILendingPoolAddressesProvider _addressProvider) FlashLoanReceiverBase(_addressProvider) {}
/**
This function is called after your contract has received the flash loaned amount
*/
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
)
external
override
returns (bool)
{
//
// This contract now has the funds requested.
// Your logic goes here.
//
// At the end of your logic above, this contract owes
// the flashloaned amounts + premiums.
// Therefore ensure your contract has enough to repay
// these amounts.
// Approve the LendingPool contract allowance to *pull* the owed amount
for (uint i = 0; i < assets.length; i++) {
uint amountOwing = amounts[i].add(premiums[i]);
IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
}
return true;
}
function myFlashLoanCall(address addy) public {
address receiverAddress = address(this);
address[] memory assets = new address[](1);
assets[0] = address(addy); //goerli USDC 0x9FD21bE27A2B059a288229361E2fA632D8D2d074
// assets[1] = address(INSERT_ASSET_TWO_ADDRESS);
uint256[] memory amounts = new uint256[](1);
amounts[0] = 1*10**6;
// amounts[1] = INSERT_ASSET_TWO_AMOUNT;
// 0 = no debt, 1 = stable, 2 = variable
uint256[] memory modes = new uint256[](1);
modes[0] = 0;
// modes[1] = INSERT_ASSET_TWO_MODE;
address onBehalfOf = address(this);
bytes memory params = "";
uint16 referralCode = 0;
LENDING_POOL.flashLoan(
receiverAddress,
assets,
amounts,
modes,
onBehalfOf,
params,
referralCode
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment