From f316a512c346d11ad11ca2951573b22f1832e455 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Khashkhashi Moghaddam Date: Wed, 23 Aug 2023 16:28:27 +0200 Subject: [PATCH] [contract-manager] Utility script to fetch fees on different contracts (#1026) * Utility script to fetch fees on different contracts --- contract_manager/scripts/fetch_fees.ts | 35 +++++++++++++++++++ contract_manager/src/contracts/aptos.ts | 6 ++++ contract_manager/src/contracts/cosmwasm.ts | 9 +++++ contract_manager/src/contracts/evm.ts | 5 +++ .../store/chains/CosmWasmChains.yaml | 2 ++ 5 files changed, 57 insertions(+) create mode 100644 contract_manager/scripts/fetch_fees.ts diff --git a/contract_manager/scripts/fetch_fees.ts b/contract_manager/scripts/fetch_fees.ts new file mode 100644 index 00000000..4d234787 --- /dev/null +++ b/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(); diff --git a/contract_manager/src/contracts/aptos.ts b/contract_manager/src/contracts/aptos.ts index c53e52dc..81f56012 100644 --- a/contract_manager/src/contracts/aptos.ts +++ b/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 { + 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); diff --git a/contract_manager/src/contracts/cosmwasm.ts b/contract_manager/src/contracts/cosmwasm.ts index 46241f15..d1f5fdf4 100644 --- a/contract_manager/src/contracts/cosmwasm.ts +++ b/contract_manager/src/contracts/cosmwasm.ts @@ -368,6 +368,15 @@ export class CosmWasmContract extends Contract { return this.chain; } + async getTotalFee(): Promise { + 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( diff --git a/contract_manager/src/contracts/evm.ts b/contract_manager/src/contracts/evm.ts index c1a3ef27..4f1fba55 100644 --- a/contract_manager/src/contracts/evm.ts +++ b/contract_manager/src/contracts/evm.ts @@ -346,6 +346,11 @@ export class EvmContract extends Contract { return Web3.utils.keccak256(strippedCode); } + async getTotalFee(): Promise { + const web3 = new Web3(this.chain.getRpcUrl()); + return BigInt(await web3.eth.getBalance(this.address)); + } + async getLastExecutedGovernanceSequence() { const pythContract = await this.getContract(); return Number( diff --git a/contract_manager/store/chains/CosmWasmChains.yaml b/contract_manager/store/chains/CosmWasmChains.yaml index a39a7f80..8d19ead2 100644 --- a/contract_manager/store/chains/CosmWasmChains.yaml +++ b/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