// Copyright © Endless // Copyright © Aptos Foundation // SPDX-License-Identifier: Apache-2.0 import { Account as AccountModule } from "../account"; import { AccountAddress, AccountAddressInput, PrivateKey } from "../core"; import { AccountBalancesResponse, AccountData, GetAccountsCoinBalanceResponse, GetAccountCoinsDataResponse, GetCreatedCoinDataResponse, LedgerVersionArg, MoveModuleBytecode, MoveResource, MoveStructId, PaginationArgs, PaginationArgsOfIndexer, PageableCollectionResponse, PageableNftOwnedCollectionResponse, PageableNftTokenResponse, TransactionResponse, } from "../types"; import { deriveAccountFromPrivateKey, getAccountCoinAmount, getAccountsCoinBalance, getAccountsCoinsBalances, getAccountCoinsData, getInfo, getModule, getModules, getResource, getResources, getTransactions, lookupOriginalAccountAddress, getCoinsDataCreatedBy, getAccountCollections, getAccountNfts, getAccountNftCollections, } from "../internal/account"; import { // ENDLESS_COIN, ENDLESS_COIN_ID, } from "../utils/const"; import { EndlessConfig } from "./endlessConfig"; /** * A class to query all `Account` related queries on Endless. */ export class Account { constructor(readonly config: EndlessConfig) {} /** * Queries the current state for an Endless account given its account address * * @param args.accountAddress Endless account address * * @returns The account data * * @example An example of the returned account * ``` * { * sequence_number: "1", * authentication_key: "0x5307b5f4bc67829097a8ba9b43dba3b88261eeccd1f709d9bde240fc100fbb69" * } * ``` */ async getAccountInfo(args: { accountAddress: AccountAddressInput }): Promise { return getInfo({ endlessConfig: this.config, ...args }); } /** * Queries for all modules in an account given an account address * * Note: In order to get all account modules, this function may call the API * multiple times as it auto paginates. * * @param args.accountAddress Endless account address * @param args.options.offset The number module to start returning results from * @param args.options.limit The number of results to return * @param args.options.ledgerVersion The ledger version to query, if not provided it will get the latest version * * @returns Account modules */ async getAccountModules(args: { accountAddress: AccountAddressInput; options?: PaginationArgs & LedgerVersionArg; }): Promise { return getModules({ endlessConfig: this.config, ...args }); } /** * Queries for a specific account module given account address and module name * * @param args.accountAddress Endless account address * @param args.moduleName The name of the module * @param args.options.ledgerVersion The ledger version to query, if not provided it will get the latest version * * @returns Account module * * @example * const module = await endless.getAccountModule({accountAddress:"0x456"}) * // An example of an account module response * ``` * { * bytecode: "0xa11ceb0b0600000006010002030206050807070f0d081c200", * abi: { address: "0x1" } * } * ``` */ async getAccountModule(args: { accountAddress: AccountAddressInput; moduleName: string; options?: LedgerVersionArg; }): Promise { return getModule({ endlessConfig: this.config, ...args }); } /** * Queries account transactions given an 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 endless.getAccountTransactions({accountAddress:"0x456"}) * * @param args.accountAddress Endless 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 getAccountTransactions(args: { accountAddress: AccountAddressInput; options?: PaginationArgs; }): Promise { return getTransactions({ endlessConfig: this.config, ...args, }); } /** * Queries all account resources given an account address * * Note: In order to get all account resources, this function may call the API * multiple times as it auto paginates. * * @example * const resources = await endless.getAccountResources({accountAddress:"0x456"}) * * @param args.accountAddress Endless account address * @param args.options.offset The number resource to start returning results from * @param args.options.limit The number of results to return * @param args.options.ledgerVersion The ledger version to query, if not provided it will get the latest version * @returns Account resources */ async getAccountResources(args: { accountAddress: AccountAddressInput; options?: PaginationArgs & LedgerVersionArg; }): Promise { return getResources({ endlessConfig: this.config, ...args }); } /** * Queries a specific account resource given account address and resource type. Note that the default is `any` in order * to allow for ease of accessing properties of the object. * * @type The typed output of the resource * @param args.accountAddress Endless account address * @param args.resourceType String representation of an on-chain Move struct type, i.e "0x1::endless_coin::EndlessCoin" * @param args.options.ledgerVersion The ledger version to query, if not provided it will get the latest version * * @returns Account resource * * @example * const resource = await endless.getAccountResource({accountAddress:"0x456"}) * */ async getAccountResource(args: { accountAddress: AccountAddressInput; resourceType: MoveStructId; options?: LedgerVersionArg; }): Promise { return getResource({ endlessConfig: this.config, ...args }); } /** * Looks up the account address for a given authentication key * * This handles both if the account's authentication key has been rotated or not. * * @example * const accountAddress = await endless.lookupOriginalAccountAddress({authenticationKey:account.accountAddress}) * * @param args.authenticationKey The authentication key * @param args.options.ledgerVersion The ledger version to query, if not provided it will get the latest version * @returns Promise The accountAddress associated with the authentication key */ async lookupOriginalAccountAddress(args: { authenticationKey: AccountAddressInput; options?: LedgerVersionArg; }): Promise { return lookupOriginalAccountAddress({ endlessConfig: this.config, ...args }); } /** * Queries an account's coins data * * @example * const accountCoinsData = await endless.getAccountCoinsData({accountAddress:"0x123"}) * * @param args.accountAddress The account address we want to get the coins data for * @param args.options.page optional. The page number * @param args.options.pageSize optional. The number of results to return * @param args.options.coin optional. Filter by coin id * @returns Array with the coins data */ async getAccountCoinsData(args: { accountAddress: AccountAddressInput; options?: PaginationArgsOfIndexer & { coin?: string }; }): Promise { return getAccountCoinsData({ endlessConfig: this.config, ...args, }); } /** * Queries coins data created by account * * @example * const accountCoinsData = await endless.getCoinsDataCreatedBy({accountAddress:"0x123"}) * * @param args.accountAddress The account address of who create the coins * @param args.options.page optional. The page number * @param args.options.pageSize optional. The number of results to return * @param args.options.coin optional. Filter by coin id * @returns Array with the coins data */ async getCoinsDataCreatedBy(args: { accountAddress: AccountAddressInput; options?: PaginationArgsOfIndexer & { coin?: string }; }): Promise { return getCoinsDataCreatedBy({ endlessConfig: this.config, ...args, }); } /** * Queries the account's EDS amount * * @example * const accountAPTAmount = await endless.getAccountAPTAmount({accountAddress:"0x123"}) * * @param args.accountAddress The account address we want to get the total count for * @returns Current amount of account's APT */ // async getAccountAPTAmount(args: { // accountAddress: AccountAddressInput; // }): Promise { // return this.getAccountCoinAmount({ coinType: ENDLESS_COIN, ...args }); // } async getAccountEDSAmount(args: { accountAddress: AccountAddressInput; }): Promise { return this.getAccountCoinAmount({ coinId: ENDLESS_COIN_ID, ...args }); } /** * Queries the account's coin amount by the coin type * * @example * const accountCoinAmount = await endless.getAccountCoinAmount({accountAddress:"0x123", coinType:"0x1::endless_coin::EndlessCoin"}) * * @param args.accountAddress The account address we want to get the total count for * @param args.coinType The coin type to query * @returns Current amount of account's coin */ async getAccountCoinAmount(args: { accountAddress: AccountAddressInput; coinId: string; }): Promise { return getAccountCoinAmount({ endlessConfig: this.config, ...args }); } /** * Derives an account by providing a private key. * This functions resolves the provided private key type and derives the public key from it. * * If the privateKey is a Secp256k1 type, it derives the account using the derived public key and * auth key using the SingleKey scheme locally. * * If the privateKey is a ED25519 type, it looks up the authentication key on chain, and uses it to resolve * whether it is a Legacy ED25519 key or a Unified ED25519 key. It then derives the account based * on that. * * @example * const account = await endless.deriveAccountFromPrivateKey({privateKey:new Ed25519PrivateKey("0x123")}) * * @param args.privateKey An account private key * @returns Account type */ async deriveAccountFromPrivateKey(args: { privateKey: PrivateKey }): Promise { return deriveAccountFromPrivateKey({ endlessConfig: this.config, ...args }); } /** * Queries an account's coins data * * @example * const accountCoinsData = await endless.getAccountCoinsData({accountAddress:["0x123"]}) * * @param args.accountAddress The account address we want to get the coins data for * @param args.coinAddress The coinAddress address we want to get the coins data for * @returns Array with the coins data */ async getAccountsCoinBalance(args: { accountAddress: AccountAddressInput[]; coinAddress: AccountAddressInput; }): Promise { return getAccountsCoinBalance({ endlessConfig: this.config, ...args, }); } /** * Queries the balances of specified coins for multiple accounts. * * @example * const balances = await endless.getAccountsCoinsBalances({ * accounts: ["0x123", "0x456"], * coins: ["0x1::aptos_coin::AptosCoin"] * }); * * @param args.accounts The account addresses to query balances for. * @param args.coins The coin types (e.g., "0x1::aptos_coin::AptosCoin") to query balances for. * @returns An array of account balances corresponding to the requested accounts. */ async getAccountsCoinsBalances(args: { accounts: AccountAddressInput[]; coins: string[]; }): Promise { return getAccountsCoinsBalances({ endlessConfig: this.config, ...args, }); } /** * Queries for collections created by an account * * @param args.accountAddress The account address * @param args.options.page optional. The page number * @param args.options.pageSize optional. The number of results per page * @param args.options.collection optional. Filter by collection name or ID * @returns PageableCollectionResponse */ async getAccountCollections(args: { accountAddress: AccountAddressInput; options?: PaginationArgsOfIndexer & { collection?: string }; }): Promise { return getAccountCollections({ endlessConfig: this.config, ...args }); } /** * Queries for NFTs owned by an account * * @param args.accountAddress The account address * @param args.options.page optional. The page number * @param args.options.pageSize optional. The number of results per page * @param args.options.token optional. Filter by token name or ID * @param args.options.collection optional. Filter by collection name or ID * @returns PageableNftTokenResponse */ async getAccountNfts(args: { accountAddress: AccountAddressInput; options?: PaginationArgsOfIndexer & { token?: string; collection?: string }; }): Promise { return getAccountNfts({ endlessConfig: this.config, ...args }); } /** * Queries for NFT collections where the account owns tokens * * @param args.accountAddress The account address * @param args.options.page optional. The page number * @param args.options.pageSize optional. The number of results per page * @param args.options.collection optional. Filter by collection name or ID * @returns PageableNftOwnedCollectionResponse */ async getAccountNftCollections(args: { accountAddress: AccountAddressInput; options?: PaginationArgsOfIndexer & { collection?: string }; }): Promise { return getAccountNftCollections({ endlessConfig: this.config, ...args }); } }