Skip to content

Commit

Permalink
Commented PrizePool and removed some dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Jul 31, 2020
1 parent 095dc97 commit d3677e6
Showing 1 changed file with 29 additions and 41 deletions.
70 changes: 29 additions & 41 deletions contracts/prize-pool/PrizePool.sol
Expand Up @@ -45,8 +45,6 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
mapping(address => uint256) internal timelockBalances;
mapping(address => uint256) internal unlockTimestamps;

uint256 internal __awardBalance;

/// @notice Initializes the Prize Pool with required contract connections
/// @param _trustedForwarder Address of the Forwarding Contract for GSN Meta-Txs
/// @param _prizeStrategy Address of the component-controller that manages the prize-strategy
Expand Down Expand Up @@ -126,6 +124,10 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
return _canAwardExternal(_externalToken);
}

/// @notice Deposits timelocked tokens for a user back into the Prize Pool as another asset.
/// @param to The address receiving the tokens
/// @param amount The amount of timelocked assets to re-deposit
/// @param controlledToken The type of token to be minted in exchange (i.e. tickets or sponsorship)
function timelockDepositTo(
address to,
uint256 amount,
Expand All @@ -136,7 +138,6 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
nonReentrant
{
require(_hasPrizeStrategy(), "PrizePool/prize-strategy-detached");
_updateAwardBalance();

address operator = _msgSender();

Expand All @@ -149,13 +150,12 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
emit TimelockDeposited(operator, to, controlledToken, amount);
}

/// @notice Deposit assets into the Prize Pool to Purchase Tickets
/// @param to The address receiving the Tickets
/// @param amount The amount of assets to deposit to purchase tickets
/// @param controlledToken The address of the Asset Token being deposited
/// @notice Deposit assets into the Prize Pool in exchange for tokens
/// @param to The address receiving the newly minted tokens
/// @param amount The amount of assets to deposit
/// @param controlledToken The address of the type of token the user is minting
function depositTo(address to, uint256 amount, address controlledToken) external onlyControlledToken(controlledToken) nonReentrant {
require(_hasPrizeStrategy(), "PrizePool/prize-strategy-detached");
_updateAwardBalance();

address operator = _msgSender();

Expand All @@ -168,12 +168,12 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
emit Deposited(operator, to, controlledToken, amount);
}

/// @notice Withdraw assets from the Prize Pool instantly by paying a Fairness fee if exiting early
/// @param from The address to withdraw assets from by redeeming tickets
/// @param amount The amount of assets to redeem for tickets
/// @param controlledToken The address of the asset token being withdrawn
/// @param sponsorAmount An optional amount of assets paid by the operator used to cover exit fees
/// @param maximumExitFee The maximum exit fee the user is willing to pay. This should be pre-calculated
/// @notice Withdraw assets from the Prize Pool instantly. A fairness fee may be charged for an early exit.
/// @param from The address to redeem tokens from.
/// @param amount The amount of tokens to redeem for assets.
/// @param controlledToken The address of the token to redeem (i.e. ticket or sponsorship)
/// @param sponsorAmount An optional amount of assets paid by the caller to cover exit fees
/// @param maximumExitFee The maximum exit fee the caller is willing to pay. This can be pre-calculated.
/// @return exitFee The amount of the fairness fee paid
function withdrawInstantlyFrom(
address from,
Expand All @@ -187,7 +187,6 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
onlyControlledToken(controlledToken)
returns (uint256 exitFee)
{
_updateAwardBalance();

bool hasPrizeStrategy = _hasPrizeStrategy();
if (hasPrizeStrategy) {
Expand Down Expand Up @@ -224,13 +223,12 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
emit InstantWithdrawal(operator, from, controlledToken, amount, exitFee, sponsoredExitFeePortion);
}

/// @notice Withdraw assets from the Prize Pool with a timelock on the assets
/// @dev The timelock is used to ensure that the tickets have contributed their equal weight
/// in the Prize before being withdrawn, in order to prevent gaming the system
/// @param from The address to withdraw assets from by redeeming tickets
/// @param amount The amount of assets to redeem for tickets
/// @param controlledToken The address of the asset token being withdrawn
/// @return unlockTimestamp The unlock timestamp that the assets will be released upon
/// @notice Withdraw assets from the Prize Pool by placing them into the timelock.
/// @dev The timelock is used to ensure that the tickets have contributed their fair share of the prize.
/// @param from The address to withdraw from
/// @param amount The amount to withdraw
/// @param controlledToken The type of token being withdrawn
/// @return unlockTimestamp The timestamp after which the funds can be swept
function withdrawWithTimelockFrom(
address from,
uint256 amount,
Expand All @@ -242,7 +240,6 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
returns (uint256 unlockTimestamp)
{
uint256 blockTime = _currentTime();
_updateAwardBalance();

bool hasPrizeStrategy = _hasPrizeStrategy();
if (hasPrizeStrategy) {
Expand Down Expand Up @@ -282,35 +279,28 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
return unlockTimestamp;
}

/// @notice Updates the Prize Strategy when Tickets are transferred between holders
/// @param from The address the tickets are being transferred from
/// @param to The address the tickets are being transferred to
/// @param amount The amount of tickets being trasferred
/// @notice Updates the Prize Strategy when tokens are transferred between holders. Only transfers, not minting or burning.
/// @param from The address the tokens are being transferred from
/// @param to The address the tokens are being transferred to
/// @param amount The amount of tokens being trasferred
function beforeTokenTransfer(address from, address to, uint256 amount) external override onlyControlledToken(msg.sender) {
// minting and redeeming are handled separately
if (from != address(0) && to != address(0) && _hasPrizeStrategy()) {
prizeStrategy.beforeTokenTransfer(from, to, amount, msg.sender);
}
}

/// @notice Pokes the current award balance of the Prize Pool
/// @notice Updates and returns the current prize.
/// @dev Updates the internal rolling interest rate since the last poke
/// @return award The total amount of assets to be awarded for the current prize
function awardBalance() external returns (uint256 award) {
_updateAwardBalance();
return __awardBalance;
}

/// @dev Calculates the current award balance based on the collateral & rolling interest rate
/// @dev The interest-index is the rolling or "accrued" exchange-rate on the unaccounted collateral since the last update.
function _updateAwardBalance() internal {
function awardBalance() public returns (uint256 award) {
uint256 tokenTotalSupply = _tokenTotalSupply();
uint256 bal = _balance();

if (bal > tokenTotalSupply) {
__awardBalance = bal.sub(tokenTotalSupply);
return bal.sub(tokenTotalSupply);
} else {
__awardBalance = 0;
return 0;
}
}

Expand All @@ -331,9 +321,8 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc
return;
}

_updateAwardBalance();
require(amount <= awardBalance(), "PrizePool/award-exceeds-avail");
ControlledToken(controlledToken).controllerMint(to, amount);
__awardBalance = __awardBalance.sub(amount);

emit Awarded(to, controlledToken, amount);
}
Expand Down Expand Up @@ -389,7 +378,6 @@ abstract contract PrizePool is OwnableUpgradeSafe, BaseRelayRecipient, Reentranc

_redeem(totalWithdrawal);


BalanceChange[] memory changes = new BalanceChange[](users.length);

IERC20 underlyingToken = IERC20(_token());
Expand Down

0 comments on commit d3677e6

Please sign in to comment.