fetch_fees.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import yargs from "yargs";
  2. import { hideBin } from "yargs/helpers";
  3. import {
  4. AptosPriceFeedContract,
  5. CosmWasmPriceFeedContract,
  6. EvmPriceFeedContract,
  7. TonPriceFeedContract,
  8. } from "../src/core/contracts";
  9. import { DefaultStore } from "../src/node/utils/store";
  10. const parser = yargs(hideBin(process.argv))
  11. .usage("Usage: $0")
  12. .options({
  13. testnet: {
  14. type: "boolean",
  15. default: false,
  16. desc: "Fetch testnet contract fees instead of mainnet",
  17. },
  18. });
  19. async function main() {
  20. const argv = await parser.argv;
  21. const prices: Record<string, number> = {};
  22. for (const token of Object.values(DefaultStore.tokens)) {
  23. const price = await token.getPriceForMinUnit();
  24. // We're going to ignore the value of tokens that aren't configured
  25. // in the store -- these are likely not worth much anyway.
  26. if (price !== undefined) {
  27. prices[token.id] = price;
  28. }
  29. }
  30. let totalFeeUsd = 0;
  31. for (const contract of Object.values(DefaultStore.contracts)) {
  32. if (contract.getChain().isMainnet() === argv.testnet) continue;
  33. if (
  34. contract instanceof AptosPriceFeedContract ||
  35. contract instanceof EvmPriceFeedContract ||
  36. contract instanceof CosmWasmPriceFeedContract ||
  37. contract instanceof TonPriceFeedContract
  38. ) {
  39. try {
  40. const fee = await contract.getTotalFee();
  41. let feeUsd = 0;
  42. if (fee.denom !== undefined && prices[fee.denom] !== undefined) {
  43. feeUsd = Number(fee.amount) * prices[fee.denom];
  44. totalFeeUsd += feeUsd;
  45. console.log(
  46. `${contract.getId()} ${fee.amount} ${fee.denom} ($${feeUsd})`,
  47. );
  48. } else {
  49. console.log(
  50. `${contract.getId()} ${fee.amount} ${fee.denom} ($ value unknown)`,
  51. );
  52. }
  53. } catch (e) {
  54. console.error(`Error fetching fees for ${contract.getId()}`, e);
  55. }
  56. }
  57. }
  58. console.log(`Total fees in USD: $${totalFeeUsd}`);
  59. }
  60. main();