Add Positions tracking based on mint, burn and collect events#258
Open
VGabriel45 wants to merge 3 commits into
Open
Add Positions tracking based on mint, burn and collect events#258VGabriel45 wants to merge 3 commits into
VGabriel45 wants to merge 3 commits into
Conversation
feat: add positions tracking
dlbnco
reviewed
Jun 28, 2025
| @@ -1,28 +1 @@ | |||
| # Uniswap V3 Subgraph | |||
There was a problem hiding this comment.
Guess you shouldn't get rid of the readme?
dlbnco
reviewed
Jun 28, 2025
| * @param event The event that triggered this function | ||
| * @returns The position entity | ||
| */ | ||
| export function getOrCreatePosition( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.