0

我正在尝试将我的合同余额从 UI 转移到 ganache 帐户。

这是我的 Solidity 函数,在混音中效果很好:

function tapGreen(address _receiverAddress) onlySwiper payable public {
    _receiverAddress.transfer(this.balance); 
}

这是我的 js 文件中的内容

swipeRight: function() {
    console.log(addressInput.value);
    App.contracts.HackathonDapp.deployed().then(function (instance) {
        return instance.tapGreen(addressInput.value).sendTransaction ({
            from: web3.eth.accounts[1],
            value: web3.eth.getBalance(contracts.address) 
});

addressInput.value 来自 HTML 表单。

当我点击绿色按钮并尝试将以太币发送到另一个帐户时,我的元掩码中出现此错误在此处输入图像描述

有什么想法可以让这个工作吗?谢谢。

4

1 回答 1

1

web3 API 有时会令人困惑,因为 0.20.x 和 1.0 之间存在重大变化。很难判断您使用的是哪个版本。

如果您使用的是 0.20.x,则呼叫应该是

instance.tapGreen.sendTransaction(addressInput.value, {
    from: fromAccount,
    value: valueInWei 
});

如果您使用的是 1.0,则调用将是

instance.methods.tapGreen(addressInput.value).send({
    from: fromAccount,
    value: valueInWei 
});

请注意,我故意将事务对象中的值更改为变量,因为 1.0 中不提供 和 的同步版本,web3.eth.account并且web3.eth.getBalance在 0.20.x 中也使用异步版本(使用回调)是最佳实践。

于 2018-05-08T16:17:16.480 回答