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

Commit edb9a0c

Browse files
authored
Merge pull request #14 from SetProtocol/0.2.0-big-number
0.2.0 big number
2 parents 0eb4d03 + a3a6281 commit edb9a0c

File tree

9 files changed

+8413
-127
lines changed

9 files changed

+8413
-127
lines changed

contracts/SetToken.sol

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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";
7+
import "./external/SafeMathUint256.sol";
88
import "./lib/Set.sol";
99

1010

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

1919
uint256 public totalSupply;
20-
2120
address[] public components;
2221
uint[] public units;
2322

@@ -26,7 +25,7 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
2625
* @param _components address[] A list of component address which you want to include
2726
* @param _units uint[] A list of quantities in gWei of each component (corresponds to the {Set} of _components)
2827
*/
29-
function SetToken(address[] _components, uint[] _units, string _name, string _symbol) public {
28+
function SetToken(address[] _components, uint[] _units) public {
3029
// There must be component present
3130
require(_components.length > 0);
3231

@@ -54,8 +53,6 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
5453

5554
components = _components;
5655
units = _units;
57-
name = _name;
58-
symbol = _symbol;
5956
}
6057

6158
/**
@@ -68,14 +65,16 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
6865
*/
6966
function issue(uint quantity) public returns (bool success) {
7067
// Transfers the sender's components to the contract
68+
// Since the component length is defined ahead of time, this is not
69+
// an unbounded loop
7170
for (uint i = 0; i < components.length; i++) {
7271
address currentComponent = components[i];
7372
uint currentUnits = units[i];
7473

7574
// Transfer value is defined as the currentUnits (in GWei)
7675
// multiplied by quantity in Wei divided by the units of gWei.
7776
// We do this to allow fractional units to be defined
78-
uint transferValue = currentUnits.mul(quantity).div(10**9);
77+
uint transferValue = currentUnits.fxpMul(quantity, 10**9);
7978

8079
// Protect against the case that the gWei divisor results in a value that is
8180
// 0 and the user is able to generate Sets without sending a balance
@@ -104,6 +103,8 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
104103
*/
105104
function redeem(uint quantity) public returns (bool success) {
106105
// Check that the sender has sufficient components
106+
// Since the component length is defined ahead of time, this is not
107+
// an unbounded loop
107108
require(balances[msg.sender] >= quantity);
108109

109110
// To prevent re-entrancy attacks, decrement the user's Set balance
@@ -116,13 +117,16 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
116117
address currentComponent = components[i];
117118
uint currentUnits = units[i];
118119

119-
// The transaction will fail if any of the components fail to transfer
120-
uint transferValue = currentUnits.mul(quantity).div(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);
121124

122125
// Protect against the case that the gWei divisor results in a value that is
123126
// 0 and the user is able to generate Sets without sending a balance
124127
assert(transferValue > 0);
125128

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
pragma solidity 0.4.21;
2+
3+
import "zeppelin-solidity/contracts/math/SafeMath.sol";
4+
5+
/**
6+
* @title SafeMathUint256
7+
* @dev Uint256 math operations with safety checks that throw on error
8+
*/
9+
library SafeMathUint256 {
10+
using SafeMath for uint256;
11+
12+
function min(uint256 a, uint256 b) internal pure returns (uint256) {
13+
if (a <= b) {
14+
return a;
15+
} else {
16+
return b;
17+
}
18+
}
19+
20+
function max(uint256 a, uint256 b) internal pure returns (uint256) {
21+
if (a >= b) {
22+
return a;
23+
} else {
24+
return b;
25+
}
26+
}
27+
28+
function getUint256Min() internal pure returns (uint256) {
29+
return 0;
30+
}
31+
32+
function getUint256Max() internal pure returns (uint256) {
33+
return 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
34+
}
35+
36+
function isMultipleOf(uint256 a, uint256 b) internal pure returns (bool) {
37+
return a % b == 0;
38+
}
39+
40+
// Float [fixed point] Operations
41+
function fxpMul(uint256 a, uint256 b, uint256 base) internal pure returns (uint256) {
42+
return a.mul(b).div(base);
43+
}
44+
45+
function fxpDiv(uint256 a, uint256 b, uint256 base) internal pure returns (uint256) {
46+
return a.mul(base).div(b);
47+
}
48+
}

diagrams/SetIssuance.png

-38.5 KB
Binary file not shown.

diagrams/SetRedemption.png

-19.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)