Skip to content

Commit

Permalink
feat: add startBlock parsing to manifest handler
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmardefago committed Apr 15, 2024
1 parent d1ebd62 commit 5296f26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,8 @@ type SubgraphDeploymentManifest @entity(immutable:true) {
network: String
"Whether the subgraph is a SpS/SbS. Null if we can't parse it"
poweredBySubstreams: Boolean
"Start block for the deployment. It's the lowest startBlock found (0 if some data source doesn't contain a start block)"
startBlock: BigInt
}

# TODO - add when we have the ability to parse data sources
Expand Down
23 changes: 21 additions & 2 deletions src/mappings/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { json, Bytes, dataSource, JSONValueKind, log, DataSourceContext } from '@graphprotocol/graph-ts'
import { json, Bytes, dataSource, JSONValueKind, log, DataSourceContext, BigInt } from '@graphprotocol/graph-ts'
import {
SubgraphMeta,
SubgraphVersionMeta,
Expand Down Expand Up @@ -133,6 +133,25 @@ export function handleSubgraphDeploymentManifest(content: Bytes): void {
}
let substreamsSplitTry = manifest.split('- kind: substreams', 2)
subgraphDeploymentManifest.poweredBySubstreams = substreamsSplitTry.length > 1

// startBlock calculation
let templatesSplit = manifest.split("templates:")
let nonTemplateManifestSplit = templatesSplit[0] // we take the left as we want to remove the templates for the source checks.
let sourcesSplit = nonTemplateManifestSplit.split("source:") // We want to know how many source definitions we have
let startBlockSplit = nonTemplateManifestSplit.split("startBlock: ") // And how many startBlock definitions we have to know if we should set startBlock to 0

if (sourcesSplit.length > startBlockSplit.length) {
subgraphDeploymentManifest.startBlock = BigInt.fromI32(0)
} else {
// need to figure the minimum startBlock defined, we skip i = 0 as we know it's not gonna contain a start block num, since it's before the first appearance of "startBlock:"
let min = BigInt.fromI32(0)
for(let i = 1; i < startBlockSplit.length; i++) {
let numString = startBlockSplit[i].split("\n", 1)[0].toString()
let num = BigInt.fromString(numString)
min = min == BigInt.fromI32(0) ? num : min <= num ? min : num
}
subgraphDeploymentManifest.startBlock = min
}
}
subgraphDeploymentManifest.save()
}
}

0 comments on commit 5296f26

Please sign in to comment.