event added and eth edits

This commit is contained in:
Samyak Jain 2020-08-23 22:42:32 +10:00
parent 29dbb98bea
commit a115927405
3 changed files with 67 additions and 26 deletions

View File

@ -35,25 +35,32 @@ interface RateInterface {
contract PoolToken is ERC20, DSMath { contract PoolToken is ERC20, DSMath {
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
IERC20 public immutable baseToken; event LogDeploy(uint amount);
RegistryInterface public immutable registry; 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); 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 private tokenBalance; // total token balance since last rebalancing
uint public exchangeRate = 1000000000000000000; // initial 1 token = 1 uint public exchangeRate = 1000000000000000000; // initial 1 token = 1
uint public insuranceAmt; // insurance amount to keep pool safe uint public insuranceAmt; // insurance amount to keep pool safe
bool public shutPool; bool public shutPool; // shutdown deposits and withdrawals
constructor( constructor(
address _registry, address _registry,
string memory _name, string memory _name,
string memory _symbol, string memory _symbol,
address _baseToken,
address _origin address _origin
) public ERC20(_name, _symbol) { ) public ERC20(_name, _symbol) {
// TODO - 0 // TODO - 0
baseToken = IERC20(_baseToken); // baseToken = IERC20(_baseToken);
registry = RegistryInterface(_registry); registry = RegistryInterface(_registry);
address _dsa = instaIndex.build(address(this), 1, _origin); address _dsa = instaIndex.build(address(this), 1, _origin);
dsa = AccountInterface(_dsa); dsa = AccountInterface(_dsa);
@ -65,7 +72,8 @@ contract PoolToken is ERC20, DSMath {
} }
function deploy(uint amount) external isChief { function deploy(uint amount) external isChief {
baseToken.safeTransfer(address(dsa), amount); payable(address(dsa)).transfer(amount);
emit LogDeploy(amount);
} }
function setExchangeRate() public isChief { function setExchangeRate() public isChief {
@ -86,6 +94,7 @@ contract PoolToken is ERC20, DSMath {
tokenBalance = sub(_totalToken, insuranceAmt); tokenBalance = sub(_totalToken, insuranceAmt);
} }
exchangeRate = _currentRate; exchangeRate = _currentRate;
emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt);
} }
function settle(address[] calldata _targets, bytes[] calldata _datas, address _origin) external isChief { 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); dsa.cast(_targets, _datas, _origin);
} }
setExchangeRate(); setExchangeRate();
emit LogSettle(block.timestamp);
} }
function deposit(uint tknAmt) public payable returns(uint) { function deposit(uint tknAmt) public payable returns(uint) {
require(!shutPool, "pool-shut"); 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"); require(_newTokenBal <= registry.poolCap(address(this)), "deposit-cap-reached");
baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); uint _mintAmt = wdiv(msg.value, exchangeRate);
uint _mintAmt = wdiv(tknAmt, exchangeRate);
_mint(msg.sender, _mintAmt); _mint(msg.sender, _mintAmt);
emit LogDeposit(tknAmt, _mintAmt);
} }
function withdraw(uint tknAmt, address to) external returns (uint) { function withdraw(uint tknAmt, address to) external returns (uint) {
require(!shutPool, "pool-shut"); require(!shutPool, "pool-shut");
uint poolBal = baseToken.balanceOf(address(this)); uint poolBal = address(this).balance;
require(tknAmt <= poolBal, "not-enough-liquidity-available"); require(tknAmt <= poolBal, "not-enough-liquidity-available");
uint _bal = balanceOf(msg.sender); uint _bal = balanceOf(msg.sender);
uint _tknBal = wmul(_bal, exchangeRate); uint _tknBal = wmul(_bal, exchangeRate);
@ -124,17 +137,21 @@ contract PoolToken is ERC20, DSMath {
_burn(msg.sender, _burnAmt); _burn(msg.sender, _burnAmt);
baseToken.safeTransfer(to, _tknAmt); payable(to).transfer(_tknAmt);
emit LogWithdraw(tknAmt, _burnAmt);
} }
function addInsurance(uint tknAmt) external { function addInsurance(uint tknAmt) external payable {
baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); require(tknAmt == msg.value, "unmatched-amount");
insuranceAmt = tknAmt; insuranceAmt += tknAmt;
emit LogAddInsurance(tknAmt);
} }
function shutdown() external { function shutdown() external {
require(msg.sender == instaIndex.master(), "not-master"); require(msg.sender == instaIndex.master(), "not-master");
shutPool = !shutPool; shutPool = !shutPool;
emit LogPoolShut(shutPool);
} }
receive() external payable { receive() external payable {

View File

@ -17,6 +17,10 @@ contract Registry {
event LogAddChief(address indexed chief); event LogAddChief(address indexed chief);
event LogRemoveChief(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); IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
@ -58,25 +62,29 @@ contract Registry {
emit LogRemoveChief(_chief); emit LogRemoveChief(_chief);
} }
function enablePool(address _pool) external isMaster { function switchPool(address _pool) external isMaster {
isPool[_pool] = true; isPool[_pool] = !isPool[_pool];
emit LogSwitchPool(_pool, isPool[_pool]);
} }
function updatePoolCap(address _pool, uint _newCap) external isChief { function updatePoolCap(address _pool, uint _newCap) external isChief {
require(isPool[_pool], "not-a-pool"); require(isPool[_pool], "not-a-pool");
poolCap[_pool] = _newCap; 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(isPool[_pool], "not-a-pool");
require(_newLogic != address(0), "address-0"); require(_newLogic != address(0), "address-0");
poolLogic[_pool] = _newLogic; poolLogic[_pool] = _newLogic;
emit LogUpdatePoolLogic(_pool, _newLogic);
} }
function updateInsureFee(address _pool, uint _newFee) external isChief { function updateInsureFee(address _pool, uint _newFee) external isChief {
require(isPool[_pool], "not-a-pool"); require(isPool[_pool], "not-a-pool");
require(_newFee < 1000000000000000000, "insure-fee-limit-reached"); require(_newFee < 1000000000000000000, "insure-fee-limit-reached");
insureFee[_pool] = _newFee; insureFee[_pool] = _newFee;
emit LogUpdateInsureFee(_pool, _newFee);
} }
constructor(address _chief) public { constructor(address _chief) public {

View File

@ -35,15 +35,23 @@ interface RateInterface {
contract PoolToken is ERC20, DSMath { contract PoolToken is ERC20, DSMath {
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
IERC20 public immutable baseToken; event LogDeploy(uint amount);
RegistryInterface public immutable registry; event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt);
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); event LogSettle(uint settleTime);
AccountInterface public immutable dsa; 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 private tokenBalance; // total token balance since last rebalancing
uint public exchangeRate = 1000000000000000000; // initial 1 token = 1 uint public exchangeRate = 1000000000000000000; // initial 1 token = 1
uint public insuranceAmt; // insurance amount to keep pool safe uint public insuranceAmt; // insurance amount to keep pool safe
bool public shutPool; bool public shutPool; // shutdown deposits and withdrawals
constructor( constructor(
address _registry, address _registry,
@ -52,7 +60,6 @@ contract PoolToken is ERC20, DSMath {
address _baseToken, address _baseToken,
address _origin address _origin
) public ERC20(_name, _symbol) { ) public ERC20(_name, _symbol) {
// TODO - 0
baseToken = IERC20(_baseToken); baseToken = IERC20(_baseToken);
registry = RegistryInterface(_registry); registry = RegistryInterface(_registry);
address _dsa = instaIndex.build(address(this), 1, _origin); address _dsa = instaIndex.build(address(this), 1, _origin);
@ -66,6 +73,7 @@ contract PoolToken is ERC20, DSMath {
function deploy(uint amount) public isChief { function deploy(uint amount) public isChief {
baseToken.safeTransfer(address(dsa), amount); baseToken.safeTransfer(address(dsa), amount);
emit LogDeploy(amount);
} }
function setExchangeRate() public isChief { function setExchangeRate() public isChief {
@ -86,6 +94,7 @@ contract PoolToken is ERC20, DSMath {
tokenBalance = sub(_totalToken, insuranceAmt); tokenBalance = sub(_totalToken, insuranceAmt);
} }
exchangeRate = _currentRate; exchangeRate = _currentRate;
emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt);
} }
function settle(address[] calldata _targets, bytes[] calldata _datas, address _origin) external isChief { 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); dsa.cast(_targets, _datas, _origin);
} }
setExchangeRate(); setExchangeRate();
emit LogSettle(block.timestamp);
} }
function deposit(uint tknAmt) external payable returns(uint) { function deposit(uint tknAmt) external payable returns(uint) {
@ -103,6 +113,8 @@ contract PoolToken is ERC20, DSMath {
baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); baseToken.safeTransferFrom(msg.sender, address(this), tknAmt);
uint _mintAmt = wdiv(tknAmt, exchangeRate); uint _mintAmt = wdiv(tknAmt, exchangeRate);
_mint(msg.sender, _mintAmt); _mint(msg.sender, _mintAmt);
emit LogDeposit(tknAmt, _mintAmt);
} }
function withdraw(uint tknAmt, address to) external returns (uint) { function withdraw(uint tknAmt, address to) external returns (uint) {
@ -125,16 +137,20 @@ contract PoolToken is ERC20, DSMath {
_burn(msg.sender, _burnAmt); _burn(msg.sender, _burnAmt);
baseToken.safeTransfer(to, _tknAmt); 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); baseToken.safeTransferFrom(msg.sender, address(this), tknAmt);
insuranceAmt = tknAmt; insuranceAmt += tknAmt;
emit LogAddInsurance(tknAmt);
} }
function shutdown() external { function shutdown() external {
require(msg.sender == instaIndex.master(), "not-master"); require(msg.sender == instaIndex.master(), "not-master");
shutPool = !shutPool; shutPool = !shutPool;
emit LogPoolShut(shutPool);
} }
} }