I'm testing this ICO contract and it's giving an error "Warning! Error encountered during contract execution [ out of gas ]".
Using the BUYTOKEN function it works as normal, but when I send a direct value to the contract it gives this error message.
I've already increased the gas by 600000, but memo it gives this message.
could someone help me?
the contract is already with the tokens.
Warning! Error encountered during contract execution [ out of gas ]
pragma solidity ^0.4.21;
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
contract Ownable {
address public owner;
function Ownable() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
owner = newOwner;
}
}
interface Token {
function transfer(address _to, uint256 _value) external returns (bool);
function balanceOf(address _owner) external constant returns (uint256 balance);
}
contract LavevelICO is Ownable {
using SafeMath for uint256;
Token token;
uint256 public constant RATE = 1500000; // Number of tokens per Ether
uint256 public constant CAP = 9350; // Cap in Ether
uint256 public constant START = 1639047009;// 12/09/2021
uint256 public constant DAYS = 45; // 45 Day
uint256 public constant initialTokens = 6000000 * 10**9; // Initial number of tokens available
bool public initialized = false;
uint256 public raisedAmount = 0;
event BoughtTokens(address indexed to, uint256 value);
modifier whenSaleIsActive() {
// Check if sale is active
assert(isActive());
_;
}
function LavevelICO(address _tokenAddr) public {
require(_tokenAddr != 0);
token = Token(_tokenAddr);
}
function initialize() public onlyOwner {
require(initialized == false); // Can only be initialized once
require(tokensAvailable() == initialTokens); // Must have enough tokens allocated
initialized = true;
}
function isActive() public view returns (bool) {
return (
initialized == true &&
now >= START && // Must be after the START date
now <= START.add(DAYS * 1 days) && // Must be before the end date
goalReached() == false // Goal must not already be reached
);
}
function goalReached() public view returns (bool) {
return (raisedAmount >= CAP * 1 ether);
}
function () public payable {
buyTokens();
}
function buyTokens() public payable whenSaleIsActive {
uint256 weiAmount = msg.value; // Calculate tokens to sell
uint256 tokens = weiAmount.mul(RATE * msg.value);
emit BoughtTokens(msg.sender, tokens); // log event onto the blockchain
raisedAmount = raisedAmount.add(msg.value); // Increment raised amount
token.transfer(msg.sender, tokens); // Send tokens to buyer
owner.transfer(msg.value);// Send money to owner
}
function tokensAvailable() public constant returns (uint256) {
return token.balanceOf(this);
}
function destroy() onlyOwner public {
// Transfer tokens back to owner
uint256 balance = token.balanceOf(this);
assert(balance > 0);
token.transfer(owner, balance);
// There should be no ether in the contract but just in case
selfdestruct(owner);
}
}