diff --git a/contracts/bonding/BondingManager.sol b/contracts/bonding/BondingManager.sol index 0dee4e07..a3aad42b 100644 --- a/contracts/bonding/BondingManager.sol +++ b/contracts/bonding/BondingManager.sol @@ -13,14 +13,11 @@ import "../token/IMinter.sol"; import "../rounds/IRoundsManager.sol"; import "../snapshots/IMerkleSnapshot.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - /** * @title BondingManager * @notice Manages bonding, transcoder and rewards/fee accounting related operations of the Livepeer protocol */ contract BondingManager is ManagerProxyTarget, IBondingManager { - using SafeMath for uint256; using SortedDoublyLL for SortedDoublyLL.Data; using EarningsPool for EarningsPool.Data; using EarningsPoolLIP36 for EarningsPool.Data; @@ -235,7 +232,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { require(_recipient != address(0), "invalid recipient"); uint256 fees = delegators[msg.sender].fees; require(fees >= _amount, "insufficient fees to withdraw"); - delegators[msg.sender].fees = fees.sub(_amount); + delegators[msg.sender].fees = fees - _amount; // Tell Minter to transfer fees (ETH) to the address minter().trustedWithdrawETH(_recipient, _amount); @@ -275,7 +272,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { // LIP-36: Add fees for the current round instead of '_round' // https://github.com/livepeer/LIPs/issues/35#issuecomment-673659199 EarningsPool.Data storage earningsPool = t.earningsPoolPerRound[currentRound]; - EarningsPool.Data memory prevEarningsPool = latestCumulativeFactorsPool(t, currentRound.sub(1)); + EarningsPool.Data memory prevEarningsPool = latestCumulativeFactorsPool(t, currentRound - 1); // if transcoder hasn't called 'reward()' for '_round' its 'transcoderFeeShare', 'transcoderRewardCut' and 'totalStake' // on the 'EarningsPool' for '_round' would not be initialized and the fee distribution wouldn't happen as expected @@ -302,22 +299,22 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { // based on rewards for currentRound IMinter mtr = minter(); uint256 rewards = PreciseMathUtils.percOf( - mtr.currentMintableTokens().add(mtr.currentMintedTokens()), + mtr.currentMintableTokens() + mtr.currentMintedTokens(), totalStake, currentRoundTotalActiveStake ); uint256 transcoderCommissionRewards = MathUtils.percOf(rewards, earningsPool.transcoderRewardCut); - uint256 delegatorsRewards = rewards.sub(transcoderCommissionRewards); + uint256 delegatorsRewards = rewards - transcoderCommissionRewards; prevEarningsPool.cumulativeRewardFactor = PreciseMathUtils.percOf( earningsPool.cumulativeRewardFactor, totalStake, - delegatorsRewards.add(totalStake) + delegatorsRewards + totalStake ); } uint256 delegatorsFees = MathUtils.percOf(_fees, earningsPool.transcoderFeeShare); - uint256 transcoderCommissionFees = _fees.sub(delegatorsFees); + uint256 transcoderCommissionFees = _fees - delegatorsFees; // Calculate the fees earned by the transcoder's earned rewards uint256 transcoderRewardStakeFees = PreciseMathUtils.percOf( delegatorsFees, @@ -325,7 +322,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { totalStake ); // Track fees earned by the transcoder based on its earned rewards and feeShare - t.cumulativeFees = t.cumulativeFees.add(transcoderRewardStakeFees).add(transcoderCommissionFees); + t.cumulativeFees = t.cumulativeFees + transcoderRewardStakeFees + transcoderCommissionFees; // Update cumulative fee factor with new fees // The cumulativeFeeFactor is used to calculate fees for all delegators including the transcoder (self-delegated) // Note that delegatorsFees includes transcoderRewardStakeFees, but no delegator will claim that amount using @@ -359,13 +356,11 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { } // Decrease bonded stake - del.bondedAmount = del.bondedAmount.sub(penalty); + del.bondedAmount -= penalty; // If still bonded decrease delegate's delegated amount if (delegatorStatus(_transcoder) == DelegatorStatus.Bonded) { - delegators[del.delegateAddress].delegatedAmount = delegators[del.delegateAddress].delegatedAmount.sub( - penalty - ); + delegators[del.delegateAddress].delegatedAmount -= penalty; } // Account for penalty @@ -377,7 +372,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { minter().trustedTransferTokens(_finder, finderAmount); // Minter burns the slashed funds - finder reward - minter().trustedBurnTokens(burnAmount.sub(finderAmount)); + minter().trustedBurnTokens(burnAmount - finderAmount); emit TranscoderSlashed(_transcoder, _finder, penalty, finderAmount); } else { @@ -446,7 +441,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { tryToJoinActiveSet( msg.sender, delegators[msg.sender].delegatedAmount, - currentRound.add(1), + currentRound + 1, _newPosPrev, _newPosNext ); @@ -497,7 +492,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { // New delegate // Set start round // Don't set start round if delegator is in pending state because the start round would not change - del.startRound = currentRound.add(1); + del.startRound = currentRound + 1; // Unbonded state = no existing delegate and no bonded stake // Thus, delegation amount = provided amount } else if (currentBondedAmount > 0 && currentDelegate != _to) { @@ -511,9 +506,9 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { require(!isRegisteredTranscoder(_owner), "registered transcoders can't delegate towards other addresses"); // Changing delegate // Set start round - del.startRound = currentRound.add(1); + del.startRound = currentRound + 1; // Update amount to delegate with previous delegation amount - delegationAmount = delegationAmount.add(currentBondedAmount); + delegationAmount = delegationAmount + currentBondedAmount; decreaseTotalStake(currentDelegate, currentBondedAmount, _oldDelegateNewPosPrev, _oldDelegateNewPosNext); } @@ -536,7 +531,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { // Update delegate del.delegateAddress = _to; // Update bonded amount - del.bondedAmount = currentBondedAmount.add(_amount); + del.bondedAmount = currentBondedAmount + _amount; increaseTotalStake(_to, delegationAmount, _currDelegateNewPosPrev, _currDelegateNewPosNext); @@ -621,7 +616,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { Delegator storage newDel = delegators[_delegator]; - uint256 oldDelUnbondingLockId = oldDel.nextUnbondingLockId.sub(1); + uint256 oldDelUnbondingLockId = oldDel.nextUnbondingLockId - 1; uint256 withdrawRound = oldDel.unbondingLocks[oldDelUnbondingLockId].withdrawRound; // Burn lock for current owner @@ -631,7 +626,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { uint256 newDelUnbondingLockId = newDel.nextUnbondingLockId; newDel.unbondingLocks[newDelUnbondingLockId] = UnbondingLock({ amount: _amount, withdrawRound: withdrawRound }); - newDel.nextUnbondingLockId = newDel.nextUnbondingLockId.add(1); + newDel.nextUnbondingLockId = newDel.nextUnbondingLockId + 1; emit TransferBond(msg.sender, _delegator, oldDelUnbondingLockId, newDelUnbondingLockId, _amount); @@ -649,7 +644,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { // Move to Pending state if receiver is currently in Unbonded state if (delegatorStatus(_delegator) == DelegatorStatus.Unbonded) { - newDel.startRound = currentRound.add(1); + newDel.startRound = currentRound + 1; } // Process rebond using unbonding lock @@ -679,15 +674,15 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { address currentDelegate = del.delegateAddress; uint256 currentRound = roundsManager().currentRound(); - uint256 withdrawRound = currentRound.add(unbondingPeriod); + uint256 withdrawRound = currentRound + unbondingPeriod; uint256 unbondingLockId = del.nextUnbondingLockId; // Create new unbonding lock del.unbondingLocks[unbondingLockId] = UnbondingLock({ amount: _amount, withdrawRound: withdrawRound }); // Increment ID for next unbonding lock - del.nextUnbondingLockId = unbondingLockId.add(1); + del.nextUnbondingLockId = unbondingLockId + 1; // Decrease delegator's bonded amount - del.bondedAmount = del.bondedAmount.sub(_amount); + del.bondedAmount -= _amount; if (del.bondedAmount == 0) { // Delegator no longer delegated to anyone if it does not have a bonded amount @@ -747,7 +742,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { require(delegatorStatus(msg.sender) == DelegatorStatus.Unbonded, "caller must be unbonded"); // Set delegator's start round and transition into Pending state - delegators[msg.sender].startRound = roundsManager().currentRound().add(1); + delegators[msg.sender].startRound = roundsManager().currentRound() + 1; // Set delegator's delegate delegators[msg.sender].delegateAddress = _to; // Process rebond using unbonding lock @@ -1148,13 +1143,13 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { endPool.cumulativeRewardFactor = PreciseMathUtils.percPoints(1, 1); } - cFees = _fees.add( + cFees = + _fees + PreciseMathUtils.percOf( _stake, - endPool.cumulativeFeeFactor.sub(startPool.cumulativeFeeFactor), + endPool.cumulativeFeeFactor - startPool.cumulativeFeeFactor, startPool.cumulativeRewardFactor - ) - ); + ); cStake = PreciseMathUtils.percOf(_stake, endPool.cumulativeRewardFactor, startPool.cumulativeRewardFactor); @@ -1178,20 +1173,20 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { fees = del.fees; stake = del.bondedAmount; - uint256 startRound = del.lastClaimRound.add(1); + uint256 startRound = del.lastClaimRound + 1; address delegateAddr = del.delegateAddress; bool isTranscoder = _delegator == delegateAddr; // Make sure there is a round to claim i.e. end round - (start round - 1) > 0 if (startRound <= _endRound) { - (stake, fees) = delegatorCumulativeStakeAndFees(t, startRound.sub(1), _endRound, stake, fees); + (stake, fees) = delegatorCumulativeStakeAndFees(t, startRound - 1, _endRound, stake, fees); } // cumulativeRewards and cumulativeFees will track *all* rewards/fees earned by the transcoder // so it is important that this is only executed with the end round as the current round or else // the returned stake and fees will reflect rewards/fees earned in the future relative to the end round if (isTranscoder) { - stake = stake.add(t.cumulativeRewards); - fees = fees.add(t.cumulativeFees); + stake += t.cumulativeRewards; + fees += t.cumulativeFees; } return (stake, fees); @@ -1210,14 +1205,14 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { ) internal { if (isRegisteredTranscoder(_delegate)) { uint256 currStake = transcoderTotalStake(_delegate); - uint256 newStake = currStake.add(_amount); + uint256 newStake = currStake + _amount; uint256 currRound = roundsManager().currentRound(); - uint256 nextRound = currRound.add(1); + uint256 nextRound = currRound + 1; // If the transcoder is already in the active set update its stake and return if (transcoderPool.contains(_delegate)) { transcoderPool.updateKey(_delegate, newStake, _newPosPrev, _newPosNext); - nextRoundTotalActiveStake = nextRoundTotalActiveStake.add(_amount); + nextRoundTotalActiveStake = nextRoundTotalActiveStake + _amount; Transcoder storage t = transcoders[_delegate]; // currStake (the transcoder's delegatedAmount field) will reflect the transcoder's stake from lastActiveStakeUpdateRound @@ -1237,7 +1232,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { } // Increase delegate's delegated amount - delegators[_delegate].delegatedAmount = delegators[_delegate].delegatedAmount.add(_amount); + delegators[_delegate].delegatedAmount += _amount; } /** @@ -1253,12 +1248,12 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { ) internal { if (transcoderPool.contains(_delegate)) { uint256 currStake = transcoderTotalStake(_delegate); - uint256 newStake = currStake.sub(_amount); + uint256 newStake = currStake - _amount; uint256 currRound = roundsManager().currentRound(); - uint256 nextRound = currRound.add(1); + uint256 nextRound = currRound + 1; transcoderPool.updateKey(_delegate, newStake, _newPosPrev, _newPosNext); - nextRoundTotalActiveStake = nextRoundTotalActiveStake.sub(_amount); + nextRoundTotalActiveStake = nextRoundTotalActiveStake - _amount; Transcoder storage t = transcoders[_delegate]; // currStake (the transcoder's delegatedAmount field) will reflect the transcoder's stake from lastActiveStakeUpdateRound @@ -1274,7 +1269,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { } // Decrease old delegate's delegated amount - delegators[_delegate].delegatedAmount = delegators[_delegate].delegatedAmount.sub(_amount); + delegators[_delegate].delegatedAmount -= _amount; } /** @@ -1309,13 +1304,13 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { // 'EarningsPool.setStake()' is called whenever a transcoder becomes active again. transcoderPool.remove(lastTranscoder); transcoders[lastTranscoder].deactivationRound = _activationRound; - pendingNextRoundTotalActiveStake = pendingNextRoundTotalActiveStake.sub(lastStake); + pendingNextRoundTotalActiveStake -= lastStake; emit TranscoderDeactivated(lastTranscoder, _activationRound); } transcoderPool.insert(_transcoder, _totalStake, _newPosPrev, _newPosNext); - pendingNextRoundTotalActiveStake = pendingNextRoundTotalActiveStake.add(_totalStake); + pendingNextRoundTotalActiveStake += _totalStake; Transcoder storage t = transcoders[_transcoder]; t.lastActiveStakeUpdateRound = _activationRound; t.activationRound = _activationRound; @@ -1334,8 +1329,8 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { // Not zeroing the stake on the current round's 'EarningsPool' saves gas and should have no side effects as long as // 'EarningsPool.setStake()' is called whenever a transcoder becomes active again. transcoderPool.remove(_transcoder); - nextRoundTotalActiveStake = nextRoundTotalActiveStake.sub(transcoderTotalStake(_transcoder)); - uint256 deactivationRound = roundsManager().currentRound().add(1); + nextRoundTotalActiveStake -= transcoderTotalStake(_transcoder); + uint256 deactivationRound = roundsManager().currentRound() + 1; transcoders[_transcoder].deactivationRound = deactivationRound; emit TranscoderDeactivated(_transcoder, deactivationRound); } @@ -1363,7 +1358,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { t.activeCumulativeRewards = t.cumulativeRewards; uint256 transcoderCommissionRewards = MathUtils.percOf(_rewards, earningsPool.transcoderRewardCut); - uint256 delegatorsRewards = _rewards.sub(transcoderCommissionRewards); + uint256 delegatorsRewards = _rewards - transcoderCommissionRewards; // Calculate the rewards earned by the transcoder's earned rewards uint256 transcoderRewardStakeRewards = PreciseMathUtils.percOf( delegatorsRewards, @@ -1371,7 +1366,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { earningsPool.totalStake ); // Track rewards earned by the transcoder based on its earned rewards and rewardCut - t.cumulativeRewards = t.cumulativeRewards.add(transcoderRewardStakeRewards).add(transcoderCommissionRewards); + t.cumulativeRewards = t.cumulativeRewards + transcoderRewardStakeRewards + transcoderCommissionRewards; // Update cumulative reward factor with new rewards // The cumulativeRewardFactor is used to calculate rewards for all delegators including the transcoder (self-delegated) // Note that delegatorsRewards includes transcoderRewardStakeRewards, but no delegator will claim that amount using @@ -1393,7 +1388,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { uint256 _lastClaimRound ) internal { Delegator storage del = delegators[_delegator]; - uint256 startRound = _lastClaimRound.add(1); + uint256 startRound = _lastClaimRound + 1; uint256 currentBondedAmount = del.bondedAmount; uint256 currentFees = del.fees; @@ -1431,8 +1426,8 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { emit EarningsClaimed( del.delegateAddress, _delegator, - currentBondedAmount.sub(del.bondedAmount), - currentFees.sub(del.fees), + currentBondedAmount - del.bondedAmount, + currentFees - del.fees, startRound, _endRound ); @@ -1464,7 +1459,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { uint256 amount = lock.amount; // Increase delegator's bonded amount - del.bondedAmount = del.bondedAmount.add(amount); + del.bondedAmount += amount; // Delete lock delete del.unbondingLocks[_unbondingLockId]; diff --git a/contracts/bonding/libraries/EarningsPool.sol b/contracts/bonding/libraries/EarningsPool.sol index 1b7ad6b2..526d098a 100644 --- a/contracts/bonding/libraries/EarningsPool.sol +++ b/contracts/bonding/libraries/EarningsPool.sol @@ -3,15 +3,11 @@ pragma solidity 0.8.9; import "../../libraries/MathUtils.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - /** * @title EarningsPool * @dev Manages reward and fee pools for delegators and transcoders */ library EarningsPool { - using SafeMath for uint256; - struct Data { uint256 totalStake; // Transcoder's total stake during the earnings pool's round uint256 transcoderRewardCut; // Transcoder's reward cut during the earnings pool's round diff --git a/contracts/bonding/libraries/EarningsPoolLIP36.sol b/contracts/bonding/libraries/EarningsPoolLIP36.sol index 0489d905..26414105 100644 --- a/contracts/bonding/libraries/EarningsPoolLIP36.sol +++ b/contracts/bonding/libraries/EarningsPoolLIP36.sol @@ -4,11 +4,7 @@ pragma solidity 0.8.9; import "./EarningsPool.sol"; import "../../libraries/PreciseMathUtils.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - library EarningsPoolLIP36 { - using SafeMath for uint256; - /** * @notice Update the cumulative fee factor stored in an earnings pool with new fees * @param earningsPool Storage pointer to EarningsPools.Data struct @@ -27,15 +23,15 @@ library EarningsPoolLIP36 { // Initialize the cumulativeFeeFactor when adding fees for the first time if (earningsPool.cumulativeFeeFactor == 0) { - earningsPool.cumulativeFeeFactor = prevCumulativeFeeFactor.add( - PreciseMathUtils.percOf(prevCumulativeRewardFactor, _fees, earningsPool.totalStake) - ); + earningsPool.cumulativeFeeFactor = + prevCumulativeFeeFactor + + PreciseMathUtils.percOf(prevCumulativeRewardFactor, _fees, earningsPool.totalStake); return; } - earningsPool.cumulativeFeeFactor = earningsPool.cumulativeFeeFactor.add( - PreciseMathUtils.percOf(prevCumulativeRewardFactor, _fees, earningsPool.totalStake) - ); + earningsPool.cumulativeFeeFactor = + earningsPool.cumulativeFeeFactor + + PreciseMathUtils.percOf(prevCumulativeRewardFactor, _fees, earningsPool.totalStake); } /** @@ -53,8 +49,8 @@ library EarningsPoolLIP36 { ? _prevEarningsPool.cumulativeRewardFactor : PreciseMathUtils.percPoints(1, 1); - earningsPool.cumulativeRewardFactor = prevCumulativeRewardFactor.add( - PreciseMathUtils.percOf(prevCumulativeRewardFactor, _rewards, earningsPool.totalStake) - ); + earningsPool.cumulativeRewardFactor = + prevCumulativeRewardFactor + + PreciseMathUtils.percOf(prevCumulativeRewardFactor, _rewards, earningsPool.totalStake); } } diff --git a/contracts/governance/Governor.sol b/contracts/governance/Governor.sol index 231fdd30..033b7801 100644 --- a/contracts/governance/Governor.sol +++ b/contracts/governance/Governor.sol @@ -1,15 +1,11 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.9; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - /** * @title Governor * @dev The Governor holds the rights to stage and execute contract calls i.e. changing Livepeer protocol parameters. */ contract Governor { - using SafeMath for uint256; - address public owner; /// @dev mapping of updateHash (keccak256(update) => executeBlock (block.number + delay) @@ -68,7 +64,7 @@ contract Governor { require(updates[updateHash] == 0, "update already staged"); - updates[updateHash] = block.number.add(_delay); + updates[updateHash] = block.number + _delay; emit UpdateStaged(_update, _delay); } diff --git a/contracts/libraries/MathUtils.sol b/contracts/libraries/MathUtils.sol index b3c48680..77233028 100644 --- a/contracts/libraries/MathUtils.sol +++ b/contracts/libraries/MathUtils.sol @@ -1,11 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.9; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - library MathUtils { - using SafeMath for uint256; - // Divisor used for representing percentages uint256 public constant PERC_DIVISOR = 1000000; @@ -28,7 +24,7 @@ library MathUtils { uint256 _fracNum, uint256 _fracDenom ) internal pure returns (uint256) { - return _amount.mul(percPoints(_fracNum, _fracDenom)).div(PERC_DIVISOR); + return (_amount * percPoints(_fracNum, _fracDenom)) / PERC_DIVISOR; } /** @@ -37,7 +33,7 @@ library MathUtils { * @param _fracNum Numerator of fraction representing the percentage with PERC_DIVISOR as the denominator */ function percOf(uint256 _amount, uint256 _fracNum) internal pure returns (uint256) { - return _amount.mul(_fracNum).div(PERC_DIVISOR); + return (_amount * _fracNum) / PERC_DIVISOR; } /** @@ -46,6 +42,6 @@ library MathUtils { * @param _fracDenom Denominator of fraction represeting the percentage */ function percPoints(uint256 _fracNum, uint256 _fracDenom) internal pure returns (uint256) { - return _fracNum.mul(PERC_DIVISOR).div(_fracDenom); + return (_fracNum * PERC_DIVISOR) / _fracDenom; } } diff --git a/contracts/libraries/MathUtilsV2.sol b/contracts/libraries/MathUtilsV2.sol index 41065b48..8ba42453 100644 --- a/contracts/libraries/MathUtilsV2.sol +++ b/contracts/libraries/MathUtilsV2.sol @@ -1,11 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.9; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - library MathUtils { - using SafeMath for uint256; - // Divisor used for representing percentages uint256 public constant PERC_DIVISOR = 1000000000; @@ -28,7 +24,7 @@ library MathUtils { uint256 _fracNum, uint256 _fracDenom ) internal pure returns (uint256) { - return _amount.mul(percPoints(_fracNum, _fracDenom)).div(PERC_DIVISOR); + return (_amount * percPoints(_fracNum, _fracDenom)) / PERC_DIVISOR; } /** @@ -37,7 +33,7 @@ library MathUtils { * @param _fracNum Numerator of fraction representing the percentage with PERC_DIVISOR as the denominator */ function percOf(uint256 _amount, uint256 _fracNum) internal pure returns (uint256) { - return _amount.mul(_fracNum).div(PERC_DIVISOR); + return (_amount * _fracNum) / PERC_DIVISOR; } /** @@ -46,6 +42,6 @@ library MathUtils { * @param _fracDenom Denominator of fraction represeting the percentage */ function percPoints(uint256 _fracNum, uint256 _fracDenom) internal pure returns (uint256) { - return _fracNum.mul(PERC_DIVISOR).div(_fracDenom); + return (_fracNum * PERC_DIVISOR) / _fracDenom; } } diff --git a/contracts/libraries/PreciseMathUtils.sol b/contracts/libraries/PreciseMathUtils.sol index 4b27a619..e9729377 100644 --- a/contracts/libraries/PreciseMathUtils.sol +++ b/contracts/libraries/PreciseMathUtils.sol @@ -1,11 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.9; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - library PreciseMathUtils { - using SafeMath for uint256; - // Divisor used for representing percentages uint256 public constant PERC_DIVISOR = 10**27; @@ -28,7 +24,7 @@ library PreciseMathUtils { uint256 _fracNum, uint256 _fracDenom ) internal pure returns (uint256) { - return _amount.mul(percPoints(_fracNum, _fracDenom)).div(PERC_DIVISOR); + return (_amount * percPoints(_fracNum, _fracDenom)) / PERC_DIVISOR; } /** @@ -37,7 +33,7 @@ library PreciseMathUtils { * @param _fracNum Numerator of fraction representing the percentage with PERC_DIVISOR as the denominator */ function percOf(uint256 _amount, uint256 _fracNum) internal pure returns (uint256) { - return _amount.mul(_fracNum).div(PERC_DIVISOR); + return (_amount * _fracNum) / PERC_DIVISOR; } /** @@ -46,6 +42,6 @@ library PreciseMathUtils { * @param _fracDenom Denominator of fraction represeting the percentage */ function percPoints(uint256 _fracNum, uint256 _fracDenom) internal pure returns (uint256) { - return _fracNum.mul(PERC_DIVISOR).div(_fracDenom); + return (_fracNum * PERC_DIVISOR) / _fracDenom; } } diff --git a/contracts/libraries/SortedDoublyLL.sol b/contracts/libraries/SortedDoublyLL.sol index 188a73e7..257f93f7 100644 --- a/contracts/libraries/SortedDoublyLL.sol +++ b/contracts/libraries/SortedDoublyLL.sol @@ -1,8 +1,6 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.9; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - /** * @title A sorted doubly linked list with nodes sorted in descending order. Optionally accepts insert position hints * @@ -15,8 +13,6 @@ import "@openzeppelin/contracts/utils/math/SafeMath.sol"; * to find the appropriate insert position. */ library SortedDoublyLL { - using SafeMath for uint256; - // Information for a node in the list struct Node { uint256 key; // Node's key used for sorting @@ -99,7 +95,7 @@ library SortedDoublyLL { self.nodes[nextId].prevId = _id; } - self.size = self.size.add(1); + self.size += 1; } /** @@ -139,7 +135,7 @@ library SortedDoublyLL { } delete self.nodes[_id]; - self.size = self.size.sub(1); + self.size -= 1; } /** diff --git a/contracts/pm/mixins/MixinReserve.sol b/contracts/pm/mixins/MixinReserve.sol index f3e9ace8..85a224b9 100644 --- a/contracts/pm/mixins/MixinReserve.sol +++ b/contracts/pm/mixins/MixinReserve.sol @@ -3,11 +3,8 @@ pragma solidity 0.8.9; import "./interfaces/MReserve.sol"; import "./MixinContractRegistry.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; abstract contract MixinReserve is MixinContractRegistry, MReserve { - using SafeMath for uint256; - struct Reserve { uint256 funds; // Amount of funds in the reserve mapping(uint256 => uint256) claimedForRound; // Mapping of round => total amount claimed @@ -48,8 +45,8 @@ abstract contract MixinReserve is MixinContractRegistry, MReserve { } // Total claimable funds = remaining funds + amount claimed for the round - uint256 totalClaimable = reserve.funds.add(reserve.claimedForRound[currentRound]); - return totalClaimable.div(poolSize).sub(reserve.claimedByAddress[currentRound][_claimant]); + uint256 totalClaimable = reserve.funds + reserve.claimedForRound[currentRound]; + return (totalClaimable / poolSize) - reserve.claimedByAddress[currentRound][_claimant]; } /** @@ -70,7 +67,7 @@ abstract contract MixinReserve is MixinContractRegistry, MReserve { * @param _amount Amount of funds to add to reserve */ function addReserve(address _reserveHolder, uint256 _amount) internal override { - reserves[_reserveHolder].funds = reserves[_reserveHolder].funds.add(_amount); + reserves[_reserveHolder].funds += _amount; emit ReserveFunded(_reserveHolder, _amount); } @@ -117,13 +114,11 @@ abstract contract MixinReserve is MixinContractRegistry, MReserve { uint256 currentRound = roundsManager().currentRound(); Reserve storage reserve = reserves[_reserveHolder]; // Increase total amount claimed for the round - reserve.claimedForRound[currentRound] = reserve.claimedForRound[currentRound].add(claimAmount); + reserve.claimedForRound[currentRound] += claimAmount; // Increase amount claimed by claimant for the round - reserve.claimedByAddress[currentRound][_claimant] = reserve.claimedByAddress[currentRound][_claimant].add( - claimAmount - ); + reserve.claimedByAddress[currentRound][_claimant] += claimAmount; // Decrease remaining reserve - reserve.funds = reserve.funds.sub(claimAmount); + reserve.funds -= claimAmount; emit ReserveClaimed(_reserveHolder, _claimant, claimAmount); } diff --git a/contracts/pm/mixins/MixinTicketBrokerCore.sol b/contracts/pm/mixins/MixinTicketBrokerCore.sol index eb5704be..eb9e7816 100644 --- a/contracts/pm/mixins/MixinTicketBrokerCore.sol +++ b/contracts/pm/mixins/MixinTicketBrokerCore.sol @@ -6,11 +6,8 @@ import "./interfaces/MTicketProcessor.sol"; import "./interfaces/MTicketBrokerCore.sol"; import "./MixinContractRegistry.sol"; import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTicketProcessor, MTicketBrokerCore { - using SafeMath for uint256; - struct Sender { uint256 deposit; // Amount of funds deposited uint256 withdrawRound; // Round that sender can withdraw deposit & reserve @@ -28,7 +25,7 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic // Checks if msg.value is equal to the given deposit and reserve amounts modifier checkDepositReserveETHValueSplit(uint256 _depositAmount, uint256 _reserveAmount) { require( - msg.value == _depositAmount.add(_reserveAmount), + msg.value == _depositAmount + _reserveAmount, "msg.value does not equal sum of deposit amount and reserve amount" ); @@ -38,7 +35,7 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic // Process deposit funding modifier processDeposit(address _sender, uint256 _amount) { Sender storage sender = senders[_sender]; - sender.deposit = sender.deposit.add(_amount); + sender.deposit += _amount; if (_isUnlockInProgress(sender)) { _cancelUnlock(sender, _sender); } @@ -136,9 +133,9 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic // If ticket face value > sender's deposit then claim from // the sender's reserve - amountToTransfer = sender.deposit.add( - claimFromReserve(_ticket.sender, _ticket.recipient, _ticket.faceValue.sub(sender.deposit)) - ); + amountToTransfer = + sender.deposit + + claimFromReserve(_ticket.sender, _ticket.recipient, _ticket.faceValue - sender.deposit); sender.deposit = 0; } else { @@ -146,7 +143,7 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic // from sender's deposit amountToTransfer = _ticket.faceValue; - sender.deposit = sender.deposit.sub(_ticket.faceValue); + sender.deposit -= _ticket.faceValue; } if (amountToTransfer > 0) { @@ -176,7 +173,7 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic require(!_isUnlockInProgress(sender), "unlock already initiated"); uint256 currentRound = roundsManager().currentRound(); - sender.withdrawRound = currentRound.add(unlockPeriod); + sender.withdrawRound = currentRound + unlockPeriod; emit Unlock(msg.sender, currentRound, sender.withdrawRound); } @@ -206,7 +203,7 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic sender.deposit = 0; clearReserve(msg.sender); - withdrawTransfer(payable(msg.sender), deposit.add(reserve)); + withdrawTransfer(payable(msg.sender), deposit + reserve); emit Withdrawal(msg.sender, deposit, reserve); } diff --git a/contracts/pm/mixins/MixinTicketProcessor.sol b/contracts/pm/mixins/MixinTicketProcessor.sol index 604fd24c..96f26995 100644 --- a/contracts/pm/mixins/MixinTicketProcessor.sol +++ b/contracts/pm/mixins/MixinTicketProcessor.sol @@ -3,11 +3,8 @@ pragma solidity 0.8.9; import "./interfaces/MTicketProcessor.sol"; import "./MixinContractRegistry.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; abstract contract MixinTicketProcessor is MixinContractRegistry, MTicketProcessor { - using SafeMath for uint256; - // Number of rounds that a ticket is valid for starting from // its creationRound uint256 public ticketValidityPeriod; @@ -61,7 +58,7 @@ abstract contract MixinTicketProcessor is MixinContractRegistry, MTicketProcesso uint256 currRound = roundsManager().currentRound(); - require(creationRound.add(ticketValidityPeriod) > currRound, "ticket is expired"); + require(creationRound + ticketValidityPeriod > currRound, "ticket is expired"); } /** diff --git a/contracts/rounds/RoundsManager.sol b/contracts/rounds/RoundsManager.sol index 1bc8b8be..dd294c77 100644 --- a/contracts/rounds/RoundsManager.sol +++ b/contracts/rounds/RoundsManager.sol @@ -7,15 +7,11 @@ import "../bonding/IBondingManager.sol"; import "../token/IMinter.sol"; import "../libraries/MathUtils.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - /** * @title RoundsManager * @notice Manages round progression and other blockchain time related operations of the Livepeer protocol */ contract RoundsManager is ManagerProxyTarget, IRoundsManager { - using SafeMath for uint256; - // Round length in blocks uint256 public roundLength; // Lock period of a round as a % of round length @@ -96,7 +92,7 @@ contract RoundsManager is ManagerProxyTarget, IRoundsManager { // Set current round as initialized lastInitializedRound = currRound; // Store block hash for round - bytes32 roundBlockHash = blockHash(blockNum().sub(1)); + bytes32 roundBlockHash = blockHash(blockNum() - 1); _blockHashForRound[currRound] = roundBlockHash; // Set total active stake for the round bondingManager().setCurrentRoundTotalActiveStake(); @@ -148,9 +144,9 @@ contract RoundsManager is ManagerProxyTarget, IRoundsManager { */ function currentRound() public view returns (uint256) { // Compute # of rounds since roundLength was last updated - uint256 roundsSinceUpdate = blockNum().sub(lastRoundLengthUpdateStartBlock).div(roundLength); + uint256 roundsSinceUpdate = (blockNum() - lastRoundLengthUpdateStartBlock) / roundLength; // Current round = round that roundLength was last updated + # of rounds since roundLength was last updated - return lastRoundLengthUpdateRound.add(roundsSinceUpdate); + return lastRoundLengthUpdateRound + roundsSinceUpdate; } /** @@ -158,9 +154,9 @@ contract RoundsManager is ManagerProxyTarget, IRoundsManager { */ function currentRoundStartBlock() public view returns (uint256) { // Compute # of rounds since roundLength was last updated - uint256 roundsSinceUpdate = blockNum().sub(lastRoundLengthUpdateStartBlock).div(roundLength); + uint256 roundsSinceUpdate = (blockNum() - lastRoundLengthUpdateStartBlock) / roundLength; // Current round start block = start block of round that roundLength was last updated + (# of rounds since roundLenght was last updated * roundLength) - return lastRoundLengthUpdateStartBlock.add(roundsSinceUpdate.mul(roundLength)); + return lastRoundLengthUpdateStartBlock + (roundsSinceUpdate * roundLength); } /** @@ -175,7 +171,7 @@ contract RoundsManager is ManagerProxyTarget, IRoundsManager { */ function currentRoundLocked() public view returns (bool) { uint256 lockedBlocks = MathUtils.percOf(roundLength, roundLockAmount); - return blockNum().sub(currentRoundStartBlock()) >= roundLength.sub(lockedBlocks); + return (blockNum() - currentRoundStartBlock()) >= (roundLength - lockedBlocks); } /** diff --git a/contracts/test/TestEarningsPoolLIP36.sol b/contracts/test/TestEarningsPoolLIP36.sol index 2d989b8d..38704b6a 100644 --- a/contracts/test/TestEarningsPoolLIP36.sol +++ b/contracts/test/TestEarningsPoolLIP36.sol @@ -4,11 +4,8 @@ pragma solidity 0.8.9; import "./mocks/EarningsPoolFixture.sol"; import "./helpers/truffle/Assert.sol"; import "../libraries/PreciseMathUtils.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; contract TestEarningsPoolLIP36 { - using SafeMath for uint256; - EarningsPoolFixture fixture; function beforeEach() public { @@ -29,7 +26,7 @@ contract TestEarningsPoolLIP36 { // earningsPool.cumulativeFeeFactor != 0 fixture.updateCumulativeFeeFactor(fees); - expFeeFactor = expFeeFactor.add(PreciseMathUtils.percPoints(fees, fixture.getTotalStake())); + expFeeFactor += PreciseMathUtils.percPoints(fees, fixture.getTotalStake()); Assert.equal(fixture.getCumulativeFeeFactor(), expFeeFactor, "should update cumulativeFeeFactor"); } @@ -44,29 +41,27 @@ contract TestEarningsPoolLIP36 { // earningsPool.cumulativeFeeFactor == 0 fixture.updateCumulativeFeeFactor(fees); - uint256 expFeeFactor = prevFeeFactor.add(PreciseMathUtils.percOf(prevRewFactor, fees, fixture.getTotalStake())); + uint256 expFeeFactor = prevFeeFactor + PreciseMathUtils.percOf(prevRewFactor, fees, fixture.getTotalStake()); Assert.equal(fixture.getCumulativeFeeFactor(), expFeeFactor, "should update cumulativeFeeFactor"); // earningsPool.cumulativeFeeFactor != 0 fixture.updateCumulativeFeeFactor(fees); - expFeeFactor = expFeeFactor.add(PreciseMathUtils.percOf(prevRewFactor, fees, fixture.getTotalStake())); + expFeeFactor += PreciseMathUtils.percOf(prevRewFactor, fees, fixture.getTotalStake()); } function test_updateCumulativeRewardFactor() public { uint256 rewards = 1000; // prevEarningsPool.cumulativeRewardFactor == 0 - uint256 expRewardFactor = PreciseMathUtils.percPoints(1, 1).add( - PreciseMathUtils.percOf(PreciseMathUtils.percPoints(1, 1), rewards, fixture.getTotalStake()) - ); + uint256 expRewardFactor = PreciseMathUtils.percPoints(1, 1) + + PreciseMathUtils.percOf(PreciseMathUtils.percPoints(1, 1), rewards, fixture.getTotalStake()); + fixture.updateCumulativeRewardFactor(1000); Assert.equal(expRewardFactor, fixture.getCumulativeRewardFactor(), "incorrect cumulative reward factor"); // prevEarningsPool.cumulativeRewardFactor != 0 fixture.setPrevPoolEarningsFactors(0, expRewardFactor); - expRewardFactor = expRewardFactor.add( - PreciseMathUtils.percOf(expRewardFactor, rewards, fixture.getTotalStake()) - ); + expRewardFactor += PreciseMathUtils.percOf(expRewardFactor, rewards, fixture.getTotalStake()); fixture.updateCumulativeRewardFactor(1000); Assert.equal(expRewardFactor, fixture.getCumulativeRewardFactor(), "incorrect cumulative reward factor"); } diff --git a/contracts/test/mocks/EarningsPoolFixture.sol b/contracts/test/mocks/EarningsPoolFixture.sol index acbd0e10..a37250f7 100644 --- a/contracts/test/mocks/EarningsPoolFixture.sol +++ b/contracts/test/mocks/EarningsPoolFixture.sol @@ -5,10 +5,7 @@ import "../../libraries/MathUtils.sol"; import "../../bonding/libraries/EarningsPool.sol"; import "../../bonding/libraries/EarningsPoolLIP36.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - contract EarningsPoolFixture { - using SafeMath for uint256; using EarningsPool for EarningsPool.Data; using EarningsPoolLIP36 for EarningsPool.Data; diff --git a/contracts/token/Minter.sol b/contracts/token/Minter.sol index ce86354a..05f9cfea 100644 --- a/contracts/token/Minter.sol +++ b/contracts/token/Minter.sol @@ -8,8 +8,6 @@ import "../rounds/IRoundsManager.sol"; import "../bonding/IBondingManager.sol"; import "../libraries/MathUtilsV2.sol"; -import "@openzeppelin/contracts/utils/math/SafeMath.sol"; - interface IL2LPTDataCache { function l1CirculatingSupply() external view returns (uint256); } @@ -19,8 +17,6 @@ interface IL2LPTDataCache { * @dev Manages inflation rate and the minting of new tokens for each round of the Livepeer protocol */ contract Minter is Manager, IMinter { - using SafeMath for uint256; - // Per round inflation rate uint256 public inflation; // Change in inflation rate per round until the target bonding rate is achieved @@ -152,7 +148,7 @@ contract Minter is Manager, IMinter { // Compute and mint fraction of mintable tokens to include in reward uint256 mintAmount = MathUtils.percOf(currentMintableTokens, _fracNum, _fracDenom); // Update amount of minted tokens for round - currentMintedTokens = currentMintedTokens.add(mintAmount); + currentMintedTokens += mintAmount; // Minted tokens must not exceed mintable tokens require(currentMintedTokens <= currentMintableTokens, "minted tokens cannot exceed mintable tokens"); // Mint new tokens @@ -225,7 +221,7 @@ contract Minter is Manager, IMinter { */ function getGlobalTotalSupply() public view returns (uint256) { // Global total supply = L2 total supply + L1 circulating supply - return livepeerToken().totalSupply().add(l2LPTDataCache().l1CirculatingSupply()); + return livepeerToken().totalSupply() + l2LPTDataCache().l1CirculatingSupply(); } /** @@ -242,13 +238,13 @@ contract Minter is Manager, IMinter { if (currentBondingRate < targetBondingRate) { // Bonding rate is below the target - increase inflation - inflation = inflation.add(inflationChange); + inflation += inflationChange; } else if (currentBondingRate > targetBondingRate) { // Bonding rate is above the target - decrease inflation if (inflationChange > inflation) { inflation = 0; } else { - inflation = inflation.sub(inflationChange); + inflation -= inflationChange; } } }