Solidity
UnhandledPromiseRejectionWarning:錯誤:交易未在 750 秒內開採,請確保您的交易已正確發送
我正在嘗試執行以下程式碼:
// Import modules var Web3 = require("web3"); const HDWalletProvider = require("truffle-hdwallet-provider"); const { interface, bytecode } = require("./compile"); // Get provider link! const Project_ID_Infura = "put your own one"; const apiKey = "https://rinkeby.infura.io/v3/" + Project_ID_Infura; const Menmonic_Phrase_2 = "Create your own wallet"; const provider = new HDWalletProvider(Menmonic_Phrase_2, apiKey); // Create a web3 instance const web3 = new Web3(provider); const deploy = async () => { const accounts = await web3.eth.getAccounts(); console.log("Attempting to deploy from account", accounts[0]); const result = await new web3.eth.Contract(JSON.parse(interface)) .deploy({ data: bytecode, arguments: ["Hi there!"] }) .send({ gas: "1000000", gasPrice: "5000000000", from: accounts[0] }); // instance with the new contract address console.log("Contract deployed to", result.options.address); }; deploy();
compile.js 在這裡:
const path = require("path"); const fs = require("fs"); const solc = require("solc"); const inboxPath = path.resolve(__dirname, "contracts", "Inbox.sol"); const source = fs.readFileSync(inboxPath, "utf8"); // console.log(solc.compile(source, 1)); a = solc.compile(source, 1).contracts[":Inbox"]; module.exports = a; // console.log(a.interface);
Inbox.sol 程式碼位於工作目錄中名為 contracts 的單獨文件夾中,具有以下程式碼:
pragma solidity ^0.4.17; contract Inbox { string public message; function Inbox(string initialMessage) public { message=initialMessage; } function setMessage(string newMessage) public { message=newMessage; } }
我收到以下錯誤:
(節點:29588)UnhandledPromiseRejectionWarning:錯誤:交易未在 750 秒內開採,請確保您的交易已正確發送。請注意,它可能仍被開採!
我不知道是什麼問題,我確實有足夠的測試乙太幣,而且我給出了一個不錯的汽油價格。請幫我理解問題。
我非常感謝為我提供答案的人,如果不是他,我不會理解它。
所以主要問題是gasPrice存在不確定性,似乎有更新包考慮到了這一點。
因此,一旦您看到我在問題中寫的這個錯誤,這些就是需要採取的步驟。
- 我訪問這個網路應用程序以取消最後一個交易,它要求我連接到 Metamask 並在 Rinkeby 測試網上傳輸 .001 測試乙太!
- 我安裝了最新的錢包提供商庫,它消除了 gasPrice 的必要性,因為這將在內部處理。轉到 PowerShell 視窗和程式碼
npm install @truffle/hdwallet-provider
- 更改程式碼中的一些修飾行,例如刪除 gasPrice 和更新所需的包。在 deploy.js 底部添加 provider.engine.stop() 呼叫行以支持優雅退出
新程式碼由下式給出
// Import modules var Web3 = require("web3"); // const HDWalletProvider = require("truffle-hdwallet-provider"); const HDWalletProvider = require("@truffle/hdwallet-provider"); const { interface, bytecode } = require("./compile"); // Get provider link! const Project_ID_Infura = "Put your own id"; const apiKey = "https://rinkeby.infura.io/v3/" + Project_ID_Infura; const Menmonic_Phrase_2 = "Put your own Mnemonic"; const provider = new HDWalletProvider(Menmonic_Phrase_2, apiKey); // Create a web3 instance // const web3 = new Web3(provider); const ganache = require("ganache-cli"); const web3 = new Web3(ganache.provider()); const deploy = async () => { const accounts = await web3.eth.getAccounts(); console.log("Attempting to deploy from account", accounts[0]); const result = await new web3.eth.Contract(JSON.parse(interface)) .deploy({ data: bytecode, arguments: ["Hi there!"] }) .send({ gas: "1000000", from: accounts[0] }); // instance with the new contract address console.log("Contract deployed to", result.options.address); provider.engine.stop(); }; deploy();