diff --git a/subgraph/src/OptimisticRecipientRegistryMapping.ts b/subgraph/src/OptimisticRecipientRegistryMapping.ts index dc7c5280b..201c7f2f2 100644 --- a/subgraph/src/OptimisticRecipientRegistryMapping.ts +++ b/subgraph/src/OptimisticRecipientRegistryMapping.ts @@ -5,7 +5,7 @@ import { RequestSubmitted, } from '../generated/OptimisticRecipientRegistry/OptimisticRecipientRegistry' -import { Recipient } from '../generated/schema' +import { Recipient, RecipientRegistry } from '../generated/schema' // It is also possible to access smart contracts from mappings. For // example, the contract that has emitted the event can be connected to @@ -31,9 +31,18 @@ export function handleOwnershipTransferred(event: OwnershipTransferred): void { } export function handleRequestResolved(event: RequestResolved): void { + log.info('handleRequestResolved', []) + let recipientRegistryId = event.address.toHexString() + let recipientRegistry = RecipientRegistry.load(recipientRegistryId) + if (!recipientRegistry) { + log.warning( + 'handleRequestResolved - ignore unknown recipient registry {} hash {}', + [event.address.toHexString(), event.transaction.hash.toHex()] + ) + return + } - log.info('handleRequestResolved', []) let recipientId = event.params._recipientId.toHexString() let recipient = new Recipient(recipientId) @@ -72,7 +81,14 @@ export function handleRequestSubmitted(event: RequestSubmitted): void { log.info('handleRequestSubmitted', []) let recipientRegistryId = event.address.toHexString() - //TODO: create RecipientRegistry entity here if it does not exist. + let recipientRegistery = RecipientRegistry.load(recipientRegistryId) + if (!recipientRegistery) { + log.warning( + 'handleRequestSubmitted - ignore unknown recipient registry {} hash {}', + [event.address.toHexString(), event.transaction.hash.toHex()] + ) + return + } let recipientId = event.params._recipientId.toHexString() let recipient = new Recipient(recipientId) diff --git a/vue-app/src/api/projects.ts b/vue-app/src/api/projects.ts index c6a85de33..7bcc19164 100644 --- a/vue-app/src/api/projects.ts +++ b/vue-app/src/api/projects.ts @@ -1,6 +1,6 @@ import { BigNumber, Contract, Signer } from 'ethers' import type { TransactionResponse } from '@ethersproject/abstract-provider' -import { FundingRound } from './abi' +import { FundingRound, OptimisticRecipientRegistry } from './abi' import { factory, provider, recipientRegistryType, ipfsGatewayUrl } from './core' import SimpleRegistry from './recipient-registry-simple' @@ -9,6 +9,7 @@ import KlerosRegistry from './recipient-registry-kleros' import sdk from '@/graphql/sdk' import { getLeaderboardData } from '@/api/leaderboard' import type { RecipientApplicationData } from '@/api/types' +import { getEventArg } from '@/utils/contracts' export interface LeaderboardProject { id: string // Address or another ID depending on registry implementation @@ -164,28 +165,28 @@ export async function getProjectByIndex( } /** - * Check if the recipient with the submission hash exists in the subgraph + * Return the recipientId for the given transaction hash * @param transactionHash recipient submission hash - * @returns true if recipients with the submission hash was found + * @returns recipientId or null for not found */ -export async function recipientExists(transactionHash: string): Promise { - const data = await sdk.GetRecipientBySubmitHash({ transactionHash }) - return data.recipients.length > 0 -} - -/** - * Return the recipient for the given submission hash - * @param transactionHash recipient submission hash - * @returns project or null for not found - */ -export async function getRecipientBySubmitHash(transactionHash: string): Promise { +export async function getRecipientIdByHash(transactionHash: string): Promise { try { - const data = await sdk.GetRecipientBySubmitHash({ transactionHash }) - const exists = data.recipients.length > 0 - return exists ? OptimisticRegistry.decodeProject(data.recipients[0]) : null + const receipt = await provider.getTransactionReceipt(transactionHash) + + // should only have 1 event, just in case, return the first matching event + for (const log of receipt.logs) { + const registry = new Contract(log.address, OptimisticRecipientRegistry, provider) + try { + const recipientId = getEventArg(receipt, registry, 'RequestSubmitted', '_recipientId') + return recipientId + } catch { + // try next log + } + } } catch { return null } + return null } export function toLeaderboardProject(project: any): LeaderboardProject { diff --git a/vue-app/src/api/subgraph.ts b/vue-app/src/api/subgraph.ts new file mode 100644 index 000000000..db01532a2 --- /dev/null +++ b/vue-app/src/api/subgraph.ts @@ -0,0 +1,17 @@ +import type { TransactionReceipt } from '@ethersproject/abstract-provider' +import sdk from '@/graphql/sdk' + +/** + * Check if the transaction in the receipt exists in the subgraph + * @param receipt transaction receipt + * @returns true if the latest block number in subgraph is greater than the transaction block number + */ +export async function isTransactionInSubgraph(receipt: TransactionReceipt): Promise { + const data = await sdk.GetLatestBlockNumber() + + if (!data._meta?.block.number) { + return false + } + + return data._meta.block.number > receipt.blockNumber +} diff --git a/vue-app/src/utils/contracts.ts b/vue-app/src/utils/contracts.ts index e7cfeb67b..92a71c594 100644 --- a/vue-app/src/utils/contracts.ts +++ b/vue-app/src/utils/contracts.ts @@ -43,14 +43,14 @@ export async function waitForTransaction( */ export async function waitForTransactionAndCheck( pendingTransaction: Promise, - checkFn: (hash: string) => Promise, + checkFn: (receipt: TransactionReceipt) => Promise, onTransactionHash?: (hash: string) => void, ): Promise { const receipt = await waitForTransaction(pendingTransaction, onTransactionHash) return new Promise(resolve => { async function checkAndWait(depth = 0) { - if (await checkFn(receipt.transactionHash)) { + if (await checkFn(receipt)) { resolve(receipt) } else { if (depth > MAX_WAIT_DEPTH) { diff --git a/vue-app/src/views/JoinView.vue b/vue-app/src/views/JoinView.vue index 868187de2..57169cd16 100644 --- a/vue-app/src/views/JoinView.vue +++ b/vue-app/src/views/JoinView.vue @@ -717,7 +717,8 @@ import { useVuelidate } from '@vuelidate/core' import { required, requiredIf, email, maxLength, url, helpers } from '@vuelidate/validators' import type { RecipientApplicationData } from '@/api/types' import type { Project } from '@/api/projects' -import { recipientExists, formToProjectInterface } from '@/api/projects' +import { isTransactionInSubgraph } from '@/api/subgraph' +import { formToProjectInterface } from '@/api/projects' import { chain, showComplianceRequirement, isOptimisticRecipientRegistry } from '@/api/core' import { DateTime } from 'luxon' import { useRecipientStore, useAppStore, useUserStore } from '@/stores' @@ -948,8 +949,8 @@ async function addRecipient() { recipientRegistryInfo.value.deposit, currentUser.value.walletProvider.getSigner(), ), - hash => { - return isOptimisticRecipientRegistry ? recipientExists(hash) : Promise.resolve(true) + receipt => { + return isOptimisticRecipientRegistry ? isTransactionInSubgraph(receipt) : Promise.resolve(true) }, hash => (txHash.value = hash), ) diff --git a/vue-app/src/views/ProjectAdded.vue b/vue-app/src/views/ProjectAdded.vue index abfbbf3ce..59fe6290c 100644 --- a/vue-app/src/views/ProjectAdded.vue +++ b/vue-app/src/views/ProjectAdded.vue @@ -49,7 +49,7 @@ import { useAppStore } from '@/stores' import { useRoute } from 'vue-router' import { storeToRefs } from 'pinia' import { isOptimisticRecipientRegistry } from '@/api/core' -import { getRecipientBySubmitHash } from '@/api/projects' +import { getRecipientIdByHash } from '@/api/projects' const route = useRoute() const appStore = useAppStore() @@ -59,9 +59,9 @@ const hash = computed(() => route.params.hash as string) const recipientId = ref('') onMounted(async () => { - const recipient = await getRecipientBySubmitHash(hash.value) - if (recipient) { - recipientId.value = recipient.id + const id = await getRecipientIdByHash(hash.value) + if (id) { + recipientId.value = id } })