Web3js
沒有足夠的資金來發送 Tx - TestRPC 和 Metamask
我正在測試並創建一個智能合約,以使用 TestRPC、Truffle 和 Metamask 將一些乙太幣發送到一個帳戶。
我有一個帶有按鈕的頁面,當我點擊該按鈕時,它會觸發事件 submitEtherWallet
submitEtherToWallet: function() { MyWallet.deployed().then(function(instance){ return instance.sendTransaction({from: account, to: instance.address, value: web3.toWei(5, 'ether')}); }).then(function(result) { App.basicInfoUpdate(); }); },
該方法顯然運作良好,因為元遮罩會打開一個彈出視窗來確認交易。
點擊送出交易後:
在解決這個問題幾個小時後,我意識到我的元遮罩顯然是在與我的 testrpc 不同的 web3 實例中執行,這是一個奇怪的情況。
Metamask 正在打開正確的帳戶彈出視窗以確認交易,但在後端 metamask 試圖用不同的帳戶發送交易,我不知道這是怎麼發生的,但我將 addEventListener 從 truffle webpack 替換回原來的它解決了這個問題。
window.addEventListener('load', function() { // Checking if Web3 has been injected by the browser (Mist/MetaMask) if (typeof web3 !== 'undefined') { console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask") // Use Mist/MetaMask's provider window.web3 = new Web3(web3.currentProvider); } else { console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask"); // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail) window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545")); } App.start(); });
正如您在列印螢幕中看到的那樣,metamask 試圖通過錢包“0xf9 …”而不是彈出視窗中的錢包發送交易。
您確定在程式碼中“來自:帳戶”是指帳戶嗎
$$ 0 $$?