Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add totalRewards and event on tapETH #40

Merged
merged 2 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading