Aptos

Aptos is a Layer 1 blockchain utilizing the Move language and its resource-based model.

Core Concepts

Processors

Aptos integration utilizes several processor types tailored for different data sources:

  1. AptosModulesProcessor: Binds to a specific Aptos module (smart contract) address. Use this to process events, function calls, and resource changes originating from or related to that module.

    • Typical Use Case: Monitoring a specific dApp's activity.
    • Binding: AptosModulesProcessor.bind({ address: '0x...', network: AptosNetwork.MAINNET, startVersion: 123456n })
  2. AptosGlobalProcessor: Processes transactions across the entire Aptos network, optionally filtered by sender or success status. Useful for chain-wide analytics or monitoring specific user activities regardless of the contract interacted with.

    • Typical Use Case: Tracking gas usage patterns, monitoring whale wallets.
    • Binding: AptosGlobalProcessor.bind({ network: AptosNetwork.MAINNET, startVersion: 123456n })
  3. AptosResourcesProcessor: Periodically fetches and processes Aptos resources associated with one or more accounts. Ideal for tracking account state changes over time or by block version.

    • Typical Use Case: Monitoring account balances, tracking NFT ownership changes stored in resources.
    • Binding: AptosResourcesProcessor.bind({ address: '0x...', network: AptosNetwork.MAINNET, startVersion: 123456n })

Handlers

Processors define handlers to react to specific blockchain activities:

  • Transaction-Based (for AptosModulesProcessor, AptosGlobalProcessor):

    • onMoveEvent(handler(event, ctx), filter): Triggered when a specific Move event matching the filter is emitted within a transaction. The event object is automatically decoded based on the module's ABI if available.
    • onEntryFunctionCall(handler(call, ctx), filter): Triggered when an entry function matching the filter is called within a transaction. The call payload is automatically decoded.
    • onTransaction(handler(tx, ctx), filter?): Triggered for every transaction processed by the processor (optionally filtered by sender or includeFailed). Provides the raw UserTransactionResponse.
    • onTimeInterval(handler(txns, ctx), intervalMinutes?, backfillIntervalMinutes?): Processes transactions in time-based batches.
    • onVersionInterval(handler(txns, ctx), interval?, backfillInterval?): Processes transactions in version-based batches.
  • Resource-Based (for AptosResourcesProcessor):

    • onTimeInterval(handler(resources, ctx), intervalMinutes?, backfillIntervalMinutes?, type?, fetchConfig?): Periodically fetches resources based on time.
    • onVersionInterval(handler(resources, ctx), interval?, backfillInterval?, type?, fetchConfig?): Periodically fetches resources based on block versions.
    • onResourceChange(handler(changes, ctx), typeDescriptor): Processes detailed changes (write, delete, create) to specific resource types within transactions included by the bound module (Note: Requires AptosModulesProcessor and appropriate fetch config).

Context (ctx)

Each handler receives a Context object (AptosContext for transaction-based handlers, AptosResourcesContext for resource-based handlers) providing:

  • Chain-specific information: network, version.
  • Transaction details: transaction (for AptosContext).
  • Timestamp: timestampInMicros or getTimestamp().
  • Helper methods: client.getAccountResource(...), coder.decodeEvent(...), etc.
  • Standard SDK outputs: ctx.meter.Counter('...'), ctx.eventLogger.emit('...'), ctx.exporter.aptos_Account(...).

Fetch Configuration

Handlers can optionally include a fetchConfig parameter to specify what data (events, resources changes, function arguments) should be retrieved for the transaction being processed. This optimizes data fetching.

processor.onMoveEvent(..., { resources: true }); // Fetch resource changes for transactions with this event

Getting Started Example

import { AptosModulesProcessor, AptosContext, AptosNetwork } from "@sentio/sdk/aptos";
import { pancakeswap } from './types/aptos/0xc7efb4076dbe143cbcd98cfaaa929ecfc8f299203dfff63b95ccb6bfe19850fa.js'; // Generated types

AptosModulesProcessor.bind({
  address: pancakeswap.DEFAULT_OPTIONS.address,
  network: AptosNetwork.MAINNET,
  startVersion: 1000000n // Start processing from version 1,000,000
})
.onMoveEvent(async (event: pancakeswap.SwapEvent, ctx: AptosContext) => {
  ctx.meter.Counter("swap_volume").add(event.data_decoded.amount_x_in + event.data_decoded.amount_y_in);
  ctx.eventLogger.emit("Swap", {
    distinctId: ctx.transaction.sender,
    pair: event.type_arguments[0], // Assuming type argument represents the pair
    amountXIn: event.data_decoded.amount_x_in,
    amountYIn: event.data_decoded.amount_y_in,
    amountXOut: event.data_decoded.amount_x_out,
    amountYOut: event.data_decoded.amount_y_out,
  });
}, { type: `${pancakeswap.DEFAULT_OPTIONS.address}::swap::SwapEvent` }) // Filter for SwapEvent
.onEntryFunctionCall(async (call: pancakeswap.Add_liquidityPayload, ctx: AptosContext) => {
  if (!ctx.transaction.success) return; // Skip failed transactions
  ctx.meter.Counter("liquidity_adds").add(1);
  ctx.eventLogger.emit("AddLiquidity", {
    distinctId: ctx.transaction.sender,
    amountX: call.arguments_decoded[0],
    amountY: call.arguments_decoded[1],
  });
}, { function: `${pancakeswap.DEFAULT_OPTIONS.address}::swap::add_liquidity` }); // Filter for add_liquidity function

This documentation provides a starting point for understanding the Aptos module within the Sentio SDK. Refer to the specific source files (aptos-processor.ts, context.ts, etc.) for detailed implementation and API references.