2

So I modified the ERC20 token a little bit added two extra functions so that my ICO token contract can call it to send some tokens to the people.

This is my erc20 token code.

pragma solidity ^0.4.24;

contract SafeMath {

    function safeAdd(uint a, uint b) public pure returns (uint c) {
        c = a + b;
        require(c >= a);
    }

    function safeSub(uint a, uint b) public pure returns (uint c) {
        require(b <= a);
        c = a - b;
    }

    function safeMul(uint a, uint b) public pure returns (uint c) {
        c = a * b;
        require(a == 0 || c / a == b);
    }

    function safeDiv(uint a, uint b) public pure returns (uint c) {
        require(b > 0);
        c = a / b;
    }
}


contract ERC20Token is SafeMath {

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

    function totalSupply() public constant returns (uint) {
        return _totalSupply  - balances[address(0)];
    }

    function balanceOf(address tokenOwner) public constant returns (uint balance) {
        return balances[tokenOwner];
    }

    function transfer(address to, uint tokens) public returns (bool success) {
        balances[msg.sender] = safeSub(balances[msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        emit Transfer(msg.sender, to, tokens);
        return true;
    }

    function approve(address spender, uint tokens) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        return true;
    }

    function transferFrom(address from, address to, uint tokens) public returns (bool success) {
        balances[from] = safeSub(balances[from], tokens);
        allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        emit Transfer(from, to, tokens);
        return true;
    }

    function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
        return allowed[tokenOwner][spender];
    }

    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);

}

contract Owned {

    address public owner;
    address public newOwner;

    event OwnershipTransferred(address indexed _from, address indexed _to);

    constructor() public {
        owner = msg.sender;
    }

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

    function transferOwnership(address _newOwner) public onlyOwner {
        newOwner = _newOwner;
    }

    function acceptOwnership() public {
        require(msg.sender == newOwner);
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
        newOwner = address(0);
    }
}

contract OurToken is ERC20Token, Owned {

    string public symbol;
    string public  name;
    uint public decimals;
    address public crowdsaleAddress;
    uint public ICOEndTime = 0;

   modifier onlyCrowdsale {
      require(msg.sender == crowdsaleAddress);
      _;
   }

   modifier afterCrowdsale {
      require(now > ICOEndTime);
      _;
   }

  constructor (string _symbol,string _name,uint _decimals,uint totalSupply) public {

      symbol = _symbol;
      name = _name;
      decimals = _decimals;
      _totalSupply = totalSupply * 10 ** decimals;
      owner = msg.sender;
      balances[msg.sender] = _totalSupply;
   }

    function transfer(address to, uint tokens) public afterCrowdsale returns (bool) {
        super.transfer(to,tokens);
    }

    function transferFrom(address from, address to, uint tokens) public afterCrowdsale returns (bool) {
        super.transferFrom(from,to,tokens);
    }

    function emergencyExtract() external onlyOwner {
        owner.transfer(address(this).balance);
    }

   function setICOEndtime(uint time) public onlyOwner {
        require(time>0);
        ICOEndTime = time;
   }

   function setCrowdsale(address _crowdsaleAddress) public onlyOwner {
      require(_crowdsaleAddress != address(0));
      crowdsaleAddress = _crowdsaleAddress;
   }

   function releaseTokens(address _receiver, uint256 _amount) public onlyCrowdsale {
      require(_receiver != address(0));
      require(_amount > 0);
      balances[owner] = safeSub(balances[owner], _amount);
      balances[_receiver] = safeAdd(balances[_receiver], _amount);
      emit Transfer(owner, _receiver, _amount);

   }

   function refundTokens(address _sender, uint256 _amount) public onlyCrowdsale {
      balances[_sender] = safeSub(balances[_sender], _amount);
      balances[owner] = safeAdd(balances[owner], _amount);
      emit Transfer(_sender,owner,_amount);

   }
}

ICO contract

pragma solidity ^0.4.24;


contract OurToken {

   function balanceOf(address tokenOwner) public constant returns (uint balance);

   // To release tokens to the address that have send ether.
   function releaseTokens(address _receiver, uint _amount) public;

   // To take back tokens after refunding ether.
   function refundTokens(address _receiver, uint _amount) public;

}

contract ICO {

   uint public icoStart;
   uint public icoEnd;
   uint public tokenRate;
   OurToken public token;   
   uint public fundingGoal;
   uint public tokensRaised;
   uint public etherRaised;
   address public owner;
   address public extractor;

   modifier whenIcoCompleted {
      require(now>icoEnd);
      _;
   }

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

   modifier onlyExtractor {
      require(msg.sender == extractor || msg.sender == owner);
      _;
   }

   constructor(uint256 _icoStart, uint _icoEnd, uint _tokenRate, address _tokenAddress, uint _fundingGoal, address _extractor) public {

      require(_icoStart != 0 &&
      _icoEnd != 0 &&
      _icoStart < _icoEnd &&
      _tokenRate != 0 &&
      _tokenAddress != address(0) &&
      _fundingGoal != 0);
      icoStart = _icoStart;
      icoEnd = _icoEnd;
      tokenRate = _tokenRate;
      token = OurToken(_tokenAddress);
      fundingGoal = _fundingGoal;
      owner = msg.sender;
      extractor = _extractor;

   }

   function () public payable {
      buy();
   }

   function buy() public payable {

      require(msg.sender!=owner);
      require(tokensRaised < fundingGoal);
      require(now < icoEnd && now > icoStart);
      uint tokensToBuy;
      uint etherUsed = msg.value;
      tokensToBuy = (etherUsed/ 1 ether) * tokenRate;

      if(tokensRaised + tokensToBuy > fundingGoal) {
         uint exceedingTokens = tokensRaised + tokensToBuy - fundingGoal;
         uint exceedingEther;

         exceedingEther = (exceedingTokens * 1 ether) / tokenRate;
         msg.sender.transfer(exceedingEther);

         tokensToBuy -= exceedingTokens;
         etherUsed -= exceedingEther;
      }

      token.releaseTokens(msg.sender, tokensToBuy);


      tokensRaised += tokensToBuy;
      etherRaised += etherUsed;
   }

   function returnEther() public whenIcoCompleted {

       require(tokensRaised < fundingGoal);
       uint balance = token.balanceOf(msg.sender);
       uint etherToBeReturned = (balance / tokenRate) * 1 ether;
       msg.sender.transfer(etherToBeReturned);
       token.refundTokens(msg.sender,balance);
       etherRaised -= etherToBeReturned;

   }

   function extractEther() public whenIcoCompleted onlyExtractor {
      extractor.transfer(address(this).balance);
   }

 }

Yes, I set the ico address and end time in erc20 token after the ico is deployed.

The contract is accepting ether when payable method is being called directly but sending just ether to the contract address doesn't trigger the payable function.

Can someone explain why?

1

Your first function is a payable fallback function which accepts funds. To trigger your second payable function you have to send the funds specifically to this function, you can use Remix or Web3 to test it.

  • Noob here, but the first payable function calls the second one by itself right? – Nobody Oct 23 '18 at 17:34
  • they are typically used to emit events or revert, im not sure about this case. Better take a detailed look here – heildever Oct 23 '18 at 18:57
1

Don't really understand what your problem is, but probably is not in the invocation of the fallback function. If you try to deploy the following contract, whichever function you call (buy, fallback directly, contract) it always deposit the value in the balance of the contract.

pragma solidity ^0.4.25;

contract Test {
    event Debug(address indexed _from, uint256 _value);

    function () public payable {
        buy();
    }

    function buy() public payable {
        emit Debug(msg.sender, msg.value);
    }
}

Try yourself at the address 0x019939e64d9858a1588cf1bb5fe1515e4c142695 on Rinkeby: https://rinkeby.etherscan.io/address/0x019939e64d9858a1588cf1bb5fe1515e4c142695

  • Yes, even for me the corresponding function is happening when I directly call the payable method when I just send ether to the contract address the payable method is supposed to be invoked right ? – Nobody Oct 23 '18 at 17:43
  • Yes, the first "anonymous" method is called whenever you send ether directly to the contract. And if you look at the events emitted you see that the method buy() is always called. – Marco Ottolini Oct 23 '18 at 17: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.