![](/img/trans.png)
[英]Call specific solidity function from deployed smart contract within react through javascript (web3.js)
[英]Transfer Value From Solidity Contract To Ganache Account With Web3.js
我正在尝试将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表单。
当我点击绿色按钮并尝试将以太币发送到另一个帐户时,我在Metamask中收到此错误
有什么想法可以使它起作用吗? 谢谢。
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也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.