Skip to content

Commit

Permalink
fix: futureproof duplicate ID
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmardefago committed Jan 17, 2024
1 parent 734b54c commit 874d1f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
12 changes: 8 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,10 @@ type SubgraphMetadataUpdatedEvent implements SubgraphEvent & Event @entity {
type SubgraphMetadata @entity(immutable:true) {
"Subgraph metadata ipfs hash"
id: ID!
"Subgraph entity"
subgraph: SubgraphMetadataUpdatedEvent @derivedFrom(field:"metadata")
"Subgraph that reference this metadata. For compatibility purposes. For the full list use subgraphs"
subgraph: Subgraph @derivedFrom(field:"metadata")
"Subgraphs that reference this metadata"
subgraphs: [Subgraph!]! @derivedFrom(field:"metadata")
"Short description of the subgraph"
description: String
"Image in string format"
Expand Down Expand Up @@ -427,8 +429,10 @@ type SubgraphVersionMetadataUpdatedEvent implements SubgraphEvent & SubgraphDepl
type SubgraphVersionMetadata @entity(immutable:true) {
"Subgraph version metadata ipfs hash"
id: ID!
"Subgraph version entity"
subgraphVersion: SubgraphVersionMetadataUpdatedEvent @derivedFrom(field:"metadata")
"SubgraphVersion entity that references this metadata. For compatibility purposes. For the full list use subgraphVersions"
subgraphVersion: SubgraphVersion @derivedFrom(field:"metadata")
"SubgraphVersion entities that reference this metadata"
subgraphVersions: [SubgraphVersion!]! @derivedFrom(field:"metadata")
"Short description of the version"
description: String
"Semantic versioning label"
Expand Down
26 changes: 18 additions & 8 deletions src/mappings/gns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, BigDecimal, Bytes, json } from '@graphprotocol/graph-ts'
import { BigInt, BigDecimal, Bytes, json, DataSourceContext } from '@graphprotocol/graph-ts'
import {
SubgraphPublished,
SubgraphDeprecated,
Expand Down Expand Up @@ -120,6 +120,7 @@ export function handleSubgraphMetadataUpdated(event: SubgraphMetadataUpdated): v

let hexHash = changetype<Bytes>(addQm(event.params.subgraphMetadata))
let base58Hash = hexHash.toBase58()
let metadataId = subgraph.id.concat('-').concat(base58Hash)

let eventEntity = new SubgraphMetadataUpdatedEvent(eventId)
eventEntity.timestamp = event.block.timestamp
Expand All @@ -133,7 +134,7 @@ export function handleSubgraphMetadataUpdated(event: SubgraphMetadataUpdated): v
eventEntity.subgraph = subgraph.id
eventEntity.accounts = accounts
eventEntity.ipfsFileHash = base58Hash
eventEntity.metadata = base58Hash
eventEntity.metadata = metadataId
eventEntity.save()

let counter = getCounter()
Expand All @@ -144,7 +145,9 @@ export function handleSubgraphMetadataUpdated(event: SubgraphMetadataUpdated): v
counter.eventCount = counter.eventCount.plus(BIGINT_ONE)
counter.save()

SubgraphMetadataTemplate.create(base58Hash)
let context = new DataSourceContext()
context.setString('id', metadataId)
SubgraphMetadataTemplate.createWithContext(base58Hash, context)
}

/**
Expand Down Expand Up @@ -603,6 +606,7 @@ export function handleSubgraphMetadataUpdatedV2(event: SubgraphMetadataUpdated1)

let hexHash = changetype<Bytes>(addQm(event.params.subgraphMetadata))
let base58Hash = hexHash.toBase58()
let metadataId = subgraph.id.concat('-').concat(base58Hash)

let eventEntity = new SubgraphMetadataUpdatedEvent(eventId)
eventEntity.timestamp = event.block.timestamp
Expand All @@ -616,7 +620,7 @@ export function handleSubgraphMetadataUpdatedV2(event: SubgraphMetadataUpdated1)
eventEntity.subgraph = subgraph.id
eventEntity.accounts = accounts
eventEntity.ipfsFileHash = base58Hash
eventEntity.metadata = base58Hash
eventEntity.metadata = metadataId
eventEntity.save()

let counter = getCounter()
Expand All @@ -626,7 +630,9 @@ export function handleSubgraphMetadataUpdatedV2(event: SubgraphMetadataUpdated1)
counter.subgraphMetadataUpdatedEventCount.plus(BIGINT_ONE)
counter.save()

SubgraphMetadataTemplate.create(base58Hash)
let context = new DataSourceContext()
context.setString('id', metadataId)
SubgraphMetadataTemplate.createWithContext(base58Hash, context)
}

// - event: SignalMinted(indexed uint256,indexed address,uint256,uint256,uint256)
Expand Down Expand Up @@ -796,13 +802,14 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi
counter.subgraphVersionMetadataUpdatedEventCount =
counter.subgraphVersionMetadataUpdatedEventCount.plus(BIGINT_ONE)

versionID = joinID([subgraphID, subgraph.versionCount.toString()])

if (subgraph.initializing) {
subgraph.initializing = false
subgraph.save()

// attach
} else {
versionID = joinID([subgraphID, subgraph.versionCount.toString()])
subgraph.currentVersion = versionID
subgraph.versionCount = subgraph.versionCount.plus(BigInt.fromI32(1))
subgraph.save()
Expand Down Expand Up @@ -840,6 +847,7 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi

let hexHash = changetype<Bytes>(addQm(event.params.versionMetadata))
let base58Hash = hexHash.toBase58()
let metadataId = versionID.concat('-').concat(base58Hash)

let otherEventEntity = new SubgraphVersionMetadataUpdatedEvent(eventId.concat('0'))
otherEventEntity.timestamp = event.block.timestamp
Expand All @@ -854,11 +862,13 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi
otherEventEntity.deployment = deployment.id
otherEventEntity.accounts = accounts
otherEventEntity.ipfsFileHash = base58Hash
otherEventEntity.metadata = base58Hash
otherEventEntity.metadata = metadataId
otherEventEntity.save()
counter.save()

SubgraphVersionMetadataTemplate.create(base58Hash)
let context = new DataSourceContext()
context.setString('id', metadataId)
SubgraphVersionMetadataTemplate.createWithContext(base58Hash, context)
}

// - event: LegacySubgraphClaimed(indexed address,uint256)
Expand Down
6 changes: 4 additions & 2 deletions src/mappings/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
import { jsonToString } from './utils'

export function handleSubgraphMetadata(content: Bytes): void {
let subgraphMetadata = new SubgraphMetadata(dataSource.stringParam())
let id = dataSource.context().getString("id")
let subgraphMetadata = new SubgraphMetadata(id)
let tryData = json.try_fromBytes(content)
if (tryData.isOk) {
let data = tryData.value.toObject()
Expand All @@ -33,7 +34,8 @@ export function handleSubgraphMetadata(content: Bytes): void {
}

export function handleSubgraphVersionMetadata(content: Bytes): void {
let subgraphVersionMetadata = new SubgraphVersionMetadata(dataSource.stringParam())
let id = dataSource.context().getString("id")
let subgraphVersionMetadata = new SubgraphVersionMetadata(id)
let tryData = json.try_fromBytes(content)
if (tryData.isOk) {
let data = tryData.value.toObject()
Expand Down

0 comments on commit 874d1f5

Please sign in to comment.