从Solidity合同转移价值到使用Web3.js的Ganache账户

时间:2018-05-08 15:56:37

标签: javascript ethereum solidity web3 remix

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

这是我的可靠性功能,可以在混音中正常工作:

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表单。

当我点按绿色按钮并尝试将以太网发送到另一个帐户时,我的metamask enter image description here

中出现此错误

我有什么想法可以让它发挥作用?感谢。

1 个答案:

答案 0 :(得分: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 
});

请注意,我故意将事务对象中的值更改为变量,因为web3.eth.accountweb3.eth.getBalance的同步版本在1.0中不可用,并且最佳做法是使用异步版本(使用回调)在0.20.x中也是如此。

相关问题