Skip to content

Commit

Permalink
add new apt 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 6864cc4
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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 both sent and received by the given address.

# 1.11.0 (2024-03-26)

Expand Down
24 changes: 24 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 @@ -120,6 +121,8 @@ export class Account {
*
* Note: In order to get all account transactions, this function may call the API
* multiple times as it auto paginates.
* This only returns transactions sent by the account.
* To get transactions both sent and received by the account, use `getAccountAllTransactionVersions`
*
* @param args.accountAddress Aptos account address
* @param args.options.offset The number transaction to start returning results from
Expand All @@ -137,6 +140,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<TransactionResponse[]> {
return getAccountAllTransactionVersions({
aptosConfig: this.config,
...args,
});
}

/**
* Queries all account resources given an account address
*
Expand Down
48 changes: 44 additions & 4 deletions src/internal/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
WhereArg,
} from "../types";
import {
GetAccountAllTransactionVersionsQuery,
GetAccountCoinsCountQuery,
GetAccountCoinsDataQuery,
GetAccountCollectionsWithOwnedTokensQuery,
Expand All @@ -43,6 +44,7 @@ import {
GetAccountTransactionsCountQuery,
} from "../types/generated/operations";
import {
GetAccountAllTransactionVersions,
GetAccountCoinsCount,
GetAccountCoinsData,
GetAccountCollectionsWithOwnedTokens,
Expand All @@ -55,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 @@ -137,12 +140,49 @@ export async function getTransactions(args: {
options?: PaginationArgs;
}): Promise<TransactionResponse[]> {
const { aptosConfig, accountAddress, options } = args;
return paginateWithCursor<{}, TransactionResponse[]>({
// return paginateWithCursor<{}, TransactionResponse[]>({
// aptosConfig,
// originMethod: "getTransactions",
// path: `accounts/${AccountAddress.from(accountAddress).toString()}/transactions`,
// params: { start: options?.offset, limit: options?.limit },
// });
const versions = await getAccountAllTransactionVersions({ aptosConfig, accountAddress, options });
const results = [];
for (const version of versions) {
const tx = getTransactionByVersion({ aptosConfig, ledgerVersion: version.version });
results.push(tx);
}
return Promise.all(results);
}

export async function getAccountAllTransactionVersions(args: {
aptosConfig: AptosConfig;
accountAddress: AccountAddressInput;
options?: PaginationArgs;
}): Promise<any[]> {
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;
}

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($address: String, $limit: Int, $offset: Int) {
account_transactions(
where: { account_address: { _eq: $address } }
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<{
address?: Types.InputMaybe<Types.Scalars["String"]["input"]>;
limit?: Types.InputMaybe<Types.Scalars["Int"]["input"]>;
offset?: 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($address: String, $limit: Int, $offset: Int) {
account_transactions(
where: {account_address: {_eq: $address}}
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 6864cc4

Please sign in to comment.