Skip to content

Instantly share code, notes, and snippets.

@yoyoismee
Created June 1, 2022 10:44
Show Gist options
  • Save yoyoismee/d4739fdac9cf32a5400f3f84b3a2c804 to your computer and use it in GitHub Desktop.
Save yoyoismee/d4739fdac9cf32a5400f3f84b3a2c804 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
// author: yoyoismee.eth -- it's opensource but also feel free to send me coffee/beer.
// 8 8888888888 8 8888 .8. d888888o. 8 8888 8 8 8888 ,88' 8 8888 88 8 888888888o
// 8 8888 8 8888 .888. .`8888:' `88. 8 8888 8 8 8888 ,88' 8 8888 88 8 8888 `88.
// 8 8888 8 8888 :88888. 8.`8888. Y8 8 8888 8 8 8888 ,88' 8 8888 88 8 8888 `88
// 8 8888 8 8888 . `88888. `8.`8888. 8 8888 8 8 8888 ,88' 8 8888 88 8 8888 ,88
// 8 888888888888 8 8888 .8. `88888. `8.`8888. 8 8888 8 8 8888 ,88' 8 8888 88 8 8888. ,88'
// 8 8888 8 8888 .8`8. `88888. `8.`8888. 8 8888 8 8 8888 88' 8 8888 88 8 8888888888
// 8 8888 8 8888 .8' `8. `88888. `8.`8888. 8 8888888888888 8 888888< 8 8888 88 8 8888 `88.
// 8 8888 8 8888 .8' `8. `88888. 8b `8.`8888. 8 8888 8 8 8888 `Y8. ` 8888 ,8P 8 8888 88
// 8 8888 8 8888 .888888888. `88888. `8b. ;8.`8888 8 8888 8 8 8888 `Y8. 8888 ,d8P 8 8888 ,88'
// 8 8888 8 888888888888 .8' `8. `88888. `Y8888P ,88P' 8 8888 8 8 8888 `Y8. `Y88888P' 8 888888888P
pragma solidity 0.8.14;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
interface IFlashLoanReceiver {
function executeOperation(address _reserve, uint256 _amount, uint256 _fee, bytes calldata _params) external;
}
// flash kub protocol
contract FlashKub is ReentrancyGuard{
using SafeERC20 for IERC20;
IERC20 constant KUSDT = IERC20(0x7d984C24d2499D840eB3b7016077164e15E5faA6);
mapping(address => uint256) share;
uint feeBips = 9; // 0.09 % fees
uint256 totalShare;
function deposit(uint amount) external nonReentrant{
uint initialBal = KUSDT.balanceOf(address(this));
if (totalShare == 0){
totalShare = amount;
share[msg.sender] += amount;
}else{
uint newShare = amount * initialBal / totalShare;
totalShare += newShare;
share[msg.sender] += newShare;
}
KUSDT.safeTransferFrom(msg.sender,address(this), amount);
}
function flashloan(address _receiver, address _reserve, uint256 _amount, bytes memory _params) external{
_flashLoan(_receiver,_reserve,_amount,_params);
}
function flashloan(address _receiver, uint _amount, bytes memory _params) external{
_flashLoan(_receiver,address(KUSDT),_amount,_params);
}
function flashloan( uint _amount, bytes memory _params) external{
_flashLoan(msg.sender,address(KUSDT),_amount,_params);
}
function withdraw(uint amount) external nonReentrant{
require(amount <= share[msg.sender]);
uint initialBal = KUSDT.balanceOf(address(this));
uint toSend = amount * initialBal / totalShare;
totalShare -= amount;
share[msg.sender] -= amount;
KUSDT.safeTransfer(msg.sender, toSend);
}
function _flashLoan(address _receiver, address _reserve, uint256 _amount, bytes memory _params)
public
nonReentrant
{
uint256 availableLiquidityBefore = KUSDT.balanceOf(address(this));
require(
availableLiquidityBefore >= _amount,
"There is not enough liquidity available to borrow"
);
//calculate amount fee
uint256 amountFee = _amount * feeBips / 10000;
//get the FlashLoanReceiver instance
IFlashLoanReceiver receiver = IFlashLoanReceiver(_receiver);
//transfer funds to the receiver
KUSDT.safeTransfer(_receiver, _amount);
//execute action of the receiver
receiver.executeOperation(address(KUSDT), _amount, amountFee, _params);
//check that the actual balance of the core contract includes the returned amount
uint256 availableLiquidityAfter = KUSDT.balanceOf(address(this));
require(
availableLiquidityAfter == availableLiquidityBefore + amountFee,
"The actual balance of the protocol is inconsistent"
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment