Search
Generic filters
Share on facebook
Share on twitter
Share on linkedin

The Graph Alternative for Smart Contract Indexing

The blockchain world is growing and evolving fast and we’ve seen countless projects appear in a short amount of time. Many of these crypto projects offer tangible solutions and use cases. The Graph seems to be one of such projects; however, only time will tell if it’s here to stay. While the Graph offers a powerful APIs and smart contracts indexing solution, there are also several The Graph alternative solutions. This article takes a closer look at The Graph alternatives.

For example, one The Graph alternative is the Web3 development platform Moralis (a.k.a. the Firebase for crypto), which provides developers with the ultimate Web3 SDK. Among many useful tools, Moralis has a special “Sync” feature available that enables you to sync and index smart contract events in minutes. Moreover, the on-chain data related to the smart contract in question then become available in your Moralis dashboard, which is essentially a neat database. 

Moving forward we’ll first make sure that we are all caught up to speed by covering the basics. As such, we will answer “what is the Graph?” and “what is smart contract indexing?” before taking on an example project. The latter will serve as a showcase of Moralis’ smart contract indexing feature. Thus, you’ll be able to see how quickly and easy it is to use the Graph alternative. Furthermore, the best thing about Moralis is that anyone who can code using JavaScript and has experience with React development has all it takes to create killer dApps (decentralized applications) in the shortest possible time. In addition, Moralis is all about cross-chain interoperability, which means it supports several leading programmable blockchains and thus enables you to deploy your dApps across multiple chains with ease. As such, it future-proofs your projects.

What is The Graph?

The Graph is an indexing protocol that enables developers to query reputable networks like Ethereum and IPFS and thus use the on-chain data for various use cases. It is a sort of data-sorting layer between the blockchain and a particular dApp. The Graph enables anyone to build and publish open-source APIs (subgraphs) and thus contribute to making useful data easily accessible for fellow developers. Furthermore, subgraphs can be merged or composed into a global graph of all the world’s public data or information. Moreover, by transforming, organizing, and sharing that data across applications, developers can query it with a few keystrokes using GraphQL programming language. Essentially, all on-chain data is stored and processed on open networks with verifiable integrity. The Graph then makes querying this data fast, reliable, and secure. 

If you are interested in learning more about the Graph, make sure to visit their official website. Moreover, you can also watch an experienced developer take on a simple example project in the video below (at 5:40) using the Graph.

What is Smart Contract Indexing?

If we want to talk about smart contract indexing, we need to make sure you know what smart contracts are. Well, smart contracts are computer programs or transaction protocols. They incorporate a specific set of conditions that when met trigger predefined actions. As such, the terms of smart contracts are automatically respected, hence the name “smart” contracts. Moreover, every time something meaningful happens within a smart contract or based on its code, it emits an event (smart contract events). By listening to those events, we can arrange the on-chain data by indexing it. So, smart contract indexing refers to the act of capturing blockchain data based on smart contract events and arranging that data into a structured database. 

The latter is possible thanks to the fact that in most cases smart contracts are “auditable by design”, which means that both the code and all transactions are publicly available. However, it turns out that indexing smart contracts is not all that simple. Sure, using a tool like the Graph enables you to do so but if you try it yourself, you’ll see that it is still rather tricky and quite time-consuming. This is why the Graph alternative you’ll learn about herein currently offers the most practical and fastest solution for smart contract indexing. It is a real game-changer and thus the way to go for projects where time is of the essence.  

The Graph Alternative in Action

In case you have plenty of time and want to approach smart contract indexing in a slightly more advanced manner (covering most of the backend infrastructure yourself), then make sure to explore the Graph further. However, if you are eager to cover this aspect in the simplest possible way and rather devote your time to creating the best user interface, then using the Graph alternative is the way. 

Herein, we’ll show you how to use the Graph alternative to index smart contracts by taking on an example project. As such, we will use a blockchain roulette that enables users to bet on red or black with no green (zero) option. We will use Moralis to index the relevant smart contract and thus make all the on-chain data neatly available in a Moralis database. From there, we can easily use the indexed data using Moralis’ cloud function and its ultimate Web3 API by copying and pasting short snippets of code provided by Moralis’ documentation

The Graph Alternative in Action – an Example Project

In the screenshot below, you can see our example casino dApp (it’s live on Rinkeby) that will be of our interest during this example task. Moving forward we will index its smart contract using Moralis. However, let’s first do a quick overview of this dApp to provide you with clear context.

As mentioned in the previous section, the dApp in question enables users to enter the amount in gwei that they want to bet either on red or black (there is no zero). After entering the amount and selecting the color, users need to click the “Spin” button, which executes a transaction. Moreover, our casino dApp also displays top winners, top losers, biggest bets placed, and the total number of wins and losses for each color. Of course, the thing that interests us for the sake of our task is the smart contract behind this dApp (flipcontract.sol), which we made available at GitHub with the rest of the code. Furthermore, the smart contract controls several events; however, the one for placing the bet (the amount bet, color to bet on, and the outcome) is the one we will focus on.  

Smart Contract Indexing with Moralis

In order to use Moralis for smart contract indexing, you need to first set it up properly. For that purpose, we’ve prepared the following section covering all the steps you need to complete to gain access to the Graph alternative. 

Initial Setup – Create a Moralis Server

Whether you want to use Moralis for smart contract indexing, use it as one of Infura Alternatives (Moralis’ Speedy Nodes), or use its Web3 SDK to develop dApps, you need to start by creating a Moralis server. Here are the steps you need to follow:

  1. Create Your Free Moralis Account If you don’t have your free Moralis account yet, click the link, enter your email address, create your password, and don’t forget to click the confirmation link that will be sent to your email. However, if you already have a Moralis account, log in to your admin area.
  1. Create a Moralis Server – Once inside your Moralis admin area, go to the “Servers” tab and click the “+ Create a new Server” button in the top-right corner (see the image below). Then select the network type that best suits your needs from the drop-down menu. Since the example “Casino” dApp is live on Rinkeby, select the “Testnet Server” option (connect to Ethereum testnets). 

Next, enter the server name (this can be anything you want), select your region, select network type, chain(s), and spin up your server by clicking on the “Add Instance” button:

  1. Access Server Details(You can skip steps 3 and 4 for now; you will need this when you decide to create a dApp using smart contract indexing.) Once your server is live, you get to access its details by clicking on the “View Details” button: 


All the information you need will be displayed in a new window. There, you can simply copy them (one by one) by clicking on the copy icons:

  1. Initialize Moralis (Again, you need to complete this step to create your dApps with Moralis.) – Populate your coding files with the above-obtained Moralis server details (application ID and server URL):

Using the Graph Alternative for Smart Contract Indexing 

Once you’ve completed the initial Moralis setup, you’re all set to use the Graph alternative for smart contract indexing. Start by clicking the “View Details” button next to the server you’ve created above. In the pop-up window that appears, click the “Sync” tab:

Once on the “Sync” tab, you’ll have a button to add new sync. By clicking the “Add New Sync” button you get two options:

For the purpose of indexing smart contracts, you need to select the top option (“Sync And Watch Contract Events”). After you select the proper choice, you will see this window:

As you can see in the screenshot above, you need to enter some details to set up smart contract indexing. To make things as easy as possible for you, we provided you with all the details below. You can simply copy and paste these details:

  • ChainId: Select “Eth (Rinkeby)” (if you’ve followed the above instructions properly, it should be your only option).
  • Description: This can be anything you want; however, feel free to follow our lead and use “Casino Spins”.
  • Sync Historical Data: Select this option in order to collect past events. 
  • Topic: Here we need to enter the “bet” event (only types of data required): 
bet(address,uint256,bool,uint8);
  • ABI for the “bet” event:
{
“anonymous”: false,
“Inputs”: [
	{
		“Indexed”: true,
		“internalType”: “address”,
		“name”: “user”,
		“type”: “address”,
	},
	{
		“Indexed”: true,
		“internalType”: “uint256”,
		“name”: “bet”,
		“type”: “uint256”,
	},
	{
		“Indexed”: true,
		“internalType”: “bool”,
		“name”: “win”,
		“type”: “bool”,
	},	
	{
		“Indexed”: false,
		“internalType”: “uint8”,
		“name”: “side”,
		“type”: “uint8”,
	},	
	]
	“name”: “bet”,
	“type”: “event” 
}
  • Smart contract’s address: 0x387ECDc3E80e501B5b0B80EA92CA69Dd2Fdc3415
  • Table Name: This can again be anything you like (this is the class name that will be used within your Moralis dashboard). Again, feel free to follow our lead and use “Casino Spins”.

Moralis’ Dashboard

Once you’ve entered all of the details above, create your sync event by clicking the “Confirm” button. If you’ve done everything properly, you should be looking at the following screen:

Now, you can close that window, so that we can take a look at the dashboard. This is where all past smart contract events should already be indexed. Of course, this is also where all future events will be indexed. To access your database, click the “Dashboard” button for your server:

Looking at the sidebar of your Moralis dashboard, you should see the “Spin” class. This was automatically created with our “sync and watch” event. It already indexed all the details related to the “bet” event:

The above columns contain users’ addresses, bet amount, 0 or 1 (red or black), or if the bet won. With all that data at your disposal, you can start querying it and use it in your dApps. If that’s something you’d like to learn more about, make sure to check out the video below starting at 30:12. Moreover, in case you’re more of a video person, feel free to watch the entire video:

The Graph Alternative for Smart Contract Indexing – Summary

You now know what the Graph is and how to use it for smart contract indexing. However, you’ve also learned that the Graph isn’t the simplest nor the fastest way of indexing smart contracts. For one it requires running your own servers, is quite heavy on nodes, and takes quite some time to code it properly using a new programming language. Luckily, there’s the Graph alternative that enables developers who are JavaScript proficient to do smart contract indexing in minutes. As such, it enables you to save a lot of time, and how to create the most appealing frontend. Moreover, the Graph alternative is so much more than just a smart contract indexing tool, it is the ultimate Web3 development platform. 

If you’d like to take on other interesting example projects and thus upgrade your blockchain development skills, make sure to visit the Moralis’ blog and the Moralis YouTube channel. Some of our latest topics show you how to connect to Arbitrum nodes, how to build Ethereum dApps in Angular, how to build an NFT trading interface, how to use an OpenSea API alternative, how do flash loans work, how to use MetaMask for developers, how to create a DAO fast, how to do gasless transactions on Ethereum, how to build a cross-chain bridge, how to build a prediction market dApp, and much more. Furthermore, if you are interested in going full-time crypto as soon as possible, we recommend taking a bit more professional approach by enrolling in some of the high-quality courses available at Moralis Academy

January 1, 2022
Moralis Blog
Moralis Blog
Become a Moralis Mage
Get the latest information about Moralis Web3 in your email
Search
Generic filters
Related Articles
Become a Web3 Expert
Subscribe to our newsletter and get our E-book Web3 Speed run for FREE