From 899482879e235b0552d4f5909aeb76475ee19eb2 Mon Sep 17 00:00:00 2001 From: Juan Manuel Rodriguez Defago Date: Mon, 19 Jun 2023 13:03:58 -0300 Subject: [PATCH] feat: add manifest FDS handler, schema scaffolding for once FDS offchain creation is ready --- schema.graphql | 10 ++-- src/mappings/gns.ts | 13 ++++-- src/mappings/helpers/helpers.ts | 13 +++--- src/mappings/ipfs.ts | 83 ++++++++++++++++++++++++++------- 4 files changed, 86 insertions(+), 33 deletions(-) diff --git a/schema.graphql b/schema.graphql index 0e8e9c7e..3381a54f 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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" @@ -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 } @@ -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" diff --git a/src/mappings/gns.ts b/src/mappings/gns.ts index 7f4dbdcb..3ecb1873 100644 --- a/src/mappings/gns.ts +++ b/src/mappings/gns.ts @@ -25,7 +25,8 @@ import { } from '../types/GNS/GNSStitched' import { - SubgraphMetadata as SubgraphMetadataTemplate + SubgraphMetadata as SubgraphMetadataTemplate, + SubgraphVersionMetadata as SubgraphVersionMetadataTemplate } from '../types/templates' import { @@ -235,10 +236,10 @@ export function handleSubgraphPublished(event: SubgraphPublished): void { let hexHash = changetype(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!)! @@ -1075,8 +1076,9 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi let hexHash = changetype(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 @@ -1101,7 +1103,6 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi let hexHash = changetype(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) { @@ -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) } } diff --git a/src/mappings/helpers/helpers.ts b/src/mappings/helpers/helpers.ts index 40df0a8f..5aef193c 100644 --- a/src/mappings/helpers/helpers.ts +++ b/src/mappings/helpers/helpers.ts @@ -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, @@ -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) @@ -117,6 +116,8 @@ export function createOrLoadSubgraphDeployment( deployment.deprecatedSubgraphCount = 0 deployment.save() + SubgraphDeploymentManifestTemplate.create(deployment.ipfsHash) + graphNetwork.subgraphDeploymentCount = graphNetwork.subgraphDeploymentCount + 1 graphNetwork.save() } diff --git a/src/mappings/ipfs.ts b/src/mappings/ipfs.ts index b79fc315..b933cf1a 100644 --- a/src/mappings/ipfs.ts +++ b/src/mappings/ipfs.ts @@ -1,4 +1,4 @@ -import { json, Bytes, dataSource, JSONValueKind } from '@graphprotocol/graph-ts' +import { json, Bytes, dataSource, JSONValueKind, log } from '@graphprotocol/graph-ts' import { SubgraphMetadata, SubgraphVersionMetadata, @@ -6,6 +6,9 @@ import { SubgraphDeploymentSchema, SubgraphDeploymentManifest, } from '../types/schema' +import { + SubgraphDeploymentSchema as SubgraphDeploymentSchemaTemplate +} from '../types/templates' import { jsonToString } from './utils' export function handleSubgraphMetadata(content: Bytes): void { @@ -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 { @@ -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()]) + } + } +} \ No newline at end of file