javascript - 使用 Web3.js 将值(value)从 Solidity 合约转移到 Ganache 账户

标签 javascript ethereum solidity web3js remix

我正在尝试从 UI 将契约(Contract)余额转移到 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 表单。

当我点击绿色按钮并尝试将以太发送到其他帐户时,我的元掩码中出现此错误 enter image description here

有什么想法可以让它发挥作用吗?

最佳答案

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 中使用异步版本(使用回调)。

关于javascript - 使用 Web3.js 将值(value)从 Solidity 合约转移到 Ganache 账户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50237775/

相关文章:

javascript - LESS.js - 错误消息 "failed to save"

solidity - Uniswap V2 ERC20 代币可靠性代码 : Are token names hard coded?

casting - Solidity: 错误:请将数字作为字符串或 BN 对象传递以避免精度错误

unit-testing - 编写混音单元测试时出错 : URL not parseable: remix_accounts. sol

javascript - 如果已经使用了两个选择框组合,如何使用 jQuery 进行验证

javascript - try/catch 的替代方法,用于检查 javascript 中是否存在函数

javascript - 如何将可点击的 HTML 链接添加到 Highcharts 5.0.x 的工具提示中?

javascript - MetaMask - RPC 错误 : Error: [ethjs-query] while formatting outputs from RPC

node.js - 发送 ERC-20 代币失败。交易成功,但没有发送 token

token - 使用 web3 Ropsten Infura 测试网传输 ERC20 代币