Merge pull request #10 from InstaDApp/Instapool-connector-fee

Updated InstaPool Connector(Added Fee)
This commit is contained in:
Thrilok kumar 2020-07-18 01:45:45 +05:30 committed by GitHub
commit 56c7d10a43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 267 additions and 42 deletions

View File

@ -1,8 +1,8 @@
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "../../node_modules/@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { IERC20 } from "../../node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface LiqudityInterface {
function deposit(address, uint) external payable;
@ -18,6 +18,11 @@ interface LiqudityInterface {
function borrowedToken(address) external view returns(uint);
}
interface InstaPoolFeeInterface {
function fee() external view returns(uint);
function feeCollector() external view returns(address);
}
interface CTokenInterface {
function borrowBalanceCurrent(address account) external returns (uint);
function balanceOf(address owner) external view returns (uint256 balance);
@ -57,6 +62,10 @@ contract DSMath {
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
}
function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x, "sub-overflow");
}
uint constant WAD = 10 ** 18;
function wmul(uint x, uint y) internal pure returns (uint z) {
@ -66,10 +75,8 @@ contract DSMath {
function wdiv(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, WAD), y / 2) / y;
}
}
contract Helpers is DSMath {
using SafeERC20 for IERC20;
@ -110,16 +117,22 @@ contract Helpers is DSMath {
}
/**
* @dev Connector Details
* @dev Connector Details.
*/
function connectorID() public pure returns(uint _type, uint _id) {
(_type, _id) = (1, 8);
(_type, _id) = (1, 33);
}
function _transfer(address payable to,address token, uint _amt) internal {
token == getAddressETH() ?
function _transfer(address payable to, IERC20 token, uint _amt) internal {
address(token) == getAddressETH() ?
to.transfer(_amt) :
IERC20(token).safeTransfer(to, _amt);
token.safeTransfer(to, _amt);
}
function _getBalance(IERC20 token) internal view returns (uint256) {
return address(token) == getAddressETH() ?
address(this).balance :
token.balanceOf(address(this));
}
}
@ -129,7 +142,61 @@ contract LiquidityHelpers is Helpers {
* @dev Return InstaPool address
*/
function getLiquidityAddress() internal pure returns (address) {
return 0x1879BEE186BFfBA9A8b1cAD8181bBFb218A5Aa61;
return 0x06cB7C24990cBE6b9F99982f975f9147c000fec6;
}
/**
* @dev Return InstaPoolFee address
*/
function getInstaPoolFeeAddr() internal pure returns (address) {
return 0xAaA91046C1D1a210017e36394C83bD5070dadDa5;
}
function calculateTotalFeeAmt(IERC20 token, uint amt) internal view returns (uint totalAmt) {
uint fee = InstaPoolFeeInterface(getInstaPoolFeeAddr()).fee();
uint flashAmt = LiqudityInterface(getLiquidityAddress()).borrowedToken(address(token));
if (fee == 0) {
totalAmt = amt;
} else {
uint feeAmt = wmul(flashAmt, fee);
totalAmt = add(amt, feeAmt);
}
}
function calculateFeeAmt(IERC20 token, uint amt) internal view returns (address feeCollector, uint feeAmt) {
InstaPoolFeeInterface feeContract = InstaPoolFeeInterface(getInstaPoolFeeAddr());
uint fee = feeContract.fee();
feeCollector = feeContract.feeCollector();
if (fee == 0) {
feeAmt = 0;
} else {
feeAmt = wmul(amt, fee);
uint totalAmt = add(amt, feeAmt);
uint totalBal = _getBalance(token);
require(totalBal >= totalAmt - 10, "Not-enough-balance");
feeAmt = totalBal > totalAmt ? feeAmt : sub(totalBal, amt);
}
}
function calculateFeeAmtOrigin(IERC20 token, uint amt)
internal
view
returns (
address feeCollector,
uint poolFeeAmt,
uint originFee
)
{
uint feeAmt;
(feeCollector, feeAmt) = calculateFeeAmt(token, amt);
if (feeAmt == 0) {
poolFeeAmt = 0;
originFee = 0;
} else {
originFee = wmul(feeAmt, 20 * 10 ** 16); // 20%
poolFeeAmt = sub(feeAmt, originFee);
}
}
}
@ -154,8 +221,9 @@ contract LiquidityManage is LiquidityHelpers {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
ethAmt = _amt;
} else {
_amt = _amt == uint(-1) ? TokenInterface(token).balanceOf(address(this)) : _amt;
TokenInterface(token).approve(getLiquidityAddress(), _amt);
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
tokenContract.approve(getLiquidityAddress(), _amt);
}
LiqudityInterface(getLiquidityAddress()).deposit.value(ethAmt)(token, _amt);
@ -189,11 +257,72 @@ contract LiquidityManage is LiquidityHelpers {
}
}
contract EventHelpers is LiquidityManage {
event LogFlashBorrow(
address indexed token,
uint256 tokenAmt,
uint256 getId,
uint256 setId
);
contract LiquidityAccess is LiquidityManage {
event LogFlashBorrow(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogFlashPayback(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogFlashPayback(
address indexed token,
uint256 tokenAmt,
uint256 feeCollected,
uint256 getId,
uint256 setId
);
event LogOriginFeeCollected(
address indexed origin,
address indexed token,
uint256 tokenAmt,
uint256 originFeeAmt
);
function emitFlashBorrow(address token, uint256 tokenAmt, uint256 getId, uint256 setId) internal {
emit LogFlashBorrow(token, tokenAmt, getId, setId);
bytes32 _eventCode = keccak256("LogFlashBorrow(address,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, tokenAmt, getId, setId);
(uint _type, uint _id) = connectorID();
EventInterface(getEventAddr()).emitEvent(_type, _id, _eventCode, _eventParam);
}
function emitFlashPayback(address token, uint256 tokenAmt, uint256 feeCollected, uint256 getId, uint256 setId) internal {
emit LogFlashPayback(token, tokenAmt, feeCollected, getId, setId);
bytes32 _eventCode = keccak256("LogFlashPayback(address,uint256,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, tokenAmt, feeCollected, getId, setId);
(uint _type, uint _id) = connectorID();
EventInterface(getEventAddr()).emitEvent(_type, _id, _eventCode, _eventParam);
}
function emitOriginFeeCollected(address origin, address token, uint256 tokenAmt, uint256 originFeeAmt) internal {
emit LogOriginFeeCollected(origin, token, tokenAmt, originFeeAmt);
bytes32 _eventCodeOrigin = keccak256("LogOriginFeeCollected(address,address,uint256,uint256)");
bytes memory _eventParamOrigin = abi.encode(origin, token, tokenAmt, originFeeAmt);
(uint _type, uint _id) = connectorID();
EventInterface(getEventAddr()).emitEvent(_type, _id, _eventCodeOrigin, _eventParamOrigin);
}
}
contract LiquidityAccessHelper is EventHelpers {
/**
* @dev Add Fee Amount to borrowed flashloan/
* @param amt Get token amount at this ID from `InstaMemory` Contract.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function addFeeAmount(address token, uint amt, uint getId, uint setId) external payable {
uint _amt = getUint(getId, amt);
require(_amt != 0, "amt-is-0");
uint totalFee = calculateTotalFeeAmt(IERC20(token), _amt);
setUint(setId, totalFee);
}
}
contract LiquidityAccess is LiquidityAccessHelper {
/**
* @dev Access Token Liquidity from InstaPool.
* @param token token address.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
@ -212,12 +341,7 @@ contract LiquidityAccess is LiquidityManage {
LiqudityInterface(getLiquidityAddress()).accessLiquidity(_tknAddrs, _amts);
setUint(setId, _amt);
emit LogFlashBorrow(token, _amt, getId, setId);
bytes32 _eventCode = keccak256("LogFlashBorrow(address,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, _amt, getId, setId);
(uint _type, uint _id) = connectorID();
EventInterface(getEventAddr()).emitEvent(_type, _id, _eventCode, _eventParam);
emitFlashBorrow(token, _amt, getId, setId);
}
/**
@ -229,22 +353,56 @@ contract LiquidityAccess is LiquidityManage {
function flashPayback(address token, uint getId, uint setId) external payable {
LiqudityInterface liquidityContract = LiqudityInterface(getLiquidityAddress());
uint _amt = liquidityContract.borrowedToken(token);
IERC20 tokenContract = IERC20(token);
(address feeCollector, uint feeAmt) = calculateFeeAmt(tokenContract, _amt);
address[] memory _tknAddrs = new address[](1);
_tknAddrs[0] = token;
_transfer(payable(address(liquidityContract)), token, _amt);
_transfer(payable(address(liquidityContract)), tokenContract, _amt);
liquidityContract.returnLiquidity(_tknAddrs);
if (feeAmt > 0) _transfer(payable(feeCollector), tokenContract, feeAmt);
setUint(setId, _amt);
emitFlashPayback(token, _amt, feeAmt, getId, setId);
}
/**
* @dev Return Token Liquidity from InstaPool and Transfer 20% of Collected Fee to `origin`.
* @param origin origin address to transfer 20% of the collected fee.
* @param token token address.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function flashPaybackOrigin(address origin, address token, uint getId, uint setId) external payable {
require(origin != address(0), "origin-is-address(0)");
LiqudityInterface liquidityContract = LiqudityInterface(getLiquidityAddress());
uint _amt = liquidityContract.borrowedToken(token);
IERC20 tokenContract = IERC20(token);
(address feeCollector, uint poolFeeAmt, uint originFeeAmt) = calculateFeeAmtOrigin(tokenContract, _amt);
address[] memory _tknAddrs = new address[](1);
_tknAddrs[0] = token;
_transfer(payable(address(liquidityContract)), tokenContract, _amt);
liquidityContract.returnLiquidity(_tknAddrs);
if (poolFeeAmt > 0) {
_transfer(payable(feeCollector), tokenContract, poolFeeAmt);
_transfer(payable(origin), tokenContract, originFeeAmt);
}
setUint(setId, _amt);
emit LogFlashPayback(token, _amt, getId, setId);
bytes32 _eventCode = keccak256("LogFlashPayback(address,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, _amt, getId, setId);
(uint _type, uint _id) = connectorID();
EventInterface(getEventAddr()).emitEvent(_type, _id, _eventCode, _eventParam);
emitFlashPayback(token, _amt, poolFeeAmt, getId, setId);
emitOriginFeeCollected(origin, token, _amt, originFeeAmt);
}
}
contract LiquidityAccessMulti is LiquidityAccess {
/**
* @dev Access Multiple Token liquidity from InstaPool.
* @param tokens Array of token addresses.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
@ -268,12 +426,7 @@ contract LiquidityAccess is LiquidityManage {
for (uint i = 0; i < _length; i++) {
setUint(setId[i], _amts[i]);
emit LogFlashBorrow(tokens[i], _amts[i], getId[i], setId[i]);
bytes32 _eventCode = keccak256("LogFlashBorrow(address,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(tokens[i], _amts[i], getId[i], setId[i]);
(uint _type, uint _id) = connectorID();
EventInterface(getEventAddr()).emitEvent(_type, _id, _eventCode, _eventParam);
emitFlashBorrow(tokens[i], _amts[i], getId[i], setId[i]);
}
}
@ -290,23 +443,54 @@ contract LiquidityAccess is LiquidityManage {
for (uint i = 0; i < _length; i++) {
uint _amt = liquidityContract.borrowedToken(tokens[i]);
IERC20 tokenContract = IERC20(tokens[i]);
(address feeCollector, uint feeAmt) = calculateFeeAmt(tokenContract, _amt);
_transfer(payable(address(liquidityContract)), tokens[i], _amt);
_transfer(payable(address(liquidityContract)), tokenContract, _amt);
if (feeAmt > 0) _transfer(payable(feeCollector), tokenContract, feeAmt);
setUint(setId[i], _amt);
emit LogFlashPayback(tokens[i], _amt, getId[i], setId[i]);
bytes32 _eventCode = keccak256("LogFlashPayback(address,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(tokens[i], _amt, getId[i], setId[i]);
(uint _type, uint _id) = connectorID();
EventInterface(getEventAddr()).emitEvent(_type, _id, _eventCode, _eventParam);
emitFlashPayback(tokens[i], _amt, feeAmt, getId[i], setId[i]);
}
liquidityContract.returnLiquidity(tokens);
}
/**
* @dev Return Multiple token liquidity from InstaPool and Tranfer 20% of the Fee to Origin.
* @param tokens Array of token addresses.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param getId get token amounts at this IDs from `InstaMemory` Contract.
* @param setId set token amounts at this IDs in `InstaMemory` Contract.
*/
function flashMultiPaybackOrigin(address origin, address[] calldata tokens, uint[] calldata getId, uint[] calldata setId) external payable {
LiqudityInterface liquidityContract = LiqudityInterface(getLiquidityAddress());
uint _length = tokens.length;
for (uint i = 0; i < _length; i++) {
uint _amt = liquidityContract.borrowedToken(tokens[i]);
IERC20 tokenContract = IERC20(tokens[i]);
(address feeCollector, uint poolFeeAmt, uint originFeeAmt) = calculateFeeAmtOrigin(tokenContract, _amt);
_transfer(payable(address(liquidityContract)), tokenContract, _amt);
if (poolFeeAmt > 0) {
_transfer(payable(feeCollector), tokenContract, poolFeeAmt);
_transfer(payable(origin), tokenContract, originFeeAmt);
}
setUint(setId[i], _amt);
emitFlashPayback(tokens[i], _amt, poolFeeAmt, getId[i], setId[i]);
emitOriginFeeCollected(origin, tokens[i], _amt, originFeeAmt);
}
liquidityContract.returnLiquidity(tokens);
}
}
contract ConnectInstaPool is LiquidityAccess {
string public name = "InstaPool-v2";
contract ConnectInstaPool is LiquidityAccessMulti {
string public name = "InstaPool-v2.1";
}

View File

@ -0,0 +1,41 @@
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
interface IndexInterface {
function master() external view returns (address);
}
contract Helpers {
event LogChangeFee(uint256 _fee);
event LogChangeFeeCollector(address _feeCollector);
address public constant instaIndex = 0x2971AdFa57b20E5a416aE5a708A8655A9c74f723;
uint256 public fee;
address public feeCollector;
modifier isChief {
require(IndexInterface(instaIndex).master() == msg.sender, "not-Master");
_;
}
function changeFee(uint256 _fee) external isChief {
require(_fee <= 2 * 10 ** 15, "Fee is more than 0.2%");
fee = uint64(_fee);
emit LogChangeFee(_fee);
}
function changeFeeCollector(address _feeCollector) external isChief {
require(feeCollector != _feeCollector, "Same-feeCollector");
require(_feeCollector != address(0), "feeCollector-is-address(0)");
feeCollector = _feeCollector;
emit LogChangeFeeCollector(_feeCollector);
}
}
contract InstaPoolFee is Helpers {
constructor () public {
fee = 9 * 10 ** 14; // 0.09%
feeCollector = IndexInterface(instaIndex).master();
}
}