将固定供应令牌合同中的ERC20令牌发送到TokenCrowdSale合同

时间:2018-08-24 05:58:22

标签: token blockchain ethereum truffle erc20

对于ICO Crowd Sale,我创建了2个合约,其中一个是MyToken.sol,另一个是MyTokenCrowdSale.sol。 MyToken.sol创建固定供应的ERC20令牌。

contract MyToken is DetailedERC20, StandardToken {

    /**
    * Init token by setting its total supply
    *
    * @param totalSupply total token supply
    */
    function MyToken(
        uint256 totalSupply
    ) DetailedERC20(
        "My Token",
        "MYE",
        18
    ) {
        totalSupply_ = totalSupply;
        balances[msg.sender] = totalSupply;
    }
}

另一个合同MyTokenCrowdSale是一项众包合同,买方/受益人在该合同上发送以太坊,并在验证后作为回报,仅管理员将相应的代币转移给买方。

pragma solidity ^0.4.21;

import "../node_modules/zeppelin-solidity/contracts/crowdsale/Crowdsale.sol";
import '../node_modules/zeppelin-solidity/contracts/ownership/Ownable.sol';
import '../node_modules/zeppelin-solidity/contracts/math/SafeMath.sol';

contract MyTokenCrowdSale is Crowdsale, Ownable {
    using SafeMath for uint256;

    //Decimal Facotor
    uint256 private constant decimalFactor = 10**uint256(18);

    // Map of all purchaiser's balances
    mapping(address => uint256) public balances;

    uint256 public tokensIssued;

    ERC20 public tokenReward;

    // Amount raised in PreICO
    // ==================
    // uint256 public totalWeiRaisedDuringPreICO;
    // ===================

    function MyTokenCrowdSale(
        uint256 _rate,
        address _wallet,
        ERC20 _token
    ) Crowdsale(
        _rate,
        _wallet,
        _token
    ){
        tokenReward = ERC20(_token);
    }

    // Token Purchase
    // =========================
    function () external payable {
          buyTokens(msg.sender);
    }

    /**
     * @dev Checks whether the period in which the crowdsale is open has already elapsed.
     * @return Whether crowdsale period has elapsed
     */
    function hasClosed() public view returns (bool) {
        return closed;
    }

    /**
     * @dev Closes the period in which the crowdsale is open.
     */
    function closeCrowdsale(bool closed_) public onlyOwner {
        closed = closed_;
    }

    /**
     * @dev Overrides parent by storing balances instead of issuing tokens right away.
     * @param _beneficiary Token purchaser
     * @param _tokenAmount Amount of tokens purchased
     */
    function _processPurchase(address _beneficiary, uint256 _tokenAmount) internal {
        require(!hasClosed()); 
        balances[_beneficiary] = balances[_beneficiary].add(_tokenAmount);
        tokensIssued = tokensIssued.add(_tokenAmount);
    }

    /**
     * @dev Deliver tokens to receiver_ after crowdsale ends.
     */
    function withdrawTokensFor(address receiver_) public onlyOwner {
        emit Test("Testing withdrawTokensFor ");
        _withdrawTokensFor(receiver_);
    }

    /**
     * @dev Withdraw tokens for receiver_ after crowdsale ends.
     */
    function _withdrawTokensFor(address receiver_) internal {
        require(hasClosed());
        uint256 amount = balances[receiver_];
        require(amount > 0);
        balances[receiver_] = 0;
        emit TokenDelivered(receiver_, amount);
       _deliverTokens(receiver_, amount);
    }

}

我的问题是我无法将令牌转让给用户。它重复地说:

Error: VM Exception while processing transaction: revert
    at Object.InvalidResponse

调试问题时,我发现唯一的问题是

_deliverTokens(receiver_, amount); 

不允许向用户发送令牌。可能是因为MyTokenCrowdSale没有可访问的令牌或其他令牌。

需要帮助!

0 个答案:

没有答案