Skip to content

Commit

Permalink
get recipientId from transaction hash instead of query the subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Jul 28, 2023
1 parent 8479cf9 commit f4873a0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 29 deletions.
22 changes: 19 additions & 3 deletions subgraph/src/OptimisticRecipientRegistryMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
35 changes: 18 additions & 17 deletions vue-app/src/api/projects.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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<boolean> {
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<Project | null> {
export async function getRecipientIdByHash(transactionHash: string): Promise<string | null> {
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 {
Expand Down
17 changes: 17 additions & 0 deletions vue-app/src/api/subgraph.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
const data = await sdk.GetLatestBlockNumber()

if (!data._meta?.block.number) {
return false
}

return data._meta.block.number > receipt.blockNumber
}
4 changes: 2 additions & 2 deletions vue-app/src/utils/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ export async function waitForTransaction(
*/
export async function waitForTransactionAndCheck(
pendingTransaction: Promise<TransactionResponse>,
checkFn: (hash: string) => Promise<boolean>,
checkFn: (receipt: TransactionReceipt) => Promise<boolean>,
onTransactionHash?: (hash: string) => void,
): Promise<TransactionReceipt> {
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) {
Expand Down
7 changes: 4 additions & 3 deletions vue-app/src/views/JoinView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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),
)
Expand Down
8 changes: 4 additions & 4 deletions vue-app/src/views/ProjectAdded.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
})
</script>
Expand Down

0 comments on commit f4873a0

Please sign in to comment.