Error: Invalid number of arguments to Solidity function in simple crowdsale contract

Error: Invalid number of arguments to Solidity function in simple crowdsale contract

我是智能合约开发的初学者。我正在使用 openZeppelin、truffle 和 Ganache 来开发一些非常基本的代币和众筹合同。当我尝试从 Truffle 控制台中的众筹合约调用 buytoken() 方法时遇到错误。有人可以帮我解决问题吗?迁移和部署合约时没有问题。

contract Crowdsale {
    using SafeMath for uint256;

// The token being sold
    ERC20 public token;

// Address where funds are collected
    address public wallet;

// How many token units a buyer gets per wei.
// The rate is the conversion between wei and the smallest and indivisible token unit.
// So, if you are using a rate of 1 with a DetailedERC20 token with 3 decimals called TOK
// 1 wei will give you 1 unit, or 0.001 TOK.
uint256 public rate;

// Amount of wei raised
    uint256 public weiRaised;

/**
* 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
);

/**
* @param _rate Number of token units a buyer gets per wei
* @param _wallet Address where collected funds will be forwarded to
* @param _token Address of the token being sold
*/
    constructor(uint256 _rate, address _wallet, ERC20 _token) public {
        require(_rate > 0);
        require(_wallet != address(0));
        require(_token != address(0));

        rate = _rate;
        wallet = _wallet;
        token = _token;
}

// -----------------------------------------
// Crowdsale external interface
// -----------------------------------------

/**
* @dev fallback function ***DO NOT OVERRIDE***
*/
function () external payable {

}

/**
* @dev low level token purchase ***DO NOT OVERRIDE***
* @param _beneficiary Address performing the token purchase
*/
    function buyTokens(address _beneficiary, uint256 amount) public payable {

        uint256 weiAmount = amount.mul(rate);

        token.transfer(_beneficiary, weiAmount);

}

truffle控制台命令如下:

myToken.deployed().then(function(i){BT = i})
myCrowdsale.deployed().then(function(i){BTC = i})
BT.transferOwnership(BTC.address)
purchaser = web3.eth.accounts[2]
BTC.buyTokens(purchaser,web3.toWei(5, "ether") )

实施 payable 时,支付的金额不得作为参数,但可以在 msg.value 中获得。否则你不会发送任何以太币,and/or 可以被利用,如果我用以下方法调用方法: 5 ether as amount 但我只发送 1 wei.

function buyTokens(address _beneficiary) public payable {

        uint256 weiAmount = msg.value.mul(rate);

        token.transfer(_beneficiary, weiAmount);

}

此外,如果受益人地址与购买代币的地址相同,则可以使用:msg.sender

并且该方法必须这样调用:

BTC.buyTokens(purchaser, { value: web3.toWei(5, "ether"), from: purchaser });

或使用msg.sender

BTC.buyTokens({ value: web3.toWei(5, "ether"), from: purchaser });

如果您不使用:from 以太币将由默认帐户发送,在您的情况下不是 purchaser