From 5366abf33361cd9e3a3170705f749a295cb720f2 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Tue, 7 Nov 2023 20:54:40 -0500 Subject: [PATCH 1/2] create missing recipient registry and filter out foreign events --- subgraph/src/FundingRoundFactoryMapping.ts | 51 +---------------- .../src/OptimisticRecipientRegistryMapping.ts | 9 +-- subgraph/src/RecipientRegistry.ts | 56 +++++++++++++++++++ 3 files changed, 63 insertions(+), 53 deletions(-) create mode 100644 subgraph/src/RecipientRegistry.ts diff --git a/subgraph/src/FundingRoundFactoryMapping.ts b/subgraph/src/FundingRoundFactoryMapping.ts index 5233214e6..460f0fcf9 100644 --- a/subgraph/src/FundingRoundFactoryMapping.ts +++ b/subgraph/src/FundingRoundFactoryMapping.ts @@ -17,6 +17,7 @@ import { FundingRound as FundingRoundContract } from '../generated/FundingRoundF import { OptimisticRecipientRegistry as RecipientRegistryContract } from '../generated/FundingRoundFactory/OptimisticRecipientRegistry' import { BrightIdUserRegistry as BrightIdUserRegistryContract } from '../generated/FundingRoundFactory/BrightIdUserRegistry' +import { loadRecipientRegistry } from './RecipientRegistry' import { FundingRound as FundingRoundTemplate, @@ -31,48 +32,6 @@ import { Token, } from '../generated/schema' -function createRecipientRegistry( - fundingRoundFactoryAddress: Address, - recipientRegistryAddress: Address -): RecipientRegistry { - log.info('New recipientRegistry {}', [recipientRegistryAddress.toHex()]) - let recipientRegistryId = recipientRegistryAddress.toHexString() - let recipientRegistry = new RecipientRegistry(recipientRegistryId) - - recipientRegistryTemplate.create(recipientRegistryAddress) - let recipientRegistryContract = RecipientRegistryContract.bind( - recipientRegistryAddress - ) - let baseDeposit = recipientRegistryContract.try_baseDeposit() - if (baseDeposit.reverted) { - recipientRegistry.baseDeposit = BigInt.fromI32(0) - recipientRegistry.challengePeriodDuration = BigInt.fromI32(0) - } else { - recipientRegistry.baseDeposit = baseDeposit.value - let challengePeriodDuration = - recipientRegistryContract.challengePeriodDuration() - recipientRegistry.challengePeriodDuration = challengePeriodDuration - } - let controller = recipientRegistryContract.try_controller() - let maxRecipients = recipientRegistryContract.try_maxRecipients() - let owner = recipientRegistryContract.try_owner() - - if (!controller.reverted) { - recipientRegistry.controller = controller.value - } - if (!maxRecipients.reverted) { - recipientRegistry.maxRecipients = maxRecipients.value - } - if (!owner.reverted) { - recipientRegistry.owner = owner.value - } - recipientRegistry.fundingRoundFactory = - fundingRoundFactoryAddress.toHexString() - recipientRegistry.save() - - return recipientRegistry -} - function createContributorRegistry( fundingRoundFactoryAddress: Address, contributorRegistryAddress: Address @@ -189,13 +148,7 @@ function createOrUpdateFundingRoundFactory( //Check if these registries already exist/are being tracked let recipientRegistryAddress = fundingRoundFactoryContract.recipientRegistry() let recipientRegistryId = recipientRegistryAddress.toHexString() - let recipientRegistry = RecipientRegistry.load(recipientRegistryId) - if (!recipientRegistry) { - createRecipientRegistry( - fundingRoundFactoryAddress, - recipientRegistryAddress - ) - } + let recipientRegistry = loadRecipientRegistry(recipientRegistryAddress) let contributorRegistryAddress = fundingRoundFactoryContract.userRegistry() let contributorRegistryId = contributorRegistryAddress.toHexString() diff --git a/subgraph/src/OptimisticRecipientRegistryMapping.ts b/subgraph/src/OptimisticRecipientRegistryMapping.ts index 24394f231..41c83aa3f 100644 --- a/subgraph/src/OptimisticRecipientRegistryMapping.ts +++ b/subgraph/src/OptimisticRecipientRegistryMapping.ts @@ -5,7 +5,8 @@ import { RequestSubmitted, } from '../generated/OptimisticRecipientRegistry/OptimisticRecipientRegistry' -import { Recipient, RecipientRegistry } from '../generated/schema' +import { Recipient } from '../generated/schema' +import { loadRecipientRegistry } from './RecipientRegistry' // It is also possible to access smart contracts from mappings. For // example, the contract that has emitted the event can be connected to @@ -34,7 +35,7 @@ export function handleRequestResolved(event: RequestResolved): void { log.info('handleRequestResolved', []) let recipientRegistryId = event.address.toHexString() - let recipientRegistry = RecipientRegistry.load(recipientRegistryId) + let recipientRegistry = loadRecipientRegistry(event.address) if (!recipientRegistry) { log.warning( 'handleRequestResolved - ignore unknown recipient registry {} hash {}', @@ -81,8 +82,8 @@ export function handleRequestSubmitted(event: RequestSubmitted): void { log.info('handleRequestSubmitted', []) let recipientRegistryId = event.address.toHexString() - let recipientRegistery = RecipientRegistry.load(recipientRegistryId) - if (!recipientRegistery) { + let recipientRegistry = loadRecipientRegistry(event.address) + if (!recipientRegistry) { log.warning( 'handleRequestSubmitted - ignore unknown recipient registry {} hash {}', [event.address.toHexString(), event.transaction.hash.toHex()] diff --git a/subgraph/src/RecipientRegistry.ts b/subgraph/src/RecipientRegistry.ts new file mode 100644 index 000000000..4d6c687e3 --- /dev/null +++ b/subgraph/src/RecipientRegistry.ts @@ -0,0 +1,56 @@ +import { Address, BigInt } from '@graphprotocol/graph-ts' +import { OptimisticRecipientRegistry as RecipientRegistryContract } from '../generated/OptimisticRecipientRegistry/OptimisticRecipientRegistry' + +import { RecipientRegistry, FundingRoundFactory } from '../generated/schema' +import { OptimisticRecipientRegistry as recipientRegistryTemplate } from '../generated/templates' + +/* + * Load the recipient registry entity from the subgraph with the given address + */ +export function loadRecipientRegistry( + address: Address +): RecipientRegistry | null { + let recipientRegistryId = address.toHexString() + let recipientRegistry = RecipientRegistry.load(recipientRegistryId) + if (!recipientRegistry) { + let recipientRegistryContract = RecipientRegistryContract.bind(address) + let controller = recipientRegistryContract.try_controller() + if (!controller.reverted) { + // Recipient registry's controller must be the factory + let factoryId = controller.value.toHexString() + let factory = FundingRoundFactory.load(factoryId) + if (factory) { + /* This is our registry, create it */ + recipientRegistry = new RecipientRegistry(recipientRegistryId) + recipientRegistryTemplate.create(address) + let baseDeposit = recipientRegistryContract.try_baseDeposit() + if (baseDeposit.reverted) { + recipientRegistry.baseDeposit = BigInt.fromI32(0) + recipientRegistry.challengePeriodDuration = BigInt.fromI32(0) + } else { + recipientRegistry.baseDeposit = baseDeposit.value + let challengePeriodDuration = + recipientRegistryContract.challengePeriodDuration() + recipientRegistry.challengePeriodDuration = challengePeriodDuration + } + let controller = recipientRegistryContract.try_controller() + let maxRecipients = recipientRegistryContract.try_maxRecipients() + let owner = recipientRegistryContract.try_owner() + + if (!controller.reverted) { + recipientRegistry.controller = controller.value + } + if (!maxRecipients.reverted) { + recipientRegistry.maxRecipients = maxRecipients.value + } + if (!owner.reverted) { + recipientRegistry.owner = owner.value + } + recipientRegistry.fundingRoundFactory = factory.id + recipientRegistry.save() + } + } + } + + return recipientRegistry +} From 99ecef18c7e49668ec35495e4bd0ac276232a427 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Tue, 7 Nov 2023 23:43:03 -0500 Subject: [PATCH 2/2] refactor code --- subgraph/src/FundingRoundFactoryMapping.ts | 7 +- subgraph/src/RecipientRegistry.ts | 78 ++++++++++++++-------- vue-app/src/api/projects.ts | 13 ---- vue-app/src/views/RecipientProfile.vue | 6 +- 4 files changed, 59 insertions(+), 45 deletions(-) diff --git a/subgraph/src/FundingRoundFactoryMapping.ts b/subgraph/src/FundingRoundFactoryMapping.ts index 460f0fcf9..6a081c229 100644 --- a/subgraph/src/FundingRoundFactoryMapping.ts +++ b/subgraph/src/FundingRoundFactoryMapping.ts @@ -17,7 +17,7 @@ import { FundingRound as FundingRoundContract } from '../generated/FundingRoundF import { OptimisticRecipientRegistry as RecipientRegistryContract } from '../generated/FundingRoundFactory/OptimisticRecipientRegistry' import { BrightIdUserRegistry as BrightIdUserRegistryContract } from '../generated/FundingRoundFactory/BrightIdUserRegistry' -import { loadRecipientRegistry } from './RecipientRegistry' +import { createRecipientRegistry } from './RecipientRegistry' import { FundingRound as FundingRoundTemplate, @@ -148,7 +148,10 @@ function createOrUpdateFundingRoundFactory( //Check if these registries already exist/are being tracked let recipientRegistryAddress = fundingRoundFactoryContract.recipientRegistry() let recipientRegistryId = recipientRegistryAddress.toHexString() - let recipientRegistry = loadRecipientRegistry(recipientRegistryAddress) + let recipientRegistry = RecipientRegistry.load(recipientRegistryId) + if (!recipientRegistry) { + createRecipientRegistry(fundingRoundFactoryId, recipientRegistryAddress) + } let contributorRegistryAddress = fundingRoundFactoryContract.userRegistry() let contributorRegistryId = contributorRegistryAddress.toHexString() diff --git a/subgraph/src/RecipientRegistry.ts b/subgraph/src/RecipientRegistry.ts index 4d6c687e3..f809b70f6 100644 --- a/subgraph/src/RecipientRegistry.ts +++ b/subgraph/src/RecipientRegistry.ts @@ -2,7 +2,50 @@ import { Address, BigInt } from '@graphprotocol/graph-ts' import { OptimisticRecipientRegistry as RecipientRegistryContract } from '../generated/OptimisticRecipientRegistry/OptimisticRecipientRegistry' import { RecipientRegistry, FundingRoundFactory } from '../generated/schema' -import { OptimisticRecipientRegistry as recipientRegistryTemplate } from '../generated/templates' +import { OptimisticRecipientRegistry as RecipientRegistryTemplate } from '../generated/templates' + +/* + * Create the recipient registry entity + */ +export function createRecipientRegistry( + fundingRoundFactoryId: string, + recipientRegistryAddress: Address +): RecipientRegistry { + let recipientRegistryId = recipientRegistryAddress.toHexString() + let recipientRegistry = new RecipientRegistry(recipientRegistryId) + RecipientRegistryTemplate.create(recipientRegistryAddress) + + let recipientRegistryContract = RecipientRegistryContract.bind( + recipientRegistryAddress + ) + let baseDeposit = recipientRegistryContract.try_baseDeposit() + if (baseDeposit.reverted) { + recipientRegistry.baseDeposit = BigInt.fromI32(0) + recipientRegistry.challengePeriodDuration = BigInt.fromI32(0) + } else { + recipientRegistry.baseDeposit = baseDeposit.value + let challengePeriodDuration = + recipientRegistryContract.challengePeriodDuration() + recipientRegistry.challengePeriodDuration = challengePeriodDuration + } + let controller = recipientRegistryContract.try_controller() + let maxRecipients = recipientRegistryContract.try_maxRecipients() + let owner = recipientRegistryContract.try_owner() + + if (!controller.reverted) { + recipientRegistry.controller = controller.value + } + if (!maxRecipients.reverted) { + recipientRegistry.maxRecipients = maxRecipients.value + } + if (!owner.reverted) { + recipientRegistry.owner = owner.value + } + recipientRegistry.fundingRoundFactory = fundingRoundFactoryId + recipientRegistry.save() + + return recipientRegistry +} /* * Load the recipient registry entity from the subgraph with the given address @@ -21,33 +64,12 @@ export function loadRecipientRegistry( let factory = FundingRoundFactory.load(factoryId) if (factory) { /* This is our registry, create it */ - recipientRegistry = new RecipientRegistry(recipientRegistryId) - recipientRegistryTemplate.create(address) - let baseDeposit = recipientRegistryContract.try_baseDeposit() - if (baseDeposit.reverted) { - recipientRegistry.baseDeposit = BigInt.fromI32(0) - recipientRegistry.challengePeriodDuration = BigInt.fromI32(0) - } else { - recipientRegistry.baseDeposit = baseDeposit.value - let challengePeriodDuration = - recipientRegistryContract.challengePeriodDuration() - recipientRegistry.challengePeriodDuration = challengePeriodDuration - } - let controller = recipientRegistryContract.try_controller() - let maxRecipients = recipientRegistryContract.try_maxRecipients() - let owner = recipientRegistryContract.try_owner() - - if (!controller.reverted) { - recipientRegistry.controller = controller.value - } - if (!maxRecipients.reverted) { - recipientRegistry.maxRecipients = maxRecipients.value - } - if (!owner.reverted) { - recipientRegistry.owner = owner.value - } - recipientRegistry.fundingRoundFactory = factory.id - recipientRegistry.save() + recipientRegistry = createRecipientRegistry(factory.id, address) + + // update factory + factory.recipientRegistry = recipientRegistryId + factory.recipientRegistryAddress = address + factory.save() } } } diff --git a/vue-app/src/api/projects.ts b/vue-app/src/api/projects.ts index 7bcc19164..c1a065bd8 100644 --- a/vue-app/src/api/projects.ts +++ b/vue-app/src/api/projects.ts @@ -61,19 +61,6 @@ export async function getRecipientRegistryAddress(roundAddress: string | null): } } -export async function getCurrentRecipientRegistryAddress(): Promise { - const data = await sdk.GetRecipientRegistryInfo({ - factoryAddress: factory.address.toLowerCase(), - }) - - const registryAddress = - data.fundingRoundFactory?.currentRound?.recipientRegistry?.id || - data.fundingRoundFactory?.recipientRegistry?.id || - '' - - return registryAddress -} - export async function getProjects(registryAddress: string, startTime?: number, endTime?: number): Promise { if (recipientRegistryType === 'simple') { return await SimpleRegistry.getProjects(registryAddress, startTime, endTime) diff --git a/vue-app/src/views/RecipientProfile.vue b/vue-app/src/views/RecipientProfile.vue index 7a5a9ada5..cb2c548eb 100644 --- a/vue-app/src/views/RecipientProfile.vue +++ b/vue-app/src/views/RecipientProfile.vue @@ -72,10 +72,11 @@