fetch_fees.ts 1.8 KB

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