فهرست منبع

Add fee estimate for terra relay (#112)

Ali Behjati 3 سال پیش
والد
کامیت
637d529918

+ 1 - 1
third_party/pyth/p2w-terra-relay/package-lock.json

@@ -17,6 +17,7 @@
         "@typechain/ethers-v5": "^7.0.1",
         "@types/express": "^4.17.13",
         "async-mutex": "^0.3.2",
+        "axios": "^0.24.0",
         "body-parser": "^1.19.0",
         "condition-variable": "^1.0.0",
         "cors": "^2.8.5",
@@ -31,7 +32,6 @@
         "@types/jest": "^27.0.2",
         "@types/long": "^4.0.1",
         "@types/node": "^16.6.1",
-        "axios": "^0.24.0",
         "esm": "^3.2.25",
         "ethers": "^5.4.4",
         "jest": "^27.3.1",

+ 1 - 1
third_party/pyth/p2w-terra-relay/package.json

@@ -20,7 +20,6 @@
     "@types/jest": "^27.0.2",
     "@types/long": "^4.0.1",
     "@types/node": "^16.6.1",
-    "axios": "^0.24.0",
     "esm": "^3.2.25",
     "ethers": "^5.4.4",
     "jest": "^27.3.1",
@@ -39,6 +38,7 @@
     "@typechain/ethers-v5": "^7.0.1",
     "@types/express": "^4.17.13",
     "async-mutex": "^0.3.2",
+    "axios": "^0.24.0",
     "body-parser": "^1.19.0",
     "condition-variable": "^1.0.0",
     "cors": "^2.8.5",

+ 28 - 2
third_party/pyth/p2w-terra-relay/src/relay/terra.ts

@@ -6,12 +6,13 @@ import {
   MsgExecuteContract,
 } from "@terra-money/terra.js";
 import { hexToUint8Array } from "@certusone/wormhole-sdk";
-import { redeemOnTerra } from "@certusone/wormhole-sdk";
-
+import axios from "axios";
 import { logger } from "../helpers";
 
 import { Relay, RelayResult, RelayRetcode, PriceId } from "./iface";
 
+export const TERRA_GAS_PRICES_URL = "https://fcd.terra.dev/v1/txs/gas_prices";
+
 export class TerraRelay implements Relay {
   readonly nodeUrl: string;
   readonly terraChainId: string;
@@ -80,10 +81,35 @@ export class TerraRelay implements Relay {
         msgs.push(msg);
       }
 
+      let gasPrices, feeEstimate;
+      try {
+       gasPrices = await axios
+        .get(TERRA_GAS_PRICES_URL)
+        .then((result) => result.data);
+        
+        feeEstimate = await lcdClient.tx.estimateFee(
+          [{
+            sequenceNumber: await wallet.sequence(),
+            publicKey: wallet.key.publicKey,
+          }],
+          {
+            msgs: [...msgs],
+            memo: "P2T",
+            feeDenoms: [this.coin],
+            gasPrices
+          }
+        )
+      } catch (e: any) {
+        logger.warn("Couldn't fetch gas price and fee estimate. Using default values");
+        logger.warn(e, e.stack);
+      }
+
       const tx = await wallet.createAndSignTx({
         msgs: msgs,
         memo: "P2T",
         feeDenoms: [this.coin],
+        gasPrices,
+        fee: feeEstimate
       });
 
       logger.debug("TIME: sending msg");