How to Efficiently Get Aptos Event Data - Full Walkthrough

How to Efficiently Get Aptos Event Data - Full Walkthrough

lxcong

lxcong

Tech

What is Event?

Most blockchain networks as of now have “event”.

It plays an important role in the blockchain interaction. In traditional Web2 development, most of the interactions between clients and servers can read-and-write synchronously, while the interaction with the blockchain networks is like an asynchronous read-write separation system (similar to the currently popular CQRS model). Results returned from many contracts are generated after the mining of blocks, therefore, submitting events in contracts is an essential way to interact with the blockchain users.

Event has two functions:

  1. Get the returned data of the contract in a transaction
  2. Monitor various operating states of the contract

Event in Aptos Architecture Design

image.png

In Aptos architecture design, you can use fullnode’s REST service to interact with the chain through Node API. For a single transaction, the process will be:

  1. Proxy the transaction from REST service to validator’s mempool component for caching
  2. Through the efforts of several components, the block reaches consensus. Call Storage component via Execution to permanently store the data off-chain, including the event data representing the running result
  3. REST service gets the event data via Storage component.

image (1).png

Query Event via Node API

Chainbase has now provided a stable and efficient fullnode cluster. To get an Aptos Mainnet Node API endpoint, please check out the documentation.

image (2).png

After registration, you will have your own Node API: https://aptos-mainnet.s.chainbase.online/{your-api-key}/v1

(Note: don’t forget to replace {your-api-key} )

Take querying Aptos name v1’s (provided by the official team) domain mapping event as an example, Through on-chain data analysis, the project’s account address is:

0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c.

Let’s look at the event definition of ABI (Aptos is developer-friendly, that ABI can be queried on-chain), through Get account modules. The request is:

https://aptos-mainnet.s.chainbase.online/{your-api-key}/v1/accounts/0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c/modules

image (3).png

Extract the key information defined by the event, you can use Get events by event handle to construct a request for this event:

https://aptos-mainnet.s.chainbase.online/{your-api-key}/v1/accounts/0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c/events/0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c::domains::SetNameAddressEventsV1/set_name_events

  • address = "0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c"
  • event_handle = "0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c::domains::SetNameAddressEventsV1"
  • field_name = "set_name_events"

image (4).png

In this way, you are able to continuously polling the event data through Node API. The new_address will be the mapping address.

The Limitation of Node API

From the example above, you can constantly get the latest event. But if you want to query the latest mapping address for a specific domain, or analyze the number of registered domains per day, etc. Node API will not satisfy your needs.

Essentially, Aptos Node API is for basic interactions with the chain, the storage and other components behind it are not designed for multi-dimensional data query, index, and analytics.

How Chainbase Data Cloud Solve the Limitation

Chainbase Data Cloud enables users to access any Aptos data efficiently and easily, as well as generate customized APIs via SQL, through structuring, processing, indexing, and importing data into a cloud-based data warehouse, which is more suitable for on-chain data analytic purposes.

Again, take Aptos name v1 as an example:

Query address, expiration time when domain = 000.apt

select
    JSONExtract(data, 'domain_name', 'String') as domain,
    JSONExtract(JSONExtract(data, 'new_address', 'String'), 'vec', 'Array(String)') as registered_address,
    toDateTime(JSONExtract(data, 'expiration_time_secs', 'String')) as expiration,
    transaction_version
from
    aptos.events
where
    type = '0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c::domains::SetNameAddressEventV1'
    and account_address = '0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c'
    and domain = '000'

image (5).png

Query total number of registered domains

select
    count(1)
from
    aptos.events
where
    type = '0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c::domains::RegisterNameEventV1'
    and account_address = '0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c'

image (6).png

Query Top 10 holder accounts

select
    JSONExtract(JSONExtract(data, 'new_address', 'String'), 'vec', 'String') as registered_address,
    count(*) as holder_total
from
    aptos.events
where
    type = '0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c::domains::SetNameAddressEventV1'
    and account_address = '0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c'
    and registered_address != "[]"
group by registered_address
order by holder_total desc
limit
    10

image (7).png

As you can see from the example, Chainbase Data Cloud is very fast and efficient in getting the Aptos event data. More examples and respective codes can be found in our developer documentation.

Have fun at Aptos!

About Chainbase

Chainbase is a leading Web3 blockchain interaction layer infrastructure. By providing cloud-based API services, it helps developers quickly access and utilize blockchain networks and easily build Web3 applications.

Chainbase makes blockchain interaction and data query/index on chains simple and easy to operate. Anyone can use, build and publish open APIs, which allows developers to focus on application-level innovation instead of solving the back-end hassles.

Chainbase currently supports Ethereum, Polygon, BSC, Fantom, Avalanche, Arbitrum, Aptos and other chains. This allows projects of all sizes to quickly reduce development time and costs, no matter which chains they are building on!

Website | Blog | Twitter | Discord | Link3

Want to learn more about Chainbase?

Stay connected with Chainbase on MediumTwitter, and Discord. If you are a back-end dev and would like to experience the product, please submit a request as well as read the dev documentation on our website.

More articles

EVM VS NonEVM: How do NonEVM Ecosystems Attract Developers/Builders?

EVM VS NonEVM: How do NonEVM Ecosystems Attract Developers/Builders?

In this Blog, we explore the pros and cons of building on EVM and non-EVM, as well as potential growth strategies for non-EVM chains! Thanks to those who spoke freely in the Space, your ideas will know us better to understand and choose the right chain.

Learn Morearrow-blue
Unlock the role of Developer Relations/Advocate in Web3

Unlock the role of Developer Relations/Advocate in Web3

If you’re curious about the role of developer relations or developer advocate in web3, you should def check out this space & the summary transcription!

Learn Morearrow-blue
Chainbase x Adot: Create Unlimited Possibilities for Web3 Search Engine

Chainbase x Adot: Create Unlimited Possibilities for Web3 Search Engine

Thrilled to announce the partnership with Adot. Possibilities are open for this collab! As the data infra provider, we will help more promising companies like Adot build a decentralized future.

Learn Morearrow-blue