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:
-
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 })
-
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 })
-
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 thefilter
is emitted within a transaction. Theevent
object is automatically decoded based on the module's ABI if available.onEntryFunctionCall(handler(call, ctx), filter)
: Triggered when an entry function matching thefilter
is called within a transaction. Thecall
payload is automatically decoded.onTransaction(handler(tx, ctx), filter?)
: Triggered for every transaction processed by the processor (optionally filtered bysender
orincludeFailed
). Provides the rawUserTransactionResponse
.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: RequiresAptosModulesProcessor
and appropriate fetch config).
Context (ctx
)
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
(forAptosContext
). - Timestamp:
timestampInMicros
orgetTimestamp()
. - 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 additional data should be fetched for the transaction being processed. This optimizes data fetching. The configuration is defined by the MoveFetchConfig
interface:
interface MoveFetchConfig {
allEvents: boolean; // Fetch all events for the transaction
includeFailedTransaction?: boolean; // Include failed transactions
inputs: boolean; // Fetch transaction input arguments
resourceChanges: boolean; // Fetch resource changes
resourceConfig?: ResourceConfig; // Specific configuration for resource fetching
supportMultisigFunc?: boolean; // Support for multisig functions
}
For example, to fetch resource changes for transactions that emit a specific event:
processor.onEvent(..., { fetchConfig: { resourceChanges: 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
})
.onEvent(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
Updated 21 days ago