From 8e0e8902a3616779f126b4c68f33ce2804653269 Mon Sep 17 00:00:00 2001 From: alari Date: Tue, 8 Aug 2017 22:35:10 +0300 Subject: [PATCH] tiny fixes --- .gitignore | 3 +- contracts/ConvertLib.sol | 8 ---- contracts/FluencePreSale.sol | 80 +++++++++++++----------------------- contracts/MetaCoin.sol | 34 --------------- test/TestFluencePreSale.sol | 2 +- test/TestMetacoin.sol | 25 ----------- test/metacoin.js | 3 +- 7 files changed, 33 insertions(+), 122 deletions(-) delete mode 100644 contracts/ConvertLib.sol delete mode 100644 contracts/MetaCoin.sol delete mode 100644 test/TestMetacoin.sol diff --git a/.gitignore b/.gitignore index caeb80a..4702569 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /.idea *.iml /node_modules -1501* \ No newline at end of file +1501* +tmp \ No newline at end of file diff --git a/contracts/ConvertLib.sol b/contracts/ConvertLib.sol deleted file mode 100644 index b97c88d..0000000 --- a/contracts/ConvertLib.sol +++ /dev/null @@ -1,8 +0,0 @@ -pragma solidity ^0.4.4; - -library ConvertLib{ - function convert(uint amount,uint conversionRate) returns (uint convertedAmount) - { - return amount * conversionRate; - } -} diff --git a/contracts/FluencePreSale.sol b/contracts/FluencePreSale.sol index cee8fe7..77b934f 100644 --- a/contracts/FluencePreSale.sol +++ b/contracts/FluencePreSale.sol @@ -1,50 +1,26 @@ pragma solidity ^0.4.13; -/** - * Math operations with safety checks - */ -library SafeMath { - function mul(uint256 a, uint256 b) internal returns (uint256) { - uint256 c = a * b; - assert(a == 0 || c / a == b); - return c; +/* taking ideas from FirstBlood token */ +contract SafeMath { + + function safeAdd(uint256 x, uint256 y) internal returns(uint256) { + uint256 z = x + y; + assert((z >= x) && (z >= y)); + return z; } - function div(uint256 a, uint256 b) internal returns (uint256) { - // assert(b > 0); // Solidity automatically throws when dividing by 0 - uint256 c = a / b; - // assert(a == b * c + a % b); // There is no case in which this doesn't hold - return c; + function safeSubtract(uint256 x, uint256 y) internal returns(uint256) { + assert(x >= y); + uint256 z = x - y; + return z; } - function sub(uint256 a, uint256 b) internal returns (uint256) { - assert(b <= a); - return a - b; + function safeMult(uint256 x, uint256 y) internal returns(uint256) { + uint256 z = x * y; + assert((x == 0)||(z/x == y)); + return z; } - - function add(uint256 a, uint256 b) internal returns (uint256) { - uint256 c = a + b; - assert(c >= a); - return c; - } - - function max64(uint64 a, uint64 b) internal constant returns (uint64) { - return a >= b ? a : b; - } - - function min64(uint64 a, uint64 b) internal constant returns (uint64) { - return a < b ? a : b; - } - - function max256(uint256 a, uint256 b) internal constant returns (uint256) { - return a >= b ? a : b; - } - - function min256(uint256 a, uint256 b) internal constant returns (uint256) { - return a < b ? a : b; - } - } @@ -110,9 +86,7 @@ contract Haltable is Ownable { } -contract FluencePreSale is Haltable { - using SafeMath for uint; - using SafeMath for uint256; +contract FluencePreSale is Haltable, SafeMath { mapping (address => uint256) public balanceOf; @@ -193,7 +167,7 @@ contract FluencePreSale is Haltable { } function setBeneficiary(address to) onlyOwner external { - require(to != 0x0); + require(to != address(0)); beneficiary = to; } @@ -208,20 +182,22 @@ contract FluencePreSale is Haltable { uint256 tokensToIssue; if(msg.value >= expertThreshold) { - tokensToIssue = (msg.value / 1 ether).mul(expertTokensPerEth); + tokensToIssue = safeMult(msg.value, expertTokensPerEth); } else if(msg.value >= advancedThreshold) { - tokensToIssue = (msg.value / 1 ether).mul(advancedTokensPerEth); + tokensToIssue = safeMult(msg.value, advancedTokensPerEth); } else { - tokensToIssue = (msg.value / 1 ether).mul(basicTokensPerEth); + tokensToIssue = safeMult(msg.value, basicTokensPerEth); } - totalSupply = totalSupply.add(tokensToIssue); + assert(tokensToIssue > 0); + + totalSupply = safeAdd(totalSupply, tokensToIssue); require(totalSupply <= SUPPLY_LIMIT); - etherContributions[_address] = etherContributions[_address].add(msg.value); + etherContributions[_address] = safeAdd(etherContributions[_address], msg.value); uint collectedBefore = etherCollected; - etherCollected = etherCollected.add(msg.value); - balanceOf[_address] = balanceOf[_address].add(tokensToIssue); + etherCollected = safeAdd(etherCollected, msg.value); + balanceOf[_address] = safeAdd(balanceOf[_address], tokensToIssue); NewContribution(_address, tokensToIssue, msg.value); @@ -250,8 +226,8 @@ contract FluencePreSale is Haltable { etherContributions[msg.sender] = 0; // Clear state // Reduce counters - etherCollected = etherCollected.sub(amount); - totalSupply = totalSupply.sub(balanceOf[msg.sender]); + etherCollected = safeSubtract(etherCollected, amount); + totalSupply = safeSubtract(totalSupply, tokensToBurn); msg.sender.transfer(amount); // Process refund. In case of error, it will be thrown diff --git a/contracts/MetaCoin.sol b/contracts/MetaCoin.sol deleted file mode 100644 index fa570e9..0000000 --- a/contracts/MetaCoin.sol +++ /dev/null @@ -1,34 +0,0 @@ -pragma solidity ^0.4.4; - -import "./ConvertLib.sol"; - -// This is just a simple example of a coin-like contract. -// It is not standards compatible and cannot be expected to talk to other -// coin/token contracts. If you want to create a standards-compliant -// token, see: https://github.com/ConsenSys/Tokens. Cheers! - -contract MetaCoin { - mapping (address => uint) balances; - - event Transfer(address indexed _from, address indexed _to, uint256 _value); - - function MetaCoin() { - balances[tx.origin] = 10000; - } - - function sendCoin(address receiver, uint amount) returns(bool sufficient) { - if (balances[msg.sender] < amount) return false; - balances[msg.sender] -= amount; - balances[receiver] += amount; - Transfer(msg.sender, receiver, amount); - return true; - } - - function getBalanceInEth(address addr) returns(uint){ - return ConvertLib.convert(getBalance(addr),2); - } - - function getBalance(address addr) returns(uint) { - return balances[addr]; - } -} diff --git a/test/TestFluencePreSale.sol b/test/TestFluencePreSale.sol index 93e572d..e8ba6c8 100644 --- a/test/TestFluencePreSale.sol +++ b/test/TestFluencePreSale.sol @@ -21,7 +21,7 @@ contract TestFluencePreSale { Assert.equal(fluence.owner(), tx.origin, "Owner must be set to sender"); - fluence.transfer.value(1 ether).gas(210000)(); + assert(fluence.call.gas(3000000).value(1 ether)()); assert(fluence.balanceOf(tx.origin) == 1500); diff --git a/test/TestMetacoin.sol b/test/TestMetacoin.sol deleted file mode 100644 index 7268664..0000000 --- a/test/TestMetacoin.sol +++ /dev/null @@ -1,25 +0,0 @@ -pragma solidity ^0.4.2; - -import "truffle/Assert.sol"; -import "truffle/DeployedAddresses.sol"; -import "../contracts/MetaCoin.sol"; - -contract TestMetacoin { - - function testInitialBalanceUsingDeployedContract() { - MetaCoin meta = new MetaCoin(); - - uint expected = 10000; - - Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); - } - - function testInitialBalanceWithNewMetaCoin() { - MetaCoin meta = new MetaCoin(); - - uint expected = 10000; - - Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); - } - -} diff --git a/test/metacoin.js b/test/metacoin.js index c61c093..4b4d28f 100644 --- a/test/metacoin.js +++ b/test/metacoin.js @@ -1,4 +1,4 @@ -var MetaCoin = artifacts.require("./MetaCoin.sol"); +/*var MetaCoin = artifacts.require("./MetaCoin.sol"); contract('MetaCoin', function(accounts) { it("should put 10000 MetaCoin in the first account", function() { @@ -61,3 +61,4 @@ contract('MetaCoin', function(accounts) { }); }); }); +*/ \ No newline at end of file