Skip to content

Conversation

VGabriel45
Copy link

This PR adds tracking for the LP positions, this is available on the published UniswapV3 subgraph at https://thegraph.com/explorer/subgraphs/5zvR82QoaXYFyDEKLZ9t6v9adgnptxYpKpSbxtgVENFV?view=Query&chain=arbitrum-one but is missing from this repository making it confusing for developers that are looking to fork the subgraph and work with it.

@@ -1,28 +1 @@
# Uniswap V3 Subgraph
Copy link

@dlbnco dlbnco Jun 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess you shouldn't get rid of the readme?

* @param event The event that triggered this function
* @returns The position entity
*/
export function getOrCreatePosition(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add the tokenId field? (sequential number that gets incremented on each minted position).

It is required in some parts of the Uniswap interface and also for operations like increasing or decreasing liquidity on a position.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a rough diff for how I have achieved this:

diff --git a/packages/v3-subgraph/generated/schema.ts b/packages/v3-subgraph/generated/schema.ts
index 823c54ea9..b704b9aba 100644

diff --git a/packages/v3-subgraph/src/common/position.ts b/packages/v3-subgraph/src/common/position.ts
index 3a4031bce..3f4b7bbe8 100644
--- a/packages/v3-subgraph/src/common/position.ts
+++ b/packages/v3-subgraph/src/common/position.ts
@@ -41,7 +41,8 @@ export function getOrCreatePosition(
   pool: Pool,
   tickLower: BigInt,
   tickUpper: BigInt,
-  event: ethereum.Event
+  event: ethereum.Event,
+  tokenId: BigInt
 ): Position {
   const positionId =
     owner.toHexString() + '-' + pool.id.toHexString() + '-' + tickLower.toString() + '-' + tickUpper.toString()
@@ -66,6 +67,7 @@ export function getOrCreatePosition(
     position.createdAtTimestamp = event.block.timestamp
     position.createdAtBlockNumber = event.block.number
     position.closed = false
+    position.tokenId = tokenId
   }
 
   position.updatedAtTimestamp = event.block.timestamp
diff --git a/packages/v3-subgraph/src/v3/mappings/factory.ts b/packages/v3-subgraph/src/v3/mappings/factory.ts
index 8fa018803..52d6a80b4 100644
--- a/packages/v3-subgraph/src/v3/mappings/factory.ts
+++ b/packages/v3-subgraph/src/v3/mappings/factory.ts
@@ -35,6 +35,7 @@ export function handlePoolCreated(event: PoolCreated): void {
     factory.totalValueLockedETHUntracked = ZERO_BD
     factory.txCount = ZERO_BI
     factory.owner = ADDRESS_ZERO
+    factory.positionCount = ZERO_BI
 
     // create new bundle for tracking eth price
     const bundle = new Bundle('1')
diff --git a/packages/v3-subgraph/src/v3/mappings/mint.ts b/packages/v3-subgraph/src/v3/mappings/mint.ts
index 1dca90036..4bdadee08 100644
--- a/packages/v3-subgraph/src/v3/mappings/mint.ts
+++ b/packages/v3-subgraph/src/v3/mappings/mint.ts
@@ -40,6 +40,9 @@ export function handleMint(event: MintEvent): void {
     // update globals
     factory.txCount = factory.txCount.plus(ONE_BI)
 
+    // update position count
+    factory.positionCount = factory.positionCount.plus(ONE_BI)
+
     // update token0 data
     token0.txCount = token0.txCount.plus(ONE_BI)
     token0.totalValueLocked = token0.totalValueLocked.plus(amount0)
@@ -122,7 +125,8 @@ export function handleMint(event: MintEvent): void {
       pool,
       BigInt.fromI32(event.params.tickLower),
       BigInt.fromI32(event.params.tickUpper),
-      event
+      event,
+      factory.positionCount
     )
     updatePositionWithMint(position, event.params.amount, amount0, amount1)
     position.save()
diff --git a/packages/v3-subgraph/src/v3/schema.graphql b/packages/v3-subgraph/src/v3/schema.graphql
index b5c4b955f..95a38947c 100644
--- a/packages/v3-subgraph/src/v3/schema.graphql
+++ b/packages/v3-subgraph/src/v3/schema.graphql
@@ -27,7 +27,8 @@ type Factory @entity(immutable: false) {
   totalValueLockedUSDUntracked: BigDecimal!
   # TVL derived in ETH untracked
   totalValueLockedETHUntracked: BigDecimal!
-
+  # Position count
+  positionCount: BigInt!
   # current owner of the factory
   owner: ID!
 }
@@ -171,6 +172,7 @@ type Tick @entity(immutable: false) {
 type Position @entity(immutable: false) {
   # Format: <owner address>-<pool address>-<lower tick>-<upper tick>
   id: ID!
+  tokenId: BigInt!
   # Owner of the position
   owner: Bytes!
   # Pool the position is in
@@ -215,6 +217,7 @@ type Position @entity(immutable: false) {
 type PositionSnapshot @entity(immutable: false) {
   # Format: <position id>-<block number>
   id: ID!
+  tokenId: BigInt!
   # Owner of the position
   owner: Bytes!
   # Pool the position is in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants