Skip to content

Commit

Permalink
feat: add manifest FDS handler, schema scaffolding for once FDS offch…
Browse files Browse the repository at this point in the history
…ain creation is ready
  • Loading branch information
juanmardefago committed Sep 11, 2023
1 parent fd59f64 commit 8994828
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 33 deletions.
10 changes: 5 additions & 5 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,6 @@ type SubgraphDeployment @entity {
# dataSources: [DataSource!]
"Entity that represents the manifest of the deployment. Filled by File Data Sources"
manifest: SubgraphDeploymentManifest
"Entity that represents the schema of the deployment. Filled by File Data Sources"
schema: SubgraphDeploymentSchema
"shcema ipfs hash string"
schemaIpfsHash: String

# Counters for currentSignalledTokens tracking on Subgraph
"Total amount of Subgraph entities that used this deployment at some point. subgraphCount >= activeSubgraphCount + deprecatedSubgraphCount"
Expand Down Expand Up @@ -601,7 +597,7 @@ type SubgraphDeploymentSchema @entity(immutable:true) {
"IPFS Hash"
id: ID!
"Link to SubgraphDeployment entity"
deployment: SubgraphDeployment @derivedFrom(field:"schema")
manifest: SubgraphDeploymentManifest @derivedFrom(field:"schema")
"Contents of the Schema file"
schema: String
}
Expand All @@ -611,6 +607,10 @@ type SubgraphDeploymentManifest @entity(immutable:true) {
id: ID!
"Link to SubgraphDeployment entity"
deployment: SubgraphDeployment @derivedFrom(field:"manifest")
"Schema entity. Not yet working due to limitations with File Data Sources"
schema: SubgraphDeploymentSchema
"Schema ipfs hash"
schemaIpfsHash: String
"Contents of the Manifest file"
manifest: String
"Network where the contracts that the subgraph indexes are located"
Expand Down
13 changes: 8 additions & 5 deletions src/mappings/gns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
} from '../types/GNS/GNSStitched'

import {
SubgraphMetadata as SubgraphMetadataTemplate
SubgraphMetadata as SubgraphMetadataTemplate,
SubgraphVersionMetadata as SubgraphVersionMetadataTemplate
} from '../types/templates'

import {
Expand Down Expand Up @@ -235,10 +236,10 @@ export function handleSubgraphPublished(event: SubgraphPublished): void {
let hexHash = changetype<Bytes>(addQm(event.params.versionMetadata))
let base58Hash = hexHash.toBase58()
subgraphVersion.metadataHash = event.params.versionMetadata
//subgraphVersion = fetchSubgraphVersionMetadata(subgraphVersion, base58Hash)

subgraphVersion.save()

SubgraphVersionMetadataTemplate.create(base58Hash)

let oldDeployment: SubgraphDeployment | null = null
if (oldVersionID != null) {
let oldVersion = SubgraphVersion.load(oldVersionID!)!
Expand Down Expand Up @@ -1075,8 +1076,9 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi
let hexHash = changetype<Bytes>(addQm(event.params.versionMetadata))
let base58Hash = hexHash.toBase58()
subgraphVersion.metadataHash = event.params.versionMetadata
//subgraphVersion = fetchSubgraphVersionMetadata(subgraphVersion, base58Hash)
subgraphVersion.save()

SubgraphVersionMetadataTemplate.create(base58Hash)
} else {
let oldVersionID = subgraph.currentVersion

Expand All @@ -1101,7 +1103,6 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi
let hexHash = changetype<Bytes>(addQm(event.params.versionMetadata))
let base58Hash = hexHash.toBase58()
subgraphVersion.metadataHash = event.params.versionMetadata
//subgraphVersion = fetchSubgraphVersionMetadata(subgraphVersion, base58Hash)

let oldDeployment: SubgraphDeployment | null = null
if (oldVersionID != null) {
Expand All @@ -1111,6 +1112,8 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi
// create deployment - named subgraph relationship, and update the old one
updateCurrentDeploymentLinks(oldDeployment, deployment, subgraph as Subgraph)
subgraphVersion.save()

SubgraphVersionMetadataTemplate.create(base58Hash)
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/mappings/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ import {
NameSignalSubgraphRelation,
CurrentSubgraphDeploymentRelation,
} from '../../types/schema'
import {
SubgraphDeploymentManifest as SubgraphDeploymentManifestTemplate
} from '../../types/templates'
import { ENS } from '../../types/GNS/ENS'
import { Controller } from '../../types/Controller/Controller'
import { EpochManager } from '../../types/EpochManager/EpochManager'
import { fetchSubgraphDeploymentManifest } from './metadata'
import { addresses } from '../../../config/addresses'


export function createOrLoadSubgraph(
bigIntID: BigInt,
owner: Address,
Expand Down Expand Up @@ -87,11 +90,7 @@ export function createOrLoadSubgraphDeployment(
let prefix = '1220'
deployment = new SubgraphDeployment(subgraphID)
deployment.ipfsHash = Bytes.fromHexString(prefix.concat(subgraphID.slice(2))).toBase58()
/*
deployment = fetchSubgraphDeploymentManifest(
deployment as SubgraphDeployment,
deployment.ipfsHash,
)*/
deployment.manifest = deployment.ipfsHash
deployment.createdAt = timestamp.toI32()
deployment.stakedTokens = BigInt.fromI32(0)
deployment.indexingRewardAmount = BigInt.fromI32(0)
Expand All @@ -117,6 +116,8 @@ export function createOrLoadSubgraphDeployment(
deployment.deprecatedSubgraphCount = 0
deployment.save()

SubgraphDeploymentManifestTemplate.create(deployment.ipfsHash)

graphNetwork.subgraphDeploymentCount = graphNetwork.subgraphDeploymentCount + 1
graphNetwork.save()
}
Expand Down
83 changes: 66 additions & 17 deletions src/mappings/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { json, Bytes, dataSource, JSONValueKind } from '@graphprotocol/graph-ts'
import { json, Bytes, dataSource, JSONValueKind, log } from '@graphprotocol/graph-ts'
import {
SubgraphMetadata,
SubgraphVersionMetadata,
GraphAccountMetadata,
SubgraphDeploymentSchema,
SubgraphDeploymentManifest,
} from '../types/schema'
import {
SubgraphDeploymentSchema as SubgraphDeploymentSchemaTemplate
} from '../types/templates'
import { jsonToString } from './utils'

export function handleSubgraphMetadata(content: Bytes): void {
Expand Down Expand Up @@ -36,11 +39,14 @@ export function handleSubgraphMetadata(content: Bytes): void {
}

export function handleSubgraphVersionMetadata(content: Bytes): void {
// let subgraphVersionMetadata = SubgraphVersionMetadata.load(dataSource.stringParam())
// const value = json.fromBytes(content).toObject()
// if (value) {
// subgraphVersionMetadata.save()
// }
let subgraphVersionMetadata = new SubgraphVersionMetadata(dataSource.stringParam())
let tryData = json.try_fromBytes(content)
if (tryData.isOk) {
let data = tryData.value.toObject()
subgraphVersionMetadata.description = jsonToString(data.get('description'))
subgraphVersionMetadata.label = jsonToString(data.get('label'))
}
subgraphVersionMetadata.save()
}

export function handleGraphAccountMetadata(content: Bytes): void {
Expand All @@ -61,18 +67,61 @@ export function handleGraphAccountMetadata(content: Bytes): void {
}
}


export function handleSubgraphDeploymentSchema(content: Bytes): void {
// let subgraphDeploymentSchema = new SubgraphDeploymentSchema(dataSource.stringParam())
// const value = json.fromBytes(content).toObject()
// if (value) {
// subgraphDeploymentSchema.save()
// }
let subgraphDeploymentSchema = new SubgraphDeploymentSchema(dataSource.stringParam())
if (content !== null) {
subgraphDeploymentSchema.schema = content.toString()
}
}

export function handleSubgraphDeploymentManifest(content: Bytes): void {
// let subgraphDeploymentManifest = new SubgraphDeploymentManifest(dataSource.stringParam())
// const value = json.fromBytes(content).toObject()
// if (value) {
// subgraphDeploymentManifest.save()
// }
}
let subgraphDeploymentManifest = new SubgraphDeploymentManifest(dataSource.stringParam())
if (content !== null) {
subgraphDeploymentManifest.manifest = content.toString()

let manifest = subgraphDeploymentManifest.manifest!
// we take the right side of the split, since it's the one which will have the schema ipfs hash
let schemaSplitTry = manifest.split('schema:\n', 2)
if (schemaSplitTry.length == 2) {
let schemaSplit = schemaSplitTry[1]

let schemaFileSplitTry = schemaSplit.split('/ipfs/', 2)
if (schemaFileSplitTry.length == 2) {
let schemaFileSplit = schemaFileSplitTry[1]

let schemaIpfsHashTry = schemaFileSplit.split('\n', 2)
if (schemaIpfsHashTry.length == 2) {
let schemaIpfsHash = schemaIpfsHashTry[0]
subgraphDeploymentManifest.schema = schemaIpfsHash
subgraphDeploymentManifest.schemaIpfsHash = schemaIpfsHash

// Can't create this template here yet (due to current implementation limitations on File Data Sources, but once that's sorted out, this should work.)
//SubgraphDeploymentSchemaTemplate.create(schemaIpfsHash)
} else {
log.warning("[MANIFEST PARSING FAIL] subgraphDeploymentManifest: {}, schema file hash can't be retrieved. Error: schemaIpfsHashTry.length isn't 2, actual length: {}", [dataSource.stringParam(), schemaIpfsHashTry.length.toString()])
}
} else {
log.warning("[MANIFEST PARSING FAIL] subgraphDeploymentManifest: {}, schema file hash can't be retrieved. Error: schemaFileSplitTry.length isn't 2, actual length: {}", [dataSource.stringParam(), schemaFileSplitTry.length.toString()])
}
} else {
log.warning("[MANIFEST PARSING FAIL] subgraphDeploymentManifest: {}, schema file hash can't be retrieved. Error: schemaSplitTry.length isn't 2, actual length: {}", [dataSource.stringParam(), schemaSplitTry.length.toString()])
}

// We get the first occurrence of `network` since subgraphs can only have data sources for the same network
let networkSplitTry = manifest.split('network: ', 2)
if (networkSplitTry.length == 2) {
let networkSplit = networkSplitTry[1]
let networkTry = networkSplit.split('\n', 2)
if (networkTry.length == 2) {
let network = networkTry[0]

subgraphDeploymentManifest.network = network
} else {
log.warning("[MANIFEST PARSING FAIL] subgraphDeploymentManifest: {}, network can't be parsed. Error: networkTry.length isn't 2, actual length: {}", [dataSource.stringParam(), networkTry.length.toString()])
}
} else {
log.warning("[MANIFEST PARSING FAIL] subgraphDeploymentManifest: {}, network can't be parsed. Error: networkSplitTry.length isn't 2, actual length: {}", [dataSource.stringParam(), networkSplitTry.length.toString()])
}
}
}

0 comments on commit 8994828

Please sign in to comment.