Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Prometheo/092cc0a0e7664d0c286184fb9c68a8cf to your computer and use it in GitHub Desktop.
Save Prometheo/092cc0a0e7664d0c286184fb9c68a8cf to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.8.1;
import { SafeMath } from '@openzeppelin/contracts/utils/math/SafeMath.sol';
import { IERC20 } from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import { SafeERC20 } from '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import { IFlashLoanReceiverV2 } from '../interfaces/IFlashLoanReceiverV2.sol';
import { ILendingPoolAddressesProviderV2 } from '../interfaces/ILendingPoolAddressesProviderV2.sol';
import { ILendingPoolV2 } from '../interfaces/ILendingPoolV2.sol';
import "./Withdrawable.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.
!!!
*/
abstract contract FlashLoanReceiverBaseV2 is IFlashLoanReceiverV2 {
using SafeERC20 for IERC20;
using SafeMath for uint256;
ILendingPoolAddressesProviderV2 public immutable override ADDRESSES_PROVIDER;
ILendingPoolV2 public immutable override LENDING_POOL;
constructor(address provider) {
ADDRESSES_PROVIDER = ILendingPoolAddressesProviderV2(provider);
LENDING_POOL = ILendingPoolV2(ILendingPoolAddressesProviderV2(provider).getLendingPool());
}
receive() payable external {}
}
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.8.1;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
/**
Ensures that any contract that inherits from this contract is able to
withdraw funds that are accidentally received or stuck.
*/
contract Withdrawable is Ownable {
using SafeERC20 for ERC20;
address constant ETHER = address(0);
event LogWithdraw(
address indexed _from,
address indexed _assetAddress,
uint amount
);
/**
* @dev Withdraw asset.
* @param _assetAddress Asset to be withdrawn.
*/
function withdraw(address _assetAddress) public onlyOwner {
uint assetBalance;
if (_assetAddress == ETHER) {
address self = address(this); // workaround for a possible solidity bug
assetBalance = self.balance;
payable(msg.sender).transfer(assetBalance);
} else {
assetBalance = ERC20(_assetAddress).balanceOf(address(this));
ERC20(_assetAddress).safeTransfer(msg.sender, assetBalance);
}
emit LogWithdraw(msg.sender, _assetAddress, assetBalance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment