Guidelines for Indexing SUI Blockchain Data

Guidelines for Indexing SUI Blockchain Data

masterdai

masterdai

Devrel

Introduction

Indexing is an essential tool in the blockchain ecosystem, streamlining the organization and retrieval of data. It bolsters efficiency, enhances user experience, simplifies data analysis, and ensures scalability. By facilitating rapid data access, it supports real-time applications and optimizes interactions, a feature that is particularly crucial for platforms like DeFi. Indexing also aids in various tasks, from detecting fraudulent activity to understanding user behavior. As the volume of blockchain data grows, indexing ensures that application performance remains high.

As the only data cloud indexing provider in the Sui blockchain ecosystem, Chainbase plays a critical role in enabling efficient data retrieval. In the upcoming guide, I will illustrate how developers can effectively access on-chain data using several SQL examples. This will offer a hands-on understanding of how to utilize Chainbase's indexing capabilities to improve data interaction and analysis within the Sui blockchain.

About SUI

Sui is a groundbreaking smart contract platform that revolutionizes the traditional blockchain model by centering storage around objects, which are programmable entities managed by smart contracts. Unlike the conventional account-based storage model where each account contains a key-value store, Sui's approach focuses on objects as the fundamental unit of data storage.

These objects are digital entities that store data directly on the blockchain. Developers can define, create, and manage these objects, which represent user-level assets. Each object possesses distinct attributes, including ownership, and these attributes can be updated based on the governing logic of the smart contract that created the object.

This object-centric data model of Sui allows digital assets and their attributes to exist on-chain and independently of smart contracts. This innovative approach offers a new level of flexibility and functionality in the blockchain space, enabling more complex and dynamic interactions with digital assets.

Prerequisites

  1. A free account atĀ ChainbaseĀ with an API key.
  2. An IDE. Our examples are shown in JavaScript, you can useĀ VS CodeĀ as your IDE for example.

Register and Get API Key

To begin with, you'll need to register on Chainbase and obtain an API key. This key will be used to authenticate your requests to the Chainbase API.

SQL Example

Log in to your Chainbase dashboard.

Execute the following SQL query to retrieve the required data:

Query 1: Finding the Most Active Address

Want to identify the most transaction-heavy address in your blockchain? The following SQL query can help:

SELECT
    sender,
    count() as total
FROM
    sui.transactions
GROUP BY
    sender
ORDER BY
    total DESC

Query 2: Most Utilized Module and Event Type

To identify the most commonly used module and event type, use the query below:

SELECT
  module,
  event_type,
  COUNT(*) AS total
FROM
  sui.events
GROUP BY
  event_type,
  module
ORDER BY
  total DESC;

Query 3: Filtered Data Extraction

Looking for specific transaction content? Filter your search using this SQL query:

select
    *
from
    sui.transactions
where
    transaction_content like "%fuddies%%orderbook%"
order by
    id DESC
limit
    100

Query 4: Fetching Specific Event Data

For retrieving specific event data from the Sui blockchain, execute the following query:

SELECT
	id,
	module,
	sender,
	transaction_digest,
	event_type
FROM
	sui.events
WHERE
	event_type LIKE "%0xac176715abe5bcdaae627c5048958bbe320a8474f524674f3278e31af3c8b86b%"
ORDER BY
	id DESC
LIMIT
	100

Generating the API

Once you have the SQL query to retrieve the desired data, the next step involves generating an API request to fetch the real-time Sui blockchain data. Follow the steps below:

chainbases-api.png

Step 1: Install Node.js and the Axios library if you haven't already.

Step 2: Use the following code snippet to make the API call:

const axios = require('axios');

axios.post('https://api.chainbase.online/v1/dw/query', {"query":"SELECT\n    id,\n    module,\n    sender,\n    transaction_digest,\n    event_type\nFROM\n    sui.events\nWHERE\n    event_type LIKE \"%0xac176715abe5bcdaae627c5048958bbe320a8474f524674f3278e31af3c8b86b%\"\norder by\n    id desc\nlimit\n    100"}, {
  headers: {
    'x-api-key': '[api-key]'
  }
})
.then(response => {
        const data = response.data.data;
        console.log(data);
    })
    .catch(error => {
        console.error(error);
    });

This code snippet utilizes the Axios library to send a POST request to the Chainbase API endpoint (https://api.chainbase.online/v1/dw/query). The SQL query is included in the request payload, and the 'x-api-key' header is used for authentication.

Retrieving and Printing Sui Blockchain Data

After making the API call, you will receive a response containing the real-time Sui onchain data. Follow the steps below to extract and print the data:

Access the retrieved data from the response using response.data.data.

Display the data by printing it on the console log.

Run node 'filename'.js

{
event_type: '0xbc3df36be17f27ac98e3c839b2589db8475fa07b20657b08e8891e3aaf5ee5f9::mint_event::MintEvent<0xac176715abe5bcdaae627c5048958bbe320a8474f524674f3278e31af3c8b86b::fuddies::Fuddies>',
id: '306968',
module: 'fuddies',
sender: '0x65afcd7679a1570f11b3865d8c0d13e070b4cf7587af11c3eacd98e97b6252a3',
transaction_digest: '6GMShTdQyeLzieJBLyVjrJn7nZTeFKEEnhRDTKZd6dqX'
},
{
event_type: '0xbc3df36be17f27ac98e3c839b2589db8475fa07b20657b08e8891e3aaf5ee5f9::mint_event::MintEvent<0xac176715abe5bcdaae627c5048958bbe320a8474f524674f3278e31af3c8b86b::fuddies::Fuddies>',
id: '304865',
module: 'fuddies',
sender: '0x65afcd7679a1570f11b3865d8c0d13e070b4cf7587af11c3eacd98e97b6252a3',
transaction_digest: 'FDPsGKnux8gCDcPWbKqkLbg28bnu6A16NUdsQN9LQb5x'
},
{
event_type: '0xbc3df36be17f27ac98e3c839b2589db8475fa07b20657b08e8891e3aaf5ee5f9::mint_event::MintEvent<0xac176715abe5bcdaae627c5048958bbe320a8474f524674f3278e31af3c8b86b::fuddies::Fuddies>',
id: '304864',
module: 'fuddies',
sender: '0x65afcd7679a1570f11b3865d8c0d13e070b4cf7587af11c3eacd98e97b6252a3',
transaction_digest: 'FDPsGKnux8gCDcPWbKqkLbg28bnu6A16NUdsQN9LQb5x'
},
{
event_type: '0xbc3df36be17f27ac98e3c839b2589db8475fa07b20657b08e8891e3aaf5ee5f9::mint_event::MintEvent<0xac176715abe5bcdaae627c5048958bbe320a8474f524674f3278e31af3c8b86b::fuddies::Fuddies>',
id: '304863',
module: 'fuddies',
sender: '0x65afcd7679a1570f11b3865d8c0d13e070b4cf7587af11c3eacd98e97b6252a3',
transaction_digest: 'FDPsGKnux8gCDcPWbKqkLbg28bnu6A16NUdsQN9LQb5x'
},
...

Experience Sui's Dashboard with a Front-End Demo

Take your understanding of Sui to the next level by trying out the front-end demo of Sui's dashboard. The demo, available on GitHub, offers hands-on experience with the Sui interface, helping you to familiarize yourself with its features and capabilities. Visit the demo here. chainbase-sui-dashboard.11.png

Conclusion

In the fast-paced world of blockchain technology, data management can seem daunting. However, with a tool like Chainbase at your disposal, you can easily navigate through the sea of data that blockchain platforms like Sui generate. From generating insightful SQL queries to making API calls, Chainbase empowers developers to make the most of blockchain data.

Frequently Asked Questions (FAQs)

1. What is Chainbase?

Chainbase is a leading data cloud indexing provider in the Sui blockchain ecosystem, enhancing data retrieval processes for developers.

2. What makes Sui different from other blockchain platforms?

Sui uses an innovative object-oriented storage model, contrasting the traditional account-based models of other blockchain systems.

3. What do I need to start using Chainbase?

You will need a free account at Chainbase and an API key, along with an Integrated Development Environment (IDE) like VS Code.

4. How can I extract specific data from the Sui blockchain?

You can utilize SQL queries to extract specific data. We also allows you to generate API requests for real-time data retrieval.

5. How do I authenticate my requests to the Chainbase API?

You will use an API key acquired from Chainbase to authenticate all your requests.

About Chainbase

Chainbase is an all-in-one data infrastructure for Web3 that allows you to index, transform, and use on-chain data at scale. By leveraging enriched on-chain data and streaming computing technologies across one data infrastructure, Chainbase automates the indexing and querying of blockchain data, enabling developers to accomplish more with less effort.

Want to learn more about Chainbase?

Visit our websiteĀ chainbase.com Sign up for aĀ free account, and Check out ourĀ documentation.

Websiteļ½œBlogļ½œTwitterļ½œDiscordļ½œLink3