Class AptosClient

Provides methods for retrieving data from Aptos node. For more detailed API specification see https://fullnode.devnet.aptoslabs.com/v1/spec

Hierarchy

Constructors

  • Build a client configured to connect to an Aptos node at the given URL.

    Note: If you forget to append /v1 to the URL, the client constructor will automatically append it. If you don't want this URL processing to take place, set doNotFixNodeUrl to true.

    Parameters

    • nodeUrl: string

      URL of the Aptos Node API endpoint.

    • Optional config: Partial<OpenAPIConfig>

      Additional configuration options for the generated Axios client.

    • doNotFixNodeUrl: boolean = false

    Returns AptosClient

Properties

nodeUrl: string

Methods

  • Queries an Aptos account by address

    Returns

    Core account resource, used for identifying account and transaction execution

    Example

    An example of the returned account

    {
    sequence_number: "1",
    authentication_key: "0x5307b5f4bc67829097a8ba9b43dba3b88261eeccd1f709d9bde240fc100fbb69"
    }

    Parameters

    • accountAddress: MaybeHexString

      Hex-encoded 32 byte Aptos account address

    Returns Promise<AccountData>

  • Queries module associated with given account by module name

    Note: In order to get all account resources, this function may call the API multiple times as it paginates.

    Returns

    Specified module. Module is represented by MoveModule interface. It contains module bytecode and abi, which JSON representation of a module

    Parameters

    • accountAddress: MaybeHexString

      Hex-encoded 32 byte Aptos account address

    • moduleName: string

      The name of the module

    • Optional query: {
          ledgerVersion?: AnyNumber;
      }
      • Optional ledgerVersion?: AnyNumber

        Specifies ledger version of transactions. By default latest version will be used

    Returns Promise<MoveModuleBytecode>

  • Queries modules associated with given account

    Note: In order to get all account modules, this function may call the API multiple times as it paginates.

    Returns

    Account modules array for a specific ledger version. Module is represented by MoveModule interface. It contains module bytecode and abi, which is JSON representation of a module

    Parameters

    • accountAddress: MaybeHexString

      Hex-encoded 32 byte Aptos account address

    • Optional query: {
          ledgerVersion?: AnyNumber;
      }
      • Optional ledgerVersion?: AnyNumber

        Specifies ledger version of transactions. By default latest version will be used

    Returns Promise<MoveModuleBytecode[]>

  • Queries resource associated with given account by resource type

    Returns

    Account resource of specified type and ledger version

    Example

    An example of an account resource

    {
    type: "0x1::aptos_coin::AptosCoin",
    data: { value: 6 }
    }

    Parameters

    • accountAddress: MaybeHexString

      Hex-encoded 32 byte Aptos account address

    • resourceType: string

      String representation of an on-chain Move struct type

    • Optional query: {
          ledgerVersion?: AnyNumber;
      }
      • Optional ledgerVersion?: AnyNumber

        Specifies ledger version of transactions. By default latest version will be used

    Returns Promise<MoveResource>

  • Queries all resources associated with given account

    Returns

    Account resources for a specific ledger version

    Parameters

    • accountAddress: MaybeHexString

      Hex-encoded 32 byte Aptos account address

    • Optional query: {
          ledgerVersion?: AnyNumber;
      }
      • Optional ledgerVersion?: AnyNumber

        Specifies ledger version of transactions. By default latest version will be used

    Returns Promise<MoveResource[]>

  • Queries transactions sent by given account

    Returns

    An array of on-chain transactions, sent by account

    Parameters

    • accountAddress: MaybeHexString

      Hex-encoded 32 byte Aptos account address

    • Optional query: PaginationArgs

      Optional pagination object

    Returns Promise<Types.Transaction[]>

  • Get block by height

    Returns

    Block

    Parameters

    • blockHeight: number

      Block height to lookup. Starts at 0

    • Optional withTransactions: boolean

      If set to true, include all transactions in the block

    Returns Promise<Block>

  • Get block by block transaction version

    Returns

    Block

    Parameters

    • version: number

      Ledger version to lookup block information for

    • Optional withTransactions: boolean

      If set to true, include all transactions in the block

    Returns Promise<Block>

  • Event types are globally identifiable by an account address and monotonically increasing creation_number, one per event type emitted to the given account. This API returns events corresponding to that that event type.

    Returns

    Array of events assotiated with the given account and creation number.

    Parameters

    • address: MaybeHexString

      Hex-encoded 32 byte Aptos account, with or without a 0x prefix, for which events are queried. This refers to the account that events were emitted to, not the account hosting the move module that emits that event type.

    • creationNumber: string | AnyNumber

      Creation number corresponding to the event type.

    • Optional query: PaginationArgs

    Returns Promise<Event[]>

  • This API uses the given account address, eventHandle, and fieldName to build a key that can globally identify an event types. It then uses this key to return events emitted to the given account matching that event type.

    Returns

    Array of events

    Parameters

    • address: MaybeHexString

      Hex-encoded 32 byte Aptos account, with or without a 0x prefix, for which events are queried. This refers to the account that events were emitted to, not the account hosting the move module that emits that event type.

    • eventHandleStruct: string

      String representation of an on-chain Move struct type. (e.g. 0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>)

    • fieldName: string

      The field name of the EventHandle in the struct

    • Optional query: PaginationArgs

      Optional query object

    Returns Promise<Event[]>

  • Queries the latest ledger information

    Returns

    Latest ledger information

    Example

    Example of returned data

    {
    chain_id: 15,
    epoch: 6,
    ledgerVersion: "2235883",
    ledger_timestamp:"1654580922321826"
    }

    Returns Promise<IndexResponse>

  • Gets a table item for a table identified by the handle and the key for the item. Key and value types need to be passed in to help with key serialization and value deserialization.

    Returns

    Table item value rendered in JSON

    Parameters

    • handle: string

      A pointer to where that table is stored

    • data: TableItemRequest

      Object, that describes table item

    • Optional query: {
          ledgerVersion?: AnyNumber;
      }

    Returns Promise<any>

  • Returns

    Transaction from mempool (pending) or on-chain (committed) transaction

    Parameters

    • txnHash: string

      Transaction hash should be hex-encoded bytes string with 0x prefix.

    Returns Promise<Types.Transaction>

  • Queries on-chain transactions. This function will not return pending transactions. For that, use getTransactionsByHash.

    Returns

    Array of on-chain transactions

    Parameters

    • Optional query: PaginationArgs

      Optional pagination object

    Returns Promise<Types.Transaction[]>

  • Rotate an account's auth key. After rotation, only the new private key can be used to sign txns for the account. WARNING: You must create a new instance of AptosAccount after using this function.

    Returns

    PendingTransaction

    Parameters

    • forAccount: AptosAccount

      Account of which the auth key will be rotated

    • toPrivateKeyBytes: Uint8Array

      New private key

    • Optional extraArgs: OptionalTransactionArgs

      Extra args for building the transaction payload.

    Returns Promise<PendingTransaction>

  • Converts a transaction request produced by generateTransaction into a properly signed transaction, which can then be submitted to the blockchain

    Returns

    A transaction, signed with sender account

    Parameters

    • accountFrom: AptosAccount

      AptosAccount of transaction sender

    • rawTransaction: RawTransaction

      A raw transaction generated by generateTransaction method

    Returns Promise<Uint8Array>

  • Generates and submits a transaction to the transaction simulation endpoint. For this we generate a transaction with a fake signature.

    Returns

    The BCS encoded signed transaction, which you should then provide

    Parameters

    • accountOrPubkey: Ed25519PublicKey | MultiEd25519PublicKey | AptosAccount

      The sender or sender's public key. When private key is available, AptosAccount instance can be used to send the transaction for simulation. If private key is not available, sender's public key can be used to send the transaction for simulation.

    • rawTransaction: RawTransaction

      The raw transaction to be simulated, likely created by calling the generateTransaction function.

    • Optional query: {
          estimateGasUnitPrice?: boolean;
          estimateMaxGasAmount?: boolean;
          estimatePrioritizedGasUnitPrice: boolean;
      }
      • Optional estimateGasUnitPrice?: boolean

        If set to true, the gas unit price in the transaction will be ignored and the estimated value will be used.

      • Optional estimateMaxGasAmount?: boolean

        If set to true, the max gas value in the transaction will be ignored and the maximum possible gas will be used.

      • estimatePrioritizedGasUnitPrice: boolean

        If set to true, the transaction will use a higher price than the original estimate.

    Returns Promise<Types.UserTransaction[]>

  • Submits the BCS serialization of a signed transaction to the simulation endpoint.

    Returns

    Simulation result in the form of UserTransaction.

    Parameters

    • bcsBody: Uint8Array

      The output of generateBCSSimulation.

    • Optional query: {
          estimateGasUnitPrice?: boolean;
          estimateMaxGasAmount?: boolean;
          estimatePrioritizedGasUnitPrice?: boolean;
      }
      • Optional estimateGasUnitPrice?: boolean
      • Optional estimateMaxGasAmount?: boolean
      • Optional estimatePrioritizedGasUnitPrice?: boolean

    Returns Promise<Types.UserTransaction[]>

  • Submits a signed transaction to the endpoint that takes BCS payload

    Returns

    Transaction that is accepted and submitted to mempool

    Parameters

    • signedTxn: Uint8Array

      A BCS transaction representation

    Returns Promise<PendingTransaction>

  • Submits a signed transaction to the transaction endpoint.

    Returns

    Transaction that is accepted and submitted to mempool

    Parameters

    • signedTxn: Uint8Array

      A transaction, signed by signTransaction method

    Returns Promise<PendingTransaction>

  • Defines if specified transaction is currently in pending state

    Returns

    true if transaction is in pending state and false otherwise

    Parameters

    • txnHash: string

      A hash of transaction

      To create a transaction hash:

      1. Create hash message bytes: "Aptos::Transaction" bytes + BCS bytes of Transaction.
      2. Apply hash algorithm SHA3-256 to the hash message bytes.
      3. Hex-encode the hash bytes with 0x prefix.

    Returns Promise<boolean>

  • This function works the same as waitForTransactionWithResult except it doesn't return the transaction in those cases, it returns nothing. For more information, see the documentation for waitForTransactionWithResult.

    Parameters

    • txnHash: string
    • Optional extraArgs: {
          checkSuccess?: boolean;
          timeoutSecs?: number;
      }
      • Optional checkSuccess?: boolean
      • Optional timeoutSecs?: number

    Returns Promise<void>

  • Wait for a transaction to move past pending state.

    There are 4 possible outcomes:

    1. Transaction is processed and successfully committed to the blockchain.
    2. Transaction is rejected for some reason, and is therefore not committed to the blockchain.
    3. Transaction is committed but execution failed, meaning no changes were written to the blockchain state.
    4. Transaction is not processed within the specified timeout.

    In case 1, this function resolves with the transaction response returned by the API.

    In case 2, the function will throw an ApiError, likely with an HTTP status code indicating some problem with the request (e.g. 400).

    In case 3, if checkSuccess is false (the default), this function returns the transaction response just like in case 1, in which the success field will be false. If checkSuccess is true, it will instead throw a FailedTransactionError.

    In case 4, this function throws a WaitForTransactionError.

    Returns

    See above.

    Example

    const rawTransaction = await this.generateRawTransaction(sender.address(), payload, extraArgs);
    const bcsTxn = AptosClient.generateBCSTransaction(sender, rawTransaction);
    const pendingTransaction = await this.submitSignedBCSTransaction(bcsTxn);
    const transasction = await this.aptosClient.waitForTransactionWithResult(pendingTransaction.hash);

    Parameters

    • txnHash: string

      The hash of a transaction previously submitted to the blockchain.

    • Optional extraArgs: {
          checkSuccess?: boolean;
          timeoutSecs?: number;
      }
      • Optional checkSuccess?: boolean

        See above. Defaults to false.

      • Optional timeoutSecs?: number

        Timeout in seconds. Defaults to 20 seconds.

    Returns Promise<Types.Transaction>

  • Note: Unless you have a specific reason for using this, it'll probably be simpler to use simulateTransaction.

    Generates a BCS transaction that can be submitted to the chain for simulation.

    Returns

    The BCS encoded signed transaction, which you should then pass into the submitBCSSimulation function.

    Parameters

    • accountFrom: AptosAccount

      The account that will be used to send the transaction for simulation.

    • rawTxn: RawTransaction

      The raw transaction to be simulated, likely created by calling the generateTransaction function.

    Returns Uint8Array

Generated using TypeDoc