1

Hi i'm quite new to java and solidity, i was wondering how i could make a function in my crowdsale that only me as the owner of the contract could call at anytime to switch the crowdsale on and off, set new caps and new crowdsale time.

Im using remix for the coding, and the objective is to create a crowdsale to get funds for buying particular assets for different campaigns over time but on the same token contract each crowdsale with its own added cap and time to fullfill it.

Many thanks.

Here's my code.

pragma solidity ^0.4.11;

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

contract JoseToken is IERC20 {

    using SafeMath for uint256;

    uint public  _totalSupply = 0;

    string public constant symbol = "VIC";
    string public constant name = "JoseToken";
    uint8 public constant decimals = 18;

    // 1 ether = 500 VIC
    uint256 public constant RATE = 500;

    address public owner;

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

    function () payable {
        createTokens();
    }

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

    function createTokens() payable {
        require(msg.value > 0);

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

        owner.transfer(msg.value);
    }

    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];
    }

    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

After the modifications suggested by Cristian This is the code :

contract JoseToken is IERC20 {

using SafeMath for uint256;

uint public  _totalSupply = 0;

string public constant symbol = "VIC";
string public constant name = "JoseToken";
uint8 public constant decimals = 18;

bool isEnabled;

// 1 ether = 500 VIC
uint256 public constant RATE = 500;

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 JoseToken() {
    owner = msg.sender;
}

function createTokens() payable {
    require(msg.value > 0);
    require(isEnabled);

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

    owner.transfer(msg.value);
}

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];
}

event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);

}

1

It should be simple. First, create a bool variable that controls whether the crowdsale is on or off.

bool isEnabled;

Then, if you want to follow Solidity's conventions, create a modifier that allows transactions only from your account:

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

Then create a toggle function:

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

Now you can use that flag to enable/disable parts of your crowsale. e.g.:

function transfer(address _to, uint256 _value) returns (bool success) {
    require(isEnabled);
    ... etc.

You can also put that check in a modifier.

  • thx i posted the code updated with your suggestions, does it look good ? – Jose Victor Sep 20 '17 at 12:34
  • Yes, that should do it. – Cristian Sep 20 '17 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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