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.