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

tgld audit fix m12 #1037

Merged
merged 4 commits into from
Jun 3, 2024
Merged

tgld audit fix m12 #1037

merged 4 commits into from
Jun 3, 2024

Conversation

princetonbishop
Copy link
Collaborator

Description

The TempleGoldStaking.sol contract is a fork of the Synthetix rewards distribution contract, with slight modifications. The code special-cases the scenario where there are no users, by not updating the cumulative rate when the _totalSupply is zero, but it does not include such a condition for the tracking of the timestamp from L476.

File: contracts\templegold\TempleGoldStaking.sol
475:     function _rewardPerToken() internal view returns (uint256) {
476:         if (totalSupply == 0) {
477:             return rewardData.rewardPerTokenStored;
478:         }
479: 
480:         return
481:             rewardData.rewardPerTokenStored +
482:             (((_lastTimeRewardApplicable(rewardData.periodFinish) -
483:                 rewardData.lastUpdateTime) *
484:                 rewardData.rewardRate * 1e18) 
485:                 / totalSupply);
486:     }

Because of this, even when there are no users staking, the accounting logic still thinks funds were being dispersed during that timeframe (because the starting timestamp is updated),

As a result, if the distributeRewards() function is called prior to there being any users staking, the funds that should have gone to the first stakers will instead accrue to nobody, and be locked in the contract forever.

Example Scenario
Alice is distributionStarter and Bob is a person who wants to stake Temple.

Alice calls the distributeRewards() function to mint TGLD for this contract.
Let's suppose the minted TGLD is 786400 ether to calculate simply. Then rewardRate becomes 1 ether.
After 24 hours, Bob stakes 10000 TGLD into the contract.
After 6 days, Bob withdraw all staked TGLD and claim rewards. Then he gets 6
86400 ether.
As a result, 86400 ether is locked in the contract.

Recommendation

In the function distributeRewards(), check if there are enough reward tokens already in the contract.

File: contracts\templegold\TempleGoldStaking.sol
245:     function distributeRewards() updateReward(address(0)) external {
246:         if (distributionStarter != address(0) && msg.sender != distributionStarter) 
247:             { revert CommonEventsAndErrors.InvalidAccess(); }
248:         // Mint and distribute TGLD if no cooldown set
249:         if (lastRewardNotificationTimestamp + rewardDistributionCoolDown > block.timestamp) 
250:                 { revert CannotDistribute(); }
251:         _distributeGold();
252:         uint256 rewardAmount = nextRewardAmount;
253:         if (rewardAmount == 0 ) { revert CommonEventsAndErrors.ExpectedNonZero(); }
254:         nextRewardAmount = 0;
+            if (totalSupply == 0) { revert CommonEventsAndErrors.NoStaker(); }
255:         _notifyReward(rewardAmount);
256:     }
File: contracts\common\CommonEventsAndErrors.sol
06: library CommonEventsAndErrors {
07:     error InsufficientBalance(address token, uint256 required, uint256 balance);
08:     error InvalidParam();
09:     error InvalidAddress();
10:     error InvalidAccess();
11:     error InvalidAmount(address token, uint256 amount);
12:     error ExpectedNonZero();
13:     error Unimplemented();
+       error NoStaker(); 
14:     event TokenRecovered(address indexed to, address indexed token, uint256 amount);
15: }

Checklist

  • Code follows the style guide
  • I have performed a self-review of my own code
  • New and existing tests pass locally
  • This PR is targeting the correct branch

@princetonbishop princetonbishop requested a review from a team as a code owner May 28, 2024 15:42
@princetonbishop princetonbishop merged commit bf38a22 into templegold Jun 3, 2024
5 checks passed
@princetonbishop princetonbishop deleted the tgld-audit-fix-m12 branch June 3, 2024 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant