Web3js

UnhandledPromiseRejectionWarning:錯誤:返回錯誤:nonce 太低

  • December 25, 2019

我正在嘗試在 Ropsten 網路上部署一些合約。這是我的程式碼

async function finalDeploy(r){

 for(i=0; i<constructorParams.length; i++) {

   const options = {
     data: '0x' + myBin[r]+constructorParams[i],
     gas: 5000000,
 };
 const signed = await web3.eth.accounts.signTransaction(options, privateKey1);
 const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); // here error occured 
 console.log("Contract: " +r+" is deployed at  " +receipt.contractAddress);
 contractAddressess.push(receipt.contractAddress)

 }

}

上面的程式碼在 Ganache UI 上執行良好,但是當我將其提供者從 localhost 更改為Web3.providers.HttpProvider("https://ropsten.infura.io/v3/7fb0bdc97cbe419..."); then我收到此錯誤;

UnhandledPromiseRejectionWarning: Error: Returned error: nonce too low
   at Object.ErrorResponse (C:\Users\aa\node_modules\web3-core-helpers\src\errors.js:29:16)
   at C:\Users\aa\node_modules\web3-core-requestmanager\src\index.js:140:36
   at XMLHttpRequest.request.onreadystatechange (C:\Users\aa\node_modules\web3-providers-http\src\index.js:102:13)
   at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\aa\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22)
   at XMLHttpRequest._setReadyState (C:\Users\aa\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14)
   at XMLHttpRequest._onHttpResponseEnd (C:\Users\aa\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14)
   at IncomingMessage.<anonymous> (C:\Users\aa\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61)
   at IncomingMessage.emit (events.js:203:15)
   at endReadableNT (_stream_readable.js:1143:12)
   at process._tickCallback (internal/process/next_tick.js:63:19)
(node:3816) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3816) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

再次執行它。

try更一般地說,在/塊內執行它catch直到成功。

對於您的特定錯誤,該節點可能與您帳戶的目前 nonce 不同步,因此您可能希望將其添加到您的對options像中:

nonce: await web3.eth.getTransactionCount(0xYourAccountAddress)

由於您可能還會遇到其他類型的錯誤,因此我建議您嘗試以下操作:

async function scan(message) {
   process.stdout.write(message);
   return await new Promise(function(resolve, reject) {
       process.stdin.resume();
       process.stdin.once("data", function(data) {
           process.stdin.pause();
           resolve(data.toString().trim());
       });
   });
}

async function getTransactionReceipt(web3) {
   while (true) {
       const hash = await scan("Enter transaction-hash or leave empty to retry: ");
       if (/^0x([0-9A-Fa-f]{64})$/.test(hash)) {
           const receipt = await web3.eth.getTransactionReceipt(hash);
           if (receipt)
               return receipt;
           console.log("Invalid transaction-hash");
       }
       else if (hash) {
           console.log("Illegal transaction-hash");
       }
       else {
           return null;
       }
   }
}

async function send(web3, account, data, gas) {
   while (true) {
       try {
           const options = {
               nonce: await web3.eth.getTransactionCount(account.address),
               data: "0x" + data,
               gas: gas
           };
           const signed = await web3.eth.accounts.signTransaction(options, account.privateKey);
           const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
           return receipt;
       }
       catch (error) {
           console.log(error.message);
           const receipt = await getTransactionReceipt(web3);
           if (receipt)
               return receipt;
       }
   }
}

async function finalDeploy(r) {
   const account = web3.eth.accounts.privateKeyToAccount(privateKey1);
   for (let i = 0; i < constructorParams.length; i++) {
       const receipt = await send(web3, account, myBin[r] + constructorParams[i], 5000000);
       console.log("Contract: " + r + " is deployed at " + receipt.contractAddress);
       contractAddressess.push(receipt.contractAddress)
   }
}

引用自:https://ethereum.stackexchange.com/questions/78484