Jelajahi Sumber

[contract-manager] Utility script to fetch fees on different contracts (#1026)

* Utility script to fetch fees on different contracts
Mohammad Amin Khashkhashi Moghaddam 2 tahun lalu
induk
melakukan
f316a512c3

+ 35 - 0
contract_manager/scripts/fetch_fees.ts

@@ -0,0 +1,35 @@
+import yargs from "yargs";
+import { hideBin } from "yargs/helpers";
+import {
+  AptosContract,
+  CosmWasmContract,
+  DefaultStore,
+  EvmContract,
+} from "../src";
+import { PriceServiceConnection } from "@pythnetwork/price-service-client";
+
+const parser = yargs(hideBin(process.argv))
+  .usage("Usage: $0")
+  .options({
+    testnet: {
+      type: "boolean",
+      default: false,
+      desc: "Fetch testnet contract fees instead of mainnet",
+    },
+  });
+
+async function main() {
+  const argv = await parser.argv;
+  for (const contract of Object.values(DefaultStore.contracts)) {
+    if (contract.getChain().isMainnet() === argv.testnet) continue;
+    if (
+      contract instanceof AptosContract ||
+      contract instanceof EvmContract ||
+      contract instanceof CosmWasmContract
+    ) {
+      console.log(`${contract.getId()} ${await contract.getTotalFee()}`);
+    }
+  }
+}
+
+main();

+ 6 - 0
contract_manager/src/contracts/aptos.ts

@@ -2,6 +2,7 @@ import { Contract, PriceFeed } from "../base";
 import { AptosAccount, BCS, TxnBuilderTypes } from "aptos";
 import { AptosChain, Chain } from "../chains";
 import { DataSource } from "xc_admin_common";
+import { CoinClient } from "aptos";
 
 export class AptosContract extends Contract {
   static type: string = "AptosContract";
@@ -174,6 +175,11 @@ export class AptosContract extends Contract {
     return AptosContract.type;
   }
 
+  async getTotalFee(): Promise<bigint> {
+    const client = new CoinClient(this.chain.getClient());
+    return await client.checkBalance(this.stateId);
+  }
+
   async getValidTimePeriod() {
     const data = (await this.findResource("StalePriceThreshold")) as any;
     return Number(data.threshold_secs);

+ 9 - 0
contract_manager/src/contracts/cosmwasm.ts

@@ -368,6 +368,15 @@ export class CosmWasmContract extends Contract {
     return this.chain;
   }
 
+  async getTotalFee(): Promise<bigint> {
+    const client = await CosmWasmClient.connect(this.chain.endpoint);
+    const coin = await client.getBalance(
+      this.address,
+      this.getChain().feeDenom
+    );
+    return BigInt(coin.amount);
+  }
+
   async getValidTimePeriod() {
     let client = await CosmWasmClient.connect(this.chain.endpoint);
     let result = await client.queryContractSmart(

+ 5 - 0
contract_manager/src/contracts/evm.ts

@@ -346,6 +346,11 @@ export class EvmContract extends Contract {
     return Web3.utils.keccak256(strippedCode);
   }
 
+  async getTotalFee(): Promise<bigint> {
+    const web3 = new Web3(this.chain.getRpcUrl());
+    return BigInt(await web3.eth.getBalance(this.address));
+  }
+
   async getLastExecutedGovernanceSequence() {
     const pythContract = await this.getContract();
     return Number(

+ 2 - 0
contract_manager/store/chains/CosmWasmChains.yaml

@@ -3,6 +3,7 @@
   wormholeChainName: injective
   mainnet: true
   type: CosmWasmChain
+  feeDenom: inj
 - endpoint: https://rpc.atlantic-2.seinetwork.io/
   id: sei_testnet_atlantic_2
   wormholeChainName: sei_testnet_atlantic_2
@@ -23,6 +24,7 @@
   id: injective_testnet
   wormholeChainName: injective_testnet
   mainnet: false
+  feeDenom: inj
   type: CosmWasmChain
 - endpoint: https://rpc-palvus.pion-1.ntrn.tech/
   id: neutron_testnet_pion_1