{"version":3,"sources":["../../src/internal/general.ts"],"sourcesContent":["// Copyright © Aptos Foundation\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * This file contains the underlying implementations for exposed API surface in\n * the {@link api/general}. By moving the methods out into a separate file,\n * other namespaces and processes can access these methods without depending on the entire\n * general namespace and without having a dependency cycle error.\n * @group Implementation\n */\n\nimport { AptosConfig } from \"../api/aptosConfig\";\nimport { getAptosFullNode, postAptosIndexer } from \"../client\";\nimport { GetChainTopUserTransactionsResponse, GetProcessorStatusResponse, GraphqlQuery, LedgerInfo } from \"../types\";\nimport { GetChainTopUserTransactionsQuery, GetProcessorStatusQuery } from \"../types/generated/operations\";\nimport { GetChainTopUserTransactions, GetProcessorStatus } from \"../types/generated/queries\";\nimport { ProcessorType } from \"../utils/const\";\nimport { memoizeAsync } from \"../utils/memoize\";\n\n/**\n * Cache TTL for ledger info in milliseconds (10 seconds).\n * Ledger info changes frequently but we can cache briefly to reduce redundant calls\n * when building multiple transactions in quick succession.\n */\nconst LEDGER_INFO_CACHE_TTL_MS = 10 * 1000;\n\n/**\n * Retrieves information about the current ledger.\n * Results are cached for 10 seconds to reduce redundant network calls during\n * rapid transaction building.\n *\n * @param args - The arguments for retrieving ledger information.\n * @param args.aptosConfig - The configuration object for connecting to the Aptos network.\n * @group Implementation\n */\nexport async function getLedgerInfo(args: { aptosConfig: AptosConfig }): Promise<LedgerInfo> {\n  const { aptosConfig } = args;\n  const cacheKey = `ledger-info-${aptosConfig.network}`;\n\n  return memoizeAsync(\n    async () => {\n      const { data } = await getAptosFullNode<{}, LedgerInfo>({\n        aptosConfig,\n        originMethod: \"getLedgerInfo\",\n        path: \"\",\n      });\n      return data;\n    },\n    cacheKey,\n    LEDGER_INFO_CACHE_TTL_MS,\n  )();\n}\n\n/**\n * Retrieves the top user transactions for a specific blockchain chain.\n *\n * @param args - The arguments for the function.\n * @param args.aptosConfig - The configuration object for Aptos.\n * @param args.limit - The maximum number of transactions to retrieve.\n * @returns An array of user transactions.\n * @group Implementation\n */\nexport async function getChainTopUserTransactions(args: {\n  aptosConfig: AptosConfig;\n  limit: number;\n}): Promise<GetChainTopUserTransactionsResponse> {\n  const { aptosConfig, limit } = args;\n  const graphqlQuery = {\n    query: GetChainTopUserTransactions,\n    variables: { limit },\n  };\n\n  const data = await queryIndexer<GetChainTopUserTransactionsQuery>({\n    aptosConfig,\n    query: graphqlQuery,\n    originMethod: \"getChainTopUserTransactions\",\n  });\n\n  return data.user_transactions;\n}\n\n/**\n * Executes a GraphQL query against the Aptos indexer and retrieves the resulting data.\n *\n * @param args - The arguments for the query.\n * @param args.aptosConfig - The configuration settings for the Aptos client.\n * @param args.query - The GraphQL query to be executed.\n * @param args.originMethod - An optional string to specify the origin method for tracking purposes.\n * @returns The data returned from the query execution.\n * @group Implementation\n */\nexport async function queryIndexer<T extends {}>(args: {\n  aptosConfig: AptosConfig;\n  query: GraphqlQuery;\n  originMethod?: string;\n}): Promise<T> {\n  const { aptosConfig, query, originMethod } = args;\n  const { data } = await postAptosIndexer<GraphqlQuery, T>({\n    aptosConfig,\n    originMethod: originMethod ?? \"queryIndexer\",\n    path: \"\",\n    body: query,\n    overrides: { WITH_CREDENTIALS: false },\n  });\n  return data;\n}\n\n/**\n * Retrieves the current statuses of processors.\n *\n * @param args - The arguments for the function.\n * @param args.aptosConfig - The configuration object for Aptos.\n * @returns The statuses of the processors.\n * @group Implementation\n */\nexport async function getProcessorStatuses(args: { aptosConfig: AptosConfig }): Promise<GetProcessorStatusResponse> {\n  const { aptosConfig } = args;\n\n  const graphqlQuery = {\n    query: GetProcessorStatus,\n  };\n\n  const data = await queryIndexer<GetProcessorStatusQuery>({\n    aptosConfig,\n    query: graphqlQuery,\n    originMethod: \"getProcessorStatuses\",\n  });\n\n  return data.processor_status;\n}\n\n/**\n * Retrieves the last success version from the indexer.\n *\n * @param args - The arguments for the function.\n * @param args.aptosConfig - The configuration object for Aptos.\n * @returns The last success version as a BigInt.\n * @group Implementation\n */\nexport async function getIndexerLastSuccessVersion(args: { aptosConfig: AptosConfig }): Promise<bigint> {\n  const response = await getProcessorStatuses({ aptosConfig: args.aptosConfig });\n  return BigInt(response[0].last_success_version);\n}\n\n/**\n * Retrieves the status of a specified processor in the Aptos network.\n * This function allows you to check the current operational status of a processor, which can be useful for monitoring and troubleshooting.\n *\n * @param args - The arguments for the function.\n * @param args.aptosConfig - The configuration object for connecting to the Aptos network.\n * @param args.processorType - The type of processor whose status you want to retrieve.\n * @returns The status of the specified processor.\n * @group Implementation\n */\nexport async function getProcessorStatus(args: {\n  aptosConfig: AptosConfig;\n  processorType: ProcessorType;\n}): Promise<GetProcessorStatusResponse[0]> {\n  const { aptosConfig, processorType } = args;\n\n  const whereCondition: { processor: { _eq: string } } = {\n    processor: { _eq: processorType },\n  };\n\n  const graphqlQuery = {\n    query: GetProcessorStatus,\n    variables: {\n      where_condition: whereCondition,\n    },\n  };\n\n  const data = await queryIndexer<GetProcessorStatusQuery>({\n    aptosConfig,\n    query: graphqlQuery,\n    originMethod: \"getProcessorStatus\",\n  });\n\n  return data.processor_status[0];\n}\n"],"mappings":"2KAwBA,IAAMA,EAA2B,GAAK,IAWtC,eAAsBC,EAAcC,EAAyD,CAC3F,GAAM,CAAE,YAAAC,CAAY,EAAID,EAClBE,EAAW,eAAeD,EAAY,OAAO,GAEnD,OAAOE,EACL,SAAY,CACV,GAAM,CAAE,KAAAC,CAAK,EAAI,MAAMC,EAAiC,CACtD,YAAAJ,EACA,aAAc,gBACd,KAAM,EACR,CAAC,EACD,OAAOG,CACT,EACAF,EACAJ,CACF,EAAE,CACJ,CAWA,eAAsBQ,EAA4BN,EAGD,CAC/C,GAAM,CAAE,YAAAC,EAAa,MAAAM,CAAM,EAAIP,EAY/B,OANa,MAAMQ,EAA+C,CAChE,YAAAP,EACA,MAPmB,CACnB,MAAOQ,EACP,UAAW,CAAE,MAAAF,CAAM,CACrB,EAKE,aAAc,6BAChB,CAAC,GAEW,iBACd,CAYA,eAAsBC,EAA2BR,EAIlC,CACb,GAAM,CAAE,YAAAC,EAAa,MAAAS,EAAO,aAAAC,CAAa,EAAIX,EACvC,CAAE,KAAAI,CAAK,EAAI,MAAMQ,EAAkC,CACvD,YAAAX,EACA,aAAcU,GAAgB,eAC9B,KAAM,GACN,KAAMD,EACN,UAAW,CAAE,iBAAkB,EAAM,CACvC,CAAC,EACD,OAAON,CACT,CAUA,eAAsBS,EAAqBb,EAAyE,CAClH,GAAM,CAAE,YAAAC,CAAY,EAAID,EAYxB,OANa,MAAMQ,EAAsC,CACvD,YAAAP,EACA,MANmB,CACnB,MAAOa,CACT,EAKE,aAAc,sBAChB,CAAC,GAEW,gBACd,CAUA,eAAsBC,EAA6Bf,EAAqD,CACtG,IAAMgB,EAAW,MAAMH,EAAqB,CAAE,YAAab,EAAK,WAAY,CAAC,EAC7E,OAAO,OAAOgB,EAAS,CAAC,EAAE,oBAAoB,CAChD,CAYA,eAAsBC,EAAmBjB,EAGE,CACzC,GAAM,CAAE,YAAAC,EAAa,cAAAiB,CAAc,EAAIlB,EAmBvC,OANa,MAAMQ,EAAsC,CACvD,YAAAP,EACA,MATmB,CACnB,MAAOa,EACP,UAAW,CACT,gBAPmD,CACrD,UAAW,CAAE,IAAKI,CAAc,CAClC,CAME,CACF,EAKE,aAAc,oBAChB,CAAC,GAEW,iBAAiB,CAAC,CAChC","names":["LEDGER_INFO_CACHE_TTL_MS","getLedgerInfo","args","aptosConfig","cacheKey","memoizeAsync","data","getAptosFullNode","getChainTopUserTransactions","limit","queryIndexer","GetChainTopUserTransactions","query","originMethod","postAptosIndexer","getProcessorStatuses","GetProcessorStatus","getIndexerLastSuccessVersion","response","getProcessorStatus","processorType"]}