Indexing Parent-Child Smart Contracts using Unmarshal Parser

In this blog we are going to explore how having multiple contracts nested within a main contract is useful in an application such as a DEX. These are referred to as either Factory-Router-Child or Parent-Router-Child contracts. We will also go on to see how a dApp developer can index events and functions associated with both Parent and Child contracts for a given application using the Unmarshal Parser.

Factory, Router, and Child Contracts

Certain dApps such as decentralized exchanges rely on three types of smart contracts — the Parent or Factory, Router, and Child contracts.

In a DEX, there are multiple trading pairs created, one each for every two given tokens. If a new trading pair is to be created, then there needs to be a mechanism in place to add liquidity to this pool.

The role of the Parent/Factory Contract is to create the different trading pair “markets’’ within the DEX, while also acting as a registry for all these LP pools. Factory contracts will ensure that all of the smart contracts that it produces adhere to certain arbitrary qualities. This is a useful setup for many reasons:-

  • One being that it allows the creation of multiple instances of the same contract.
  • You are able to track all of the contracts that a factory has deployed if you so choose.
  • It saves on gas as well, since you can deploy only the factory, and then use it to deploy other smart contracts.

Now each of these existing pools have a “Child/Pair” contract associated with them. But it can become difficult to directly interact with these pair contracts. So many major exchanges such as Uniswap and Pancakeswap introduced the concept of a “Router” contract, which acts as an intermediary between Parent and Child. Other advantages of using a Router contract are:

  • It enables the possibility of making complex trades. For example if you are looking to trade token A with token C, but there are only pairs available between A-B and B-C, without a direct pair of A-C, users can “Route” the tokens and trade across the 2 different pools.
  • Nesting contracts increases security by isolating the risk of vulnerabilities; when all contract variables are included within one smart contract, it’s easier to miss an error or weaknesses that could be exploited by a bad actor.
  • Multiple contracts enable you to break the main contract into smaller pieces with less complex logic.

How Unmarshal Parser Indexes the Child contract within the Parent

In the latest version of Unmarshal Parser (V2), a key feature addition is the introduction of indexing for a child contract within the Factory contract. Let’s take the example of Apeswap DEX for which we have built an analytics dashboard using Parser.

You can find the Factory, Router, and Child contracts for all supported chains of Apeswap here. Below are the contracts that we will use which are on the BNB Chain.

ApeFactory (Parent): 0x0841BD0B734E4F5853f0dD8d7Ea041c241fb0Da6

BNB Chain Zap Contract (Child): 0x7E060D0e0563fbD4CD2b3B845a992Eab31e47f8b

Step 1: After a user inputs the main Factory contract in Tab 1 and goes through the regular flow of Parser setup, they will now be able to input even the child contract associated with the Parent in Tab 5, as shown in screenshot below.

Step 2: The “Add Child Parser” button in the previous step will open a separate window where you can select the parent event and the input field responsible for generating child contracts. In case there are no events displayed, they will need to be selected in the Parent Event section.

Step 3: Here you will need to enter the sample child contract’s address. The parser will automatically load the contract ABI. If not, it can be entered manually.

Step 4: Next, the events can be selected from a drop-down. Choose all the events that you would like to track.

Step 5: Below is an optional tab where Token & Price plugins can be added to track exact token prices at time of transaction. This is useful in tracking accurate trading volumes. Stay tuned for a detailed blog regarding price/token plugins.

Step 6: In the last tab, you can select the block from which you want to start indexing (by default it will be the contract start block), name your Parser, and finally assign a Schema name.

If you are looking to index a popular DEX and its constituent parent and child contracts, visit http://unmarshal.io/parser to start now.

For any queries on using the Parser, reach out to us at support@unmarshal.io. We also have a dedicated developer hive on Discord. Just drop us a message in the “Parser Support” channel and we’ll get back to you.

About Unmarshal

Unmarshal is a Multi-chain Web 3.0 data network aiming to deliver granular, reliable & real-time data to dApps, DeFi protocols, NFTs, Metaverse and GameFi solutions. Unmarshal provides the easiest way to query Blockchain data from Ethereum, Polygon, BNB Chain, XDC Network, Avalanche, Fantom, Celo, Solana, Klaytn, Cronos, and Fuse Network. Unmarshal product suite consists of data indexers and transforming tools to power Web 3.0 applications on any chain while providing a latent view of transformed data.

Website|Telegram Chat|Telegram Ann|Twitter|Medium|Discord

--

--

Blockchain data indexer and api provider

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store