fetch_fees.ts 2.2 KB

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