Skip to content

Commit 5e7fcf9

Browse files
authored
removed safeMath (#571)
* removed safeMath dependency and updated contracts * fix: wrong operation in ticketBroker
1 parent d1e2222 commit 5e7fcf9

15 files changed

+101
-161
lines changed

Diff for: contracts/bonding/BondingManager.sol

+48-53
Large diffs are not rendered by default.

Diff for: contracts/bonding/libraries/EarningsPool.sol

-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ pragma solidity 0.8.9;
33

44
import "../../libraries/MathUtils.sol";
55

6-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
7-
86
/**
97
* @title EarningsPool
108
* @dev Manages reward and fee pools for delegators and transcoders
119
*/
1210
library EarningsPool {
13-
using SafeMath for uint256;
14-
1511
struct Data {
1612
uint256 totalStake; // Transcoder's total stake during the earnings pool's round
1713
uint256 transcoderRewardCut; // Transcoder's reward cut during the earnings pool's round

Diff for: contracts/bonding/libraries/EarningsPoolLIP36.sol

+9-13
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ pragma solidity 0.8.9;
44
import "./EarningsPool.sol";
55
import "../../libraries/PreciseMathUtils.sol";
66

7-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
8-
97
library EarningsPoolLIP36 {
10-
using SafeMath for uint256;
11-
128
/**
139
* @notice Update the cumulative fee factor stored in an earnings pool with new fees
1410
* @param earningsPool Storage pointer to EarningsPools.Data struct
@@ -27,15 +23,15 @@ library EarningsPoolLIP36 {
2723

2824
// Initialize the cumulativeFeeFactor when adding fees for the first time
2925
if (earningsPool.cumulativeFeeFactor == 0) {
30-
earningsPool.cumulativeFeeFactor = prevCumulativeFeeFactor.add(
31-
PreciseMathUtils.percOf(prevCumulativeRewardFactor, _fees, earningsPool.totalStake)
32-
);
26+
earningsPool.cumulativeFeeFactor =
27+
prevCumulativeFeeFactor +
28+
PreciseMathUtils.percOf(prevCumulativeRewardFactor, _fees, earningsPool.totalStake);
3329
return;
3430
}
3531

36-
earningsPool.cumulativeFeeFactor = earningsPool.cumulativeFeeFactor.add(
37-
PreciseMathUtils.percOf(prevCumulativeRewardFactor, _fees, earningsPool.totalStake)
38-
);
32+
earningsPool.cumulativeFeeFactor =
33+
earningsPool.cumulativeFeeFactor +
34+
PreciseMathUtils.percOf(prevCumulativeRewardFactor, _fees, earningsPool.totalStake);
3935
}
4036

4137
/**
@@ -53,8 +49,8 @@ library EarningsPoolLIP36 {
5349
? _prevEarningsPool.cumulativeRewardFactor
5450
: PreciseMathUtils.percPoints(1, 1);
5551

56-
earningsPool.cumulativeRewardFactor = prevCumulativeRewardFactor.add(
57-
PreciseMathUtils.percOf(prevCumulativeRewardFactor, _rewards, earningsPool.totalStake)
58-
);
52+
earningsPool.cumulativeRewardFactor =
53+
prevCumulativeRewardFactor +
54+
PreciseMathUtils.percOf(prevCumulativeRewardFactor, _rewards, earningsPool.totalStake);
5955
}
6056
}

Diff for: contracts/governance/Governor.sol

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.9;
33

4-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
5-
64
/**
75
* @title Governor
86
* @dev The Governor holds the rights to stage and execute contract calls i.e. changing Livepeer protocol parameters.
97
*/
108
contract Governor {
11-
using SafeMath for uint256;
12-
139
address public owner;
1410

1511
/// @dev mapping of updateHash (keccak256(update) => executeBlock (block.number + delay)
@@ -68,7 +64,7 @@ contract Governor {
6864

6965
require(updates[updateHash] == 0, "update already staged");
7066

71-
updates[updateHash] = block.number.add(_delay);
67+
updates[updateHash] = block.number + _delay;
7268

7369
emit UpdateStaged(_update, _delay);
7470
}

Diff for: contracts/libraries/MathUtils.sol

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.9;
33

4-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
5-
64
library MathUtils {
7-
using SafeMath for uint256;
8-
95
// Divisor used for representing percentages
106
uint256 public constant PERC_DIVISOR = 1000000;
117

@@ -28,7 +24,7 @@ library MathUtils {
2824
uint256 _fracNum,
2925
uint256 _fracDenom
3026
) internal pure returns (uint256) {
31-
return _amount.mul(percPoints(_fracNum, _fracDenom)).div(PERC_DIVISOR);
27+
return (_amount * percPoints(_fracNum, _fracDenom)) / PERC_DIVISOR;
3228
}
3329

3430
/**
@@ -37,7 +33,7 @@ library MathUtils {
3733
* @param _fracNum Numerator of fraction representing the percentage with PERC_DIVISOR as the denominator
3834
*/
3935
function percOf(uint256 _amount, uint256 _fracNum) internal pure returns (uint256) {
40-
return _amount.mul(_fracNum).div(PERC_DIVISOR);
36+
return (_amount * _fracNum) / PERC_DIVISOR;
4137
}
4238

4339
/**
@@ -46,6 +42,6 @@ library MathUtils {
4642
* @param _fracDenom Denominator of fraction represeting the percentage
4743
*/
4844
function percPoints(uint256 _fracNum, uint256 _fracDenom) internal pure returns (uint256) {
49-
return _fracNum.mul(PERC_DIVISOR).div(_fracDenom);
45+
return (_fracNum * PERC_DIVISOR) / _fracDenom;
5046
}
5147
}

Diff for: contracts/libraries/MathUtilsV2.sol

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.9;
33

4-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
5-
64
library MathUtils {
7-
using SafeMath for uint256;
8-
95
// Divisor used for representing percentages
106
uint256 public constant PERC_DIVISOR = 1000000000;
117

@@ -28,7 +24,7 @@ library MathUtils {
2824
uint256 _fracNum,
2925
uint256 _fracDenom
3026
) internal pure returns (uint256) {
31-
return _amount.mul(percPoints(_fracNum, _fracDenom)).div(PERC_DIVISOR);
27+
return (_amount * percPoints(_fracNum, _fracDenom)) / PERC_DIVISOR;
3228
}
3329

3430
/**
@@ -37,7 +33,7 @@ library MathUtils {
3733
* @param _fracNum Numerator of fraction representing the percentage with PERC_DIVISOR as the denominator
3834
*/
3935
function percOf(uint256 _amount, uint256 _fracNum) internal pure returns (uint256) {
40-
return _amount.mul(_fracNum).div(PERC_DIVISOR);
36+
return (_amount * _fracNum) / PERC_DIVISOR;
4137
}
4238

4339
/**
@@ -46,6 +42,6 @@ library MathUtils {
4642
* @param _fracDenom Denominator of fraction represeting the percentage
4743
*/
4844
function percPoints(uint256 _fracNum, uint256 _fracDenom) internal pure returns (uint256) {
49-
return _fracNum.mul(PERC_DIVISOR).div(_fracDenom);
45+
return (_fracNum * PERC_DIVISOR) / _fracDenom;
5046
}
5147
}

Diff for: contracts/libraries/PreciseMathUtils.sol

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.9;
33

4-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
5-
64
library PreciseMathUtils {
7-
using SafeMath for uint256;
8-
95
// Divisor used for representing percentages
106
uint256 public constant PERC_DIVISOR = 10**27;
117

@@ -28,7 +24,7 @@ library PreciseMathUtils {
2824
uint256 _fracNum,
2925
uint256 _fracDenom
3026
) internal pure returns (uint256) {
31-
return _amount.mul(percPoints(_fracNum, _fracDenom)).div(PERC_DIVISOR);
27+
return (_amount * percPoints(_fracNum, _fracDenom)) / PERC_DIVISOR;
3228
}
3329

3430
/**
@@ -37,7 +33,7 @@ library PreciseMathUtils {
3733
* @param _fracNum Numerator of fraction representing the percentage with PERC_DIVISOR as the denominator
3834
*/
3935
function percOf(uint256 _amount, uint256 _fracNum) internal pure returns (uint256) {
40-
return _amount.mul(_fracNum).div(PERC_DIVISOR);
36+
return (_amount * _fracNum) / PERC_DIVISOR;
4137
}
4238

4339
/**
@@ -46,6 +42,6 @@ library PreciseMathUtils {
4642
* @param _fracDenom Denominator of fraction represeting the percentage
4743
*/
4844
function percPoints(uint256 _fracNum, uint256 _fracDenom) internal pure returns (uint256) {
49-
return _fracNum.mul(PERC_DIVISOR).div(_fracDenom);
45+
return (_fracNum * PERC_DIVISOR) / _fracDenom;
5046
}
5147
}

Diff for: contracts/libraries/SortedDoublyLL.sol

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.9;
33

4-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
5-
64
/**
75
* @title A sorted doubly linked list with nodes sorted in descending order. Optionally accepts insert position hints
86
*
@@ -15,8 +13,6 @@ import "@openzeppelin/contracts/utils/math/SafeMath.sol";
1513
* to find the appropriate insert position.
1614
*/
1715
library SortedDoublyLL {
18-
using SafeMath for uint256;
19-
2016
// Information for a node in the list
2117
struct Node {
2218
uint256 key; // Node's key used for sorting
@@ -99,7 +95,7 @@ library SortedDoublyLL {
9995
self.nodes[nextId].prevId = _id;
10096
}
10197

102-
self.size = self.size.add(1);
98+
self.size += 1;
10399
}
104100

105101
/**
@@ -139,7 +135,7 @@ library SortedDoublyLL {
139135
}
140136

141137
delete self.nodes[_id];
142-
self.size = self.size.sub(1);
138+
self.size -= 1;
143139
}
144140

145141
/**

Diff for: contracts/pm/mixins/MixinReserve.sol

+6-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ pragma solidity 0.8.9;
33

44
import "./interfaces/MReserve.sol";
55
import "./MixinContractRegistry.sol";
6-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
76

87
abstract contract MixinReserve is MixinContractRegistry, MReserve {
9-
using SafeMath for uint256;
10-
118
struct Reserve {
129
uint256 funds; // Amount of funds in the reserve
1310
mapping(uint256 => uint256) claimedForRound; // Mapping of round => total amount claimed
@@ -48,8 +45,8 @@ abstract contract MixinReserve is MixinContractRegistry, MReserve {
4845
}
4946

5047
// Total claimable funds = remaining funds + amount claimed for the round
51-
uint256 totalClaimable = reserve.funds.add(reserve.claimedForRound[currentRound]);
52-
return totalClaimable.div(poolSize).sub(reserve.claimedByAddress[currentRound][_claimant]);
48+
uint256 totalClaimable = reserve.funds + reserve.claimedForRound[currentRound];
49+
return (totalClaimable / poolSize) - reserve.claimedByAddress[currentRound][_claimant];
5350
}
5451

5552
/**
@@ -70,7 +67,7 @@ abstract contract MixinReserve is MixinContractRegistry, MReserve {
7067
* @param _amount Amount of funds to add to reserve
7168
*/
7269
function addReserve(address _reserveHolder, uint256 _amount) internal override {
73-
reserves[_reserveHolder].funds = reserves[_reserveHolder].funds.add(_amount);
70+
reserves[_reserveHolder].funds += _amount;
7471

7572
emit ReserveFunded(_reserveHolder, _amount);
7673
}
@@ -117,13 +114,11 @@ abstract contract MixinReserve is MixinContractRegistry, MReserve {
117114
uint256 currentRound = roundsManager().currentRound();
118115
Reserve storage reserve = reserves[_reserveHolder];
119116
// Increase total amount claimed for the round
120-
reserve.claimedForRound[currentRound] = reserve.claimedForRound[currentRound].add(claimAmount);
117+
reserve.claimedForRound[currentRound] += claimAmount;
121118
// Increase amount claimed by claimant for the round
122-
reserve.claimedByAddress[currentRound][_claimant] = reserve.claimedByAddress[currentRound][_claimant].add(
123-
claimAmount
124-
);
119+
reserve.claimedByAddress[currentRound][_claimant] += claimAmount;
125120
// Decrease remaining reserve
126-
reserve.funds = reserve.funds.sub(claimAmount);
121+
reserve.funds -= claimAmount;
127122

128123
emit ReserveClaimed(_reserveHolder, _claimant, claimAmount);
129124
}

Diff for: contracts/pm/mixins/MixinTicketBrokerCore.sol

+8-11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ import "./interfaces/MTicketProcessor.sol";
66
import "./interfaces/MTicketBrokerCore.sol";
77
import "./MixinContractRegistry.sol";
88
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
9-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
109

1110
abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTicketProcessor, MTicketBrokerCore {
12-
using SafeMath for uint256;
13-
1411
struct Sender {
1512
uint256 deposit; // Amount of funds deposited
1613
uint256 withdrawRound; // Round that sender can withdraw deposit & reserve
@@ -28,7 +25,7 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic
2825
// Checks if msg.value is equal to the given deposit and reserve amounts
2926
modifier checkDepositReserveETHValueSplit(uint256 _depositAmount, uint256 _reserveAmount) {
3027
require(
31-
msg.value == _depositAmount.add(_reserveAmount),
28+
msg.value == _depositAmount + _reserveAmount,
3229
"msg.value does not equal sum of deposit amount and reserve amount"
3330
);
3431

@@ -38,7 +35,7 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic
3835
// Process deposit funding
3936
modifier processDeposit(address _sender, uint256 _amount) {
4037
Sender storage sender = senders[_sender];
41-
sender.deposit = sender.deposit.add(_amount);
38+
sender.deposit += _amount;
4239
if (_isUnlockInProgress(sender)) {
4340
_cancelUnlock(sender, _sender);
4441
}
@@ -136,17 +133,17 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic
136133
// If ticket face value > sender's deposit then claim from
137134
// the sender's reserve
138135

139-
amountToTransfer = sender.deposit.add(
140-
claimFromReserve(_ticket.sender, _ticket.recipient, _ticket.faceValue.sub(sender.deposit))
141-
);
136+
amountToTransfer =
137+
sender.deposit +
138+
claimFromReserve(_ticket.sender, _ticket.recipient, _ticket.faceValue - sender.deposit);
142139

143140
sender.deposit = 0;
144141
} else {
145142
// If ticket face value <= sender's deposit then only deduct
146143
// from sender's deposit
147144

148145
amountToTransfer = _ticket.faceValue;
149-
sender.deposit = sender.deposit.sub(_ticket.faceValue);
146+
sender.deposit -= _ticket.faceValue;
150147
}
151148

152149
if (amountToTransfer > 0) {
@@ -176,7 +173,7 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic
176173
require(!_isUnlockInProgress(sender), "unlock already initiated");
177174

178175
uint256 currentRound = roundsManager().currentRound();
179-
sender.withdrawRound = currentRound.add(unlockPeriod);
176+
sender.withdrawRound = currentRound + unlockPeriod;
180177

181178
emit Unlock(msg.sender, currentRound, sender.withdrawRound);
182179
}
@@ -206,7 +203,7 @@ abstract contract MixinTicketBrokerCore is MixinContractRegistry, MReserve, MTic
206203
sender.deposit = 0;
207204
clearReserve(msg.sender);
208205

209-
withdrawTransfer(payable(msg.sender), deposit.add(reserve));
206+
withdrawTransfer(payable(msg.sender), deposit + reserve);
210207

211208
emit Withdrawal(msg.sender, deposit, reserve);
212209
}

Diff for: contracts/pm/mixins/MixinTicketProcessor.sol

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ pragma solidity 0.8.9;
33

44
import "./interfaces/MTicketProcessor.sol";
55
import "./MixinContractRegistry.sol";
6-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
76

87
abstract contract MixinTicketProcessor is MixinContractRegistry, MTicketProcessor {
9-
using SafeMath for uint256;
10-
118
// Number of rounds that a ticket is valid for starting from
129
// its creationRound
1310
uint256 public ticketValidityPeriod;
@@ -61,7 +58,7 @@ abstract contract MixinTicketProcessor is MixinContractRegistry, MTicketProcesso
6158

6259
uint256 currRound = roundsManager().currentRound();
6360

64-
require(creationRound.add(ticketValidityPeriod) > currRound, "ticket is expired");
61+
require(creationRound + ticketValidityPeriod > currRound, "ticket is expired");
6562
}
6663

6764
/**

0 commit comments

Comments
 (0)