1

I'm trying to create a token sale for a colection. The idea is to restart the crowdsale everytime we grow funds to amplify the collection, the tokens are burnable to purchase items from the colection. The sales should have a funding cap and a time cap.

However when the time is up or the funding goal is reached I can keep sending funds to the contract and create new tokens as if the conditions i coded didn't matter.

Here's my code :

pragma solidity ^0.4.11;

import './IERC20.sol';
import './SafeMath.sol';

contract TestToken4 is IERC20 {

    using SafeMath for uint256;

    uint public _totalSupply = 0;
    uint public fundingGoal;
    uint public deadline;
    uint public amountRaised = 0;

    string public constant symbol = "TST4";
    string public constant name = "TestToken4";
    uint8 public constant decimals = 18;

    bool isEnabled;
    bool GoalReached = false;
    bool TokenSaleEnded = false;

    // 1 ether = 2000 tst3
    uint256 public RATE = 2000;

    address public owner;

    mapping(address => uint256) balances;
    mapping(address => mapping(address => uint256)) allowed;


    modifier isOwner() {
        require(msg.sender == owner);
        _;
    }

    function toggle() isOwner {
        isEnabled = !isEnabled;
    }

    function () payable {
        createTokens();
    }

    function TestToken4() {
        owner = msg.sender;
    }

    function createTokens() payable {
        require(
            msg.value > 0
            && isEnabled
            && !GoalReached
            && !TokenSaleEnded);

        uint256 tokens = msg.value.mul(RATE);
        balances[msg.sender] = balances[msg.sender].add(tokens);
        _totalSupply = _totalSupply.add(tokens);
        amountRaised = amountRaised.add(msg.value); 

        owner.transfer(msg.value);
    }

    function setCrowdsale (
        uint fundingGoalInEthers,
        uint durationInMinutes,
        uint tokensPerEth
        ) isOwner {
            fundingGoal = fundingGoalInEthers.mul(1 ether);
            deadline = now + durationInMinutes * 1 minutes;
            RATE = tokensPerEth;
        }

    function checkGoalReached() {
        if (amountRaised >= fundingGoal) {
            GoalReached = true;
        }
        if (now >= deadline) {
            TokenSaleEnded = true;
        }
        else {
            GoalReached = false;
            TokenSaleEnded = false;
        }
    }

    function totalSupply () constant returns (uint256 totalSupply) {
        return _totalSupply;
    }

    function balanceOf(address _owner) constant returns (uint256 balance) {
        return balances [_owner];
    }

    function transfer(address _to, uint256 _value) returns (bool success) {
        require(
            balances[msg.sender] >= _value
            && _value > 0
        );
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);
        Transfer(msg.sender, _to, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
        require(
            allowed[_from][msg.sender] >= _value
            && balances[_from] >= _value
            && _value > 0
        );
        balances[_from] = balances[_from].sub(_value);
        balances[_to] = balances[_to].add(_value);
        allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
        Transfer(_from, _to, _value);
        return true;
    }

    function approve(address _spender, uint256 _value) returns (bool success) {
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);
        return true;
    }

    function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
        return allowed[_owner][_spender];
    }

    function burn(uint256 _value) isOwner returns (bool success) {
        require(
            balances[msg.sender] >= _value
            && _value > 0
        );
        balances[msg.sender] = balances[msg.sender].sub(_value);
        _totalSupply = _totalSupply.sub(_value);
        Burn (msg.sender, _value);
        return true;
    } 

    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
    event Burn(address indexed from, uint256 value);
}
4
  • Whats your question? Do you want to review your code? Commented Sep 21, 2017 at 16:39
  • Yes, what i dont know is where i made a mistake, because i dont know the reason for the contract to not work properly Commented Sep 21, 2017 at 17:07
  • What is the issue you are facing while running the contract? Commented Sep 21, 2017 at 17:09
  • the contract runs good but the crowdsale does not stop when the funding goal is achieved or the time runs out. I can keep buying tokens. Commented Sep 21, 2017 at 17:11

1 Answer 1

1

Since I can't comment due to my low reputation on this stackoverflow...

I have the feeling that the problem is in this section of your code

function checkGoalReached() {
    if (amountRaised >= fundingGoal) {
        GoalReached = true;
    }
    if (now >= deadline) {
        TokenSaleEnded = true;
    }
    else {
        GoalReached = false;
        TokenSaleEnded = false;
    }
}
2
  • Yes, you have to call checkGoalReached() when the goal is reached or the deadline has passed.
    – Ismael
    Commented Sep 21, 2017 at 23:51
  • I tried diferent ways of doing that but i couldnt figure out the correct one, most of the time i end up unable to START the crowdsale when i call the function somewhere else, Could you explain a little further ? Commented Sep 25, 2017 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.