Skip to content

Commit

Permalink
add new api to get all user tx versions and update get user txs
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaptosj committed Apr 15, 2024
1 parent 26b3e64 commit 11b9531
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T
- [`Breaking`] Change any generate transaction function to return `SimpleTransaction` or `MultiAgentTransaction` instance
- Adds `getUserTransactionHash` which can generate a transaction hash after signing, but before submission
- Add function to create resource address locally
- Add `getAccountAllTransactionVersions` which returns versions of all transactions related to the given address.
- Update the behavior of existing `getAccountTransactions` so it 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.11.0 (2024-03-26)

Expand Down
22 changes: 22 additions & 0 deletions 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,
getAccountAllTransactionVersions,
getAccountCoinAmount,
getAccountCoinsCount,
getAccountCoinsData,
Expand Down Expand Up @@ -137,6 +138,27 @@ export class Account {
});
}

/**
* Queries account's all transaction versions given an account address
*
* Note: This returns both the transaction sent by the account and the transaction 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 getAccountAllTransactionVersions(args: {
accountAddress: AccountAddressInput;
options?: PaginationArgs;
}): Promise<AnyNumber[]> {
return getAccountAllTransactionVersions({
aptosConfig: this.config,
...args,
});
}

/**
* Queries all account resources given an account address
*
Expand Down
45 changes: 41 additions & 4 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 @@ -33,6 +34,7 @@ import {
WhereArg,
} from "../types";
import {
GetAccountAllTransactionVersionsQuery,
GetAccountCoinsCountQuery,
GetAccountCoinsDataQuery,
GetAccountCollectionsWithOwnedTokensQuery,
Expand All @@ -43,6 +45,7 @@ import {
GetAccountTransactionsCountQuery,
} from "../types/generated/operations";
import {
GetAccountAllTransactionVersions,
GetAccountCoinsCount,
GetAccountCoinsData,
GetAccountCollectionsWithOwnedTokens,
Expand All @@ -55,6 +58,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 @@ -137,12 +141,45 @@ export async function getTransactions(args: {
options?: PaginationArgs;
}): Promise<TransactionResponse[]> {
const { aptosConfig, accountAddress, options } = args;
return paginateWithCursor<{}, TransactionResponse[]>({
// TODO: Ideally indexer should provide one API that returns all transactions details in one go
// But now we have to query all transaction versions first and then query each transaction by version
const versions = await getAccountAllTransactionVersions({ 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 getAccountAllTransactionVersions(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: GetAccountAllTransactionVersions,
variables: {
where_condition: whereCondition,
offset: options?.offset,
limit: options?.limit,
},
};

const data = await queryIndexer<GetAccountAllTransactionVersionsQuery>({
aptosConfig,
originMethod: "getTransactions",
path: `accounts/${AccountAddress.from(accountAddress).toString()}/transactions`,
params: { start: options?.offset, limit: options?.limit },
query: graphqlQuery,
originMethod: "getAccountAllTransactionVersions",
});

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

export async function getResources(args: {
Expand Down
10 changes: 10 additions & 0 deletions src/internal/queries/getAccountAllTransactionVersions.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query getAccountAllTransactionVersions($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
}
}
13 changes: 8 additions & 5 deletions src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export type CurrentTokenOwnershipFieldsFragment = {
token_properties: any;
token_standard: string;
token_uri: string;
decimals: any;
current_collection?: {
collection_id: string;
collection_name: string;
Expand All @@ -75,6 +74,14 @@ export type CurrentTokenOwnershipFieldsFragment = {
} | null;
};

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

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

export type GetAccountCoinsCountQueryVariables = Types.Exact<{
address?: Types.InputMaybe<Types.Scalars["String"]["input"]>;
}>;
Expand Down Expand Up @@ -212,7 +219,6 @@ export type GetAccountOwnedTokensQuery = {
token_properties: any;
token_standard: string;
token_uri: string;
decimals: any;
current_collection?: {
collection_id: string;
collection_name: string;
Expand Down Expand Up @@ -268,7 +274,6 @@ export type GetAccountOwnedTokensByTokenDataQuery = {
token_properties: any;
token_standard: string;
token_uri: string;
decimals: any;
current_collection?: {
collection_id: string;
collection_name: string;
Expand Down Expand Up @@ -324,7 +329,6 @@ export type GetAccountOwnedTokensFromCollectionQuery = {
token_properties: any;
token_standard: string;
token_uri: string;
decimals: any;
current_collection?: {
collection_id: string;
collection_name: string;
Expand Down Expand Up @@ -597,7 +601,6 @@ export type GetCurrentTokenOwnershipQuery = {
token_properties: any;
token_standard: string;
token_uri: string;
decimals: any;
current_collection?: {
collection_id: string;
collection_name: string;
Expand Down
27 changes: 26 additions & 1 deletion src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export const CurrentTokenOwnershipFieldsFragmentDoc = `
token_properties
token_standard
token_uri
decimals
current_collection {
collection_id
collection_name
Expand All @@ -80,6 +79,18 @@ export const CurrentTokenOwnershipFieldsFragmentDoc = `
}
}
`;
export const GetAccountAllTransactionVersions = `
query getAccountAllTransactionVersions($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 GetAccountCoinsCount = `
query getAccountCoinsCount($address: String) {
current_fungible_asset_balances_aggregate(
Expand Down Expand Up @@ -465,6 +476,20 @@ const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationTy

export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
getAccountAllTransactionVersions(
variables: Types.GetAccountAllTransactionVersionsQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
): Promise<Types.GetAccountAllTransactionVersionsQuery> {
return withWrapper(
(wrappedRequestHeaders) =>
client.request<Types.GetAccountAllTransactionVersionsQuery>(GetAccountAllTransactionVersions, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
"getAccountAllTransactionVersions",
"query",
);
},
getAccountCoinsCount(
variables?: Types.GetAccountCoinsCountQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
Expand Down
Loading

0 comments on commit 11b9531

Please sign in to comment.