Skip to content

Commit

Permalink
Eliminate throw() per issue #44.
Browse files Browse the repository at this point in the history
  • Loading branch information
timloh-gtoken authored and villesundell committed Oct 31, 2017
1 parent 2c1096e commit 42c5ce4
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 295 deletions.
2 changes: 1 addition & 1 deletion contracts/AllocatedCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ contract AllocatedCrowdsale is Crowdsale {
* Use approve() given to this crowdsale to distribute the tokens.
*/
function assignTokens(address receiver, uint tokenAmount) private {
if(!token.transferFrom(beneficiary, receiver, tokenAmount)) throw;
require(token.transferFrom(beneficiary, receiver, tokenAmount));
}
}
13 changes: 2 additions & 11 deletions contracts/BonusFinalizeAgent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,10 @@ contract BonusFinalizeAgent is FinalizeAgent {
uint public allocatedBonus;

function BonusFinalizeAgent(CrowdsaleToken _token, Crowdsale _crowdsale, uint _bonusBasePoints, address _teamMultisig) {
require(address(_crowdsale) != 0 && address(_teamMultisig) != 0);
token = _token;
crowdsale = _crowdsale;
if(address(crowdsale) == 0) {
throw;
}

teamMultisig = _teamMultisig;
if(address(teamMultisig) == 0) {
throw;
}

bonusBasePoints = _bonusBasePoints;
}

Expand All @@ -56,9 +49,7 @@ contract BonusFinalizeAgent is FinalizeAgent {

/** Called once by crowdsale finalize() if the sale was success. */
function finalizeCrowdsale() {
if(msg.sender != address(crowdsale)) {
throw;
}
require(msg.sender == address(crowdsale));

// How many % of tokens the founders and others get
uint tokensSold = crowdsale.tokensSold();
Expand Down
91 changes: 30 additions & 61 deletions contracts/Crowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,23 @@ contract Crowdsale is Haltable {

function Crowdsale(address _token, PricingStrategy _pricingStrategy, address _multisigWallet, uint _start, uint _end, uint _minimumFundingGoal) {

require(_multisigWallet != 0);
require(_start != 0 && _end != 0);

// Don't mess the dates
require(_start < _end);

owner = msg.sender;

token = FractionalERC20(_token);

setPricingStrategy(_pricingStrategy);

multisigWallet = _multisigWallet;
if(multisigWallet == 0) {
throw;
}

if(_start == 0) {
throw;
}

startsAt = _start;

if(_end == 0) {
throw;
}

endsAt = _end;

// Don't mess the dates
if(startsAt >= endsAt) {
throw;
}

// Minimum funding goal can be zero
minimumFundingGoal = _minimumFundingGoal;
}
Expand All @@ -163,7 +152,7 @@ contract Crowdsale is Haltable {
* Don't expect to just send in money and get tokens.
*/
function() payable {
throw;
require(false);
}

/**
Expand All @@ -181,26 +170,21 @@ contract Crowdsale is Haltable {
// Determine if it's a good time to accept investment from this participant
if(getState() == State.PreFunding) {
// Are we whitelisted for early deposit
if(!earlyParticipantWhitelist[receiver]) {
throw;
}
require(earlyParticipantWhitelist[receiver]);
} else if(getState() == State.Funding) {
// Retail participants can only come in when the crowdsale is running
// pass
} else {
// Unwanted state
throw;
assert(false);
}

uint weiAmount = msg.value;

// Account presale sales separately, so that they do not count against pricing tranches
uint tokenAmount = pricingStrategy.calculatePrice(weiAmount, weiRaised - presaleWeiRaised, tokensSold, msg.sender, token.decimals());

if(tokenAmount == 0) {
// Dust transaction
throw;
}
require(tokenAmount != 0); // Dust transaction

if(investedAmountOf[receiver] == 0) {
// A new investor
Expand All @@ -220,14 +204,12 @@ contract Crowdsale is Haltable {
}

// Check that we did not bust the cap
if(isBreakingCap(weiAmount, tokenAmount, weiRaised, tokensSold)) {
throw;
}
require(!isBreakingCap(weiAmount, tokenAmount, weiRaised, tokensSold));

assignTokens(receiver, tokenAmount);

// Pocket the money
if(!multisigWallet.send(weiAmount)) throw;
require(multisigWallet.send(weiAmount));

// Tell us invest was success
Invested(receiver, weiAmount, tokenAmount, customerId);
Expand Down Expand Up @@ -270,26 +252,26 @@ contract Crowdsale is Haltable {
*/
function investWithSignedAddress(address addr, uint128 customerId, uint8 v, bytes32 r, bytes32 s) public payable {
bytes32 hash = sha256(addr);
if (ecrecover(hash, v, r, s) != signerAddress) throw;
if(customerId == 0) throw; // UUIDv4 sanity check
require(ecrecover(hash, v, r, s) == signerAddress);
require(customerId != 0); // UUIDv4 sanity check
investInternal(addr, customerId);
}

/**
* Track who is the customer making the payment so we can send thank you email.
*/
function investWithCustomerId(address addr, uint128 customerId) public payable {
if(requiredSignedAddress) throw; // Crowdsale allows only server-side signed participants
if(customerId == 0) throw; // UUIDv4 sanity check
require(!requiredSignedAddress); // Crowdsale allows only server-side signed participants
require(customerId != 0); // UUIDv4 sanity check
investInternal(addr, customerId);
}

/**
* Allow anonymous contributions to this crowdsale.
*/
function invest(address addr) public payable {
if(requireCustomerId) throw; // Crowdsale needs to track participants for thank you email
if(requiredSignedAddress) throw; // Crowdsale allows only server-side signed participants
require(!requireCustomerId); // Crowdsale needs to track partipants for thank you email
require(!requiredSignedAddress); // Crowdsale allows only server-side signed participants
investInternal(addr, 0);
}

Expand Down Expand Up @@ -326,9 +308,7 @@ contract Crowdsale is Haltable {
function finalize() public inState(State.Success) onlyOwner stopInEmergency {

// Already finalized
if(finalized) {
throw;
}
require(!finalized);

// Finalizing is optional. We only call it if we are given a finalizing agent.
if(address(finalizeAgent) != 0) {
Expand All @@ -344,12 +324,10 @@ contract Crowdsale is Haltable {
* Design choice: no state restrictions on setting this, so that we can fix fat finger mistakes.
*/
function setFinalizeAgent(FinalizeAgent addr) onlyOwner {
finalizeAgent = addr;

// Don't allow setting bad agent
if(!finalizeAgent.isFinalizeAgent()) {
throw;
}
require(addr.isFinalizeAgent());
finalizeAgent = addr;
}

/**
Expand Down Expand Up @@ -395,13 +373,8 @@ contract Crowdsale is Haltable {
*/
function setEndsAt(uint time) onlyOwner {

if(now > time) {
throw; // Don't change past
}

if(startsAt > time) {
throw; // Prevent human mistakes
}
require(now <= time); // Don't change past
require(time > startsAt); // Don't allow to end before we start

endsAt = time;
EndsAtChanged(endsAt);
Expand All @@ -413,12 +386,10 @@ contract Crowdsale is Haltable {
* Design choice: no state restrictions on the set, so that we can fix fat finger mistakes.
*/
function setPricingStrategy(PricingStrategy _pricingStrategy) onlyOwner {
pricingStrategy = _pricingStrategy;

// Don't allow setting bad agent
if(!pricingStrategy.isPricingStrategy()) {
throw;
}
require(_pricingStrategy.isPricingStrategy());
pricingStrategy = _pricingStrategy;
}

/**
Expand All @@ -431,9 +402,7 @@ contract Crowdsale is Haltable {
function setMultisig(address addr) public onlyOwner {

// Change
if(investorCount > MAX_INVESTMENTS_BEFORE_MULTISIG_CHANGE) {
throw;
}
require(investorCount <= MAX_INVESTMENTS_BEFORE_MULTISIG_CHANGE);

multisigWallet = addr;
}
Expand All @@ -444,7 +413,7 @@ contract Crowdsale is Haltable {
* The team can transfer the funds back on the smart contract in the case the minimum goal was not reached..
*/
function loadRefund() public payable inState(State.Failure) {
if(msg.value == 0) throw;
require(msg.value != 0);
loadedRefund = loadedRefund.plus(msg.value);
}

Expand All @@ -456,11 +425,11 @@ contract Crowdsale is Haltable {
*/
function refund() public inState(State.Refunding) {
uint256 weiValue = investedAmountOf[msg.sender];
if (weiValue == 0) throw;
require(weiValue != 0);
investedAmountOf[msg.sender] = 0;
weiRefunded = weiRefunded.plus(weiValue);
Refund(msg.sender, weiValue);
if (!msg.sender.send(weiValue)) throw;
require(msg.sender.send(weiValue));
}

/**
Expand Down Expand Up @@ -517,7 +486,7 @@ contract Crowdsale is Haltable {

/** Modified allowing execution only if the crowdsale is currently running. */
modifier inState(State state) {
if(getState() != state) throw;
require(getState() == state);
_;
}

Expand Down
4 changes: 1 addition & 3 deletions contracts/DefaultFinalizeAgent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ contract DefaultFinalizeAgent is FinalizeAgent {

/** Called once by crowdsale finalize() if the sale was success. */
function finalizeCrowdsale() public {
if(msg.sender != address(crowdsale)) {
throw;
}
require(msg.sender == address(crowdsale));
token.releaseTokenTransfer();
}

Expand Down
18 changes: 5 additions & 13 deletions contracts/EthTranchePricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ contract EthTranchePricing is PricingStrategy, Ownable {
/// @param _tranches uint[] tranches Pairs of (start amount, price)
function EthTranchePricing(uint[] _tranches) {
// Need to have tuples, length check
if(_tranches.length % 2 == 1 || _tranches.length >= MAX_TRANCHES*2) {
throw;
}
require((_tranches.length % 2 == 0) && (_tranches.length < MAX_TRANCHES*2));

trancheCount = _tranches.length / 2;

Expand All @@ -61,22 +59,16 @@ contract EthTranchePricing is PricingStrategy, Ownable {
tranches[i].price = _tranches[i*2+1];

// No invalid steps
if((highestAmount != 0) && (tranches[i].amount <= highestAmount)) {
throw;
}
require((highestAmount == 0) || (tranches[i].amount > highestAmount));

highestAmount = tranches[i].amount;
}

// We need to start from zero, otherwise we blow up our deployment
if(tranches[0].amount != 0) {
throw;
}
require(tranches[0].amount == 0);

// Last tranche price must be zero, terminating the crowdale
if(tranches[trancheCount-1].price != 0) {
throw;
}
require(tranches[trancheCount-1].price == 0);
}

/// @dev This is invoked once for every pre-ICO address, set pricePerToken
Expand Down Expand Up @@ -162,7 +154,7 @@ contract EthTranchePricing is PricingStrategy, Ownable {
}

function() payable {
throw; // No money on this contract
require(false); // No money on this contract
}

}
13 changes: 3 additions & 10 deletions contracts/ExtraFinalizeAgent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,12 @@ contract ExtraFinalizeAgent is FinalizeAgent {
uint public accountedTokenSales;

function ExtraFinalizeAgent(CrowdsaleToken _token, Crowdsale _crowdsale, uint _bonusBasePoints, address _teamMultisig, uint _accountedTokenSales) {
require(address(_crowdsale) != 0 && address(_teamMultisig) != 0);

token = _token;
crowdsale = _crowdsale;

if(address(crowdsale) == 0) {
throw;
}

teamMultisig = _teamMultisig;
if(address(teamMultisig) == 0) {
throw;
}

accountedTokenSales = _accountedTokenSales;
}
Expand All @@ -60,9 +55,7 @@ contract ExtraFinalizeAgent is FinalizeAgent {

/** Called once by crowdsale finalize() if the sale was success. */
function finalizeCrowdsale() {
if(msg.sender != address(crowdsale)) {
throw;
}
require(msg.sender == address(crowdsale));

// How many % of tokens the founders and others get
uint tokensSold = crowdsale.tokensSold().minus(accountedTokenSales);
Expand Down
6 changes: 3 additions & 3 deletions contracts/Haltable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ contract Haltable is Ownable {
bool public halted;

modifier stopInEmergency {
if (halted) throw;
require(!halted);
_;
}

modifier stopNonOwnersInEmergency {
if (halted && msg.sender != owner) throw;
require(!halted || msg.sender == owner);
_;
}

modifier onlyInEmergency {
if (!halted) throw;
require(halted);
_;
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/Issuer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract Issuer is Ownable {
}

function issue(address benefactor, uint amount) onlyOwner {
if(issued[benefactor]) throw;
require(!issued[benefactor]);
token.transferFrom(allower, benefactor, amount);
issued[benefactor] = true;
issuedCount += amount;
Expand Down
Loading

0 comments on commit 42c5ce4

Please sign in to comment.