![](/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.