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:
- Get the returned data of the contract in a transaction
- Monitor various operating states of the contract
Event in Aptos Architecture Design
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:
- Proxy the transaction from REST service to validator’s mempool component for caching
- 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
- REST service gets the event data via Storage component.
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.
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
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"
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'
Query total number of registered domains
select
count(1)
from
aptos.events
where
type = '0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c::domains::RegisterNameEventV1'
and account_address = '0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c'
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
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 Medium, Twitter, 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.