Skip to content

Commit

Permalink
removed safeMath (#571)
Browse files Browse the repository at this point in the history
* removed safeMath dependency and updated contracts
* fix: wrong operation in ticketBroker
  • Loading branch information
kautukkundan authored May 25, 2022
1 parent d1e2222 commit 5e7fcf9
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 161 deletions.
101 changes: 48 additions & 53 deletions contracts/bonding/BondingManager.sol

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions contracts/bonding/libraries/EarningsPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions contracts/bonding/libraries/EarningsPoolLIP36.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}
6 changes: 1 addition & 5 deletions contracts/governance/Governor.sol
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 3 additions & 7 deletions contracts/libraries/MathUtils.sol
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
10 changes: 3 additions & 7 deletions contracts/libraries/MathUtilsV2.sol
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
10 changes: 3 additions & 7 deletions contracts/libraries/PreciseMathUtils.sol
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
8 changes: 2 additions & 6 deletions contracts/libraries/SortedDoublyLL.sol
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -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
Expand Down Expand Up @@ -99,7 +95,7 @@ library SortedDoublyLL {
self.nodes[nextId].prevId = _id;
}

self.size = self.size.add(1);
self.size += 1;
}

/**
Expand Down Expand Up @@ -139,7 +135,7 @@ library SortedDoublyLL {
}

delete self.nodes[_id];
self.size = self.size.sub(1);
self.size -= 1;
}

/**
Expand Down
17 changes: 6 additions & 11 deletions contracts/pm/mixins/MixinReserve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
19 changes: 8 additions & 11 deletions contracts/pm/mixins/MixinTicketBrokerCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
);

Expand All @@ -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);
}
Expand Down Expand Up @@ -136,17 +133,17 @@ 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 {
// If ticket face value <= sender's deposit then only deduct
// from sender's deposit

amountToTransfer = _ticket.faceValue;
sender.deposit = sender.deposit.sub(_ticket.faceValue);
sender.deposit -= _ticket.faceValue;
}

if (amountToTransfer > 0) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 1 addition & 4 deletions contracts/pm/mixins/MixinTicketProcessor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

/**
Expand Down
Loading

0 comments on commit 5e7fcf9

Please sign in to comment.