@aptos-labs/ts-sdk
Version:
Aptos TypeScript SDK
136 lines • 4.92 kB
JavaScript
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
import { getAptosFullNode, postAptosIndexer } from "../client/index.js";
import { GetChainTopUserTransactions, GetProcessorStatus } from "../types/generated/queries.js";
import { memoizeAsync } from "../utils/memoize.js";
/**
* Cache TTL for ledger info in milliseconds (10 seconds).
* Ledger info changes frequently but we can cache briefly to reduce redundant calls
* when building multiple transactions in quick succession.
*/
const LEDGER_INFO_CACHE_TTL_MS = 10 * 1000;
/**
* Retrieves information about the current ledger.
* Results are cached for 10 seconds to reduce redundant network calls during
* rapid transaction building.
*
* @param args - The arguments for retrieving ledger information.
* @param args.aptosConfig - The configuration object for connecting to the Aptos network.
* @group Implementation
*/
export async function getLedgerInfo(args) {
const { aptosConfig } = args;
const cacheKey = `ledger-info-${aptosConfig.network}`;
return memoizeAsync(async () => {
const { data } = await getAptosFullNode({
aptosConfig,
originMethod: "getLedgerInfo",
path: "",
});
return data;
}, cacheKey, LEDGER_INFO_CACHE_TTL_MS)();
}
/**
* Retrieves the top user transactions for a specific blockchain chain.
*
* @param args - The arguments for the function.
* @param args.aptosConfig - The configuration object for Aptos.
* @param args.limit - The maximum number of transactions to retrieve.
* @returns An array of user transactions.
* @group Implementation
*/
export async function getChainTopUserTransactions(args) {
const { aptosConfig, limit } = args;
const graphqlQuery = {
query: GetChainTopUserTransactions,
variables: { limit },
};
const data = await queryIndexer({
aptosConfig,
query: graphqlQuery,
originMethod: "getChainTopUserTransactions",
});
return data.user_transactions;
}
/**
* Executes a GraphQL query against the Aptos indexer and retrieves the resulting data.
*
* @param args - The arguments for the query.
* @param args.aptosConfig - The configuration settings for the Aptos client.
* @param args.query - The GraphQL query to be executed.
* @param args.originMethod - An optional string to specify the origin method for tracking purposes.
* @returns The data returned from the query execution.
* @group Implementation
*/
export async function queryIndexer(args) {
const { aptosConfig, query, originMethod } = args;
const { data } = await postAptosIndexer({
aptosConfig,
originMethod: originMethod ?? "queryIndexer",
path: "",
body: query,
overrides: { WITH_CREDENTIALS: false },
});
return data;
}
/**
* Retrieves the current statuses of processors.
*
* @param args - The arguments for the function.
* @param args.aptosConfig - The configuration object for Aptos.
* @returns The statuses of the processors.
* @group Implementation
*/
export async function getProcessorStatuses(args) {
const { aptosConfig } = args;
const graphqlQuery = {
query: GetProcessorStatus,
};
const data = await queryIndexer({
aptosConfig,
query: graphqlQuery,
originMethod: "getProcessorStatuses",
});
return data.processor_status;
}
/**
* Retrieves the last success version from the indexer.
*
* @param args - The arguments for the function.
* @param args.aptosConfig - The configuration object for Aptos.
* @returns The last success version as a BigInt.
* @group Implementation
*/
export async function getIndexerLastSuccessVersion(args) {
const response = await getProcessorStatuses({ aptosConfig: args.aptosConfig });
return BigInt(response[0].last_success_version);
}
/**
* Retrieves the status of a specified processor in the Aptos network.
* This function allows you to check the current operational status of a processor, which can be useful for monitoring and troubleshooting.
*
* @param args - The arguments for the function.
* @param args.aptosConfig - The configuration object for connecting to the Aptos network.
* @param args.processorType - The type of processor whose status you want to retrieve.
* @returns The status of the specified processor.
* @group Implementation
*/
export async function getProcessorStatus(args) {
const { aptosConfig, processorType } = args;
const whereCondition = {
processor: { _eq: processorType },
};
const graphqlQuery = {
query: GetProcessorStatus,
variables: {
where_condition: whereCondition,
},
};
const data = await queryIndexer({
aptosConfig,
query: graphqlQuery,
originMethod: "getProcessorStatus",
});
return data.processor_status[0];
}
//# sourceMappingURL=general.js.map