From d21f26bc83be18d39b18da12373619add8e9d32f Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Sat, 18 Nov 2023 11:51:14 +0800 Subject: [PATCH] Add totalRewards and event on tapETH --- contracts/TapETH.sol | 81 +++++++++++++--------------- contracts/interfaces/ITapETH.sol | 6 +-- docs/contracts/interfaces/ITapETH.md | 68 +++++++++++------------ test/TapETH.ts | 23 ++++---- 4 files changed, 86 insertions(+), 92 deletions(-) diff --git a/contracts/TapETH.sol b/contracts/TapETH.sol index 1b9e63c..8c8423d 100644 --- a/contracts/TapETH.sol +++ b/contracts/TapETH.sol @@ -17,8 +17,8 @@ error InsufficientBalance(uint256 currentBalance, uint256 amount); * contract also stores the sum of all shares to calculate each account's token balance * which equals to: * - * shares[account] * _getTotalPooledEther() / _getTotalShares() - * where the _getTotalPooledEther() returns the total supply of tapETH controlled by the protocol. + * shares[account] * _totalSupply / _totalShares + * where the _totalSupply is the total supply of tapETH controlled by the protocol. */ contract TapETH is Initializable, ITapETH { @@ -26,8 +26,9 @@ contract TapETH is Initializable, ITapETH { uint256 internal constant INFINITE_ALLOWANCE = ~uint256(0); uint256 public constant BUFFER_DENOMINATOR = 10 ** 10; - uint256 private totalShares; + uint256 private _totalShares; uint256 private _totalSupply; + uint256 private _totalRewards; address public governance; address public pendingGovernance; mapping(address => uint256) private shares; @@ -53,6 +54,11 @@ contract TapETH is Initializable, ITapETH { uint256 sharesAmount ); + event RewardsMinted( + uint256 amount, + uint256 actualAmount + ); + event GovernanceModified(address indexed governance); event GovernanceProposed(address indexed governance); event PoolAdded(address indexed pool); @@ -117,21 +123,10 @@ contract TapETH is Initializable, ITapETH { /** * @return the amount of tokens in existence. * - * @dev Always equals to `_getTotalPooledEther()`: the total amount of - * tapETH controlled by the protocol. + * @dev The total amount of tapETH controlled by the protocol. */ function totalSupply() external view returns (uint256) { - return _getTotalPooledEther(); - } - - /** - * @return the entire amount of tapETH controlled by the protocol. - * - * @dev The sum of all tapETH balances in the protocol, equals to the total supply of tapETH. - */ - - function getTotalPooledEther() external view returns (uint256) { - return _getTotalPooledEther(); + return _totalSupply; } /** @@ -257,8 +252,17 @@ contract TapETH is Initializable, ITapETH { * @dev The sum of all accounts' shares can be an arbitrary number, therefore * it is necessary to store it in order to calculate each account's relative share. */ - function getTotalShares() external view returns (uint256) { - return totalShares; + function totalShares() external view returns (uint256) { + return _totalShares; + } + + /** + * @return the total amount of rewards in existence. + * + * @dev The total rewards of tapETH by the protocol. + */ + function totalRewards() external view returns (uint256) { + return _totalRewards; } /** @@ -279,7 +283,12 @@ contract TapETH is Initializable, ITapETH { require(pools[msg.sender], "TapETH: no pool"); require(_amount != 0, "TapETH: no pool"); uint256 _deltaBuffer = (buffer * _amount) / BUFFER_DENOMINATOR; - _totalSupply += _amount - _deltaBuffer; + uint256 actualAmount = _amount - _deltaBuffer; + + _totalSupply += actualAmount; + _totalRewards += actualAmount; + + emit RewardsMinted(_amount, actualAmount); } /** @@ -295,11 +304,10 @@ contract TapETH is Initializable, ITapETH { function getSharesByPooledEth( uint256 _tapETHAmount ) public view returns (uint256) { - uint256 _totalPooledEther = _getTotalPooledEther(); - if (_totalPooledEther == 0) { + if (_totalSupply == 0) { return 0; } else { - return (_tapETHAmount * _getTotalShares()) / _totalPooledEther; + return (_tapETHAmount * _totalShares) / _totalSupply; } } @@ -309,10 +317,10 @@ contract TapETH is Initializable, ITapETH { function getPooledEthByShares( uint256 _sharesAmount ) public view returns (uint256) { - if (totalShares == 0) { + if (_totalShares == 0) { return 0; } else { - return (_sharesAmount * _totalSupply) / (totalShares); + return (_sharesAmount * _totalSupply) / _totalShares; } } @@ -370,14 +378,6 @@ contract TapETH is Initializable, ITapETH { _burnShares(_account, _tokenAmount); } - /** - * @return the total amount (in wei) of tapETH controlled by the protocol. - * @dev This is used for calculating tokens from shares and vice versa. - */ - function _getTotalPooledEther() internal view returns (uint256) { - return _totalSupply; - } - /** * @notice Moves `_amount` tokens from `_sender` to `_recipient`. * Emits a `Transfer` event. @@ -433,13 +433,6 @@ contract TapETH is Initializable, ITapETH { } } - /** - * @return the total amount of shares in existence. - */ - function _getTotalShares() internal view returns (uint256) { - return totalShares; - } - /** * @return the amount of shares owned by `_account`. */ @@ -478,14 +471,14 @@ contract TapETH is Initializable, ITapETH { ) internal returns (uint256 newTotalShares) { require(_recipient != address(0), "TapETH: MINT_TO_ZERO_ADDR"); uint256 _sharesAmount; - if (_totalSupply != 0 && totalShares != 0) { + if (_totalSupply != 0 && _totalShares != 0) { _sharesAmount = getSharesByPooledEth(_tokenAmount); } else { _sharesAmount = _tokenAmount; } shares[_recipient] += _sharesAmount; - totalShares += _sharesAmount; - newTotalShares = totalShares; + _totalShares += _sharesAmount; + newTotalShares = _totalShares; _totalSupply += _tokenAmount; emit SharesMinted(_recipient, _tokenAmount, _sharesAmount); @@ -507,8 +500,8 @@ contract TapETH is Initializable, ITapETH { uint256 _sharesAmount = getSharesByPooledEth(_tokenAmount); shares[_account] -= _sharesAmount; - totalShares -= _sharesAmount; - newTotalShares = totalShares; + _totalShares -= _sharesAmount; + newTotalShares = _totalShares; _totalSupply -= _tokenAmount; emit SharesBurnt(_account, _tokenAmount, _sharesAmount); diff --git a/contracts/interfaces/ITapETH.sol b/contracts/interfaces/ITapETH.sol index e723ec1..ceae1c4 100644 --- a/contracts/interfaces/ITapETH.sol +++ b/contracts/interfaces/ITapETH.sol @@ -11,8 +11,6 @@ interface ITapETH is IERC20 { function removePool(address _pool) external; - function getTotalPooledEther() external view returns (uint256); - function increaseAllowance( address _spender, uint256 _addedValue @@ -23,7 +21,9 @@ interface ITapETH is IERC20 { uint256 _subtractedValue ) external returns (bool); - function getTotalShares() external view returns (uint256); + function totalShares() external view returns (uint256); + + function totalRewards() external view returns (uint256); function sharesOf(address _account) external view returns (uint256); diff --git a/docs/contracts/interfaces/ITapETH.md b/docs/contracts/interfaces/ITapETH.md index 11a02d9..b74f5f6 100644 --- a/docs/contracts/interfaces/ITapETH.md +++ b/docs/contracts/interfaces/ITapETH.md @@ -199,40 +199,6 @@ function getSharesByPooledEth(uint256 _ethAmount) external view returns (uint256 |---|---|---| | _ethAmount | uint256 | undefined | -#### Returns - -| Name | Type | Description | -|---|---|---| -| _0 | uint256 | undefined | - -### getTotalPooledEther - -```solidity -function getTotalPooledEther() external view returns (uint256) -``` - - - - - - -#### Returns - -| Name | Type | Description | -|---|---|---| -| _0 | uint256 | undefined | - -### getTotalShares - -```solidity -function getTotalShares() external view returns (uint256) -``` - - - - - - #### Returns | Name | Type | Description | @@ -343,6 +309,40 @@ function sharesOf(address _account) external view returns (uint256) |---|---|---| | _account | address | undefined | +#### Returns + +| Name | Type | Description | +|---|---|---| +| _0 | uint256 | undefined | + +### totalRewards + +```solidity +function totalRewards() external view returns (uint256) +``` + + + + + + +#### Returns + +| Name | Type | Description | +|---|---|---| +| _0 | uint256 | undefined | + +### totalShares + +```solidity +function totalShares() external view returns (uint256) +``` + + + + + + #### Returns | Name | Type | Description | diff --git a/test/TapETH.ts b/test/TapETH.ts index 40fa8e8..1f2b1bf 100644 --- a/test/TapETH.ts +++ b/test/TapETH.ts @@ -193,7 +193,7 @@ describe("TapETH", function () { let amount = 1_000_000_000_000_000_000_000n; await tapETH.connect(pool1).mintShares(user.address, amount); expect(await tapETH.totalSupply()).to.equal(amount); - expect(await tapETH.getTotalShares()).to.equal(amount); + expect(await tapETH.totalShares()).to.equal(amount); expect(await tapETH.sharesOf(user.address)).to.equal(amount); expect(await tapETH.balanceOf(user.address)).to.equal(amount); }); @@ -213,7 +213,7 @@ describe("TapETH", function () { await tapETH.connect(pool1).mintShares(user2.address, amount2); await tapETH.connect(pool1).mintShares(user3.address, amount3); expect(await tapETH.totalSupply()).to.equal(totalAmount); - expect(await tapETH.getTotalShares()).to.equal(totalAmount); + expect(await tapETH.totalShares()).to.equal(totalAmount); expect(await tapETH.sharesOf(user1.address)).to.equal(amount1); expect(await tapETH.balanceOf(user1.address)).to.equal(amount1); expect(await tapETH.sharesOf(user2.address)).to.equal(amount2); @@ -236,7 +236,7 @@ describe("TapETH", function () { await tapETH.connect(pool1).mintShares(user2.address, amount2); await tapETH.connect(pool1).mintShares(user3.address, amount3); expect(await tapETH.totalSupply()).to.equal(totalAmount); - expect(await tapETH.getTotalShares()).to.equal(totalAmount); + expect(await tapETH.totalShares()).to.equal(totalAmount); expect(await tapETH.sharesOf(user1.address)).to.equal(amount1); expect(await tapETH.balanceOf(user1.address)).to.equal(amount1); expect(await tapETH.sharesOf(user2.address)).to.equal(amount2); @@ -258,7 +258,7 @@ describe("TapETH", function () { await tapETH.connect(pool1).mintShares(user.address, amount1); await tapETH.connect(user).burnShares(amount2); expect(await tapETH.totalSupply()).to.equal(deltaAmount); - expect(await tapETH.getTotalShares()).to.equal(deltaAmount); + expect(await tapETH.totalShares()).to.equal(deltaAmount); expect(await tapETH.sharesOf(user.address)).to.equal(deltaAmount); expect(await tapETH.balanceOf(user.address)).to.equal(deltaAmount); }); @@ -288,7 +288,7 @@ describe("TapETH", function () { await tapETH.connect(user2).burnShares(amountToBurn); await tapETH.connect(user3).burnShares(amountToBurn); expect(await tapETH.totalSupply()).to.equal(deltaAmount); - expect(await tapETH.getTotalShares()).to.equal(deltaAmount); + expect(await tapETH.totalShares()).to.equal(deltaAmount); expect(await tapETH.sharesOf(user1.address)).to.equal( amount1 - amountToBurn ); @@ -324,7 +324,7 @@ describe("TapETH", function () { await tapETH.connect(user).approve(spender.address, amount1); await tapETH.connect(spender).burnSharesFrom(user.address, amount2); expect(await tapETH.totalSupply()).to.equal(deltaAmount); - expect(await tapETH.getTotalShares()).to.equal(deltaAmount); + expect(await tapETH.totalShares()).to.equal(deltaAmount); expect(await tapETH.sharesOf(user.address)).to.equal(deltaAmount); expect(await tapETH.balanceOf(user.address)).to.equal(deltaAmount); expect(await tapETH.allowance(user.address, spender.address)).to.equal( @@ -362,7 +362,7 @@ describe("TapETH", function () { await tapETH.connect(pool1).mintShares(user1.address, amount1); await tapETH.connect(user1).transfer(user2.address, amount2); expect(await tapETH.totalSupply()).to.equal(amount1); - expect(await tapETH.getTotalShares()).to.equal(amount1); + expect(await tapETH.totalShares()).to.equal(amount1); expect(await tapETH.sharesOf(user1.address)).to.equal(deltaAmount); expect(await tapETH.sharesOf(user2.address)).to.equal(amount2); expect(await tapETH.balanceOf(user1.address)).to.equal(deltaAmount); @@ -387,7 +387,7 @@ describe("TapETH", function () { .connect(spender) .transferFrom(user1.address, user2.address, amount2); expect(await tapETH.totalSupply()).to.equal(amount1); - expect(await tapETH.getTotalShares()).to.equal(amount1); + expect(await tapETH.totalShares()).to.equal(amount1); expect(await tapETH.sharesOf(user1.address)).to.equal(deltaAmount); expect(await tapETH.sharesOf(user2.address)).to.equal(amount2); expect(await tapETH.balanceOf(user1.address)).to.equal(deltaAmount); @@ -430,7 +430,7 @@ describe("TapETH", function () { await tapETH.connect(pool1).mintShares(user1.address, amount1); await tapETH.connect(user1).transferShares(user2.address, amount2); expect(await tapETH.totalSupply()).to.equal(amount1); - expect(await tapETH.getTotalShares()).to.equal(amount1); + expect(await tapETH.totalShares()).to.equal(amount1); expect(await tapETH.sharesOf(user1.address)).to.equal(deltaAmount); expect(await tapETH.sharesOf(user2.address)).to.equal(amount2); expect(await tapETH.balanceOf(user1.address)).to.equal(deltaAmount); @@ -455,7 +455,7 @@ describe("TapETH", function () { .connect(spender) .transferSharesFrom(user1.address, user2.address, amount2); expect(await tapETH.totalSupply()).to.equal(amount1); - expect(await tapETH.getTotalShares()).to.equal(amount1); + expect(await tapETH.totalShares()).to.equal(amount1); expect(await tapETH.sharesOf(user1.address)).to.equal(deltaAmount); expect(await tapETH.sharesOf(user2.address)).to.equal(amount2); expect(await tapETH.balanceOf(user1.address)).to.equal(deltaAmount); @@ -498,7 +498,8 @@ describe("TapETH", function () { await tapETH.connect(pool1).mintShares(user.address, amount1); await tapETH.connect(pool1).setTotalSupply(amount2); expect(await tapETH.totalSupply()).to.equal(totalAmount); - expect(await tapETH.getTotalShares()).to.equal(amount1); + expect(await tapETH.totalShares()).to.equal(amount1); + expect(await tapETH.totalRewards()).to.equal(amount2); expect(await tapETH.sharesOf(user.address)).to.equal(amount1); expect(await tapETH.balanceOf(user.address)).to.equal(totalAmount); });