Skip to content

Commit

Permalink
Add getChainTopUserTransactions.graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin authored and gregnazario committed Oct 13, 2023
1 parent e7dea8e commit e17670e
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/api/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
import {
getBlockByHeight,
getBlockByVersion,
getChainTopUserTransactions,
getLedgerInfo,
getTableItem,
queryIndexer,
view,
} from "../internal/general";
import { Block, GraphqlQuery, LedgerInfo, LedgerVersion, MoveValue, TableItemRequest, ViewRequest } from "../types";
import {
Block,
GetChainTopUserTransactionsResponse,
GraphqlQuery,
LedgerInfo,
LedgerVersion,
MoveValue,
TableItemRequest,
ViewRequest,
} from "../types";
import { AptosConfig } from "./aptos_config";

/**
Expand Down Expand Up @@ -125,6 +135,21 @@ export class General {
return data[0];
}

/**
* Queries top user transactions
*
* @param args.limit
* @returns GetChainTopUserTransactionsResponse
*/
async getChainTopUserTransactions(args: { limit: number }): Promise<GetChainTopUserTransactionsResponse> {
const data = getChainTopUserTransactions({
aptosConfig: this.config,
...args,
});

return data;
}

/**
* A generic function for retrieving data from Aptos Indexer.
* For more detailed queries specification see
Expand Down
32 changes: 31 additions & 1 deletion src/internal/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@

import { AptosConfig } from "../api/aptos_config";
import { getAptosFullNode, postAptosFullNode, postAptosIndexer } from "../client";
import { Block, GraphqlQuery, LedgerInfo, LedgerVersion, MoveValue, TableItemRequest, ViewRequest } from "../types";
import {
Block,
GetChainTopUserTransactionsResponse,
GraphqlQuery,
LedgerInfo,
LedgerVersion,
MoveValue,
TableItemRequest,
ViewRequest,
} from "../types";
import { GetChainTopUserTransactionsQuery } from "../types/generated/operations";
import { GetChainTopUserTransactions } from "../types/generated/queries";

export async function getLedgerInfo(args: { aptosConfig: AptosConfig }): Promise<LedgerInfo> {
const { aptosConfig } = args;
Expand Down Expand Up @@ -85,6 +96,25 @@ export async function view(args: {
return data;
}

export async function getChainTopUserTransactions(args: {
aptosConfig: AptosConfig;
limit: number;
}): Promise<GetChainTopUserTransactionsResponse> {
const { aptosConfig, limit } = args;
const graphqlQuery = {
query: GetChainTopUserTransactions,
variables: { limit },
};

const data = await queryIndexer<GetChainTopUserTransactionsQuery>({
aptosConfig,
query: graphqlQuery,
originMethod: "getChainTopUserTransactions",
});

return data.user_transactions;
}

export async function queryIndexer<T>(args: {
aptosConfig: AptosConfig;
query: GraphqlQuery;
Expand Down
5 changes: 5 additions & 0 deletions src/internal/queries/getChainTopUserTransactions.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query getChainTopUserTransactions($limit: Int) {
user_transactions(limit: $limit, order_by: { version: desc }) {
version
}
}
6 changes: 6 additions & 0 deletions src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ export type GetAccountTransactionsCountQuery = {
account_transactions_aggregate: { aggregate?: { count: number } | null };
};

export type GetChainTopUserTransactionsQueryVariables = Types.Exact<{
limit?: Types.InputMaybe<Types.Scalars["Int"]>;
}>;

export type GetChainTopUserTransactionsQuery = { user_transactions: Array<{ version: any }> };

export type GetCollectionDataQueryVariables = Types.Exact<{
where_condition: Types.CurrentCollectionsV2BoolExp;
}>;
Expand Down
21 changes: 21 additions & 0 deletions src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ export const GetAccountTransactionsCount = `
}
}
`;
export const GetChainTopUserTransactions = `
query getChainTopUserTransactions($limit: Int) {
user_transactions(limit: $limit, order_by: {version: desc}) {
version
}
}
`;
export const GetCollectionData = `
query getCollectionData($where_condition: current_collections_v2_bool_exp!) {
current_collections_v2(where: $where_condition) {
Expand Down Expand Up @@ -386,6 +393,20 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
"query",
);
},
getChainTopUserTransactions(
variables?: Types.GetChainTopUserTransactionsQueryVariables,
requestHeaders?: Dom.RequestInit["headers"],
): Promise<Types.GetChainTopUserTransactionsQuery> {
return withWrapper(
(wrappedRequestHeaders) =>
client.request<Types.GetChainTopUserTransactionsQuery>(GetChainTopUserTransactions, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
"getChainTopUserTransactions",
"query",
);
},
getCollectionData(
variables: Types.GetCollectionDataQueryVariables,
requestHeaders?: Dom.RequestInit["headers"],
Expand Down
2 changes: 2 additions & 0 deletions src/types/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
GetDelegatedStakingActivitiesQuery,
GetNumberOfDelegatorsQuery,
GetCollectionDataQuery,
GetChainTopUserTransactionsQuery,
} from "./generated/operations";

/**
Expand All @@ -41,6 +42,7 @@ export type GetAccountOwnedTokensFromCollectionResponse =
export type GetAccountCollectionsWithOwnedTokenResponse =
GetAccountCollectionsWithOwnedTokensQuery["current_collection_ownership_v2_view"];
export type GetAccountCoinsDataResponse = GetAccountCoinsDataQuery["current_fungible_asset_balances"];
export type GetChainTopUserTransactionsResponse = GetChainTopUserTransactionsQuery["user_transactions"];

export type GetNumberOfDelegatorsResponse = GetNumberOfDelegatorsQuery["num_active_delegator_per_pool"];
export type GetDelegatedStakingActivitiesResponse = GetDelegatedStakingActivitiesQuery["delegated_staking_activities"];
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/api/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,12 @@ describe("general api", () => {

expect(chainId.ledger_infos[0].chain_id).toBe(2);
});

test("it should fetch chain top user transactions", async () => {
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);

const topUserTransactions = await aptos.getChainTopUserTransactions({ limit: 3 });
expect(topUserTransactions.length).toEqual(3);
});
});

0 comments on commit e17670e

Please sign in to comment.