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 new indexer API to getAccountAllTransactionVersions and use it in getAccountTransactions #364

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T

# Unreleased

- Add `getAllAccountTransactionVersions` which returns versions of all transaction versions related to the given address.
- Add `getAllAccountTransactions` which returns all transactions related to given address instead of only ones sent by address. This makes the result has same length as calling `getAccountTransactionsCount`.

# 1.13.1 (2024-04-23)

- [`Fix`] Fixes Local ABI to use it locally rather than make an external network call
Expand Down
50 changes: 49 additions & 1 deletion src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from "../types";
import {
deriveAccountFromPrivateKey,
GetAllAccountTransactionVersions,
getAccountCoinAmount,
getAccountCoinsCount,
getAccountCoinsData,
Expand All @@ -31,6 +32,7 @@ import {
getAccountOwnedTokensFromCollectionAddress,
getAccountTokensCount,
getAccountTransactionsCount,
getAllAccountTransactions,
getInfo,
getModule,
getModules,
Expand Down Expand Up @@ -118,7 +120,7 @@ export class Account {
}

/**
* Queries account transactions given an account address
* Queries account transactions sent by a given account address
*
* Note: In order to get all account transactions, this function may call the API
* multiple times as it auto paginates.
Expand All @@ -142,6 +144,52 @@ export class Account {
});
}

/**
* Queries account transactions related to a given account address
*
* Note: In order to get all account transactions, this function may call the API
* multiple times as it auto paginates.
*
* @example
* const transactions = await aptos.getAllAccountTransactions({accountAddress:"0x456"})
*
* @param args.accountAddress Aptos account address
* @param args.options.offset The number transaction to start returning results from
* @param args.options.limit The number of results to return
*
* @returns The account transactions
*/
async getAllAccountTransactions(args: {
accountAddress: AccountAddressInput;
options?: PaginationArgs;
}): Promise<TransactionResponse[]> {
return getAllAccountTransactions({
aptosConfig: this.config,
...args,
});
}

/**
* Queries account's all transaction versions given an account address
*
* Note: This returns both the transaction sent by the account and received by the account.
*
* @param args.accountAddress Aptos account address
* @param args.options.offset The number transaction to start returning results from
* @param args.options.limit The number of results to return
*
* @returns The account transaction versions
*/
async GetAllAccountTransactionVersions(args: {
accountAddress: AccountAddressInput;
options?: PaginationArgs;
}): Promise<AnyNumber[]> {
return GetAllAccountTransactionVersions({
aptosConfig: this.config,
...args,
});
}

/**
* Queries all account resources given an account address
*
Expand Down
49 changes: 49 additions & 0 deletions src/internal/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { AnyPublicKey, Ed25519PublicKey, PrivateKey } from "../core/crypto";
import { getTableItem, queryIndexer } from "./general";
import {
AccountData,
AnyNumber,
GetAccountCoinsDataResponse,
GetAccountCollectionsWithOwnedTokenResponse,
GetAccountOwnedObjectsResponse,
Expand All @@ -32,6 +33,7 @@ import {
WhereArg,
} from "../types";
import {
GetAllAccountTransactionVersionsQuery,
GetAccountCoinsCountQuery,
GetAccountCoinsDataQuery,
GetAccountCollectionsWithOwnedTokensQuery,
Expand All @@ -42,6 +44,7 @@ import {
GetAccountTransactionsCountQuery,
} from "../types/generated/operations";
import {
GetAllAccountTransactionVersions as GetAllAccountTransactionVersionsGql,
GetAccountCoinsCount,
GetAccountCoinsData,
GetAccountCollectionsWithOwnedTokens,
Expand All @@ -54,6 +57,7 @@ import {
import { memoizeAsync } from "../utils/memoize";
import { Secp256k1PrivateKey, AuthenticationKey, Ed25519PrivateKey } from "../core";
import { CurrentFungibleAssetBalancesBoolExp } from "../types/generated/types";
import { getTransactionByVersion } from "./transaction";

export async function getInfo(args: {
aptosConfig: AptosConfig;
Expand Down Expand Up @@ -144,6 +148,51 @@ export async function getTransactions(args: {
});
}

export async function getAllAccountTransactions(args: {
aptosConfig: AptosConfig;
accountAddress: AccountAddressInput;
options?: PaginationArgs;
}): Promise<TransactionResponse[]> {
const { aptosConfig, accountAddress, options } = args;
const versions = await GetAllAccountTransactionVersions({ aptosConfig, accountAddress, options });
const results = [];
for (const version of versions) {
const tx = getTransactionByVersion({ aptosConfig, ledgerVersion: version });
results.push(tx);
}
return Promise.all(results);
}

export async function GetAllAccountTransactionVersions(args: {
aptosConfig: AptosConfig;
accountAddress: AccountAddressInput;
options?: PaginationArgs;
}): Promise<AnyNumber[]> {
const { aptosConfig, accountAddress, options } = args;
const address = AccountAddress.from(accountAddress).toStringLong();

const whereCondition: { account_address: { _eq: string } } = {
account_address: { _eq: address },
};

const graphqlQuery = {
query: GetAllAccountTransactionVersionsGql,
variables: {
where_condition: whereCondition,
offset: options?.offset,
limit: options?.limit,
},
};

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

return data.account_transactions.map((tx) => tx.transaction_version);
}

export async function getResources(args: {
aptosConfig: AptosConfig;
accountAddress: AccountAddressInput;
Expand Down
10 changes: 10 additions & 0 deletions src/internal/queries/getAllAccountTransactionVersions.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query getAllAccountTransactionVersions($where_condition: account_transactions_bool_exp!, $offset: Int, $limit: Int) {
account_transactions(
where: $where_condition
order_by: { transaction_version: desc }
limit: $limit
offset: $offset
) {
transaction_version
}
}
8 changes: 8 additions & 0 deletions src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ export type GetAccountTransactionsCountQuery = {
account_transactions_aggregate: { aggregate?: { count: number } | null };
};

export type GetAllAccountTransactionVersionsQueryVariables = Types.Exact<{
where_condition: Types.AccountTransactionsBoolExp;
offset?: Types.InputMaybe<Types.Scalars["Int"]["input"]>;
limit?: Types.InputMaybe<Types.Scalars["Int"]["input"]>;
}>;

export type GetAllAccountTransactionVersionsQuery = { account_transactions: Array<{ transaction_version: any }> };

export type GetChainTopUserTransactionsQueryVariables = Types.Exact<{
limit?: Types.InputMaybe<Types.Scalars["Int"]["input"]>;
}>;
Expand Down
26 changes: 26 additions & 0 deletions src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ export const GetAccountTransactionsCount = `
}
}
`;
export const GetAllAccountTransactionVersions = `
query getAllAccountTransactionVersions($where_condition: account_transactions_bool_exp!, $offset: Int, $limit: Int) {
account_transactions(
where: $where_condition
order_by: {transaction_version: desc}
limit: $limit
offset: $offset
) {
transaction_version
}
}
`;
export const GetChainTopUserTransactions = `
query getChainTopUserTransactions($limit: Int) {
user_transactions(limit: $limit, order_by: {version: desc}) {
Expand Down Expand Up @@ -593,6 +605,20 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
"query",
);
},
getAllAccountTransactionVersions(
variables: Types.GetAllAccountTransactionVersionsQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
): Promise<Types.GetAllAccountTransactionVersionsQuery> {
return withWrapper(
(wrappedRequestHeaders) =>
client.request<Types.GetAllAccountTransactionVersionsQuery>(GetAllAccountTransactionVersions, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
"getAllAccountTransactionVersions",
"query",
);
},
getChainTopUserTransactions(
variables?: Types.GetChainTopUserTransactionsQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
Expand Down
Loading
Loading