Răsfoiți Sursa

fix(price_pusher): wait on tx to succeed

Ali Behjati 2 ani în urmă
părinte
comite
37ff02f1f9
3 a modificat fișierele cu 31 adăugiri și 4 ștergeri
  1. 1 1
      package-lock.json
  2. 1 1
      price_pusher/package.json
  3. 29 2
      price_pusher/src/aptos/aptos.ts

+ 1 - 1
package-lock.json

@@ -55997,7 +55997,7 @@
     },
     "price_pusher": {
       "name": "@pythnetwork/price-pusher",
-      "version": "5.4.7",
+      "version": "5.4.8",
       "license": "Apache-2.0",
       "dependencies": {
         "@injectivelabs/sdk-ts": "1.10.72",

+ 1 - 1
price_pusher/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@pythnetwork/price-pusher",
-  "version": "5.4.7",
+  "version": "5.4.8",
   "description": "Pyth Price Pusher",
   "homepage": "https://pyth.network",
   "main": "lib/index.js",

+ 29 - 2
price_pusher/src/aptos/aptos.ts

@@ -4,7 +4,7 @@ import {
   PriceInfo,
   PriceItem,
 } from "../interface";
-import { AptosAccount, AptosClient, TxnBuilderTypes } from "aptos";
+import { AptosAccount, AptosClient } from "aptos";
 import { DurationInSeconds } from "../utils";
 import { PriceServiceConnection } from "@pythnetwork/price-service-client";
 
@@ -148,10 +148,17 @@ export class AptosPricePusher implements IPricePusher {
       const pendingTx = await client.submitTransaction(signedTx);
 
       console.log("Successfully broadcasted txHash:", pendingTx.hash);
+
+      // Sometimes broadcasted txs don't make it on-chain and they cause our sequence number
+      // to go out of sync. Missing transactions are rare and we don't want this check to block
+      // the next price update. So we use spawn a promise without awaiting on it to wait for the
+      // transaction to be confirmed and if it fails, it resets the sequence number and return.
+      this.waitForTransactionConfirmation(client, pendingTx.hash);
+
       return;
     } catch (e: any) {
       console.error("Error executing messages");
-      console.log(e);
+      console.error(e);
 
       // Reset the sequence number to re-sync it (in case that was the issue)
       this.lastSequenceNumber = undefined;
@@ -160,6 +167,26 @@ export class AptosPricePusher implements IPricePusher {
     }
   }
 
+  // Wait for the transaction to be confirmed. If it fails, reset the sequence number.
+  private async waitForTransactionConfirmation(
+    client: AptosClient,
+    txHash: string
+  ): Promise<void> {
+    try {
+      await client.waitForTransaction(txHash, {
+        checkSuccess: true,
+        timeoutSecs: 10,
+      });
+
+      console.log(`Transaction with txHash "${txHash}" confirmed.`);
+    } catch (e) {
+      console.error(`Transaction with txHash "${txHash}" failed to confirm.`);
+      console.error(e);
+
+      this.lastSequenceNumber = undefined;
+    }
+  }
+
   // Try to get the next sequence number for account. This function uses a local cache
   // to predict the next sequence number if possible; if not, it fetches the number from
   // the blockchain itself (and caches it for later).