Skip to content

Commit

Permalink
Add staking queries (#15)
Browse files Browse the repository at this point in the history
* Add staking queries

* add tests

* add orderby to staking query

* updates other count queries to return a number

* address comments

* addr comment

* update tests

* run fmt

* fix test

* fix build

* regen and fmt

---------

Co-authored-by: Greg Nazario <greg@gnazar.io>
  • Loading branch information
heliuchuan and gregnazario authored Oct 13, 2023
1 parent 1219f4d commit ad7898b
Show file tree
Hide file tree
Showing 13 changed files with 3,510 additions and 3,203 deletions.
9 changes: 3 additions & 6 deletions src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import {
TransactionResponse,
HexInput,
IndexerPaginationArgs,
GetAccountTokensCountQueryResponse,
TokenStandard,
OrderBy,
GetAccountOwnedTokensQueryResponse,
GetAccountCollectionsWithOwnedTokenResponse,
GetAccountTransactionsCountResponse,
GetAccountCoinsDataResponse,
GetAccountCoinsCountResponse,
GetAccountOwnedObjectsResponse,
GetAccountOwnedTokensFromCollectionResponse,
} from "../types";
Expand Down Expand Up @@ -198,7 +195,7 @@ export class Account {
* @param accountAddress The account address
* @returns An object { count : number }
*/
async getAccountTokensCount(args: { accountAddress: HexInput }): Promise<GetAccountTokensCountQueryResponse> {
async getAccountTokensCount(args: { accountAddress: HexInput }): Promise<number> {
const count = await getAccountTokensCount({
aptosConfig: this.config,
...args,
Expand Down Expand Up @@ -286,7 +283,7 @@ export class Account {
* @param accountAddress The account address we want to get the total count for
* @returns An object { count : number }
*/
async getAccountTransactionsCount(args: { accountAddress: HexInput }): Promise<GetAccountTransactionsCountResponse> {
async getAccountTransactionsCount(args: { accountAddress: HexInput }): Promise<number> {
const count = getAccountTransactionsCount({
aptosConfig: this.config,
...args,
Expand Down Expand Up @@ -320,7 +317,7 @@ export class Account {
* @param accountAddress The account address we want to get the total count for
* @returns An object { count : number } where `number` is the aggregated count of all account's coin
*/
async getAccountCoinsCount(args: { accountAddress: HexInput }): Promise<GetAccountCoinsCountResponse> {
async getAccountCoinsCount(args: { accountAddress: HexInput }): Promise<number> {
const count = getAccountCoinsCount({ aptosConfig: this.config, ...args });
return count;
}
Expand Down
15 changes: 14 additions & 1 deletion src/api/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Coin } from "./coin";
import { Collection } from "./collection";
import { Faucet } from "./faucet";
import { General } from "./general";
import { Staking } from "./staking";
import { Transaction } from "./transaction";
import { TransactionSubmission } from "./transaction_submission";

Expand All @@ -23,6 +24,8 @@ export class Aptos {

readonly general: General;

readonly staking: Staking;

readonly transaction: Transaction;

readonly transactionSubmission: TransactionSubmission;
Expand Down Expand Up @@ -51,12 +54,21 @@ export class Aptos {
this.collection = new Collection(this.config);
this.faucet = new Faucet(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, Coin, Collection, Faucet, General, Transaction, TransactionSubmission {}
export interface Aptos
extends Account,
Coin,
Collection,
Faucet,
General,
Staking,
Transaction,
TransactionSubmission {}

/**
In TypeScript, we can’t inherit or extend from more than one class,
Expand Down Expand Up @@ -84,5 +96,6 @@ applyMixin(Aptos, Coin, "coin");
applyMixin(Aptos, Collection, "collection");
applyMixin(Aptos, Faucet, "faucet");
applyMixin(Aptos, General, "general");
applyMixin(Aptos, Staking, "staking");
applyMixin(Aptos, Transaction, "transaction");
applyMixin(Aptos, TransactionSubmission, "transactionSubmission");
62 changes: 62 additions & 0 deletions src/api/staking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import {
getDelegatedStakingActivities,
getNumberOfDelegators,
getNumberOfDelegatorsForAllPools,
} from "../internal/staking";
import { GetDelegatedStakingActivitiesResponse, GetNumberOfDelegatorsResponse, HexInput, OrderBy } 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> {
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(args?: {
options?: {
orderBy?: OrderBy<GetNumberOfDelegatorsResponse[0]>;
};
}): Promise<GetNumberOfDelegatorsResponse> {
const numDelegatorData = await getNumberOfDelegatorsForAllPools({ aptosConfig: this.config, ...args });
return numDelegatorData;
}

/**
* Queries delegated staking activities
*
* @param delegatorAddress Delegator address
* @param poolAddress Pool address
* @returns GetDelegatedStakingActivitiesResponse response type
*/
async getDelegatedStakingActivities(args: {
delegatorAddress: HexInput;
poolAddress: HexInput;
}): Promise<GetDelegatedStakingActivitiesResponse> {
const delegatedStakingActivities = await getDelegatedStakingActivities({ aptosConfig: this.config, ...args });
return delegatedStakingActivities;
}
}
27 changes: 17 additions & 10 deletions src/internal/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ import { AccountAddress, Hex } from "../core";
import { getTableItem, queryIndexer } from "./general";
import {
AccountData,
GetAccountCoinsCountResponse,
GetAccountCoinsDataResponse,
GetAccountCollectionsWithOwnedTokenResponse,
GetAccountOwnedObjectsResponse,
GetAccountOwnedTokensFromCollectionResponse,
GetAccountOwnedTokensQueryResponse,
GetAccountTokensCountQueryResponse,
GetAccountTransactionsCountResponse,
HexInput,
IndexerPaginationArgs,
LedgerVersion,
Expand Down Expand Up @@ -236,7 +233,7 @@ export async function lookupOriginalAccountAddress(args: {
export async function getAccountTokensCount(args: {
aptosConfig: AptosConfig;
accountAddress: HexInput;
}): Promise<GetAccountTokensCountQueryResponse> {
}): Promise<number> {
const { aptosConfig, accountAddress } = args;

const address = AccountAddress.fromHexInput({
Expand All @@ -258,8 +255,10 @@ export async function getAccountTokensCount(args: {
query: graphqlQuery,
originMethod: "getAccountTokensCount",
});

return data.current_token_ownerships_v2_aggregate.aggregate;
if (!data.current_token_ownerships_v2_aggregate.aggregate) {
throw Error("Failed to get the count of account tokens");
}
return data.current_token_ownerships_v2_aggregate.aggregate.count;
}

export async function getAccountOwnedTokens(args: {
Expand Down Expand Up @@ -398,7 +397,7 @@ export async function getAccountCollectionsWithOwnedTokens(args: {
export async function getAccountTransactionsCount(args: {
aptosConfig: AptosConfig;
accountAddress: HexInput;
}): Promise<GetAccountTransactionsCountResponse> {
}): Promise<number> {
const { aptosConfig, accountAddress } = args;

const address = AccountAddress.fromHexInput({
Expand All @@ -416,7 +415,11 @@ export async function getAccountTransactionsCount(args: {
originMethod: "getAccountTransactionsCount",
});

return data.account_transactions_aggregate.aggregate;
if (!data.account_transactions_aggregate.aggregate) {
throw Error("Failed to get the count of account transactions");
}

return data.account_transactions_aggregate.aggregate.count;
}

export async function getAccountCoinsData(args: {
Expand Down Expand Up @@ -458,7 +461,7 @@ export async function getAccountCoinsData(args: {
export async function getAccountCoinsCount(args: {
aptosConfig: AptosConfig;
accountAddress: HexInput;
}): Promise<GetAccountCoinsCountResponse> {
}): Promise<number> {
const { aptosConfig, accountAddress } = args;
const address = AccountAddress.fromHexInput({
input: accountAddress,
Expand All @@ -475,7 +478,11 @@ export async function getAccountCoinsCount(args: {
originMethod: "getAccountCoinsCount",
});

return data.current_fungible_asset_balances_aggregate.aggregate;
if (!data.current_fungible_asset_balances_aggregate.aggregate) {
throw Error("Failed to get the count of account coins");
}

return data.current_fungible_asset_balances_aggregate.aggregate.count;
}

export async function getAccountOwnedObjects(args: {
Expand Down
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($where_condition: num_active_delegator_per_pool_bool_exp!, $order_by: [num_active_delegator_per_pool_order_by!]) {
num_active_delegator_per_pool(
where: $where_condition,
order_by: $order_by
) {
num_active_delegator
pool_address
}
}
68 changes: 68 additions & 0 deletions src/internal/staking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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, GetNumberOfDelegatorsResponse, HexInput, OrderBy } 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<number> {
const { aptosConfig, poolAddress } = args;
const address = Hex.fromHexInput({ hexInput: poolAddress }).toString();
const query = {
query: GetNumberOfDelegators,
variables: { where_condition: { pool_address: { _eq: 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;
options?: {
orderBy?: OrderBy<GetNumberOfDelegatorsResponse[0]>;
};
}): Promise<GetNumberOfDelegatorsResponse> {
const { aptosConfig, options } = args;
const query = {
query: GetNumberOfDelegators,
variables: { where_condition: {}, order_by: options?.orderBy },
};
const data: GetNumberOfDelegatorsQuery = await queryIndexer<GetNumberOfDelegatorsQuery>({
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;
}
Loading

0 comments on commit ad7898b

Please sign in to comment.