Skip to content

Commit

Permalink
Add staking queries
Browse files Browse the repository at this point in the history
  • Loading branch information
heliuchuan committed Oct 11, 2023
1 parent 1ec477f commit 728b2f9
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/api/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Account } from "./account";
import { AptosConfig } from "./aptos_config";
import { General } from "./general";
import { Staking } from "./staking";
import { Transaction } from "./transaction";
import { TransactionSubmission } from "./transaction_submission";

Expand All @@ -14,6 +15,8 @@ export class Aptos {

readonly general: General;

readonly staking: Staking;

readonly transaction: Transaction;

readonly transactionSubmission: TransactionSubmission;
Expand All @@ -39,6 +42,7 @@ export class Aptos {
this.config = new AptosConfig(settings);
this.account = new Account(this.config);
this.general = new General(this.config);
this.staking = new Staking(this.config);
this.transaction = new Transaction(this.config);
this.transactionSubmission = new TransactionSubmission(this.config);
}
Expand Down Expand Up @@ -69,5 +73,6 @@ function applyMixin(targetClass: any, baseClass: any, baseClassProp: string) {

applyMixin(Aptos, Account, "account");
applyMixin(Aptos, General, "general");
applyMixin(Aptos, Staking, "staking");
applyMixin(Aptos, Transaction, "transaction");
applyMixin(Aptos, TransactionSubmission, "transactionSubmission");
43 changes: 43 additions & 0 deletions src/api/staking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import { getDelegatedStakingActivities, getNumberOfDelegators } from "../internal/staking";
import { HexInput } from "../types";
import { GetDelegatedStakingActivitiesQuery, GetNumberOfDelegatorsQuery } from "../types/generated/operations";
import { AptosConfig } from "./aptos_config";

/**
* A class to query all `Staking` related queries on Aptos.
*/
export class Staking {
readonly config: AptosConfig;

constructor(config: AptosConfig) {
this.config = config;
}

/**
* Queries current number of delegators in a pool
*
* @returns GetNumberOfDelegatorsQuery response type
*/
async getNumberOfDelegators(args: { poolAddress: HexInput }): Promise<GetNumberOfDelegatorsQuery> {
const numDelegators = await getNumberOfDelegators({ aptosConfig: this.config, ...args });
return numDelegators;
}

/**
* Queries delegated staking activities
*
* @param delegatorAddress Delegator address
* @param poolAddress Pool address
* @returns GetDelegatedStakingActivitiesQuery response type
*/
async getDelegatedStakingActivities(args: {
delegatorAddress: HexInput;
poolAddress: HexInput;
}): Promise<GetDelegatedStakingActivitiesQuery> {
const delegatedStakingActivities = await getDelegatedStakingActivities({ aptosConfig: this.config, ...args });
return delegatedStakingActivities;
}
}
12 changes: 12 additions & 0 deletions src/internal/queries/getDelegatedStakingActivities.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query getDelegatedStakingActivities($delegatorAddress: String, $poolAddress: String) {
delegated_staking_activities(
where: { delegator_address: { _eq: $delegatorAddress }, pool_address: { _eq: $poolAddress } }
) {
amount
delegator_address
event_index
event_type
pool_address
transaction_version
}
}
9 changes: 9 additions & 0 deletions src/internal/queries/getNumberOfDelegatorsQuery.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query getNumberOfDelegators($poolAddress: String) {
num_active_delegator_per_pool(
where: { pool_address: { _eq: $poolAddress }, num_active_delegator: { _gt: "0" } }
distinct_on: pool_address
) {
num_active_delegator
pool_address
}
}
45 changes: 45 additions & 0 deletions src/internal/staking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

/**
* This file contains the underlying implementations for exposed API surface in
* the {@link api/staking}. By moving the methods out into a separate file,
* other namespaces and processes can access these methods without depending on the entire
* faucet namespace and without having a dependency cycle error.
*/

import { AptosConfig } from "../api/aptos_config";
import { Hex } from "../core";
import { HexInput } from "../types";
import { GetDelegatedStakingActivitiesQuery, GetNumberOfDelegatorsQuery } from "../types/generated/operations";
import { GetDelegatedStakingActivities, GetNumberOfDelegators } from "../types/generated/queries";
import { queryIndexer } from "./general";

export async function getNumberOfDelegators(args: {
aptosConfig: AptosConfig;
poolAddress: HexInput;
}): Promise<GetNumberOfDelegatorsQuery> {
const { aptosConfig, poolAddress } = args;
const address = Hex.fromHexInput({ hexInput: poolAddress }).toString();
const query = {
query: GetNumberOfDelegators,
variables: { poolAddress: address },
};
return queryIndexer({ aptosConfig, query });
}

export async function getDelegatedStakingActivities(args: {
aptosConfig: AptosConfig;
delegatorAddress: HexInput;
poolAddress: HexInput;
}): Promise<GetDelegatedStakingActivitiesQuery> {
const { aptosConfig, delegatorAddress, poolAddress } = args;
const query = {
query: GetDelegatedStakingActivities,
variables: {
delegatorAddress: Hex.fromHexInput({ hexInput: delegatorAddress }).toString(),
poolAddress: Hex.fromHexInput({ hexInput: poolAddress }).toString(),
},
};
return queryIndexer({ aptosConfig, query });
}
32 changes: 26 additions & 6 deletions src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ export type GetAccountCoinsCountQueryVariables = Types.Exact<{
}>;

export type GetAccountCoinsCountQuery = {
current_fungible_asset_balances_aggregate: {
aggregate?: { count: number } | null;
};
current_fungible_asset_balances_aggregate: { aggregate?: { count: number } | null };
};

export type GetAccountCoinsDataQueryVariables = Types.Exact<{
Expand Down Expand Up @@ -322,9 +320,7 @@ export type GetAccountTokensCountQueryVariables = Types.Exact<{
}>;

export type GetAccountTokensCountQuery = {
current_token_ownerships_v2_aggregate: {
aggregate?: { count: number } | null;
};
current_token_ownerships_v2_aggregate: { aggregate?: { count: number } | null };
};

export type GetAccountTransactionsCountQueryVariables = Types.Exact<{
Expand All @@ -334,3 +330,27 @@ export type GetAccountTransactionsCountQueryVariables = Types.Exact<{
export type GetAccountTransactionsCountQuery = {
account_transactions_aggregate: { aggregate?: { count: number } | null };
};

export type GetDelegatedStakingActivitiesQueryVariables = Types.Exact<{
delegatorAddress?: Types.InputMaybe<Types.Scalars["String"]>;
poolAddress?: Types.InputMaybe<Types.Scalars["String"]>;
}>;

export type GetDelegatedStakingActivitiesQuery = {
delegated_staking_activities: Array<{
amount: any;
delegator_address: string;
event_index: any;
event_type: string;
pool_address: string;
transaction_version: any;
}>;
};

export type GetNumberOfDelegatorsQueryVariables = Types.Exact<{
poolAddress?: Types.InputMaybe<Types.Scalars["String"]>;
}>;

export type GetNumberOfDelegatorsQuery = {
num_active_delegator_per_pool: Array<{ num_active_delegator?: any | null; pool_address?: string | null }>;
};
53 changes: 53 additions & 0 deletions src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,31 @@ export const GetAccountTransactionsCount = `
}
}
`;
export const GetDelegatedStakingActivities = `
query getDelegatedStakingActivities($delegatorAddress: String, $poolAddress: String) {
delegated_staking_activities(
where: {delegator_address: {_eq: $delegatorAddress}, pool_address: {_eq: $poolAddress}}
) {
amount
delegator_address
event_index
event_type
pool_address
transaction_version
}
}
`;
export const GetNumberOfDelegators = `
query getNumberOfDelegators($poolAddress: String) {
num_active_delegator_per_pool(
where: {pool_address: {_eq: $poolAddress}, num_active_delegator: {_gt: "0"}}
distinct_on: pool_address
) {
num_active_delegator
pool_address
}
}
`;

export type SdkFunctionWrapper = <T>(
action: (requestHeaders?: Record<string, string>) => Promise<T>,
Expand Down Expand Up @@ -344,6 +369,34 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
"query",
);
},
getDelegatedStakingActivities(
variables?: Types.GetDelegatedStakingActivitiesQueryVariables,
requestHeaders?: Dom.RequestInit["headers"],
): Promise<Types.GetDelegatedStakingActivitiesQuery> {
return withWrapper(
(wrappedRequestHeaders) =>
client.request<Types.GetDelegatedStakingActivitiesQuery>(GetDelegatedStakingActivities, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
"getDelegatedStakingActivities",
"query",
);
},
getNumberOfDelegators(
variables?: Types.GetNumberOfDelegatorsQueryVariables,
requestHeaders?: Dom.RequestInit["headers"],
): Promise<Types.GetNumberOfDelegatorsQuery> {
return withWrapper(
(wrappedRequestHeaders) =>
client.request<Types.GetNumberOfDelegatorsQuery>(GetNumberOfDelegators, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
"getNumberOfDelegators",
"query",
);
},
};
}
export type Sdk = ReturnType<typeof getSdk>;
Loading

0 comments on commit 728b2f9

Please sign in to comment.