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

Commit 2e427b0

Browse files
committed
convert to big number
1 parent 0eb4d03 commit 2e427b0

File tree

9 files changed

+8433
-85
lines changed

9 files changed

+8433
-85
lines changed

contracts/SetToken.sol

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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";
77
import "zeppelin-solidity/contracts/math/SafeMath.sol";
8+
import "./external/SafeMathUint256.sol";
89
import "./lib/Set.sol";
910

1011

@@ -68,14 +69,17 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
6869
*/
6970
function issue(uint quantity) public returns (bool success) {
7071
// Transfers the sender's components to the contract
72+
// Since the component length is defined ahead of time, this is not
73+
// an unbounded loop
7174
for (uint i = 0; i < components.length; i++) {
7275
address currentComponent = components[i];
7376
uint currentUnits = units[i];
7477

7578
// Transfer value is defined as the currentUnits (in GWei)
7679
// multiplied by quantity in Wei divided by the units of gWei.
7780
// We do this to allow fractional units to be defined
78-
uint transferValue = currentUnits.mul(quantity).div(10**9);
81+
// uint transferValue = currentUnits.mul(quantity).div(10**9);
82+
uint transferValue = SafeMathUint256.fxpMul(currentUnits, quantity, 10**9);
7983

8084
// Protect against the case that the gWei divisor results in a value that is
8185
// 0 and the user is able to generate Sets without sending a balance
@@ -104,6 +108,8 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
104108
*/
105109
function redeem(uint quantity) public returns (bool success) {
106110
// Check that the sender has sufficient components
111+
// Since the component length is defined ahead of time, this is not
112+
// an unbounded loop
107113
require(balances[msg.sender] >= quantity);
108114

109115
// To prevent re-entrancy attacks, decrement the user's Set balance
@@ -117,7 +123,8 @@ contract SetToken is StandardToken, DetailedERC20("", "", 18), Set {
117123
uint currentUnits = units[i];
118124

119125
// The transaction will fail if any of the components fail to transfer
120-
uint transferValue = currentUnits.mul(quantity).div(10**9);
126+
// uint transferValue = currentUnits.mul(quantity).div(10**9);
127+
uint transferValue = SafeMathUint256.fxpMul(currentUnits, quantity, 10**9);
121128

122129
// Protect against the case that the gWei divisor results in a value that is
123130
// 0 and the user is able to generate Sets without sending a balance
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
pragma solidity 0.4.21;
2+
3+
4+
/**
5+
* @title SafeMathUint256
6+
* @dev Uint256 math operations with safety checks that throw on error
7+
*/
8+
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+
}
35+
36+
function min(uint256 a, uint256 b) internal pure returns (uint256) {
37+
if (a <= b) {
38+
return a;
39+
} else {
40+
return b;
41+
}
42+
}
43+
44+
function max(uint256 a, uint256 b) internal pure returns (uint256) {
45+
if (a >= b) {
46+
return a;
47+
} else {
48+
return b;
49+
}
50+
}
51+
52+
function getUint256Min() internal pure returns (uint256) {
53+
return 0;
54+
}
55+
56+
function getUint256Max() internal pure returns (uint256) {
57+
return 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
58+
}
59+
60+
function isMultipleOf(uint256 a, uint256 b) internal pure returns (bool) {
61+
return a % b == 0;
62+
}
63+
64+
// Float [fixed point] Operations
65+
function fxpMul(uint256 a, uint256 b, uint256 base) internal pure returns (uint256) {
66+
return div(mul(a, b), base);
67+
}
68+
69+
function fxpDiv(uint256 a, uint256 b, uint256 base) internal pure returns (uint256) {
70+
return div(mul(a, base), b);
71+
}
72+
}

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)