Commit a0851a84040bdc2bc5a6f2067d00bc5974724c5d
safety limit that only guardians can change
Joran committed on 12/8/2016, 4:09:05 AMParent: 21d55aad9a330ba13949eae465c0088087cb9c10
Files changed
contracts/Entropy.sol | changed |
contracts/tokens/EntropyToken.sol | changed |
test/Entropy_token.js | changed |
contracts/Entropy.sol | ||
---|---|---|
@@ -31,8 +31,9 @@ | ||
31 | 31 … | // Setup token attributes |
32 | 32 … | name = "Entropy"; |
33 | 33 … | decimals = 0; |
34 | 34 … | symbol = "ENT"; //identifier |
35 … | + safety_limit = 300 ether; | |
35 | 36 … | |
36 | 37 … | // Add the creator as a Citizen and Guardian |
37 | 38 … | totalSupply = 1; |
38 | 39 … | balances[msg.sender] = 1; |
@@ -57,9 +58,9 @@ | ||
57 | 58 … | var buyer = msg.sender; |
58 | 59 … | if (value == 0) throw; |
59 | 60 … | |
60 | 61 … | // safety cap |
61 | - // if (getTotalValue() + value > SAFETY_LIMIT) throw; | |
62 … | + if (totalValue + value > safety_limit) throw; | |
62 | 63 … | |
63 | 64 … | // 1 Ether === 1 Entropy Token |
64 | 65 … | // Solidity will floor this by default, so sending 1.9 eth will result in |
65 | 66 … | // 1 token |
@@ -70,9 +71,17 @@ | ||
70 | 71 … | totalValue += value; |
71 | 72 … | Transfer(this, buyer, value); |
72 | 73 … | } |
73 | 74 … | |
75 … | + function changeSafeyLimit(uint _new_limit) onlyGuardians returns (bool success) { | |
76 … | + // Limit can only be increased | |
77 … | + if(_new_limit < safety_limit) throw; | |
74 | 78 … | |
79 … | + // Set new safety limit | |
80 … | + safety_limit = _new_limit; | |
81 … | + SafetyLimitChange(msg.sender, _new_limit); | |
82 … | + } | |
83 … | + | |
75 | 84 … | /** |
76 | 85 … | * Guardians 💂 |
77 | 86 … | */ |
78 | 87 … | |
@@ -112,5 +121,8 @@ | ||
112 | 121 … | */ |
113 | 122 … | |
114 | 123 … | // A new guardian has been elected |
115 | 124 … | event NewGuardian(address indexed _guardian, address indexed _creator); |
125 … | + | |
126 … | + // Safety Limit has been increased | |
127 … | + event SafetyLimitChange(address indexed _guardian, uint indexed limit); | |
116 | 128 … | } |
contracts/tokens/EntropyToken.sol | ||
---|---|---|
@@ -21,10 +21,10 @@ | ||
21 | 21 … | uint8 public decimals; // How many decimals to show. ie. There could 1000 base units with 3 decimals. Meaning 0.980 SBX = 980 base units. It's like comparing 1 wei to 1 ether. |
22 | 22 … | string public symbol; // identifier |
23 | 23 … | string public version; // human 0.1 standard. Just an arbitrary versioning scheme. |
24 | 24 … | uint public totalValue; // Total value in wei |
25 … | + uint public safety_limit; // Maximum safe amount of ether to hold | |
25 | 26 … | |
26 | - | |
27 | 27 … | /* Approves and then calls the receiving contract */ |
28 | 28 … | function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) { |
29 | 29 … | allowed[msg.sender][_spender] = _value; |
30 | 30 … | Approval(msg.sender, _spender, _value); |
test/Entropy_token.js | ||
---|---|---|
@@ -18,9 +18,9 @@ | ||
18 | 18 … | describe("Buying Entropy tokens", () => { |
19 | 19 … | it("Lets you buy Entropy tokens", function(done) { |
20 | 20 … | helpers.deployEntropyContract() |
21 | 21 … | .then((entropy) => { |
22 | - // Buy with 2 Eth worth of value | |
22 … | + // Buy with 2 Eth worth of tokens | |
23 | 23 … | entropy.buyTokens({ from: accounts[1], value: 2e18 }) |
24 | 24 … | .then(() => { |
25 | 25 … | entropy.balanceOf(accounts[1]) |
26 | 26 … | .then((balance) => { |
@@ -30,12 +30,13 @@ | ||
30 | 30 … | }) |
31 | 31 … | }) |
32 | 32 … | }) |
33 | 33 … | }) |
34 … | + | |
34 | 35 … | it("handles non integer amounts (should floor)", function(done) { |
35 | 36 … | helpers.deployEntropyContract() |
36 | 37 … | .then((entropy) => { |
37 | - // Buy with 3.5 Eth worth of value | |
38 … | + // Buy with 3.5 Eth worth of tokens | |
38 | 39 … | entropy.buyTokens({ from: accounts[1], value: 2500000000000000000 }) |
39 | 40 … | .then(() => { |
40 | 41 … | entropy.balanceOf(accounts[1]) |
41 | 42 … | .then((balance) => { |
@@ -46,5 +47,64 @@ | ||
46 | 47 … | }) |
47 | 48 … | }) |
48 | 49 … | }) |
49 | 50 … | }) |
51 … | + | |
52 … | + describe("Safety Limit", () => { | |
53 … | + | |
54 … | + it("is ok near the safety limit", function(done) { | |
55 … | + helpers.deployEntropyContract() | |
56 … | + .then((entropy) => { | |
57 … | + // Send 299 Eth | |
58 … | + entropy.buyTokens({ from: accounts[0], value: 299e18 }) | |
59 … | + .then(() => { | |
60 … | + // Buy with 1 Eth worth of tokens | |
61 … | + entropy.buyTokens({ from: accounts[1], value: 1e18 }) | |
62 … | + .then(() => { | |
63 … | + entropy.balanceOf(accounts[1]) | |
64 … | + .then((balance) => { | |
65 … | + // Should have 2 tokens | |
66 … | + assert.equal(balance.valueOf(), 1); | |
67 … | + done(); | |
68 … | + }) | |
69 … | + }) | |
70 … | + }) | |
71 … | + }) | |
72 … | + }) | |
73 … | + | |
74 … | + it("wont go over safety limit", function(done) { | |
75 … | + helpers.deployEntropyContract() | |
76 … | + .then((entropy) => { | |
77 … | + // Send 300 Eth | |
78 … | + entropy.buyTokens({ from: accounts[0], value: 300e18 }) | |
79 … | + .then(() => { | |
80 … | + // Buy with 1 Eth worth of tokens - should throw | |
81 … | + return helpers.expectedExceptionPromise(function () { | |
82 … | + return entropy.buyTokens({ from: accounts[1], gas: 3000000 }); | |
83 … | + }, 3000000); | |
84 … | + }) | |
85 … | + .then(() => { | |
86 … | + done(); | |
87 … | + }) | |
88 … | + }) | |
89 … | + }) | |
90 … | + | |
91 … | + it("lets guardians change safety limit", () => { | |
92 … | + helpers.deployEntropyContract() | |
93 … | + .then((entropy) => { | |
94 … | + entropy.changeSafeyLimit(500e18) | |
95 … | + .then(() => { | |
96 … | + // Buy with 399 Eth worth of tokens | |
97 … | + entropy.buyTokens({ value: 399e18 }) | |
98 … | + .then(() => { | |
99 … | + entropy.balanceOf(accounts[0]) | |
100 … | + .then((balance) => { | |
101 … | + // Should have 2 tokens | |
102 … | + assert.equal(balance.valueOf(), 400); | |
103 … | + done(); | |
104 … | + }) | |
105 … | + }) | |
106 … | + }) | |
107 … | + }) | |
108 … | + }) | |
109 … | + }) | |
50 | 110 … | }) |
Built with git-ssb-web