git ssb

0+

dangerousbeans / Entropy_contracts



Commit a0851a84040bdc2bc5a6f2067d00bc5974724c5d

safety limit that only guardians can change

Joran committed on 12/8/2016, 4:09:05 AM
Parent: 21d55aad9a330ba13949eae465c0088087cb9c10

Files changed

contracts/Entropy.solchanged
contracts/tokens/EntropyToken.solchanged
test/Entropy_token.jschanged
contracts/Entropy.solView
@@ -31,8 +31,9 @@
3131 // Setup token attributes
3232 name = "Entropy";
3333 decimals = 0;
3434 symbol = "ENT"; //identifier
35 + safety_limit = 300 ether;
3536
3637 // Add the creator as a Citizen and Guardian
3738 totalSupply = 1;
3839 balances[msg.sender] = 1;
@@ -57,9 +58,9 @@
5758 var buyer = msg.sender;
5859 if (value == 0) throw;
5960
6061 // safety cap
61- // if (getTotalValue() + value > SAFETY_LIMIT) throw;
62 + if (totalValue + value > safety_limit) throw;
6263
6364 // 1 Ether === 1 Entropy Token
6465 // Solidity will floor this by default, so sending 1.9 eth will result in
6566 // 1 token
@@ -70,9 +71,17 @@
7071 totalValue += value;
7172 Transfer(this, buyer, value);
7273 }
7374
75 + function changeSafeyLimit(uint _new_limit) onlyGuardians returns (bool success) {
76 + // Limit can only be increased
77 + if(_new_limit < safety_limit) throw;
7478
79 + // Set new safety limit
80 + safety_limit = _new_limit;
81 + SafetyLimitChange(msg.sender, _new_limit);
82 + }
83 +
7584 /**
7685 * Guardians 💂
7786 */
7887
@@ -112,5 +121,8 @@
112121 */
113122
114123 // A new guardian has been elected
115124 event NewGuardian(address indexed _guardian, address indexed _creator);
125 +
126 + // Safety Limit has been increased
127 + event SafetyLimitChange(address indexed _guardian, uint indexed limit);
116128 }
contracts/tokens/EntropyToken.solView
@@ -21,10 +21,10 @@
2121 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.
2222 string public symbol; // identifier
2323 string public version; // human 0.1 standard. Just an arbitrary versioning scheme.
2424 uint public totalValue; // Total value in wei
25 + uint public safety_limit; // Maximum safe amount of ether to hold
2526
26-
2727 /* Approves and then calls the receiving contract */
2828 function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) {
2929 allowed[msg.sender][_spender] = _value;
3030 Approval(msg.sender, _spender, _value);
test/Entropy_token.jsView
@@ -18,9 +18,9 @@
1818 describe("Buying Entropy tokens", () => {
1919 it("Lets you buy Entropy tokens", function(done) {
2020 helpers.deployEntropyContract()
2121 .then((entropy) => {
22- // Buy with 2 Eth worth of value
22 + // Buy with 2 Eth worth of tokens
2323 entropy.buyTokens({ from: accounts[1], value: 2e18 })
2424 .then(() => {
2525 entropy.balanceOf(accounts[1])
2626 .then((balance) => {
@@ -30,12 +30,13 @@
3030 })
3131 })
3232 })
3333 })
34 +
3435 it("handles non integer amounts (should floor)", function(done) {
3536 helpers.deployEntropyContract()
3637 .then((entropy) => {
37- // Buy with 3.5 Eth worth of value
38 + // Buy with 3.5 Eth worth of tokens
3839 entropy.buyTokens({ from: accounts[1], value: 2500000000000000000 })
3940 .then(() => {
4041 entropy.balanceOf(accounts[1])
4142 .then((balance) => {
@@ -46,5 +47,64 @@
4647 })
4748 })
4849 })
4950 })
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 + })
50110 })

Built with git-ssb-web