Flusher changes and added flusherLogic

This commit is contained in:
Thrilok Kumar 2020-09-07 02:41:23 +05:30
parent d40b4898e4
commit d0da53a445
3 changed files with 71 additions and 158 deletions

View File

@ -1,46 +1,44 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.8;
interface IProxy {
function setBasic(address, address) external;
}
contract Deployer {
event LogNewProxy(address indexed owner, address indexed logic, address indexed token);
mapping (address => bool) public isFlusher;
/**
* @dev deploy create2 + minimal proxy
* @param owner owner address used for salt
* @param logic flusher contract address
* @param token token address
*/
function deployLogic(address owner, address logic, address token) public returns (address proxy) {
event LogNewProxy(address indexed owner, address indexed logic);
// deploy create2 + minimal proxy
function deployLogic(address owner, address logic) public returns (address proxy) {
require(!(isFlusherDeployed(getAddress(owner, logic))), "Flusher-already-deployed");
bytes32 salt = keccak256(abi.encodePacked(owner));
bytes20 targetBytes = bytes20(logic);
// solium-disable-next-line security/no-inline-assembly
assembly {
let clone := mload(0x40)
mstore(
clone,
0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000
)
mstore(add(clone, 0x14), targetBytes)
mstore(
add(clone, 0x28),
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
)
proxy := create2(0, clone, 0x37, salt)
let clone := mload(0x40)
mstore(
clone,
0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000
)
mstore(add(clone, 0x14), targetBytes)
mstore(
add(clone, 0x28),
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
)
proxy := create2(0, clone, 0x37, salt)
}
IProxy(proxy).setBasic(owner, token);
emit LogNewProxy(owner, logic, token);
isFlusher[proxy] = true;
emit LogNewProxy(owner, logic);
}
/**
* @dev compute create2 + minimal proxy address
* @param owner owner address used for salt
* @param logic flusher contract address
*/
function isFlusherDeployed(address _address) public view returns (bool) {
uint32 size;
assembly {
size := extcodesize(_address)
}
return (size > 0);
}
// compute create2 + minimal proxy address
function getAddress(address owner, address logic) public view returns (address) {
bytes32 codeHash = keccak256(getCreationCode(logic));
bytes32 salt = keccak256(abi.encodePacked(owner));
@ -50,14 +48,15 @@ contract Deployer {
address(this),
salt,
codeHash
));
return address(bytes20(rawAddress << 96));
)
);
return address(bytes20(rawAddress << 96));
}
function getCreationCode(address logic) public pure returns (bytes memory) {
bytes20 a = bytes20(0x3D602d80600A3D3981F3363d3d373d3D3D363d73);
bytes20 b = bytes20(logic);
bytes15 c = bytes15(0x5af43d82803e903d91602b57fd5bf3);
return abi.encodePacked(a, b, c);
}
}
}

View File

@ -1,143 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.8;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
interface YieldPool {
function balanceOf(address) external view returns (uint);
function deposit(uint) external payable returns (uint);
function withdraw(uint, address) external returns (uint);
}
interface RegistryInterface {
function signer(address) external view returns (bool);
function chief(address) external view returns (bool);
function poolToken(address) external view returns (address);
function isConnector(address[] calldata) external view returns (bool);
}
contract Flusher {
using SafeERC20 for IERC20;
event LogCast(address indexed sender, uint value);
address payable public owner;
string constant public name = "Flusher-v1";
RegistryInterface public constant registry = RegistryInterface(address(0)); // TODO - Change while deploying.
bool public shield;
uint256 public shieldBlockTime;
uint256 internal waitBlockTime = 172800; // 30 days blocktime.
modifier isSigner {
require(registry.signer(msg.sender), "not-signer");
_;
}
modifier isChief {
require(registry.chief(msg.sender), "not-chief");
_;
}
event LogInit(address indexed owner);
event LogSwitch(bool indexed shieldState);
event LogDeposit(address indexed token, address indexed tokenPool, uint amount);
event LogWithdraw(address indexed token, address indexed tokenPool,uint amount);
event LogWithdrawToOwner(address indexed token, address indexed owner, uint amount);
function _deposit(address token) internal {
require(address(token) != address(0), "invalid-token");
address poolToken = registry.poolToken(token);
require(poolToken != address(0), "invalid-pool");
IERC20 tokenContract = IERC20(token);
YieldPool poolContract = YieldPool(poolToken);
uint amt;
if (address(tokenContract) == address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)) {
amt = address(this).balance;
poolContract.deposit{value: amt}(amt);
} else {
amt = tokenContract.balanceOf(address(this));
if (tokenContract.allowance(address(this), address(poolContract)) == 0)
tokenContract.approve(address(poolContract), uint(-1));
poolContract.deposit(amt);
}
emit LogDeposit(token, address(poolContract), amt);
}
function deposit(address token) external payable isSigner {
_deposit(token);
}
function withdraw(address token, uint amount) external isSigner returns (uint _amount) {
require(address(token) != address(0), "invalid-token");
address poolToken = registry.poolToken(token);
require(poolToken != address(0), "invalid-pool");
_amount = YieldPool(poolToken).withdraw(amount, owner);
emit LogWithdraw(token, poolToken, _amount);
}
/**
* @dev withdraw to owner (rare case)
*/
function claim(address token) external isSigner returns (uint) {
require(address(token) != address(0), "invalid-token");
uint amount;
if (address(token) == address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)) {
amount = address(this).balance;
payable(owner).transfer(amount);
} else {
IERC20 tokenContract = IERC20(token);
amount = tokenContract.balanceOf(address(this));
tokenContract.safeTransfer(address(owner), amount);
}
emit LogWithdrawToOwner(token, owner, amount);
}
function setBasic(address newOwner, address token) external {
require(owner == address(0), "already-an-owner");
require(newOwner != address(0), "not-vaild-owner-address");
require(token != address(0), "not-vaild-token-address");
owner = payable(newOwner);
_deposit(token);
emit LogInit(newOwner);
}
function switchShield() external isChief {
require(registry.chief(msg.sender), "not-chief");
shield = !shield;
if (!shield) {
shieldBlockTime = block.number + waitBlockTime;
} else {
delete shieldBlockTime;
}
emit LogSwitch(shield);
}
/**
* @dev backdoor function
*/
function spell(address _target, bytes calldata _data) external isChief {
require(!shield, "shield-access-denied");
require(shieldBlockTime != 0 && shieldBlockTime <= block.number, "more-than-30-days");
function spell(address _target, bytes memory _data) internal {
require(_target != address(0), "target-invalid");
require(_data.length > 0, "data-invalid");
bytes memory _callData = _data;
address _owner = owner;
assembly {
let succeeded := delegatecall(gas(), _target, add(_callData, 0x20), mload(_callData), 0, 0)
let succeeded := delegatecall(gas(), _target, add(_data, 0x20), mload(_data), 0, 0)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
let size := returndatasize()
returndatacopy(0x00, 0x00, size)
revert(0x00, size)
}
case 1 {
let size := returndatasize()
returndatacopy(0x00, 0x00, size)
revert(0x00, size)
}
}
require(_owner == owner, "owner-change-denied");
}
function cast(address[] calldata _targets, bytes[] calldata _datas) external payable {
require(registry.signer(msg.sender), "not-signer");
require(_targets.length == _datas.length , "invalid-array-length");
require(registry.isConnector(_targets), "not-connector");
for (uint i = 0; i < _targets.length; i++) {
spell(_targets[i], _datas[i]);
}
emit LogCast(msg.sender, msg.value);
}
receive() external payable {}
}
}

View File

@ -15,6 +15,7 @@ contract Registry {
event LogAddSigner(address indexed signer);
event LogRemoveSigner(address indexed signer);
event LogUpdatePoolLogic(address token, address newLogic);
event LogUpdateFlusherLogic(address token, address newLogic);
event LogUpdateFee(address token, uint newFee);
event LogUpdateCap(address token, uint newFee);
event LogAddPool(address indexed token, address indexed pool);
@ -28,6 +29,7 @@ contract Registry {
mapping (address => bool) public signer;
mapping (address => address) public poolToken;
mapping (address => address) public poolLogic;
mapping (address => address) public flusherLogic;
mapping (address => uint) public poolCap;
mapping (address => uint) public fee;
mapping (address => mapping(address => bool)) public settleLogic;
@ -124,6 +126,20 @@ contract Registry {
emit LogUpdatePoolLogic(_pool, _newLogic);
}
/**
* @dev update flusher logic
* @param _token pool address
* @param _newLogic new flusher logic address
*/
function updateFlusherLogic(address _token, address _newLogic) external isMaster {
address _pool = poolToken[_token];
require(_pool != address(0), "invalid-pool");
require(_newLogic != address(0), "invalid-address");
require(flusherLogic[_pool] != _newLogic, "same-pool-logic");
flusherLogic[_pool] = _newLogic;
emit LogUpdateFlusherLogic(_pool, _newLogic);
}
/**
* @dev update pool fee
* @param _token pool address