Crowdsale OpenZeppelin合同中的buyTokens func在msg.value大于零时不起作用

时间:2018-11-07 15:27:28

标签: unit-testing solidity truffle

我有基于OpenZepelin Crowdsale.sol编写的Crowdsale合同 我的合同:

contract MoonShardToken is StandardToken, Ownable {
    string public name = "MyContract";
    uint8 public decimals = 18;
    string public symbol = "My";
    /// this constructor added as a test
    constructor() public {
        totalSupply_ = (36000000 * (10 ** 18));
        balances[msg.sender] = totalSupply_;
    } 
}

我的众筹是OpenZepelin Crowdsale.sol的副本,我用松露部署我的合同,我可以console.login测试我的速度:200000000000000 我的令牌的TotalSupply:3.6e + 25,来自合同的测试中的所有功能都可以正常工作,但是当我使用功能buyTokens的按摩值大于零时,我变成错误VM异常在处理事务时:还原;当msg.value为零时,我成为了没有错误,并且发出了func中的事件 这是功能buyTokens:

function buyTokens(address _beneficiary) public payable {
        uint256 weiAmount = msg.value;
        // _preValidatePurchase(_beneficiary, weiAmount); this is function with required msg.value>0 was commented for test
        uint256 tokens = _getTokenAmount(weiAmount);
        weiRaised = weiRaised.add(weiAmount);
        _processPurchase(_beneficiary, tokens);
        emit TokenPurchase(
            msg.sender,
            _beneficiary,
            weiAmount,
            tokens
        );
        _forwardFunds();
   }

在松露测试中,我将此功能用作:await Crowdsale.buyTokens(wallet,{from:wallet,value:web3.toWei(0.0001,“ ether”)}); 钱包是帐户(1),令牌余额为0。我使用ganache cli,我的所有acc都有以太币?简单的交易正常工作。

1 个答案:

答案 0 :(得分:0)

尝试buyTokens时,此函数调用_getTokenAmount(weiAmount),它返回weiAmount.mul(_rate)。您可以发布迁移文件吗?可能是在部署期间未正确设置_rate。