From 728b2f9adff55fca6c891fc84be82a5ed1a947cf Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 11 Oct 2023 05:00:44 -0400 Subject: [PATCH] Add staking queries --- src/api/aptos.ts | 5 + src/api/staking.ts | 43 ++++++ .../getDelegatedStakingActivities.graphql | 12 ++ .../getNumberOfDelegatorsQuery.graphql | 9 ++ src/internal/staking.ts | 45 ++++++ src/types/generated/operations.ts | 32 +++- src/types/generated/queries.ts | 53 +++++++ src/types/generated/types.ts | 145 ++++++++++++++++-- 8 files changed, 329 insertions(+), 15 deletions(-) create mode 100644 src/api/staking.ts create mode 100644 src/internal/queries/getDelegatedStakingActivities.graphql create mode 100644 src/internal/queries/getNumberOfDelegatorsQuery.graphql create mode 100644 src/internal/staking.ts diff --git a/src/api/aptos.ts b/src/api/aptos.ts index adcb435e1..aaa518cce 100644 --- a/src/api/aptos.ts +++ b/src/api/aptos.ts @@ -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"; @@ -14,6 +15,8 @@ export class Aptos { readonly general: General; + readonly staking: Staking; + readonly transaction: Transaction; readonly transactionSubmission: TransactionSubmission; @@ -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); } @@ -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"); diff --git a/src/api/staking.ts b/src/api/staking.ts new file mode 100644 index 000000000..14b40c5d6 --- /dev/null +++ b/src/api/staking.ts @@ -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 { + 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 { + const delegatedStakingActivities = await getDelegatedStakingActivities({ aptosConfig: this.config, ...args }); + return delegatedStakingActivities; + } +} diff --git a/src/internal/queries/getDelegatedStakingActivities.graphql b/src/internal/queries/getDelegatedStakingActivities.graphql new file mode 100644 index 000000000..70083e243 --- /dev/null +++ b/src/internal/queries/getDelegatedStakingActivities.graphql @@ -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 + } +} diff --git a/src/internal/queries/getNumberOfDelegatorsQuery.graphql b/src/internal/queries/getNumberOfDelegatorsQuery.graphql new file mode 100644 index 000000000..3911f200e --- /dev/null +++ b/src/internal/queries/getNumberOfDelegatorsQuery.graphql @@ -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 + } +} diff --git a/src/internal/staking.ts b/src/internal/staking.ts new file mode 100644 index 000000000..7d396b85c --- /dev/null +++ b/src/internal/staking.ts @@ -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 { + 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 { + 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 }); +} diff --git a/src/types/generated/operations.ts b/src/types/generated/operations.ts index eedcb672b..017dc4236 100644 --- a/src/types/generated/operations.ts +++ b/src/types/generated/operations.ts @@ -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<{ @@ -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<{ @@ -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; + poolAddress?: Types.InputMaybe; +}>; + +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; +}>; + +export type GetNumberOfDelegatorsQuery = { + num_active_delegator_per_pool: Array<{ num_active_delegator?: any | null; pool_address?: string | null }>; +}; diff --git a/src/types/generated/queries.ts b/src/types/generated/queries.ts index 8f20e0dbf..52d355fdf 100644 --- a/src/types/generated/queries.ts +++ b/src/types/generated/queries.ts @@ -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 = ( action: (requestHeaders?: Record) => Promise, @@ -344,6 +369,34 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = "query", ); }, + getDelegatedStakingActivities( + variables?: Types.GetDelegatedStakingActivitiesQueryVariables, + requestHeaders?: Dom.RequestInit["headers"], + ): Promise { + return withWrapper( + (wrappedRequestHeaders) => + client.request(GetDelegatedStakingActivities, variables, { + ...requestHeaders, + ...wrappedRequestHeaders, + }), + "getDelegatedStakingActivities", + "query", + ); + }, + getNumberOfDelegators( + variables?: Types.GetNumberOfDelegatorsQueryVariables, + requestHeaders?: Dom.RequestInit["headers"], + ): Promise { + return withWrapper( + (wrappedRequestHeaders) => + client.request(GetNumberOfDelegators, variables, { + ...requestHeaders, + ...wrappedRequestHeaders, + }), + "getNumberOfDelegators", + "query", + ); + }, }; } export type Sdk = ReturnType; diff --git a/src/types/generated/types.ts b/src/types/generated/types.ts index fb6286fd6..5c5afbdd5 100644 --- a/src/types/generated/types.ts +++ b/src/types/generated/types.ts @@ -1,14 +1,8 @@ export type Maybe = T | null; export type InputMaybe = Maybe; -export type Exact = { - [K in keyof T]: T[K]; -}; -export type MakeOptional = Omit & { - [SubKey in K]?: Maybe; -}; -export type MakeMaybe = Omit & { - [SubKey in K]: Maybe; -}; +export type Exact = { [K in keyof T]: T[K] }; +export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; +export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { ID: string; @@ -2461,6 +2455,8 @@ export type CurrentCollectionOwnershipV2ViewVarianceFields = { /** columns and relationships of "current_collections_v2" */ export type CurrentCollectionsV2 = { + /** An object relationship */ + cdn_asset_uris?: Maybe; collection_id: Scalars["String"]; collection_name: Scalars["String"]; creator_address: Scalars["String"]; @@ -2482,6 +2478,7 @@ export type CurrentCollectionsV2BoolExp = { _and?: InputMaybe>; _not?: InputMaybe; _or?: InputMaybe>; + cdn_asset_uris?: InputMaybe; collection_id?: InputMaybe; collection_name?: InputMaybe; creator_address?: InputMaybe; @@ -2500,6 +2497,7 @@ export type CurrentCollectionsV2BoolExp = { /** Ordering options when selecting data from "current_collections_v2". */ export type CurrentCollectionsV2OrderBy = { + cdn_asset_uris?: InputMaybe; collection_id?: InputMaybe; collection_name?: InputMaybe; creator_address?: InputMaybe; @@ -3392,6 +3390,8 @@ export type CurrentTokenDatasStreamCursorValueInput = { export type CurrentTokenDatasV2 = { /** An object relationship */ aptos_name?: Maybe; + /** An object relationship */ + cdn_asset_uris?: Maybe; collection_id: Scalars["String"]; /** An object relationship */ current_collection?: Maybe; @@ -3422,6 +3422,7 @@ export type CurrentTokenDatasV2BoolExp = { _not?: InputMaybe; _or?: InputMaybe>; aptos_name?: InputMaybe; + cdn_asset_uris?: InputMaybe; collection_id?: InputMaybe; current_collection?: InputMaybe; current_token_ownership?: InputMaybe; @@ -3442,6 +3443,7 @@ export type CurrentTokenDatasV2BoolExp = { /** Ordering options when selecting data from "current_token_datas_v2". */ export type CurrentTokenDatasV2OrderBy = { aptos_name?: InputMaybe; + cdn_asset_uris?: InputMaybe; collection_id?: InputMaybe; current_collection?: InputMaybe; current_token_ownership?: InputMaybe; @@ -6004,6 +6006,91 @@ export type NftMarketplaceV2NftMarketplaceActivitiesStreamCursorValueInput = { transaction_version?: InputMaybe; }; +/** columns and relationships of "nft_metadata_crawler.parsed_asset_uris" */ +export type NftMetadataCrawlerParsedAssetUris = { + animation_optimizer_retry_count: Scalars["Int"]; + asset_uri: Scalars["String"]; + cdn_animation_uri?: Maybe; + cdn_image_uri?: Maybe; + cdn_json_uri?: Maybe; + image_optimizer_retry_count: Scalars["Int"]; + json_parser_retry_count: Scalars["Int"]; + raw_animation_uri?: Maybe; + raw_image_uri?: Maybe; +}; + +/** Boolean expression to filter rows from the table "nft_metadata_crawler.parsed_asset_uris". All fields are combined with a logical 'AND'. */ +export type NftMetadataCrawlerParsedAssetUrisBoolExp = { + _and?: InputMaybe>; + _not?: InputMaybe; + _or?: InputMaybe>; + animation_optimizer_retry_count?: InputMaybe; + asset_uri?: InputMaybe; + cdn_animation_uri?: InputMaybe; + cdn_image_uri?: InputMaybe; + cdn_json_uri?: InputMaybe; + image_optimizer_retry_count?: InputMaybe; + json_parser_retry_count?: InputMaybe; + raw_animation_uri?: InputMaybe; + raw_image_uri?: InputMaybe; +}; + +/** Ordering options when selecting data from "nft_metadata_crawler.parsed_asset_uris". */ +export type NftMetadataCrawlerParsedAssetUrisOrderBy = { + animation_optimizer_retry_count?: InputMaybe; + asset_uri?: InputMaybe; + cdn_animation_uri?: InputMaybe; + cdn_image_uri?: InputMaybe; + cdn_json_uri?: InputMaybe; + image_optimizer_retry_count?: InputMaybe; + json_parser_retry_count?: InputMaybe; + raw_animation_uri?: InputMaybe; + raw_image_uri?: InputMaybe; +}; + +/** select columns of table "nft_metadata_crawler.parsed_asset_uris" */ +export enum NftMetadataCrawlerParsedAssetUrisSelectColumn { + /** column name */ + AnimationOptimizerRetryCount = "animation_optimizer_retry_count", + /** column name */ + AssetUri = "asset_uri", + /** column name */ + CdnAnimationUri = "cdn_animation_uri", + /** column name */ + CdnImageUri = "cdn_image_uri", + /** column name */ + CdnJsonUri = "cdn_json_uri", + /** column name */ + ImageOptimizerRetryCount = "image_optimizer_retry_count", + /** column name */ + JsonParserRetryCount = "json_parser_retry_count", + /** column name */ + RawAnimationUri = "raw_animation_uri", + /** column name */ + RawImageUri = "raw_image_uri", +} + +/** Streaming cursor of the table "nft_metadata_crawler_parsed_asset_uris" */ +export type NftMetadataCrawlerParsedAssetUrisStreamCursorInput = { + /** Stream column input with initial value */ + initial_value: NftMetadataCrawlerParsedAssetUrisStreamCursorValueInput; + /** cursor ordering */ + ordering?: InputMaybe; +}; + +/** Initial value of the column from where the streaming should start */ +export type NftMetadataCrawlerParsedAssetUrisStreamCursorValueInput = { + animation_optimizer_retry_count?: InputMaybe; + asset_uri?: InputMaybe; + cdn_animation_uri?: InputMaybe; + cdn_image_uri?: InputMaybe; + cdn_json_uri?: InputMaybe; + image_optimizer_retry_count?: InputMaybe; + json_parser_retry_count?: InputMaybe; + raw_animation_uri?: InputMaybe; + raw_image_uri?: InputMaybe; +}; + /** columns and relationships of "num_active_delegator_per_pool" */ export type NumActiveDelegatorPerPool = { num_active_delegator?: Maybe; @@ -6482,6 +6569,10 @@ export type QueryRoot = { nft_marketplace_v2_nft_marketplace_activities: Array; /** fetch data from the table: "nft_marketplace_v2.nft_marketplace_activities" using primary key columns */ nft_marketplace_v2_nft_marketplace_activities_by_pk?: Maybe; + /** fetch data from the table: "nft_metadata_crawler.parsed_asset_uris" */ + nft_metadata_crawler_parsed_asset_uris: Array; + /** fetch data from the table: "nft_metadata_crawler.parsed_asset_uris" using primary key columns */ + nft_metadata_crawler_parsed_asset_uris_by_pk?: Maybe; /** fetch data from the table: "num_active_delegator_per_pool" */ num_active_delegator_per_pool: Array; /** fetch data from the table: "processor_status" */ @@ -7141,6 +7232,18 @@ export type QueryRootNftMarketplaceV2NftMarketplaceActivitiesByPkArgs = { transaction_version: Scalars["bigint"]; }; +export type QueryRootNftMetadataCrawlerParsedAssetUrisArgs = { + distinct_on?: InputMaybe>; + limit?: InputMaybe; + offset?: InputMaybe; + order_by?: InputMaybe>; + where?: InputMaybe; +}; + +export type QueryRootNftMetadataCrawlerParsedAssetUrisByPkArgs = { + asset_uri: Scalars["String"]; +}; + export type QueryRootNumActiveDelegatorPerPoolArgs = { distinct_on?: InputMaybe>; limit?: InputMaybe; @@ -7573,6 +7676,12 @@ export type SubscriptionRoot = { nft_marketplace_v2_nft_marketplace_activities_by_pk?: Maybe; /** fetch data from the table in a streaming manner : "nft_marketplace_v2.nft_marketplace_activities" */ nft_marketplace_v2_nft_marketplace_activities_stream: Array; + /** fetch data from the table: "nft_metadata_crawler.parsed_asset_uris" */ + nft_metadata_crawler_parsed_asset_uris: Array; + /** fetch data from the table: "nft_metadata_crawler.parsed_asset_uris" using primary key columns */ + nft_metadata_crawler_parsed_asset_uris_by_pk?: Maybe; + /** fetch data from the table in a streaming manner : "nft_metadata_crawler.parsed_asset_uris" */ + nft_metadata_crawler_parsed_asset_uris_stream: Array; /** fetch data from the table: "num_active_delegator_per_pool" */ num_active_delegator_per_pool: Array; /** fetch data from the table in a streaming manner : "num_active_delegator_per_pool" */ @@ -8512,6 +8621,24 @@ export type SubscriptionRootNftMarketplaceV2NftMarketplaceActivitiesStreamArgs = where?: InputMaybe; }; +export type SubscriptionRootNftMetadataCrawlerParsedAssetUrisArgs = { + distinct_on?: InputMaybe>; + limit?: InputMaybe; + offset?: InputMaybe; + order_by?: InputMaybe>; + where?: InputMaybe; +}; + +export type SubscriptionRootNftMetadataCrawlerParsedAssetUrisByPkArgs = { + asset_uri: Scalars["String"]; +}; + +export type SubscriptionRootNftMetadataCrawlerParsedAssetUrisStreamArgs = { + batch_size: Scalars["Int"]; + cursor: Array>; + where?: InputMaybe; +}; + export type SubscriptionRootNumActiveDelegatorPerPoolArgs = { distinct_on?: InputMaybe>; limit?: InputMaybe;