From a115927405dd50e303da8bdc6b75ecab5912269e Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Sun, 23 Aug 2020 22:42:32 +1000 Subject: [PATCH] event added and eth edits --- contracts/Samyak/ethPool.sol | 47 +++++++++++++++++++++++----------- contracts/Samyak/registry.sol | 14 +++++++--- contracts/Samyak/tokenPool.sol | 32 +++++++++++++++++------ 3 files changed, 67 insertions(+), 26 deletions(-) diff --git a/contracts/Samyak/ethPool.sol b/contracts/Samyak/ethPool.sol index 8253311..958b703 100644 --- a/contracts/Samyak/ethPool.sol +++ b/contracts/Samyak/ethPool.sol @@ -35,25 +35,32 @@ interface RateInterface { contract PoolToken is ERC20, DSMath { using SafeERC20 for IERC20; - IERC20 public immutable baseToken; - RegistryInterface public immutable registry; + event LogDeploy(uint amount); + event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt); + event LogSettle(uint settleTime); + event LogDeposit(uint depositAmt, uint poolMintAmt); + event LogWithdraw(uint withdrawAmt, uint poolBurnAmt); + event LogAddInsurance(uint amount); + event LogPoolShut(bool); + + // IERC20 public immutable baseToken; + RegistryInterface public immutable registry; // Pool Registry IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); - AccountInterface public immutable dsa; + AccountInterface public immutable dsa; // Pool's DSA account uint private tokenBalance; // total token balance since last rebalancing uint public exchangeRate = 1000000000000000000; // initial 1 token = 1 uint public insuranceAmt; // insurance amount to keep pool safe - bool public shutPool; + bool public shutPool; // shutdown deposits and withdrawals constructor( address _registry, string memory _name, string memory _symbol, - address _baseToken, address _origin ) public ERC20(_name, _symbol) { // TODO - 0 - baseToken = IERC20(_baseToken); + // baseToken = IERC20(_baseToken); registry = RegistryInterface(_registry); address _dsa = instaIndex.build(address(this), 1, _origin); dsa = AccountInterface(_dsa); @@ -65,7 +72,8 @@ contract PoolToken is ERC20, DSMath { } function deploy(uint amount) external isChief { - baseToken.safeTransfer(address(dsa), amount); + payable(address(dsa)).transfer(amount); + emit LogDeploy(amount); } function setExchangeRate() public isChief { @@ -86,6 +94,7 @@ contract PoolToken is ERC20, DSMath { tokenBalance = sub(_totalToken, insuranceAmt); } exchangeRate = _currentRate; + emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt); } function settle(address[] calldata _targets, bytes[] calldata _datas, address _origin) external isChief { @@ -93,21 +102,25 @@ contract PoolToken is ERC20, DSMath { dsa.cast(_targets, _datas, _origin); } setExchangeRate(); + + emit LogSettle(block.timestamp); } function deposit(uint tknAmt) public payable returns(uint) { require(!shutPool, "pool-shut"); - uint _newTokenBal = add(tokenBalance, tknAmt); + require(tknAmt == msg.value, "unmatched-amount"); + uint _newTokenBal = add(tokenBalance, msg.value); require(_newTokenBal <= registry.poolCap(address(this)), "deposit-cap-reached"); - baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); - uint _mintAmt = wdiv(tknAmt, exchangeRate); + uint _mintAmt = wdiv(msg.value, exchangeRate); _mint(msg.sender, _mintAmt); + + emit LogDeposit(tknAmt, _mintAmt); } function withdraw(uint tknAmt, address to) external returns (uint) { require(!shutPool, "pool-shut"); - uint poolBal = baseToken.balanceOf(address(this)); + uint poolBal = address(this).balance; require(tknAmt <= poolBal, "not-enough-liquidity-available"); uint _bal = balanceOf(msg.sender); uint _tknBal = wmul(_bal, exchangeRate); @@ -124,17 +137,21 @@ contract PoolToken is ERC20, DSMath { _burn(msg.sender, _burnAmt); - baseToken.safeTransfer(to, _tknAmt); + payable(to).transfer(_tknAmt); + + emit LogWithdraw(tknAmt, _burnAmt); } - function addInsurance(uint tknAmt) external { - baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); - insuranceAmt = tknAmt; + function addInsurance(uint tknAmt) external payable { + require(tknAmt == msg.value, "unmatched-amount"); + insuranceAmt += tknAmt; + emit LogAddInsurance(tknAmt); } function shutdown() external { require(msg.sender == instaIndex.master(), "not-master"); shutPool = !shutPool; + emit LogPoolShut(shutPool); } receive() external payable { diff --git a/contracts/Samyak/registry.sol b/contracts/Samyak/registry.sol index 7008f66..dd10eb4 100644 --- a/contracts/Samyak/registry.sol +++ b/contracts/Samyak/registry.sol @@ -17,6 +17,10 @@ contract Registry { event LogAddChief(address indexed chief); event LogRemoveChief(address indexed chief); + event LogSwitchPool(address pool, bool); + event LogUpdatePoolCap(address pool, uint newCap); + event LogUpdatePoolLogic(address pool, address newLogic); + event LogUpdateInsureFee(address pool, uint newFee); IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); @@ -58,25 +62,29 @@ contract Registry { emit LogRemoveChief(_chief); } - function enablePool(address _pool) external isMaster { - isPool[_pool] = true; + function switchPool(address _pool) external isMaster { + isPool[_pool] = !isPool[_pool]; + emit LogSwitchPool(_pool, isPool[_pool]); } function updatePoolCap(address _pool, uint _newCap) external isChief { require(isPool[_pool], "not-a-pool"); poolCap[_pool] = _newCap; + emit LogUpdatePoolCap(_pool, _newCap); } - function updateLogic(address _pool, address _newLogic) external isChief { + function updatePoolLogic(address _pool, address _newLogic) external isChief { require(isPool[_pool], "not-a-pool"); require(_newLogic != address(0), "address-0"); poolLogic[_pool] = _newLogic; + emit LogUpdatePoolLogic(_pool, _newLogic); } function updateInsureFee(address _pool, uint _newFee) external isChief { require(isPool[_pool], "not-a-pool"); require(_newFee < 1000000000000000000, "insure-fee-limit-reached"); insureFee[_pool] = _newFee; + emit LogUpdateInsureFee(_pool, _newFee); } constructor(address _chief) public { diff --git a/contracts/Samyak/tokenPool.sol b/contracts/Samyak/tokenPool.sol index def0656..a0d7d33 100644 --- a/contracts/Samyak/tokenPool.sol +++ b/contracts/Samyak/tokenPool.sol @@ -35,15 +35,23 @@ interface RateInterface { contract PoolToken is ERC20, DSMath { using SafeERC20 for IERC20; - IERC20 public immutable baseToken; - RegistryInterface public immutable registry; - IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); - AccountInterface public immutable dsa; + event LogDeploy(uint amount); + event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt); + event LogSettle(uint settleTime); + event LogDeposit(uint depositAmt, uint poolMintAmt); + event LogWithdraw(uint withdrawAmt, uint poolBurnAmt); + event LogAddInsurance(uint amount); + event LogPoolShut(bool); + + IERC20 public immutable baseToken; // Base token. Eg:- DAI, USDC, etc. + RegistryInterface public immutable registry; // Pool Registry + IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); // Main Index + AccountInterface public immutable dsa; // Pool's DSA account uint private tokenBalance; // total token balance since last rebalancing uint public exchangeRate = 1000000000000000000; // initial 1 token = 1 uint public insuranceAmt; // insurance amount to keep pool safe - bool public shutPool; + bool public shutPool; // shutdown deposits and withdrawals constructor( address _registry, @@ -52,7 +60,6 @@ contract PoolToken is ERC20, DSMath { address _baseToken, address _origin ) public ERC20(_name, _symbol) { - // TODO - 0 baseToken = IERC20(_baseToken); registry = RegistryInterface(_registry); address _dsa = instaIndex.build(address(this), 1, _origin); @@ -66,6 +73,7 @@ contract PoolToken is ERC20, DSMath { function deploy(uint amount) public isChief { baseToken.safeTransfer(address(dsa), amount); + emit LogDeploy(amount); } function setExchangeRate() public isChief { @@ -86,6 +94,7 @@ contract PoolToken is ERC20, DSMath { tokenBalance = sub(_totalToken, insuranceAmt); } exchangeRate = _currentRate; + emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt); } function settle(address[] calldata _targets, bytes[] calldata _datas, address _origin) external isChief { @@ -93,6 +102,7 @@ contract PoolToken is ERC20, DSMath { dsa.cast(_targets, _datas, _origin); } setExchangeRate(); + emit LogSettle(block.timestamp); } function deposit(uint tknAmt) external payable returns(uint) { @@ -103,6 +113,8 @@ contract PoolToken is ERC20, DSMath { baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); uint _mintAmt = wdiv(tknAmt, exchangeRate); _mint(msg.sender, _mintAmt); + + emit LogDeposit(tknAmt, _mintAmt); } function withdraw(uint tknAmt, address to) external returns (uint) { @@ -125,16 +137,20 @@ contract PoolToken is ERC20, DSMath { _burn(msg.sender, _burnAmt); baseToken.safeTransfer(to, _tknAmt); + + emit LogWithdraw(tknAmt, _burnAmt); } - function addInsurance(uint tknAmt) external { + function addInsurance(uint tknAmt) external payable { baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); - insuranceAmt = tknAmt; + insuranceAmt += tknAmt; + emit LogAddInsurance(tknAmt); } function shutdown() external { require(msg.sender == instaIndex.master(), "not-master"); shutPool = !shutPool; + emit LogPoolShut(shutPool); } }