Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add staking queries #15

Merged
merged 13 commits into from
Oct 13, 2023
7 changes: 6 additions & 1 deletion 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,12 +42,13 @@ 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);
}
}

export interface Aptos extends Account, General, Transaction, TransactionSubmission {}
export interface Aptos extends Account, General, Staking, Transaction, TransactionSubmission {}

/**
In TypeScript, we can’t inherit or extend from more than one class,
Expand All @@ -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");
58 changes: 58 additions & 0 deletions src/api/staking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import {
getDelegatedStakingActivities,
getNumberOfDelegators,
getNumberOfDelegatorsForAllPools,
} from "../internal/staking";
import { GetDelegatedStakingActivitiesResponse, GetNumberOfDelegatorsForAllPoolsResponse, HexInput } from "../types";
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. Throws an error if the pool is not found.
*
* @param poolAddress Pool address
* @returns The number of delegators for the given pool
*/
async getNumberOfDelegators(args: { poolAddress: HexInput }): Promise<number> {
heliuchuan marked this conversation as resolved.
Show resolved Hide resolved
const numDelegators = await getNumberOfDelegators({ aptosConfig: this.config, ...args });
return numDelegators;
}

/**
* Queries current number of delegators in a pool. Throws an error if the pool is not found.
*
* @param poolAddress Pool address
* @returns GetNumberOfDelegatorsForAllPoolsResponse response type
*/
async getNumberOfDelegatorsForAllPools(): Promise<GetNumberOfDelegatorsForAllPoolsResponse> {
const numDelegatorData = await getNumberOfDelegatorsForAllPools({ aptosConfig: this.config });
return numDelegatorData;
}

/**
* Queries delegated staking activities
*
* @param delegatorAddress Delegator address
* @param poolAddress Pool address
* @returns GetDelegatedStakingActivitiesResponse response type
*/
async getDelegatedStakingActivities(args: {
heliuchuan marked this conversation as resolved.
Show resolved Hide resolved
delegatorAddress: HexInput;
poolAddress: HexInput;
0xmaayan marked this conversation as resolved.
Show resolved Hide resolved
}): Promise<GetDelegatedStakingActivitiesResponse> {
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
}
}
6 changes: 6 additions & 0 deletions src/internal/queries/getNumberOfDelegatorsForAllPools.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query getNumberOfDelegatorsForAllPools {
num_active_delegator_per_pool {
num_active_delegator
pool_address
}
}
8 changes: 8 additions & 0 deletions src/internal/queries/getNumberOfDelegatorsQuery.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query getNumberOfDelegators($poolAddress: String) {
heliuchuan marked this conversation as resolved.
Show resolved Hide resolved
num_active_delegator_per_pool(
where: { pool_address: { _eq: $poolAddress } }
) {
num_active_delegator
pool_address
}
}
73 changes: 73 additions & 0 deletions src/internal/staking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 { GetDelegatedStakingActivitiesResponse, GetNumberOfDelegatorsForAllPoolsResponse, HexInput } from "../types";
import {
GetDelegatedStakingActivitiesQuery,
GetNumberOfDelegatorsForAllPoolsQuery,
GetNumberOfDelegatorsQuery,
} from "../types/generated/operations";
import {
GetDelegatedStakingActivities,
GetNumberOfDelegators,
GetNumberOfDelegatorsForAllPools,
} from "../types/generated/queries";
import { queryIndexer } from "./general";

export async function getNumberOfDelegators(args: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename here to getNumberOfDelegatorsByPoolAddress and the second one getNumberOfDelegators

aptosConfig: AptosConfig;
poolAddress: HexInput;
}): Promise<number> {
const { aptosConfig, poolAddress } = args;
const address = Hex.fromHexInput({ hexInput: poolAddress }).toString();
const query = {
query: GetNumberOfDelegators,
variables: { poolAddress: address },
};
const data: GetNumberOfDelegatorsQuery = await queryIndexer<GetNumberOfDelegatorsQuery>({ aptosConfig, query });
if (data.num_active_delegator_per_pool.length === 0) {
throw Error("Delegator pool not found");
}
return data.num_active_delegator_per_pool[0].num_active_delegator;
}

export async function getNumberOfDelegatorsForAllPools(args: {
aptosConfig: AptosConfig;
}): Promise<GetNumberOfDelegatorsForAllPoolsResponse> {
const { aptosConfig } = args;
const query = {
query: GetNumberOfDelegatorsForAllPools,
variables: {},
};
const data: GetNumberOfDelegatorsForAllPoolsQuery = await queryIndexer<GetNumberOfDelegatorsForAllPoolsQuery>({
aptosConfig,
query,
});
return data.num_active_delegator_per_pool;
}

export async function getDelegatedStakingActivities(args: {
aptosConfig: AptosConfig;
delegatorAddress: HexInput;
poolAddress: HexInput;
}): Promise<GetDelegatedStakingActivitiesResponse> {
const { aptosConfig, delegatorAddress, poolAddress } = args;
const query = {
query: GetDelegatedStakingActivities,
variables: {
delegatorAddress: Hex.fromHexInput({ hexInput: delegatorAddress }).toString(),
poolAddress: Hex.fromHexInput({ hexInput: poolAddress }).toString(),
},
};
const data = await queryIndexer<GetDelegatedStakingActivitiesQuery>({ aptosConfig, query });
return data.delegated_staking_activities;
}
38 changes: 32 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,33 @@ 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 GetNumberOfDelegatorsForAllPoolsQueryVariables = Types.Exact<{ [key: string]: never }>;

export type GetNumberOfDelegatorsForAllPoolsQuery = {
num_active_delegator_per_pool: Array<{ num_active_delegator?: any | null; pool_address?: string | null }>;
};

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 }>;
};
72 changes: 72 additions & 0 deletions src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,36 @@ 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 GetNumberOfDelegatorsForAllPools = `
query getNumberOfDelegatorsForAllPools {
num_active_delegator_per_pool {
num_active_delegator
pool_address
}
}
`;
export const GetNumberOfDelegators = `
query getNumberOfDelegators($poolAddress: String) {
num_active_delegator_per_pool(where: {pool_address: {_eq: $poolAddress}}) {
num_active_delegator
pool_address
}
}
`;

export type SdkFunctionWrapper = <T>(
action: (requestHeaders?: Record<string, string>) => Promise<T>,
Expand Down Expand Up @@ -344,6 +374,48 @@ 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",
);
},
getNumberOfDelegatorsForAllPools(
variables?: Types.GetNumberOfDelegatorsForAllPoolsQueryVariables,
requestHeaders?: Dom.RequestInit["headers"],
): Promise<Types.GetNumberOfDelegatorsForAllPoolsQuery> {
return withWrapper(
(wrappedRequestHeaders) =>
client.request<Types.GetNumberOfDelegatorsForAllPoolsQuery>(GetNumberOfDelegatorsForAllPools, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
"getNumberOfDelegatorsForAllPools",
"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