0

我正在建立一个众筹智能合约并且我正在使用 ropsen tesnet 和 remix。

我这次众筹的代币地址是“0x6f734b9a097f17bc4c1f4348801587ce5e0177e2”

一切正常,我可以调用大部分函数。例如setStart,setEnd,hasEnded等。但是当我调用函数buyTokens时,我从remix中得到了这个错误:

交易到 FastInvestTokenCrowdsale.buyTokens 错误:所需的气体超过限制:300000。重要的气体估计也可能是合约代码出现问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。

这是我众筹的编码:

/**
 *  Crowdsale for Fast Invest Tokens.
 *  Raised Ether will be stored safely at the wallet.
 *
 *  Based on OpenZeppelin framework.
 *  https://openzeppelin.org
 *
 *  Author: Paulius Tumosa
 **/

pragma solidity ^0.4.18;

/**
 * Safe Math library from OpenZeppelin framework
 * https://openzeppelin.org
 *
 * @title SafeMath
 * @dev Math operations with safety checks that throw on error
 */
library SafeMath {
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }
        uint256 c = a * b;
        assert(c / a == b);
        return c;
    }

    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        // assert(b > 0); // Solidity automatically throws when dividing by 0
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold
        return c;
    }

    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        assert(b <= a);
        return a - b;
    }

    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        assert(c >= a);
        return c;
    }
}

contract token {
    function transferFrom(address from, address to, uint256 value) public returns (bool);
}

/**
 * @title FastInvestTokenCrowdsale
 *
 * Crowdsale have a start and end timestamps, where investors can make
 * token purchases and the crowdsale will assign them tokens based
 * on a token per ETH rate. Funds collected are forwarded to a wallet
 * as they arrive.
 */
contract FastInvestTokenCrowdsale {
    using SafeMath for uint256;

    address public owner;

    // The token being sold
    token public tokenReward;

    // Tokens will be transfered from this address
    address internal tokenOwner;

    // Address where funds are collected
    address internal wallet;

    // Start and end timestamps where investments are allowed
    uint256 public startTime;
    uint256 public endTime;

    // Amount of tokens sold
    uint256 public tokensSold = 0;

    // Amount of raised money in wei
    uint256 public weiRaised = 0;

    // Funding goal and soft cap
    uint256 constant public SOFT_CAP        = 38850000000000000000000000;
    uint256 constant public FUNDING_GOAL    = 388500000000000000000000000;

    // Tokens per ETH rates before and after the soft cap is reached
    uint256 constant public RATE = 1000;
    uint256 constant public RATE_SOFT = 1200;

    // The balances in ETH of all investors
    mapping (address => uint256) public balanceOf;

    /**
     * Event for token purchase logging
     *
     * @param purchaser who paid for the tokens
     * @param beneficiary who got the tokens
     * @param value weis paid for purchase
     * @param amount amount of tokens purchased
     */
    event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function FastInvestTokenCrowdsale(address _tokenAddress, address _wallet, uint256 _start, uint256 _end) public {
        require(_tokenAddress != address(0));
        require(_wallet != address(0));

        owner = msg.sender;
        tokenOwner = msg.sender;
        wallet = _wallet;

        tokenReward = token(_tokenAddress);

        require(_start < _end);
        startTime = _start;
        endTime = _end;

    }

    // Fallback function can be used to buy tokens
    function () external payable {
        buyTokens(msg.sender);
    }

    // Low level token purchase function
    function buyTokens(address beneficiary) public payable {
        require(beneficiary != 0x0);
        require(validPurchase());

        uint256 weiAmount = msg.value;
        uint256 tokens = 0;

        // Calculate token amount
        if (tokensSold < SOFT_CAP) {
            tokens = weiAmount.mul(RATE_SOFT);

            if (tokensSold.add(tokens) > SOFT_CAP) {
                uint256 softTokens = SOFT_CAP.sub(tokensSold);
                uint256 amountLeft = weiAmount.sub(softTokens.div(RATE_SOFT));

                tokens = softTokens.add(amountLeft.mul(RATE));
            }

        } else  {
            tokens = weiAmount.mul(RATE);
        }

        require(tokens > 0);
        require(tokensSold.add(tokens) <= FUNDING_GOAL);

        forwardFunds();
        assert(tokenReward.transferFrom(tokenOwner, beneficiary, tokens));

        balanceOf[beneficiary] = balanceOf[beneficiary].add(weiAmount);

        // Update totals
        weiRaised  = weiRaised.add(weiAmount);
        tokensSold = tokensSold.add(tokens);

        TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
    }

    // Send ether to the fund collection wallet
    function forwardFunds() internal {
        wallet.transfer(msg.value);
    }

    // @return true if the transaction can buy tokens
    function validPurchase() internal view returns (bool) {
        bool withinPeriod = now >= startTime && now <= endTime;
        bool nonZeroPurchase = msg.value != 0;
        bool hasTokens = tokensSold < FUNDING_GOAL;

        return withinPeriod && nonZeroPurchase && hasTokens;
    }

    function setStart(uint256 _start) public onlyOwner {
        startTime = _start;
    }

    function setEnd(uint256 _end) public onlyOwner {
        require(startTime < _end);
        endTime = _end;
    }

    // @return true if crowdsale event has ended
    function hasEnded() public view returns (bool) {
        return now > endTime;
    }

}

如果有人能指出我正确的方向,我将不胜感激。

无论我设置什么限制,我似乎总是“需要的气体超过限制”:

交易到 FastInvestTokenCrowdsale.buyTokens 错误:所需的气体超过限制:300000。重要的气体估计也可能是合约代码出现问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。交易到 FastInvestTokenCrowdsale.buyTokens 待处理 ... 交易到 FastInvestTokenCrowdsale.buyTokens 错误:所需气体超过块气体限制:3000000000000。重要的气体估计也可能是合约代码中存在问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。交易到 FastInvestTokenCrowdsale.buyTokens 待处理 ... 交易到 FastInvestTokenCrowdsale.buyTokens 错误:所需气体超过区块气体限制:3000000000000。重要的气体估计也可能是合约代码中存在问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。交易到 FastInvestTokenCrowdsale.buyTokens 待处理 ... 交易到 FastInvestTokenCrowdsale.buyTokens 错误:所需的气体超过了块气体限制:3000000000000000000000000000000000。重要的气体估计也可能是合约代码中存在问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。交易到 FastInvestTokenCrowdsale.buyTokens 待处理 ... 交易到 FastInvestTokenCrowdsale.buyTokens 错误:所需气体超过区块气体限制:300000000000000000000000000000000。重要的气体估计也可能是合约代码中存在问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。transact to FastInvestTokenCrowdsale.buyTokens pending ... transact to FastInvestTokenCrowdsale.buyTokens errored: Gas required exceeds block gas limit: 30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000. An important gas estimation might also be the sign of a problem in the contract code. 请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。交易到 FastInvestTokenCrowdsale.buyTokens 待定 ... 交易到 FastInvestTokenCrowdsale.buyTokens 错误:所需的气体超过了区块气体限制:3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 的一个重要问题。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。transact to FastInvestTokenCrowdsale.buyTokens pending ... transact to FastInvestTokenCrowdsale.buyTokens errored: Gas required exceeds block gas limit: 30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000. An important gas estimation might also be the sign of a problem in the contract code. 请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。transact to FastInvestTokenCrowdsale.buyTokens pending ... transact to FastInvestTokenCrowdsale.buyTokens errored: Gas required exceeds block gas limit: 30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000. An important gas estimation might also be the sign of a problem in the contract code. 请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。交易到 FastInvestTokenCrowdsale.buyTokens 待处理 ... 交易到 FastInvestTokenCrowdsale.buyTokens 错误:所需气体超过限制:10。一个重要的 gas 估计也可能是合约代码中存在问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。交易到 FastInvestTokenCrowdsale.buyTokens 待处理 ... 交易到 FastInvestTokenCrowdsale.buyTokens 错误:所需气体超过限制:1. 重要的气体估计也可能是合约代码出现问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。向 FastInvestTokenCrowdsale.buyTokens 交易错误:所需气体超过限制:1. 重要的气体估计也可能是合约代码出现问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。向 FastInvestTokenCrowdsale.buyTokens 交易错误:所需气体超过限制:1. 重要的气体估计也可能是合约代码出现问题的迹象。请检查循环并确保您没有将值发送到非应付函数(这也是强气体估计的原因)。

4

0 回答 0