Javascript 使用Web3.js将价值从Solidity合同转移到Ganache帐户

Javascript 使用Web3.js将价值从Solidity合同转移到Ganache帐户,javascript,ethereum,solidity,web3,remix,Javascript,Ethereum,Solidity,Web3,Remix,我正试图将合同余额从UI转移到ganache帐户 这是我的solidity函数,在混音中效果很好: function tapGreen(address _receiverAddress) onlySwiper payable public { _receiverAddress.transfer(this.balance); } 这是我的js文件中的内容 swipeRight: function() { console.log(addressInput.value);

我正试图将合同余额从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表单

当我点击绿色按钮并尝试将以太发送到另一个帐户时,我的元掩码中出现了此错误


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

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

这是您的意思吗<代码>swipeRight:function(){console.log(addressInput.value);var swipeRight=web3.eth.getBalance(contracts.address);var accountOne=web3.eth.accounts[1];App.contracts.DatingHackathon.deployed()。然后(function(instance){return instance.tapGreen.sendTransaction(addressInput.value),{from:accountOne,value:swipeRight};}仍然收到一个错误,上面写着:“未捕获的引用错误:未定义合同”我正在使用web3版本0.2^我不知道什么是
contracts
。我猜你的意思是在那里使用
instance.address
。另外,正如我在回答中提到的,我建议使用
web3.eth.getBalance(contracts.address,(e,r)=>if(!e)balance=r)
很抱歉!这里是我给我的contracts变量赋值的地方
initContract:function(){$.getJSON('HackathonDapp.json',function(artifactsData){App.contracts.HackathonDapp=truflecontract(artifactsData);App.contracts.HackathonDapp.setProvider(App.web3Provider);console.log(“初始化的选举合同”);})返回App.bindEvents();},