@aptos-labs/ts-sdk
Version:
Aptos TypeScript SDK
71 lines • 3.13 kB
JavaScript
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
import { AccountAddress } from "../core/index.js";
import { GetDelegatedStakingActivities, GetNumberOfDelegators } from "../types/generated/queries.js";
import { queryIndexer } from "./general.js";
/**
* Retrieves the number of active delegators for a specified pool address.
*
* @param args - The arguments for the function.
* @param args.aptosConfig - The configuration object for Aptos.
* @param args.poolAddress - The address of the pool for which to retrieve the number of delegators.
* @returns The number of active delegators for the specified pool address.
* @group Implementation
*/
export async function getNumberOfDelegators(args) {
const { aptosConfig, poolAddress } = args;
const address = AccountAddress.from(poolAddress).toStringLong();
const query = {
query: GetNumberOfDelegators,
variables: { where_condition: { pool_address: { _eq: address } } },
};
const data = await queryIndexer({ aptosConfig, query });
// commonjs (aka cjs) doesn't handle Nullish Coalescing for some reason
// might be because of how ts infer the graphql generated scheme type
return data.num_active_delegator_per_pool[0] ? data.num_active_delegator_per_pool[0].num_active_delegator : 0;
}
/**
* Retrieves the number of active delegators for all pools.
*
* @param args - The arguments for the function.
* @param args.aptosConfig - The configuration for the Aptos client.
* @param [args.options] - Optional parameters for ordering the results.
* @param args.options.orderBy - Specifies the order in which to return the results.
* @returns The number of active delegators per pool.
* @group Implementation
*/
export async function getNumberOfDelegatorsForAllPools(args) {
const { aptosConfig, options } = args;
const query = {
query: GetNumberOfDelegators,
variables: { order_by: options?.orderBy },
};
const data = await queryIndexer({
aptosConfig,
query,
});
return data.num_active_delegator_per_pool;
}
/**
* Retrieves the delegated staking activities for a specified delegator and pool.
*
* @param args - The parameters for the query.
* @param args.aptosConfig - The configuration object for Aptos.
* @param args.delegatorAddress - The address of the delegator whose activities are being queried.
* @param args.poolAddress - The address of the pool associated with the delegated staking activities.
* @returns The delegated staking activities for the specified delegator and pool.
* @group Implementation
*/
export async function getDelegatedStakingActivities(args) {
const { aptosConfig, delegatorAddress, poolAddress } = args;
const query = {
query: GetDelegatedStakingActivities,
variables: {
delegatorAddress: AccountAddress.from(delegatorAddress).toStringLong(),
poolAddress: AccountAddress.from(poolAddress).toStringLong(),
},
};
const data = await queryIndexer({ aptosConfig, query });
return data.delegated_staking_activities;
}
//# sourceMappingURL=staking.js.map