Skip to content

Commit

Permalink
Add totalRewards and event on tapETH
Browse files Browse the repository at this point in the history
  • Loading branch information
zjb0807 committed Nov 18, 2023
1 parent 956d3f8 commit d21f26b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 92 deletions.
81 changes: 37 additions & 44 deletions contracts/TapETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ 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 {
using Math for uint256;
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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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`.
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions contracts/interfaces/ITapETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
68 changes: 34 additions & 34 deletions docs/contracts/interfaces/ITapETH.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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 |
Expand Down
23 changes: 12 additions & 11 deletions test/TapETH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
});
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit d21f26b

Please sign in to comment.