代码之家  ›  专栏  ›  技术社区  ›  benjamin852

将价值从Solidity Contract转移到具有Web3的Ganache帐户。js公司

  •  0
  • benjamin852  · 技术社区  · 7 年前

    我正在尝试将合同余额从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) 
    });
    

    地址输入。值来自HTML表单。

    当我轻触绿色按钮并尝试将以太发送到其他帐户时,我在元掩码中得到此错误 enter image description here

    你知道我该怎么做吗?谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   johnny 5    7 年前

    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.account web3.eth.getBalance 在1.0中不可用,最好在0.20中使用异步版本(使用回调)。x也是。