2023-06-19 03:08:10 +03:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html lang = "en" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< title > LCOV - lcov.info - v2/TornadoStakingRewards.sol< / title >
< link rel = "stylesheet" type = "text/css" href = "../gcov.css" >
< / head >
< body >
< table width = "100%" border = 0 cellspacing = 0 cellpadding = 0 >
< tr > < td class = "title" > LCOV - code coverage report< / td > < / tr >
< tr > < td class = "ruler" > < img src = "../glass.png" width = 3 height = 3 alt = "" > < / td > < / tr >
< tr >
< td width = "100%" >
< table cellpadding = 1 border = 0 width = "100%" >
< tr >
< td width = "10%" class = "headerItem" > Current view:< / td >
< td width = "35%" class = "headerValue" > < a href = "../index.html" > top level< / a > - < a href = "index.html" > v2< / a > - TornadoStakingRewards.sol< span style = "font-size: 80%;" > (source / < a href = "TornadoStakingRewards.sol.func-sort-c.html" > functions< / a > )< / span > < / td >
< td width = "5%" > < / td >
< td width = "15%" > < / td >
< td width = "10%" class = "headerCovTableHead" > Hit< / td >
< td width = "10%" class = "headerCovTableHead" > Total< / td >
< td width = "15%" class = "headerCovTableHead" > Coverage< / td >
< / tr >
< tr >
< td class = "headerItem" > Test:< / td >
< td class = "headerValue" > lcov.info< / td >
< td > < / td >
< td class = "headerItem" > Lines:< / td >
2023-06-21 00:04:29 +03:00
< td class = "headerCovTableEntry" > 8< / td >
< td class = "headerCovTableEntry" > 19< / td >
< td class = "headerCovTableEntryLo" > 42.1 %< / td >
2023-06-19 03:08:10 +03:00
< / tr >
< tr >
< td class = "headerItem" > Date:< / td >
2023-06-21 00:04:29 +03:00
< td class = "headerValue" > 2023-06-20 21:04:08< / td >
2023-06-19 03:08:10 +03:00
< td > < / td >
< td class = "headerItem" > Functions:< / td >
2023-06-21 00:04:29 +03:00
< td class = "headerCovTableEntry" > 3< / td >
< td class = "headerCovTableEntry" > 6< / td >
< td class = "headerCovTableEntryLo" > 50.0 %< / td >
2023-06-19 03:08:10 +03:00
< / tr >
< tr >
< td class = "headerItem" > Legend:< / td >
< td class = "headerValueLeg" > Lines:
< span class = "coverLegendCov" > hit< / span >
< span class = "coverLegendNoCov" > not hit< / span >
< / td >
< td > < / td >
< / tr >
< tr > < td > < img src = "../glass.png" width = 3 height = 3 alt = "" > < / td > < / tr >
< / table >
< / td >
< / tr >
< tr > < td class = "ruler" > < img src = "../glass.png" width = 3 height = 3 alt = "" > < / td > < / tr >
< / table >
< table cellpadding = 0 cellspacing = 0 border = 0 >
< tr >
< td > < br > < / td >
< / tr >
< tr >
< td >
< pre class = "sourceHeading" > Line data Source code< / pre >
< pre class = "source" >
< a name = "1" > < span class = "lineNum" > 1 < / span > : // SPDX-License-Identifier: MIT< / a >
< a name = "2" > < span class = "lineNum" > 2 < / span > : < / a >
< a name = "3" > < span class = "lineNum" > 3 < / span > : pragma solidity ^0.6.12;< / a >
< a name = "4" > < span class = "lineNum" > 4 < / span > : pragma experimental ABIEncoderV2;< / a >
< a name = "5" > < span class = "lineNum" > 5 < / span > : < / a >
2023-06-21 00:04:29 +03:00
< a name = "6" > < span class = "lineNum" > 6 < / span > : import { console2 } from " forge-std/console2.sol" ;< / a >
< a name = "7" > < span class = "lineNum" > 7 < / span > : < / a >
< a name = "8" > < span class = "lineNum" > 8 < / span > : import { IERC20 } from " @openzeppelin/contracts/token/ERC20/IERC20.sol" ;< / a >
< a name = "9" > < span class = "lineNum" > 9 < / span > : import { SafeMath } from " @openzeppelin/contracts/math/SafeMath.sol" ;< / a >
< a name = "10" > < span class = "lineNum" > 10 < / span > : import { SafeERC20 } from " @openzeppelin/contracts/token/ERC20/SafeERC20.sol" ;< / a >
< a name = "11" > < span class = "lineNum" > 11 < / span > : import { Initializable } from " @openzeppelin/contracts/proxy/Initializable.sol" ;< / a >
< a name = "12" > < span class = "lineNum" > 12 < / span > : import { EnsResolve } from " torn-token/contracts/ENS.sol" ;< / a >
< a name = "13" > < span class = "lineNum" > 13 < / span > : < / a >
< a name = "14" > < span class = "lineNum" > 14 < / span > : interface ITornadoVault {< / a >
< a name = "15" > < span class = "lineNum" > 15 < / span > : function withdrawTorn(address recipient, uint256 amount) external;< / a >
< a name = "16" > < span class = "lineNum" > 16 < / span > : }< / a >
< a name = "17" > < span class = "lineNum" > 17 < / span > : < / a >
< a name = "18" > < span class = "lineNum" > 18 < / span > : interface ITornadoGovernance {< / a >
< a name = "19" > < span class = "lineNum" > 19 < / span > : function lockedBalance(address account) external view returns (uint256);< / a >
< a name = "20" > < span class = "lineNum" > 20 < / span > : < / a >
< a name = "21" > < span class = "lineNum" > 21 < / span > : function userVault() external view returns (ITornadoVault);< / a >
< a name = "22" > < span class = "lineNum" > 22 < / span > : }< / a >
< a name = "23" > < span class = "lineNum" > 23 < / span > : < / a >
< a name = "24" > < span class = "lineNum" > 24 < / span > : /**< / a >
< a name = "25" > < span class = "lineNum" > 25 < / span > : * @notice This is the staking contract of the governance staking upgrade.< / a >
< a name = "26" > < span class = "lineNum" > 26 < / span > : * This contract should hold the staked funds which are received upon relayer registration,< / a >
< a name = "27" > < span class = "lineNum" > 27 < / span > : * and properly attribute rewards to addresses without security issues.< / a >
< a name = "28" > < span class = "lineNum" > 28 < / span > : * @dev CONTRACT RISKS:< / a >
< a name = "29" > < span class = "lineNum" > 29 < / span > : * - Relayer staked TORN at risk if contract is compromised.< / a >
< a name = "30" > < span class = "lineNum" > 30 < / span > : *< / a >
< a name = "31" > < span class = "lineNum" > 31 < / span > : */< / a >
< a name = "32" > < span class = "lineNum" > 32 < / span > : contract TornadoStakingRewards is Initializable, EnsResolve {< / a >
< a name = "33" > < span class = "lineNum" > 33 < / span > : using SafeMath for uint256;< / a >
< a name = "34" > < span class = "lineNum" > 34 < / span > : using SafeERC20 for IERC20;< / a >
< a name = "35" > < span class = "lineNum" > 35 < / span > : < / a >
< a name = "36" > < span class = "lineNum" > 36 < / span > : /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ IMMUTABLES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */< / a >
< a name = "37" > < span class = "lineNum" > 37 < / span > : < / a >
< a name = "38" > < span class = "lineNum" > 38 < / span > : /// @notice 1e25< / a >
< a name = "39" > < span class = "lineNum" > 39 < / span > : uint256 public immutable ratioConstant;< / a >
< a name = "40" > < span class = "lineNum" > 40 < / span > : ITornadoGovernance public immutable Governance;< / a >
< a name = "41" > < span class = "lineNum" > 41 < / span > : IERC20 public immutable torn;< / a >
< a name = "42" > < span class = "lineNum" > 42 < / span > : address public immutable router;< / a >
< a name = "43" > < span class = "lineNum" > 43 < / span > : < / a >
< a name = "44" > < span class = "lineNum" > 44 < / span > : /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MUTABLES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */< / a >
< a name = "45" > < span class = "lineNum" > 45 < / span > : < / a >
< a name = "46" > < span class = "lineNum" > 46 < / span > : /// @notice the sum torn_burned_i/locked_amount_i*coefficient where i is incremented at each burn< / a >
< a name = "47" > < span class = "lineNum" > 47 < / span > : uint256 public accumulatedRewardPerTorn;< / a >
< a name = "48" > < span class = "lineNum" > 48 < / span > : /// @notice notes down accumulatedRewardPerTorn for an address on a lock/unlock/claim< / a >
< a name = "49" > < span class = "lineNum" > 49 < / span > : mapping(address => uint256) public accumulatedRewardRateOnLastUpdate;< / a >
< a name = "50" > < span class = "lineNum" > 50 < / span > : /// @notice notes down how much an account may claim< / a >
< a name = "51" > < span class = "lineNum" > 51 < / span > : mapping(address => uint256) public accumulatedRewards;< / a >
< a name = "52" > < span class = "lineNum" > 52 < / span > : < / a >
< a name = "53" > < span class = "lineNum" > 53 < / span > : event RewardsUpdated(address indexed account, uint256 rewards);< / a >
< a name = "54" > < span class = "lineNum" > 54 < / span > : event RewardsClaimed(address indexed account, uint256 rewardsClaimed);< / a >
< a name = "55" > < span class = "lineNum" > 55 < / span > : < / a >
< a name = "56" > < span class = "lineNum" > 56 < / span > : /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LOGIC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */< / a >
< a name = "57" > < span class = "lineNum" > 57 < / span > : < / a >
< a name = "58" > < span class = "lineNum" > 58 < / span > : // Minor code change here we won't resolve the registry by ENS< / a >
< a name = "59" > < span class = "lineNum" > 59 < / span > : constructor(address governanceAddress, address tornAddress, address _router) public {< / a >
< a name = "60" > < span class = "lineNum" > 60 < / span > : Governance = ITornadoGovernance(governanceAddress);< / a >
< a name = "61" > < span class = "lineNum" > 61 < / span > : torn = IERC20(tornAddress);< / a >
< a name = "62" > < span class = "lineNum" > 62 < / span > : router = _router;< / a >
< a name = "63" > < span class = "lineNum" > 63 < / span > : ratioConstant = IERC20(tornAddress).totalSupply();< / a >
< a name = "64" > < span class = "lineNum" > 64 < / span > : }< / a >
< a name = "65" > < span class = "lineNum" > 65 < / span > : < / a >
< a name = "66" > < span class = "lineNum" > 66 < / span > : modifier onlyGovernance() {< / a >
< a name = "67" > < span class = "lineNum" > 67 < / span > : require(msg.sender == address(Governance), " TornadoStakingRewards: onlyGovernance" );< / a >
< a name = "68" > < span class = "lineNum" > 68 < / span > : _;< / a >
< a name = "69" > < span class = "lineNum" > 69 < / span > : }< / a >
< a name = "70" > < span class = "lineNum" > 70 < / span > : < / a >
< a name = "71" > < span class = "lineNum" > 71 < / span > : modifier onlyRouter() {< / a >
< a name = "72" > < span class = "lineNum" > 72 < / span > : require(msg.sender == router, " TornadoStakingRewards: onlyRouter" );< / a >
< a name = "73" > < span class = "lineNum" > 73 < / span > : _;< / a >
< a name = "74" > < span class = "lineNum" > 74 < / span > : }< / a >
< a name = "75" > < span class = "lineNum" > 75 < / span > : < / a >
< a name = "76" > < span class = "lineNum" > 76 < / span > : /**< / a >
< a name = "77" > < span class = "lineNum" > 77 < / span > : * @notice This function should safely send a user his rewards.< / a >
< a name = "78" > < span class = "lineNum" > 78 < / span > : * @dev IMPORTANT FUNCTION:< / a >
< a name = "79" > < span class = "lineNum" > 79 < / span > : * We know that rewards are going to be updated every time someone locks or unlocks< / a >
< a name = "80" > < span class = "lineNum" > 80 < / span > : * so we know that this function can't be used to falsely increase the amount of< / a >
< a name = "81" > < span class = "lineNum" > 81 < / span > : * lockedTorn by locking in governance and subsequently calling it.< / a >
< a name = "82" > < span class = "lineNum" > 82 < / span > : * - set rewards to 0 greedily< / a >
< a name = "83" > < span class = "lineNum" > 83 < / span > : */< / a >
< a name = "84" > < span class = "lineNum" > 84 < / span > : function getReward() external {< / a >
< a name = "85" > < span class = "lineNum" > 85 < / span > < span class = "lineNoCov" > 0 : uint256 rewards = _updateReward(msg.sender, Governance.lockedBalance(msg.sender));< / span > < / a >
< a name = "86" > < span class = "lineNum" > 86 < / span > < span class = "lineNoCov" > 0 : rewards = rewards.add(accumulatedRewards[msg.sender]);< / span > < / a >
< a name = "87" > < span class = "lineNum" > 87 < / span > < span class = "lineNoCov" > 0 : accumulatedRewards[msg.sender] = 0;< / span > < / a >
< a name = "88" > < span class = "lineNum" > 88 < / span > < span class = "lineNoCov" > 0 : torn.safeTransfer(msg.sender, rewards);< / span > < / a >
< a name = "89" > < span class = "lineNum" > 89 < / span > < span class = "lineNoCov" > 0 : emit RewardsClaimed(msg.sender, rewards);< / span > < / a >
< a name = "90" > < span class = "lineNum" > 90 < / span > : }< / a >
< a name = "91" > < span class = "lineNum" > 91 < / span > : < / a >
< a name = "92" > < span class = "lineNum" > 92 < / span > : /**< / a >
< a name = "93" > < span class = "lineNum" > 93 < / span > : * @notice This function should increment the proper amount of rewards per torn for the contract< / a >
< a name = "94" > < span class = "lineNum" > 94 < / span > : * @dev IMPORTANT FUNCTION:< / a >
< a name = "95" > < span class = "lineNum" > 95 < / span > : * - calculation must not overflow with extreme values< / a >
< a name = "96" > < span class = "lineNum" > 96 < / span > : * (amount < = 1e25) * 1e25 / (balance of vault < = 1e25) -> (extreme values)< / a >
< a name = "97" > < span class = "lineNum" > 97 < / span > : * @param amount amount to add to the rewards< / a >
< a name = "98" > < span class = "lineNum" > 98 < / span > : */< / a >
< a name = "99" > < span class = "lineNum" > 99 < / span > : function addBurnRewards(uint256 amount) external {< / a >
< a name = "100" > < span class = "lineNum" > 100 < / span > < span class = "lineCov" > 1 : require(< / span > < / a >
< a name = "101" > < span class = "lineNum" > 101 < / span > : msg.sender == address(Governance) || msg.sender == router,< / a >
< a name = "102" > < span class = "lineNum" > 102 < / span > : " TornadoStakingRewards: only gov or router" < / a >
< a name = "103" > < span class = "lineNum" > 103 < / span > : );< / a >
< a name = "104" > < span class = "lineNum" > 104 < / span > < span class = "lineCov" > 1 : accumulatedRewardPerTorn = accumulatedRewardPerTorn.add(< / span > < / a >
< a name = "105" > < span class = "lineNum" > 105 < / span > : amount.mul(ratioConstant).div(torn.balanceOf(address(Governance.userVault())))< / a >
< a name = "106" > < span class = "lineNum" > 106 < / span > : );< / a >
< a name = "107" > < span class = "lineNum" > 107 < / span > : }< / a >
< a name = "108" > < span class = "lineNum" > 108 < / span > : < / a >
< a name = "109" > < span class = "lineNum" > 109 < / span > : /**< / a >
< a name = "110" > < span class = "lineNum" > 110 < / span > : * @notice This function should allow governance to properly update the accumulated rewards rate for an< / a >
< a name = "111" > < span class = "lineNum" > 111 < / span > : * account< / a >
< a name = "112" > < span class = "lineNum" > 112 < / span > : * @param account address of account to update data for< / a >
< a name = "113" > < span class = "lineNum" > 113 < / span > : * @param amountLockedBeforehand the balance locked beforehand in the governance contract< / a >
< a name = "114" > < span class = "lineNum" > 114 < / span > : *< / a >
2023-06-19 03:08:10 +03:00
< a name = "115" > < span class = "lineNum" > 115 < / span > : */< / a >
2023-06-21 00:04:29 +03:00
< a name = "116" > < span class = "lineNum" > 116 < / span > : function updateRewardsOnLockedBalanceChange(address account, uint256 amountLockedBeforehand)< / a >
< a name = "117" > < span class = "lineNum" > 117 < / span > : external< / a >
< a name = "118" > < span class = "lineNum" > 118 < / span > : onlyGovernance< / a >
< a name = "119" > < span class = "lineNum" > 119 < / span > : {< / a >
< a name = "120" > < span class = "lineNum" > 120 < / span > < span class = "lineCov" > 4 : uint256 claimed = _updateReward(account, amountLockedBeforehand);< / span > < / a >
< a name = "121" > < span class = "lineNum" > 121 < / span > < span class = "lineCov" > 4 : accumulatedRewards[account] = accumulatedRewards[account].add(claimed);< / span > < / a >
< a name = "122" > < span class = "lineNum" > 122 < / span > : }< / a >
< a name = "123" > < span class = "lineNum" > 123 < / span > : < / a >
< a name = "124" > < span class = "lineNum" > 124 < / span > : /**< / a >
< a name = "125" > < span class = "lineNum" > 125 < / span > : * @notice This function should allow governance rescue tokens from the staking rewards contract< / a >
< a name = "126" > < span class = "lineNum" > 126 < / span > : */< / a >
< a name = "127" > < span class = "lineNum" > 127 < / span > : function withdrawTorn(uint256 amount) external onlyGovernance {< / a >
< a name = "128" > < span class = "lineNum" > 128 < / span > < span class = "lineNoCov" > 0 : if (amount == type(uint256).max) amount = torn.balanceOf(address(this));< / span > < / a >
< a name = "129" > < span class = "lineNum" > 129 < / span > < span class = "lineNoCov" > 0 : torn.safeTransfer(address(Governance), amount);< / span > < / a >
< a name = "130" > < span class = "lineNum" > 130 < / span > : }< / a >
< a name = "131" > < span class = "lineNum" > 131 < / span > : < / a >
< a name = "132" > < span class = "lineNum" > 132 < / span > : /**< / a >
< a name = "133" > < span class = "lineNum" > 133 < / span > : * @notice This function should calculated the proper amount of rewards attributed to user since the last< / a >
< a name = "134" > < span class = "lineNum" > 134 < / span > : * update< / a >
< a name = "135" > < span class = "lineNum" > 135 < / span > : * @dev IMPORTANT FUNCTION:< / a >
< a name = "136" > < span class = "lineNum" > 136 < / span > : * - calculation must not overflow with extreme values< / a >
< a name = "137" > < span class = "lineNum" > 137 < / span > : * (accumulatedReward < = 1e25) * (lockedBeforehand < = 1e25) / 1e25< / a >
< a name = "138" > < span class = "lineNum" > 138 < / span > : * - result may go to 0, since this implies on 1 TORN locked => accumulatedReward < = 1e7, meaning a< / a >
< a name = "139" > < span class = "lineNum" > 139 < / span > : * very small reward< / a >
< a name = "140" > < span class = "lineNum" > 140 < / span > : * @param account address of account to calculate rewards for< / a >
< a name = "141" > < span class = "lineNum" > 141 < / span > : * @param amountLockedBeforehand the balance locked beforehand in the governance contract< / a >
< a name = "142" > < span class = "lineNum" > 142 < / span > : * @return claimed the rewards attributed to user since the last update< / a >
< a name = "143" > < span class = "lineNum" > 143 < / span > : */< / a >
< a name = "144" > < span class = "lineNum" > 144 < / span > : function _updateReward(address account, uint256 amountLockedBeforehand)< / a >
< a name = "145" > < span class = "lineNum" > 145 < / span > : private< / a >
< a name = "146" > < span class = "lineNum" > 146 < / span > : returns (uint256 claimed)< / a >
< a name = "147" > < span class = "lineNum" > 147 < / span > : {< / a >
< a name = "148" > < span class = "lineNum" > 148 < / span > < span class = "lineCov" > 4 : if (amountLockedBeforehand != 0) {< / span > < / a >
< a name = "149" > < span class = "lineNum" > 149 < / span > < span class = "lineCov" > 4 : claimed = (accumulatedRewardPerTorn.sub(accumulatedRewardRateOnLastUpdate[account])).mul(< / span > < / a >
< a name = "150" > < span class = "lineNum" > 150 < / span > : amountLockedBeforehand< / a >
< a name = "151" > < span class = "lineNum" > 151 < / span > : ).div(ratioConstant);< / a >
< a name = "152" > < span class = "lineNum" > 152 < / span > : }< / a >
< a name = "153" > < span class = "lineNum" > 153 < / span > < span class = "lineCov" > 4 : accumulatedRewardRateOnLastUpdate[account] = accumulatedRewardPerTorn;< / span > < / a >
< a name = "154" > < span class = "lineNum" > 154 < / span > < span class = "lineCov" > 4 : emit RewardsUpdated(account, claimed);< / span > < / a >
< a name = "155" > < span class = "lineNum" > 155 < / span > : }< / a >
< a name = "156" > < span class = "lineNum" > 156 < / span > : < / a >
< a name = "157" > < span class = "lineNum" > 157 < / span > : /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GETTERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */< / a >
< a name = "158" > < span class = "lineNum" > 158 < / span > : < / a >
< a name = "159" > < span class = "lineNum" > 159 < / span > : /**< / a >
< a name = "160" > < span class = "lineNum" > 160 < / span > : * @notice This function should show a user his rewards.< / a >
< a name = "161" > < span class = "lineNum" > 161 < / span > : * @param account address of account to calculate rewards for< / a >
< a name = "162" > < span class = "lineNum" > 162 < / span > : */< / a >
< a name = "163" > < span class = "lineNum" > 163 < / span > : function checkReward(address account) external view returns (uint256 rewards) {< / a >
< a name = "164" > < span class = "lineNum" > 164 < / span > < span class = "lineNoCov" > 0 : uint256 amountLocked = Governance.lockedBalance(account);< / span > < / a >
< a name = "165" > < span class = "lineNum" > 165 < / span > < span class = "lineNoCov" > 0 : if (amountLocked != 0) {< / span > < / a >
< a name = "166" > < span class = "lineNum" > 166 < / span > < span class = "lineNoCov" > 0 : rewards = (accumulatedRewardPerTorn.sub(accumulatedRewardRateOnLastUpdate[account])).mul(< / span > < / a >
< a name = "167" > < span class = "lineNum" > 167 < / span > : amountLocked< / a >
< a name = "168" > < span class = "lineNum" > 168 < / span > : ).div(ratioConstant);< / a >
< a name = "169" > < span class = "lineNum" > 169 < / span > : }< / a >
< a name = "170" > < span class = "lineNum" > 170 < / span > < span class = "lineNoCov" > 0 : rewards = rewards.add(accumulatedRewards[account]);< / span > < / a >
< a name = "171" > < span class = "lineNum" > 171 < / span > : }< / a >
< a name = "172" > < span class = "lineNum" > 172 < / span > : }< / a >
2023-06-19 03:08:10 +03:00
< / pre >
< / td >
< / tr >
< / table >
< br >
< table width = "100%" border = 0 cellspacing = 0 cellpadding = 0 >
< tr > < td class = "ruler" > < img src = "../glass.png" width = 3 height = 3 alt = "" > < / td > < / tr >
< tr > < td class = "versionInfo" > Generated by: < a href = "https://github.com/linux-test-project/lcov" target = "_parent" > LCOV version 1.16< / a > < / td > < / tr >
< / table >
< br >
< / body >
< / html >