简体   繁体   中英

I am getting a transaction was replaced error in ether js while using transaction.wait()

I'm using ether.js to create a dapp.I want to input something in my database when something modifies on a blockchain.So I am using this for the check const receipt = await transaction.wait(1); Once the transaction receipt is received it means it has been mined successfully. But the problem is when I am speeding up the transaction I get an error saying "TRANSACTION REPLACED" because I tried to send transaction again using the same nonce.The block is mined on the network but on frontend I'm getting this error. How to handle this error? This is the explanation of this error on docs.

This error is thrown when waiting for a transaction which has been replaced by another, by the sender submitting a second transaction with the same nonce, while the transaction was pending in the transaction pool.

Just found out what the core problem is - https://medium.com/the-capital/ethereum-speed-up-transactions-how-they-work-and-what-they-mean-for-dapps-6d4ba1068580

But their service is paid.

error from console log- index.ts:261

   Uncaught (in promise) Error: transaction was replaced [ See: https://links.ethers.org/v5-errors-TRANSACTION_REPLACED ] (cancelled=false, reason="repriced", replacement={"hash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","type":2,"accessList":[],"blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6","blockNumber":27034014,"transactionIndex":4,"confirmations":1,"from":"0xB157e57e493167491c8EE69681D89873a9D68B1C","gasPrice":{"type":"BigNumber","hex":"0x08896c4c12"},"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x08896c4c0b"},"maxFeePerGas":{"type":"BigNumber","hex":"0x08896c4c16"},"gasLimit":{"type":"BigNumber","hex":"0x6e80"},"to":"0x92265B57f08EF2F30dDd6d9CdCac1BD62C1A004b","value":{"type":"BigNumber","hex":"0x038d7ea4c68000"},"nonce":38,"data":"0xf340fa01000000000000000000000000b157e57e493167491c8ee69681d89873a9d68b1c","r":"0x96346c519bd65bbe2550a1396757e92f89c4d87246bf0bd4c3a8c8d315a6e78c","s":"0x332b38044b3efcd7bbe8e7b7e860024fb13306ce47c37a7b2168765c6e82f7e1","v":0,"creates":null,"chainId":80001}, hash="0x1a7442367d14df07d598aeb0ac9479926bee663637e11d535fa698cd9fd8f113", receipt={"to":"0x92265B57f08EF2F30dDd6d9CdCac1BD62C1A004b","from":"0xB157e57e493167491c8EE69681D89873a9D68B1C","contractAddress":null,"transactionIndex":4,"gasUsed":{"type":"BigNumber","hex":"0x6e80"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000008000100000000000008000000000000000000000000000000000000000000000000000000000800000000000000000000100048000004000000000000000080000000010000000000000000000000080000000000000000000000000000000000000000000000000080001000080000000000000000000200000000000000000000000000000004000000400000100000000000000004000000000000000020001000000000000000000000000800000108040000000000000000000000000000000000000000000000000000000400000000000100000","blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6","transactionHash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","logs":[{"transactionIndex":4,"blockNumber":27034014,"transactionHash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","address":"0x0000000000000000000000000000000000001010","topics":["0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4","0x0000000000000000000000000000000000000000000000000000000000001010","0x000000000000000000000000b157e57e493167491c8ee69681d89873a9d68b1c","0x00000000000000000000000092265b57f08ef2f30ddd6d9cdcac1bd62c1a004b"],"data":"0x00000000000000000000000000000000000000000000000000038d7ea4c680000000000000000000000000000000000000000000000000004a10eca800d2aa93000000000000000000000000000000000000000000000000035801792bb0d0000000000000000000000000000000000000000000000000004a0d5f295c0c2a93000000000000000000000000000000000000000000000000035b8ef7d0775000","logIndex":10,"blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6"},{"transactionIndex":4,"blockNumber":27034014,"transactionHash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","address":"0x92265B57f08EF2F30dDd6d9CdCac1BD62C1A004b","topics":["0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c"],"data":"0x000000000000000000000000b157e57e493167491c8ee69681d89873a9d68b1c00000000000000000000000000000000000000000000000000038d7ea4c68000","logIndex":11,"blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6"},{"transactionIndex":4,"blockNumber":27034014,"transactionHash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","address":"0x0000000000000000000000000000000000001010","topics":["0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63","0x0000000000000000000000000000000000000000000000000000000000001010","0x000000000000000000000000b157e57e493167491c8ee69681d89873a9d68b1c","0x000000000000000000000000be188d6641e8b680743a4815dfa0f6208038960f"],"data":"0x0000000000000000000000000000000000000000000000000003af513ed2bf800000000000000000000000000000000000000000000000004a149bf93fa86f93000000000000000000000000000000000000000000001d2f81f5cd2c083658f80000000000000000000000000000000000000000000000004a10eca800d5b013000000000000000000000000000000000000000000001d2f81f97c7d47091878","logIndex":12,"blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6"}],"blockNumber":27034014,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x06733e"},"effectiveGasPrice":{"type":"BigNumber","hex":"0x08896c4c12"},"status":1,"type":2,"byzantium":true}, code=TRANSACTION_REPLACED, version=providers/5.6.8)
at Logger.makeError (index.ts:261:1)
at Web3Provider.<anonymous> (base-provider.ts:1372:1)
at Generator.next (<anonymous>)
at fulfilled (base-provider.ts:1:1)





 if (typeof window.ethereum !== "undefined" && logInState === true) {
      const provider = new ethers.providers.Web3Provider(window.ethereum);
      const signer = provider.getSigner();

      const contract = new ethers.Contract(gambleAddress, Gamble.abi, signer);
      const transaction = await contract.deposit(acc, {
        value: ethers.utils.parseEther(matic.toString()),
      });



 const receipt = await transaction.wait(1);
  console.log(receipt);
  if (receipt) {
    updateDepositInDb();
  } else {
    alert("transaction failed");
  }

You should surround your code with try catch to handle exceptions.

You may refer to the link below to find out how to detect replacement transactions in your scenarios.

https://blog.ricmoo.com/highlights-ethers-js-may-2021-2826e858277d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM