2

I sent ETH to a contract address, and the transaction "failed" saying : Warning! Error encountered during contract execution [Reverted].

contract address and transaction failure here:

https://rinkeby.etherscan.io/tx/0x28a0a0f7eb8cf863afb4a9a2425648d2d9693e39793f0f9cc01d006374808370

ICO Token contract code:-

pragma solidity ^0.4.24;
import "./DappToken.sol";


contract ICOToken is DappToken {

   string public name = 'ICOToken';
   string public symbol = 'ITK';
   uint256 public decimals = 18;
   uint256 public totalSupply;
   address public crowdsaleAddress;
   address public owner;
   uint256 public ICOEndTime = 1541246755;
   uint256 public balance;

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

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

//   modifier afterCrowdsale {
//  require(now > ICOEndTime || msg.sender == crowdsaleAddress);
//      _;
//  }

   constructor (uint256 _tokenSupply) public DappToken() {
      totalSupply = _tokenSupply;
      balanceOf[msg.sender] = _tokenSupply;
      owner = msg.sender;
   }


   function setCrowdsale(address _crowdsaleAddress) public onlyOwner  {

      require(_crowdsaleAddress != address(0));
      crowdsaleAddress = _crowdsaleAddress;

   }


    function buyTokens(address _receiver, uint256 _amount) public  {

      require(_receiver != address(0));
      require(_amount > 0);
      transfer(_receiver, _amount);


   }

//       /// @notice Override the functions to not allow token transfers until the end of the ICO
//   function transfer(address _to, uint256 _value) public afterCrowdsale returns(bool) {

//       return super.transfer(_to, _value);

//   }


//   /// @notice Override the functions to not allow token transfers until the end of the ICO
//   function transferFrom(address _from, address _to, uint256 _value) public afterCrowdsale returns(bool) {

//       return super.transferFrom(_from, _to, _value);

//   }


//   /// @notice Override the functions to not allow token transfers until the end of the ICO
//   function approve(address _spender, uint256 _value) public afterCrowdsale returns(bool) {

//       return super.approve(_spender, _value);
//   }

//   /// @notice Override the functions to not allow token transfers until the end of the ICO

//   function increaseApproval(address _spender, uint _addedValue) public afterCrowdsale returns(bool success) {

//       return super.increaseApproval(_spender, _addedValue);
//   }

//   /// @notice Override the functions to not allow token transfers until the end of the ICO

//   function decreaseApproval(address _spender, uint _subtractedValue) public afterCrowdsale returns(bool success) {

//       return super.decreaseApproval(_spender, _subtractedValue);
//   }

//   function emergencyExtract() external onlyOwner {

//       owner.transfer(address(this).balance);
//   }

}

Crowdsale contract code like this:-

    pragma solidity ^0.4.24;
import "./ICOToken.sol";

contract Crowdsale {
   bool public icoCompleted;
   uint256 public icoStartTime = 1538567825; // 3 oct 2018 11.57 AM UTC;
   uint256 public icoEndTime = 1541246755;  // 3 nov 2018 11.57 Am UTC;
   uint256 public tokenRate;
   uint256 public fundingGoal;
   address public owner;
   ICOToken public token;
   uint256 public tokensRaised;
   uint256 public etherRaised;
   uint256 public etherUsed = msg.value;
   uint256 public tokensToBuy;


   modifier whenIcoCompleted {
      require(icoCompleted);
      _;
   }

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

   function () public payable {
       buy();
   }

     constructor(uint256 _tokenRate, address _tokenAddress, uint256 _fundingGoal) public {
      require(icoStartTime != 0 &&
      icoEndTime != 0 &&
      icoStartTime < icoEndTime &&
      _tokenRate != 0 &&
      _tokenAddress != address(0) &&
      _fundingGoal != 0);
      tokenRate = _tokenRate;
      token = ICOToken(_tokenAddress);
      fundingGoal = _fundingGoal;
      owner = msg.sender;
      etherUsed;
      tokensToBuy;
   }

    function buy() public payable {

    //   require(tokensRaised < fundingGoal);
    //   require(now < icoEndTime && now > icoStartTime);

      etherUsed = msg.value;

      tokensToBuy = etherUsed * (10 ** token.decimals()) / 1 ether * tokenRate;

    //   Check if we have reached and exceeded the funding goal to refund the exceeding tokens and ether

    //   if(tokensRaised + tokensToBuy > fundingGoal) {

    //      uint256 exceedingTokens = tokensRaised + tokensToBuy - fundingGoal;
    //      uint256 exceedingEther;

    // //   Convert the exceedingTokens to ether and refund that ether
    //      exceedingEther = exceedingTokens * 1 ether / tokenRate / token.decimals();

    //      msg.sender.transfer(exceedingEther);

    //     //   Change the tokens to buy to the new number
    //      tokensToBuy -= exceedingTokens;

    //     //   Update the counter of ether used
    //      etherUsed -= exceedingEther;

    //   }
      // Send the tokens to the buyer
      token.buyTokens(msg.sender, tokensToBuy);

      // Increase the tokens raised and ether raised state variables
      tokensRaised += tokensToBuy;
      etherRaised += etherUsed;
   }


   function extractEther() public whenIcoCompleted onlyOwner {
       owner.transfer(address(this).balance);
   }

}

I also debugged on remix and found that when end user pays amount by metamask wallet, this msg.value is not store in etherused variable which is used in crowdsale smart contract. So token can't transfer to receiver.

So How can I resolve this issue?? Please help me.

Thanks.

2
  • 1
    It is impossible to help without seeing some relevant code. Oct 12, 2018 at 12:48
  • I updated my question please check. Oct 12, 2018 at 13:00

1 Answer 1

2

It looks like when buy() runs, it invokes token.buyTokens().

buyTokens() in turn calls transfer(). The vast majority of transfer functions check user balances before transferring, so I'm assuming your super implementation does that too.

As such, since within transfer() your msg.sender is the crowdsale contract, the crowdsale contract must hold enough tokens to make that transfer, which doesn't seem to be the case.

5
  • I again updated my question please check. I already use fallback function. Oct 12, 2018 at 13:07
  • @AnupamJain Updated my answer. Oct 12, 2018 at 13:29
  • Hi Raghav, Thanks for answering . How I transfer token to token contract to crowd-sale contract Oct 13, 2018 at 18:18
  • When I am using owner address as recipient address So transaction done successfully. But when I check from address on rinkeby.etherscan.io so it's not find appropriate token so is it happen because I am not using crowsale address.If not So why end user not find appropriate ether. Please help me resolve this query. Oct 13, 2018 at 20:11
  • Hi Raghav, Thanks a lot. Now my contract functionality working fine. When I transfer token on crow-sale address. And then send ether to crow-sale address. So transaction is successfully. Oct 14, 2018 at 18:55

Your Answer

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

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