Index and Query UniswapV3 Liquidity Pool Data with Traceye
Tags: subgraphs
Published: 2024-03-22 09:32:28

<p>Whether you are a DeFi developer or a liquidity provider, you will need access to Uniswap v3 liquidity pool data, such as global, pool, and position data, to make well-informed decisions. While there can be several ways to fetch Uniswap data, this article explains the most simplified way of indexing Uniswap data using Traceye—an innovative, low-code tool for creating subgraphs for Web3 data indexing.&nbsp;</p>
<h2 class="wp-block-heading" id="h-demystifying-uniswapv3-and-its-liquidity-pool"><strong>Demystifying UniswapV3 and its liquidity pool</strong></h2>
<p>Uniswap v3 is the latest version of non-custodial AMM (automated market maker) implemented on the Ethereum Virtual Machine. Compared to previous iterations of Uniswap protocol, Uniswap V3 offers increased capital efficiency, improved accuracy and accessibility of oracles, more flexibility in fee structure, and fine-tuned control for all liquidity providers. Anyone- individual users, high-frequency trade firms, or investment banks- can become an liquidity provider for Uniswap v3 pool, enabling the protocol to accommodate instant buying, selling, and exchange for the traders.&nbsp;</p>
<p>Uniswap v3 has already introduced a bunch of unique features and it continues to make add-ons over time.&nbsp;</p>
<p><strong>Let’s highlight some of its significant features:</strong></p>
<li><strong>Concentrated liquidity:&nbsp;</strong></li>
<p>Liquidity providers in Uniswap V3 have the flexibility to concentrate their liquidity through “bounding” within an arbitrary price range. This helps Uniswap improve the pool’s overall capital efficiency while allowing liquidity providers to get approximations for their desired reserve curve and maintain aggregation with the rest of the pool.&nbsp;</p>
<li><strong>Flexible fee model:&nbsp;</strong></li>
<p>Uniswap v3 doesn't lock the swap fee at 0.30%. Instead, the fee tier on each pool is based on initialization. The fee tiers supported at initial stages are&nbsp; 0.05%, 0.30%, and 1%.&nbsp;</p>
<li><strong>Protocol fee governance:&nbsp;</strong></li>
<p>UNI governance has even more flexibility for setting up the fraction of the swap fee that the Uniswap protocol collects as a whole.</p>
<figure class="wp-block-image size-large"><img src="https://www.zeeve.io/wp-content/uploads/2024/03/Launch-your-custom-Subgraph-in-minutes-1024x213.png" alt="Indexing uniswap data" class="wp-image-62497"/></figure>
<li><strong>Improved price fee oracles:&nbsp;</strong></li>
<p>Uniswap v3 offers powerful price feed oracles that allows users to real-time price accumulator values, which eliminates the need of checkpointing the accumulator value during the beginning and end of the period for which a Time-Weighted Average Price (TWAP) is measured.&nbsp;</p>
<p><strong>5. Liquidity oracle</strong>:&nbsp;</p>
<p>Liquidity oracles are integrated with minting contracts, introducing average, time-weighted liquidity to enable users to get comprehensive data such as trading pairs and liquidity-related stats.</p>
<h2 class="wp-block-heading" id="h-now-let-s-brief-traceye"><strong>Now, let’s brief Traceye</strong></h2>
<p>Traceye is a web3 data indexing solution designed to support quick deployment and end-to-end management of custom subgraphs for a comprehensive range of blockchain networks and dApps.&nbsp;</p>
<p>With Traceye, web3 projects and enterprises get the flexibility to deploy shared data indexes and dedicated indexers– depending on their requirements. All your subgraphs will be deployed and managed on a reliable enterprise-grade infrastructure to ensure their performance and uptime.&nbsp;</p>
<h2 class="wp-block-heading" id="h-how-to-index-and-query-uniswapv3-liquidity-pool-data-with-traceye-nbsp"><strong>How to index and query UniswapV3 Liquidity Pool Data with Traceye?&nbsp;</strong></h2>
<p>With Traceye, you have the following two different options to index Uniswap data:</p>
<ol><!-- wp:list-item -->
<li>Launch your own UniswapV3 Subgraph.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Fetch data through ready-to-use community subgraphs.</li>
<!-- /wp:list-item --></ol>
<p><strong>Option:1 Launching your own Subgraph.</strong></p>
<p>If you decide to go with the first option, you need to create a Subgraph and then deploy it to Traceye’s hosting infrastructure. For this, you need to write a subgraph manifest, get the necessary ABI files from Etherscan, and a GraphQL schema.&nbsp;</p>
<p>Here's an example of basic Subgraph manifest for indexing data from Uniswap V3 pools:</p>
<figure class="wp-block-table"><table><tbody><tr><td><strong>specVersion</strong>: 0.0.5<br><strong>schema</strong>:<br>&nbsp; <strong>file</strong>: ./schema.graphql<br><br><strong>dataSources</strong>:<br>&nbsp; - <strong>kind</strong>: ethereum/contract<br>&nbsp; &nbsp; <strong>name</strong>: UniswapV3Pool<br>&nbsp; &nbsp; <strong>network</strong>: mainnet<br>&nbsp; &nbsp; <strong>source</strong>:<br>&nbsp; &nbsp; &nbsp; <strong>address</strong>:0x1F98431c8aD98523631AE4a59f267346ea31F984<br>&nbsp; &nbsp; &nbsp; <strong>abi</strong>: UniswapV3Pool&nbsp; # Update with the correct ABI <strong>for</strong> Uniswap V3 pool contract<br>&nbsp; &nbsp; <strong>mapping</strong>:<br>&nbsp; &nbsp; &nbsp; <strong>kind</strong>: ethereum/events<br>&nbsp; &nbsp; &nbsp; <strong>apiVersion</strong>: 0.0.4<br>&nbsp; &nbsp; &nbsp; <strong>language</strong>: wasm/assemblyscript<br>&nbsp; &nbsp; &nbsp; <strong>entities</strong>:<br>&nbsp; &nbsp; &nbsp; &nbsp; - PoolData<br>&nbsp; &nbsp; &nbsp; &nbsp; - TickData<br>&nbsp; &nbsp; &nbsp; <strong>abis</strong>:<br>&nbsp; &nbsp; &nbsp; &nbsp; - <strong>name</strong>: UniswapV3Pool<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>file</strong>: ./abis/UniswapV3Pool.json&nbsp; # Path to the ABI file <strong>for</strong> Uniswap V3 pool contract<br>&nbsp; &nbsp; &nbsp; <strong>eventHandlers</strong>:<br>&nbsp; &nbsp; &nbsp; &nbsp; - <strong>event</strong>: Swap(uint256,uint256,int256,int256,address)&nbsp; # Event emitted <strong>when</strong> a swap occurs <strong>in</strong> the pool<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>handler</strong>: handleSwap<br>&nbsp; &nbsp; &nbsp; &nbsp; - <strong>event</strong>: Sync(int128,int256,uint256)&nbsp; # Event emitted <strong>when</strong> liquidity is added <strong>or</strong> removed <strong>from</strong> the pool<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>handler</strong>: handleSync<br>&nbsp; &nbsp; &nbsp; <strong>file</strong>: ./src/mappings/uniswap-v3-pool-mapping.ts</td></tr></tbody></table></figure>
<p>And, here’s the example of GraphQL Schema:</p>
<figure class="wp-block-table"><table><tbody><tr><td>type PoolData @entity {<br>&nbsp; <strong>id</strong>: ID!<br>&nbsp; <strong>poolAddress</strong>: String!<br>&nbsp; <strong>token0</strong>: String!<br>&nbsp; <strong>token1</strong>: String!<br>&nbsp; <strong>liquidity</strong>: BigInt!<br>&nbsp; <strong>fee</strong>: BigInt!<br>&nbsp; <strong>sqrtPrice</strong>: BigInt!<br>&nbsp; <strong>tick</strong>: BigInt!<br>&nbsp; <strong>timestamp</strong>: BigInt!<br>}<br><br>type TickData @entity {<br>&nbsp; <strong>id</strong>: ID!<br>&nbsp; <strong>pool</strong>: PoolData!<br>&nbsp; <strong>tickIdx</strong>: BigInt!<br>&nbsp; <strong>liquidityGross</strong>: BigInt!<br>&nbsp; <strong>liquidityNet</strong>: BigInt!<br>&nbsp; <strong>timestamp</strong>: BigInt!<br>}<br><br>type Query {<br>&nbsp; poolData(<strong>id</strong>: ID!): PoolData<br>&nbsp; tickData(<strong>id</strong>: ID!): TickData<br>}<br></td></tr></tbody></table></figure>
<p>Once you build the Subgraph, you can go to Traceye and launch your Uniswap v3 Subgraph to start indexing data from its liquidity pool. For a step-by-step process,follow our dedicated guide.&nbsp;</p>
<p>While launching your own <a href="https://www.zeeve.io/subgraphs/">Subgraph </a>can have additional benefits, this option may not be suitable for a range of users who seek an easy and simplified method to retrieve Uniswap data. Hence, let’s discuss another way.</p>
<p><strong>Option2: Launch community Subgraph</strong></p>
<p>The community subgraph option offered by Traceye is a means to provide an abstraction layer to the complex and tech-intensive process of creating subgraphs. If you launch Uniswap v3 subgraph with Traceye, it will offer you the following main benefits:</p>
<ol><!-- wp:list-item -->
<li><strong>Faster deployment:</strong> community subgraphs on Traceye are launched within a few seconds, enabling you to start indexing data immediately from any protocol. For example Uniswap protocol, in our case.<br></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Wider accessibility:</strong> community subgraphs are not at all complex, hence developers of all levels can leverage them to fetch relevant data and build sophisticated dApps.</li>
<!-- /wp:list-item --></ol>
<ol start="3"><!-- wp:list-item -->
<li><strong>Significantly lower cost:</strong> querying data with Traceye’s community subgraphs requires only $39 per month. This is significantly lower than having a dedicated subgraph.<br></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Improved performance: </strong>Hosted on an enterprise-grade, highly optimized infrastructure, your subgraphs escape from performance bottlenecks such as downtime or inefficient queries.<br></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>24/7 support: </strong>With responsive support, all your queries or subgraph-related issues will be addressed in real-time so that it can not impact the performance.</li>
<!-- /wp:list-item --></ol>
<p><br>Now, let’s deploy Uniswap V2 Subgraph with Traceye:<br></p>
<li><a href="https://app.zeeve.io/auth/login">Login to Zeeve</a><strong> </strong>and navigate to Traceye located at the left-side panel. Click on ‘Hosted Subgraphs’. A new dashboard similar to the below will open.</li>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csvvao58hc57p9b5myly.png)
<p>Click on ‘Buy Subscription’ to get started with the payment process. Select the number of community subgraphs you need. For example, we are opting for 1 unit as of now. Proceed with Buy Now to complete your payment and activate community subgraph access.&nbsp;</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/balb3mjstb92l54hxwpv.png)
<p>Upon successful payment, you will see instructions similar to the below screenshot on your dashboard:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1imz7n8wcu3c9s6z8jy.png)
<p>From here, you can start setting up your community subgraph. Again, navigate to ‘Hosted Subgraphs’ under Traceye and click ‘Add Subgraph’. A drop-down menu will open, allowing you to create new, import existing, and launch community subgraphs. Click on ‘Launch Community’ to see the community subgraphs currently supported on Traceye.&nbsp;</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/guokv90lfwnz9bju6bed.png)
<p>As you can see, Uniswap-V3 is available for Polygon, Optimism, and Ethereum on Traceye. Whichever is your preferred option, select that and click on ‘Activate’. Upon this, your Uniswap will be deployed instantly.</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/faiedag70r0ttz4tow9x.png)
<p>Now, you will be redirected to a dashboard where you can check the details of your subgraph, such as Sync Status, creation date, total requests made, total API units consumed, workspace type, and Query URL.&nbsp;</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4o1b8zotqit6440yukn2.png)
<p>From the same dashboard, you can check logs, entities, add-on services like webhooks, and settings.&nbsp;&nbsp;</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42uc9stpmm8yshumm8m6.png)
<p>Now, to index and query Uniswap v3 liquidity pool data– you need to click on ‘open’ located with the Query URL (visible in the previous screenshot). It will redirect you to a playground environment where you can query Uniswap data.&nbsp;</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ci88m7a7473mhcok3zuh.png)
<p>As you know, you need to define entities for Uniswap v3 liquidity pool to be able to fetch specific data. Within the above playground, if you click on the GraphQL explorer icon on the left panel, it will open a comprehensive list of Uniswap v3 entities, including the pool-specific entities like pool, poolDayData, poolHourData, pools, etc.&nbsp;</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrrpg13m03mfk4brak03.png)
<p>Let’s say you want to fetch data for the first 15 pools. To do this, click on ‘pools’ entity and enter ‘15’ in the ‘first’ subentity. You can increase or decrease the number of pools as required. Also, within the pools data, you have multiple options such as orderby in which you can select order id for the data. For now, let’s move to fetching data for the first 15 pools. We have already entered the details, which are reflected in the GraphQL query.&nbsp;</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pntvukib0m56iesuxo0e.png)
<p>Now, execute the query by clicking on the play button, and data for all the 15 pools will be fetched instantly.&nbsp;</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4zkkg5bf2zhapenxseo.png)
<p>Similarly, you can fetch data for all the other entities available for your Uniswap v3 subgraph. As we can see, indexing and querying the Uniswap v3 subgraph is extremely simple and effortless using Traceye’s community subgraph. That’s because Traceye already keeps bulks of indexed data, allowing subgraphs to fetch any type of data without hassle.&nbsp;</p>
<h2 class="wp-block-heading" id="h-wrap-up"><strong>Wrap up!</strong></h2>
<p>And with that, our discussion around Indexing and querying Uniswap V3 liquidity pool data reaches the end. This article has been crafted to help dApp developers, web3 projects, and data consumers understand the simplicity and effortless approach of Traceye for indexing Uniswap data. Also, as we discussed, if your application requires a dedicated subgraph, you launch this with Traceye. On top of that, you will get support from Zeeve’s highly experienced web3 developers, who will build custom subgraphs for your project at a lower cost and in significantly reduced time for faster time-to-market.&nbsp;</p>
<p>If you want more information on Traceye or Zeeve’s blockchain services, <a href="https://www.zeeve.io/talk-to-an-expert/">connect with our experts</a>. Send your queries via mail or schedule a one-on-one call for detailed discussion.</p>