From 3213fed2edbd52e95c2245d495aed5026c98f15e Mon Sep 17 00:00:00 2001 From: Rekard0 <5880388+Rekard0@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:30:18 +0100 Subject: [PATCH 1/3] create release entity --- .../manifest/subgraph.placeholder.yaml | 4 +- packages/subgraph/schema.graphql | 16 ++- packages/subgraph/src/plugin/pluginRepo.ts | 51 ++++++- .../subgraph/tests/plugin/pluginRepo.test.ts | 124 +++++++++++++++--- packages/subgraph/tests/plugin/utils.ts | 28 +++- 5 files changed, 196 insertions(+), 27 deletions(-) diff --git a/packages/subgraph/manifest/subgraph.placeholder.yaml b/packages/subgraph/manifest/subgraph.placeholder.yaml index c4e28973e..08fcf5ae3 100644 --- a/packages/subgraph/manifest/subgraph.placeholder.yaml +++ b/packages/subgraph/manifest/subgraph.placeholder.yaml @@ -303,4 +303,6 @@ templates: file: $ARAGON_OSX_MODULE/artifacts/src/framework/plugin/repo/PluginRepo.sol/PluginRepo.json eventHandlers: - event: VersionCreated(uint8,uint16,indexed address,bytes) - handler: handleVersionCreated \ No newline at end of file + handler: handleVersionCreated + - event: ReleaseMetadataUpdated(uint8,bytes) + handler: handleReleaseMetadataUpdated \ No newline at end of file diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index 89d2daf6d..e275d084f 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -176,7 +176,7 @@ type Dao @entity { type PluginRepo @entity(immutable: true) { id: ID! # address subdomain: String! - versions: [PluginVersion!]! @derivedFrom(field: "pluginRepo") + releases: [PluginRelease!]! @derivedFrom(field: "pluginRepo") # Holds all preparations. Also applied ones. preparations: [PluginPreparation!]! @derivedFrom(field: "pluginRepo") # Holds all installed and uninstalled installations. @@ -188,13 +188,23 @@ type PluginSetup @entity(immutable: true) { versions: [PluginVersion!]! @derivedFrom(field: "pluginSetup") } +type PluginRelease @entity { + id: ID! # pluginRepo + release + pluginRepo: PluginRepo! + release: Int! + metadata: String! # release metadata + builds: [PluginVersion!]! @derivedFrom(field: "release") +} + type PluginVersion @entity(immutable: true) { id: ID! # pluginRepo + release + build pluginRepo: PluginRepo! pluginSetup: PluginSetup - release: Int! + + release: PluginRelease! build: Int! - metadata: Bytes! + + metadata: String! # build metadata # Holds all preparations. Also applied ones. preparations: [PluginPreparation!]! @derivedFrom(field: "pluginVersion") # Holds all installed and uninstalled installations. diff --git a/packages/subgraph/src/plugin/pluginRepo.ts b/packages/subgraph/src/plugin/pluginRepo.ts index b6e4500eb..b188c78fe 100644 --- a/packages/subgraph/src/plugin/pluginRepo.ts +++ b/packages/subgraph/src/plugin/pluginRepo.ts @@ -1,15 +1,32 @@ -import {VersionCreated} from '../../generated/templates/PluginRepoTemplate/PluginRepo'; -import {PluginVersion, PluginSetup} from '../../generated/schema'; +import { + ReleaseMetadataUpdated, + VersionCreated +} from '../../generated/templates/PluginRepoTemplate/PluginRepo'; +import { + PluginVersion, + PluginSetup, + PluginRelease +} from '../../generated/schema'; export function handleVersionCreated(event: VersionCreated): void { - let id = `${event.address.toHexString()}_${event.params.release.toString()}_${event.params.build.toString()}`; + let pluginRepoId = event.address.toHexString(); let pluginSetupId = event.params.pluginSetup.toHexString(); - let entity = new PluginVersion(id); + let pluginRelease = event.params.release.toString(); + + let pluginVersionId = pluginRepoId + .concat('_') + .concat(pluginRelease) + .concat('_') + .concat(event.params.build.toString()); + + let pluginReleaseId = pluginRepoId.concat('_').concat(pluginRelease); + + let entity = new PluginVersion(pluginVersionId); entity.pluginRepo = event.address.toHexString(); - entity.release = event.params.release; + entity.release = pluginReleaseId; entity.build = event.params.build; entity.pluginSetup = pluginSetupId; - entity.metadata = event.params.buildMetadata; + entity.metadata = event.params.buildMetadata.toString(); entity.save(); let pluginSetupEntity = PluginSetup.load(pluginSetupId); @@ -18,3 +35,25 @@ export function handleVersionCreated(event: VersionCreated): void { pluginSetupEntity.save(); } } + +export function handleReleaseMetadataUpdated( + event: ReleaseMetadataUpdated +): void { + let pluginRepoId = event.address.toHexString(); + let pluginRelease = event.params.release; + let releaseMetadata = event.params.releaseMetadata.toString(); + + let pluginReleaseEntityId = pluginRepoId + .concat('_') + .concat(pluginRelease.toString()); + + let pluginReleaseEntity = PluginRelease.load(pluginReleaseEntityId); + if (!pluginReleaseEntity) { + pluginReleaseEntity = new PluginRelease(pluginReleaseEntityId); + pluginReleaseEntity.pluginRepo = pluginRepoId; + pluginReleaseEntity.release = pluginRelease; + } + + pluginReleaseEntity.metadata = releaseMetadata; + pluginReleaseEntity.save(); +} diff --git a/packages/subgraph/tests/plugin/pluginRepo.test.ts b/packages/subgraph/tests/plugin/pluginRepo.test.ts index 95170173c..31f0e31b7 100644 --- a/packages/subgraph/tests/plugin/pluginRepo.test.ts +++ b/packages/subgraph/tests/plugin/pluginRepo.test.ts @@ -1,34 +1,126 @@ import {assert, clearStore, test} from 'matchstick-as/assembly/index'; -import {ADDRESS_ONE} from '../constants'; -import {createVersionCreated} from './utils'; -import {handleVersionCreated} from '../../src/plugin/pluginRepo'; +import {ADDRESS_ONE, ONE} from '../constants'; +import {createReleaseMetadataUpdatedEvent, createVersionCreated} from './utils'; +import { + handleReleaseMetadataUpdated, + handleVersionCreated +} from '../../src/plugin/pluginRepo'; import {Bytes} from '@graphprotocol/graph-ts'; -test('versionCreated event', () => { +test('PluginRepo (handleVersionCreated) mappings with mock event', () => { + let release = ONE; + let build = ONE; + let pluginSetup = ADDRESS_ONE; + let buildMetadata = 'Qm1234'; + let event = createVersionCreated( - '1', - '1', - ADDRESS_ONE, - Bytes.fromHexString('0x1234') + release, + build, + pluginSetup, + Bytes.fromUTF8(buildMetadata) ); + handleVersionCreated(event); - let id = `${event.address.toHexString()}_1_1`; + + let pluginRepoId = event.address.toHexString(); + + let pluginVersionId = pluginRepoId + .concat('_') + .concat(release) + .concat('_') + .concat(build); + + let pluginReleaseId = pluginRepoId.concat('_').concat(release); assert.entityCount('PluginVersion', 1); - assert.fieldEquals('PluginVersion', id, 'id', id); + assert.fieldEquals('PluginVersion', pluginVersionId, 'id', pluginVersionId); assert.fieldEquals( 'PluginVersion', - id, + pluginVersionId, 'pluginRepo', event.address.toHexString() ); - assert.fieldEquals('PluginVersion', id, 'release', '1'); - assert.fieldEquals('PluginVersion', id, 'build', '1'); - assert.fieldEquals('PluginVersion', id, 'pluginSetup', ADDRESS_ONE); - assert.fieldEquals('PluginVersion', id, 'metadata', '0x1234'); + assert.fieldEquals( + 'PluginVersion', + pluginVersionId, + 'release', + pluginReleaseId + ); + assert.fieldEquals('PluginVersion', pluginVersionId, 'build', build); + assert.fieldEquals( + 'PluginVersion', + pluginVersionId, + 'pluginSetup', + pluginSetup + ); + assert.fieldEquals( + 'PluginVersion', + pluginVersionId, + 'metadata', + buildMetadata + ); assert.entityCount('PluginSetup', 1); - assert.fieldEquals('PluginSetup', ADDRESS_ONE, 'id', ADDRESS_ONE); + assert.fieldEquals('PluginSetup', pluginSetup, 'id', pluginSetup); clearStore(); }); + +test('PluginRepo (handleReleaseMetadataUpdated) mappings with mock event', () => { + let release = ONE; + let releaseMetadata = 'Qm1234'; + + let event = createReleaseMetadataUpdatedEvent( + release, + Bytes.fromUTF8(releaseMetadata) + ); + + handleReleaseMetadataUpdated(event); + + assert.entityCount('PluginRelease', 1); + + let pluginRepoId = event.address.toHexString(); + let pluginReleaseEntityId = pluginRepoId.concat('_').concat(release); + + assert.fieldEquals( + 'PluginRelease', + pluginReleaseEntityId, + 'id', + pluginReleaseEntityId + ); + assert.fieldEquals( + 'PluginRelease', + pluginReleaseEntityId, + 'pluginRepo', + pluginRepoId + ); + assert.fieldEquals( + 'PluginRelease', + pluginReleaseEntityId, + 'release', + release + ); + assert.fieldEquals( + 'PluginRelease', + pluginReleaseEntityId, + 'metadata', + releaseMetadata + ); + + // simulated update of release metadata + let updatedMetadata = 'Qm5678'; + let updatingMetadataEvent = createReleaseMetadataUpdatedEvent( + release, + Bytes.fromUTF8(updatedMetadata) + ); + handleReleaseMetadataUpdated(updatingMetadataEvent); + + assert.entityCount('PluginRelease', 1); + + assert.fieldEquals( + 'PluginRelease', + pluginReleaseEntityId, + 'metadata', + updatedMetadata + ); +}); diff --git a/packages/subgraph/tests/plugin/utils.ts b/packages/subgraph/tests/plugin/utils.ts index f303a1446..d190e021a 100644 --- a/packages/subgraph/tests/plugin/utils.ts +++ b/packages/subgraph/tests/plugin/utils.ts @@ -1,6 +1,9 @@ import {Address, BigInt, Bytes, ethereum} from '@graphprotocol/graph-ts'; import {newMockEvent} from 'matchstick-as'; -import {VersionCreated} from '../../generated/templates/PluginRepoTemplate/PluginRepo'; +import { + ReleaseMetadataUpdated, + VersionCreated +} from '../../generated/templates/PluginRepoTemplate/PluginRepo'; import { InstallationApplied, InstallationPrepared, @@ -13,6 +16,29 @@ import { UpdatePreparedSetupPayloadStruct } from '../../generated/PluginSetupProcessor/PluginSetupProcessor'; +export function createReleaseMetadataUpdatedEvent( + release: string, + buildMetadata: Bytes +): ReleaseMetadataUpdated { + let newEvent = changetype(newMockEvent()); + + newEvent.parameters = []; + + let releaseParam = new ethereum.EventParam( + 'release', + ethereum.Value.fromUnsignedBigInt(BigInt.fromString(release)) + ); + let buildMetadataParam = new ethereum.EventParam( + 'buildMetadata', + ethereum.Value.fromBytes(buildMetadata) + ); + + newEvent.parameters.push(releaseParam); + newEvent.parameters.push(buildMetadataParam); + + return newEvent; +} + export function createVersionCreated( release: string, build: string, From d1f1798c8ea7bdf79d48e700733f5ae499bc2480 Mon Sep 17 00:00:00 2001 From: Rekard0 <5880388+Rekard0@users.noreply.github.com> Date: Mon, 6 Mar 2023 14:15:21 +0100 Subject: [PATCH 2/3] address comment part 1 --- packages/subgraph/src/plugin/pluginRepo.ts | 29 +++++++++++----------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/subgraph/src/plugin/pluginRepo.ts b/packages/subgraph/src/plugin/pluginRepo.ts index b188c78fe..7997f8631 100644 --- a/packages/subgraph/src/plugin/pluginRepo.ts +++ b/packages/subgraph/src/plugin/pluginRepo.ts @@ -9,31 +9,32 @@ import { } from '../../generated/schema'; export function handleVersionCreated(event: VersionCreated): void { - let pluginRepoId = event.address.toHexString(); + // PluginSetup let pluginSetupId = event.params.pluginSetup.toHexString(); - let pluginRelease = event.params.release.toString(); - let pluginVersionId = pluginRepoId - .concat('_') - .concat(pluginRelease) - .concat('_') - .concat(event.params.build.toString()); + let pluginSetupEntity = PluginSetup.load(pluginSetupId); + if (!pluginSetupEntity) { + pluginSetupEntity = new PluginSetup(pluginSetupId); + pluginSetupEntity.save(); + } + // PluginVersion + let pluginRepoId = event.address.toHexString(); + let pluginRelease = event.params.release.toString(); let pluginReleaseId = pluginRepoId.concat('_').concat(pluginRelease); + let pluginVersionId = pluginReleaseId + .concat('_') + .concat(event.params.build.toString()); let entity = new PluginVersion(pluginVersionId); entity.pluginRepo = event.address.toHexString(); + entity.pluginSetup = pluginSetupId; + entity.release = pluginReleaseId; entity.build = event.params.build; - entity.pluginSetup = pluginSetupId; + entity.metadata = event.params.buildMetadata.toString(); entity.save(); - - let pluginSetupEntity = PluginSetup.load(pluginSetupId); - if (!pluginSetupEntity) { - pluginSetupEntity = new PluginSetup(pluginSetupId); - pluginSetupEntity.save(); - } } export function handleReleaseMetadataUpdated( From 9a5a6793813d163783131994783d72a2945ac362 Mon Sep 17 00:00:00 2001 From: Rekard0 <5880388+Rekard0@users.noreply.github.com> Date: Mon, 6 Mar 2023 15:10:45 +0100 Subject: [PATCH 3/3] update change log --- packages/subgraph/CHANGELOG.md | 12 ++++++++++++ packages/subgraph/manifest/subgraph.placeholder.yaml | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/subgraph/CHANGELOG.md b/packages/subgraph/CHANGELOG.md index af8aa3633..cd5a04fc8 100644 --- a/packages/subgraph/CHANGELOG.md +++ b/packages/subgraph/CHANGELOG.md @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [UPCOMING] + +### Added + +- Added `PluginRelease`. +- Added `metadata` to `PluginVersion`. + +### Changed + +- Changed `release: Int!` to `release: PluginRelease!` in `PluginVersion` +- Changed `versions` to `releases` in `PluginRepo`. + ## [1.0.1] ### Added diff --git a/packages/subgraph/manifest/subgraph.placeholder.yaml b/packages/subgraph/manifest/subgraph.placeholder.yaml index 08fcf5ae3..447e38ce3 100644 --- a/packages/subgraph/manifest/subgraph.placeholder.yaml +++ b/packages/subgraph/manifest/subgraph.placeholder.yaml @@ -305,4 +305,4 @@ templates: - event: VersionCreated(uint8,uint16,indexed address,bytes) handler: handleVersionCreated - event: ReleaseMetadataUpdated(uint8,bytes) - handler: handleReleaseMetadataUpdated \ No newline at end of file + handler: handleReleaseMetadataUpdated