【问题标题】:Error: Invalid number of arguments to Solidity function in simple crowdsale contract错误:简单众筹合约中 Solidity 函数的参数数量无效
【发布时间】:2018-11-30 18:34:10
【问题描述】:

我是智能合约开发的初学者。我正在使用 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") )

【问题讨论】:

    标签: solidity truffle openzeppelin


    【解决方案1】:

    在实现payable 时,支付的金额不能作为参数,而是可以在msg.value 获得。否则你不会发送任何以太币,和/或可以被利用,如果我调用该方法: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

    【讨论】:

    • 非常感谢。现在可以了。你能告诉我你是怎么找到这个规则的吗?是来自solidity 文档吗?
    • 关于我是怎么知道的,它是常识、web3 文档和可靠性的混合体。它没有完全固定在一页上,所以我无法链接到它。
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 2018-09-29
    • 2018-10-24
    • 2022-08-20
    • 2019-09-11
    • 2018-02-20
    • 2018-09-16
    • 1970-01-01
    相关资源
    最近更新 更多