0

i'm trying to do a a test crowdsale of an ERC20+ERC223 Token but I can't get the crowdsale to work. I can however interact with the token's functions.

The token contract is deployed seperately from the crowdsale contract. First the token is created (in Remix) with the necessary parameters such as name, symbol, .. Then the crodwsale is created and the crowdsale contract takes in the token Address as parameters.

I've tried in multiple ways but I can't get my crowdsale contract to work. Thanks in advance.

ERC20.sol

pragma solidity ^0.4.10;

import 'browser/SafeMath.sol';

interface ERC20 {
  function balanceOf(address who) public view returns (uint256);
  function transfer(address to, uint256 value) public;
  function allowance(address owner, address spender) public view returns (uint256);
  function transferFrom(address from, address to, uint256 value) public returns (bool);
  function approve(address spender, uint256 value) public returns (bool);
  event Transfer(address indexed from, address indexed to, uint256 value);
  event Approval(address indexed owner, address indexed spender, uint256 value);
}

ERC223.sol

pragma solidity ^0.4.10;

interface ERC223 {
    function transfer(address to, uint value, bytes data) public;
    event Transfer(address indexed from, address indexed to, uint value, bytes indexed data);
}

ERC223ReceivingContract.sol

pragma solidity ^0.4.10;

contract ERC223ReceivingContract { 
    function tokenFallback(address _from, uint _value, bytes _data) public;
}

StandardToken (function implementations)

pragma solidity ^0.4.10;

/**
 * @dev Imports
 * Import ERC20 Interface
 * Import ERC223 Interface
 * Import ERC223ReceivingContract Interface
 * Import SafeMath Library
 * */
import 'browser/ERC20.sol';
import 'browser/ERC223.sol';
import 'browser/SafeMath.sol';
import 'browser/ERC223ReceivingContract.sol';

contract StandardToken is ERC20, ERC223 {

    using SafeMath for uint;

    string internal _name;
    string internal _symbol;
    uint8 internal _decimals;
    uint256 internal _totalSupply;

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

    function StandardToken(string name, string symbol, uint8 decimals, uint256 totalSupply) public {
        _symbol = symbol;
        _name = name;
        _decimals = decimals;
        _totalSupply = totalSupply;
        balances[msg.sender] = totalSupply;
    }

    function name()
        public
        view
        returns (string) {
        return _name;
    }

    function symbol()
        public
        view
        returns (string) {
        return _symbol;
    }

    function decimals()
        public
        view
        returns (uint8) {
        return _decimals;
    }

    function totalSupply()
        public
        view
        returns (uint256) {
        return _totalSupply;
    }

     /**
  * @dev Gets the balance of the specified address.
  * @param _owner The address to query the the balance of.
  * @return An uint256 representing the amount owned by the passed address.
  */
  function balanceOf(address _owner) public view returns (uint256 balance) {
    return balances[_owner];
  }

  /**
   * @dev Transfer tokens from one address to another
   * @param _from address The address which you want to send tokens from
   * @param _to address The address which you want to transfer to
   * @param _value uint256 the amount of tokens to be transferred
   */
  function transferFrom(address _from, address _to, uint _value) public returns (bool) {
    require(_to != address(0));
    require(_value <= balances[_from]);
    require(_value <= allowed[_from][msg.sender]);
    balances[_from] = SafeMath.sub(balances[_from], _value);
    balances[_to] = SafeMath.add(balances[_to], _value);
    allowed[_from][msg.sender] = SafeMath.sub(allowed[_from][msg.sender], _value);
    Transfer(_from, _to, _value);
    return true;
  }

  /**
   * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
   *
   * Beware that changing an allowance with this method brings the risk that someone may use both the old
   * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
   * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
   * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
   * @param _spender The address which will spend the funds.
   * @param _value The amount of tokens to be spent.
   */
  function approve(address _spender, uint _value) public returns (bool) {
    allowed[msg.sender][_spender] = _value;
    Approval(msg.sender, _spender, _value);
    return true;
  }

  /**
   * @dev Function to check the amount of tokens that an owner allowed to a spender.
   * @param _owner address The address which owns the funds.
   * @param _spender address The address which will spend the funds.
   * @return A uint256 specifying the amount of tokens still available for the spender.
   */
  function allowance(address _owner, address _spender) public view returns (uint256) {
    return allowed[_owner][_spender];
  }

  /**
   * approve should be called when allowed[_spender] == 0. To increment
   * allowed value is better to use this function to avoid 2 calls (and wait until
   * the first transaction is mined)
   */
  function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
    allowed[msg.sender][_spender] = SafeMath.add(allowed[msg.sender][_spender], _addedValue);
    Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

  function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
    uint oldValue = allowed[msg.sender][_spender];
    if (_subtractedValue > oldValue) {
      allowed[msg.sender][_spender] = 0;
    } else {
      allowed[msg.sender][_spender] = SafeMath.sub(oldValue, _subtractedValue);
    }
    Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

  /**
     * @dev Transfer the specified amount of tokens to the specified address.
     *      Invokes the `tokenFallback` function if the recipient is a contract.
     *      The token transfer fails if the recipient is a contract
     *      but does not implement the `tokenFallback` function
     *      or the fallback function to receive funds.
     *
     * @param _to    Receiver address.
     * @param _value Amount of tokens that will be transferred.
     * @param _data  Transaction metadata.
     */
  function transfer(address _to, uint _value, bytes _data) public {
    require(_value > 0 );
    if(isContract(_to)) {
        ERC223ReceivingContract receiver = ERC223ReceivingContract(_to);
        receiver.tokenFallback(msg.sender, _value, _data);
    }
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);
        Transfer(msg.sender, _to, _value, _data);
    }


    /**
     * @dev Transfer the specified amount of tokens to the specified address.
     *      This function works the same with the previous one
     *      but doesn't contain `_data` param.
     *      Added due to backwards compatibility reasons.
     *
     * @param _to    Receiver address.
     * @param _value Amount of tokens that will be transferred.
     */
  function transfer(address _to, uint _value) public {
    return transfer(_to, _value, "");
    }

 /** Assemble address bytecode size
  * if address is contract
  * @return true 
  * else if it is a regular wallet
  * @return false
  * */
  function isContract(address _addr) private returns (bool is_contract) {
      uint length;
      assembly {
            //retrieve the size of the code on target address, this needs assembly
            length := extcodesize(_addr)
      }
      return (length>0);
    }

}

Crowdsale.sol

pragma solidity ^0.4.10;

import 'browser/ERC223ReceivingContract.sol';
import 'browser/SafeMath.sol';
import 'browser/StandardToken.sol';

contract Crowdsale {
    using SafeMath for uint;
    StandardToken private token;
    uint private availableTokens;
    uint private price;

    event Buy(address _beneficiary, uint _amount);

    function Crowdsale(address _token) public {
        token = StandardToken(_token);
        price = 1 ether;
    }

    //Fallback
    function() public payable {
        return buyFor(msg.sender);
    }

    function buy() public payable {
        return buyFor(msg.sender);
    }

    function buyFor(address _beneficiary) public payable{
        uint amount = msg.value.div(price);
        token.transfer(_beneficiary, amount);
        Buy(_beneficiary, amount);
    }
}

Error

transact to Crowdsale.(fallback) errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Note: The constructor should be payable if you send value.    Debug the ...
  • If you are deploying the token contract separately. Are you transferring the tokens to the crowdsale before attempting to call the buy/fallback function? The buy function will transfer the tokens from the contract to the specified address, but if you deployed the token contract from your own account, then they are all owner by you and not the crowdsale contract. – pabloruiz55 Dec 27 '17 at 18:12
  • This might be the case indeed. What would you suggest then? transferOwnership to change owner address, approve the crowdsale to spend X amount of tokens or have the crowdsale deploy the token (do you have a working example perhaps for the latter? ) Thank you – Kyriediculous Dec 27 '17 at 18:17
  • I am following these examples by the way (albeit slightly adjusted) github.com/willitscale/learning-solidity/blob/master/… – Kyriediculous Dec 27 '17 at 18:20
  • The most straightforward thing to do (and what's considered a good practice) would be to have the crowdsale deploy the token contract and automatically get assigned the tokens. That's very transparent as it makes sure the tokens are contained within the rules of the crowdsale, and not even you as the creator has access to them. – pabloruiz55 Dec 27 '17 at 18:31
  • Wouldn't it add security implications since the crowdsale contract can't be selfdestructed afterwards? I've also ran into really high gas costs when doing it that way, having to manually increase gas limit. What's the best practice here? Using inheritance (Crowdsale is Token) or having crowdsale create a new contract with "new" ? Thank you for the answers by the way. – Kyriediculous Dec 27 '17 at 18:34

Your Answer

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

Browse other questions tagged or ask your own question.