Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit a3a6281

Browse files
committed
Commit to using safemath
1 parent 8300250 commit a3a6281

File tree

2 files changed

+11
-36
lines changed

2 files changed

+11
-36
lines changed

contracts/SetToken.sol

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pragma solidity 0.4.21;
44
import "zeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
55
import "zeppelin-solidity/contracts/token/ERC20/ERC20.sol";
66
import "zeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol";
7-
import "zeppelin-solidity/contracts/math/SafeMath.sol";
87
import "./external/SafeMathUint256.sol";
98
import "./lib/Set.sol";
109

@@ -15,10 +14,9 @@ import "./lib/Set.sol";
1514
* @dev Implementation of the basic {Set} token.
1615
*/
1716
contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
18-
using SafeMath for uint256;
17+
using SafeMathUint256 for uint256;
1918

2019
uint256 public totalSupply;
21-
2220
address[] public components;
2321
uint[] public units;
2422

@@ -76,8 +74,7 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
7674
// Transfer value is defined as the currentUnits (in GWei)
7775
// multiplied by quantity in Wei divided by the units of gWei.
7876
// We do this to allow fractional units to be defined
79-
// uint transferValue = currentUnits.mul(quantity).div(10**9);
80-
uint transferValue = SafeMathUint256.fxpMul(currentUnits, quantity, 10**9);
77+
uint transferValue = currentUnits.fxpMul(quantity, 10**9);
8178

8279
// Protect against the case that the gWei divisor results in a value that is
8380
// 0 and the user is able to generate Sets without sending a balance
@@ -120,14 +117,16 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
120117
address currentComponent = components[i];
121118
uint currentUnits = units[i];
122119

123-
// The transaction will fail if any of the components fail to transfer
124-
// uint transferValue = currentUnits.mul(quantity).div(10**9);
125-
uint transferValue = SafeMathUint256.fxpMul(currentUnits, quantity, 10**9);
120+
// Transfer value is defined as the currentUnits (in GWei)
121+
// multiplied by quantity in Wei divided by the units of gWei.
122+
// We do this to allow fractional units to be defined
123+
uint transferValue = currentUnits.fxpMul(quantity, 10**9);
126124

127125
// Protect against the case that the gWei divisor results in a value that is
128126
// 0 and the user is able to generate Sets without sending a balance
129127
assert(transferValue > 0);
130128

129+
// The transaction will fail if any of the components fail to transfer
131130
assert(ERC20(currentComponent).transfer(msg.sender, transferValue));
132131
}
133132

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,13 @@
11
pragma solidity 0.4.21;
22

3+
import "zeppelin-solidity/contracts/math/SafeMath.sol";
34

45
/**
56
* @title SafeMathUint256
67
* @dev Uint256 math operations with safety checks that throw on error
78
*/
89
library SafeMathUint256 {
9-
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
10-
if (a == 0) {
11-
return 0;
12-
}
13-
uint256 c = a * b;
14-
assert(c / a == b);
15-
return c;
16-
}
17-
18-
function div(uint256 a, uint256 b) internal pure returns (uint256) {
19-
// assert(b > 0); // Solidity automatically throws when dividing by 0
20-
uint256 c = a / b;
21-
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
22-
return c;
23-
}
24-
25-
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
26-
require(b <= a);
27-
return a - b;
28-
}
29-
30-
function add(uint256 a, uint256 b) internal pure returns (uint256) {
31-
uint256 c = a + b;
32-
require(c >= a);
33-
return c;
34-
}
10+
using SafeMath for uint256;
3511

3612
function min(uint256 a, uint256 b) internal pure returns (uint256) {
3713
if (a <= b) {
@@ -63,10 +39,10 @@ library SafeMathUint256 {
6339

6440
// Float [fixed point] Operations
6541
function fxpMul(uint256 a, uint256 b, uint256 base) internal pure returns (uint256) {
66-
return div(mul(a, b), base);
42+
return a.mul(b).div(base);
6743
}
6844

6945
function fxpDiv(uint256 a, uint256 b, uint256 base) internal pure returns (uint256) {
70-
return div(mul(a, base), b);
46+
return a.mul(base).div(b);
7147
}
7248
}

0 commit comments

Comments
 (0)