Skip to content

Commit

Permalink
create recipient registry entity and template by registry type
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Mar 9, 2022
1 parent cc41872 commit 031b394
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 27 deletions.
38 changes: 11 additions & 27 deletions subgraph/src/FundingRoundFactoryMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import {

import { MACIFactory as MACIFactoryContract } from '../generated/FundingRoundFactory/MACIFactory'
import { FundingRound as FundingRoundContract } from '../generated/FundingRoundFactory/FundingRound'

import { OptimisticRecipientRegistry as RecipientRegistryContract } from '../generated/FundingRoundFactory/OptimisticRecipientRegistry'

import { RecipientRegistryTemplate } from './recipientRegistry/RecipientRegistryTemplate'
import { RecipientRegistryFactory } from './recipientRegistry/RecipientRegistryFactory'
import {
FundingRound as FundingRoundTemplate,
OptimisticRecipientRegistry as recipientRegistryTemplate,
MACI as MACITemplate,
} from '../generated/templates'
import {
Expand All @@ -27,6 +25,7 @@ import {
ContributorRegistry,
} from '../generated/schema'


export function handleCoordinatorChanged(event: CoordinatorChanged): void {
log.info('handleCoordinatorChanged', [])
}
Expand Down Expand Up @@ -118,29 +117,14 @@ export function handleRoundStarted(event: RoundStarted): void {
//NOTE: If the contracts aren't being tracked initialize them
if (recipientRegistry == null) {
log.info('New recipientRegistry', [])
recipientRegistryTemplate.create(recipientRegistryAddress)
let recipientRegistryContract = RecipientRegistryContract.bind(
recipientRegistryAddress
)
let baseDeposit = recipientRegistryContract.try_baseDeposit()
let challengePeriodDuration =
recipientRegistryContract.try_challengePeriodDuration()
let controller = recipientRegistryContract.controller()
let maxRecipients = recipientRegistryContract.maxRecipients()
let owner = recipientRegistryContract.owner()

let recipientRegistry = new RecipientRegistry(recipientRegistryId)

if( !baseDeposit.reverted ) {
recipientRegistry.baseDeposit = baseDeposit.value
}
if( !challengePeriodDuration.reverted ) {
recipientRegistry.challengePeriodDuration = challengePeriodDuration.value
}
recipientRegistry.controller = controller
recipientRegistry.maxRecipients = maxRecipients
recipientRegistry.owner = owner
recipientRegistry.fundingRoundFactory = fundingRoundFactoryId
RecipientRegistryTemplate.create(recipientRegistryAddress)

let createdAt = event.block.timestamp.toString()
let recipientRegistry = RecipientRegistryFactory.create({
recipientRegistryAddress,
fundingRoundFactoryId,
createdAt
})
recipientRegistry.save()
}

Expand Down
76 changes: 76 additions & 0 deletions subgraph/src/recipientRegistry/RecipientRegistryFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Address } from '@graphprotocol/graph-ts'
import { OptimisticRecipientRegistry as OptimisticRecipientRegistryContract} from '../../generated/FundingRoundFactory/OptimisticRecipientRegistry'
import { SimpleRecipientRegistry as SimpleRecipientRegistryContract } from '../../generated/FundingRoundFactory/SimpleRecipientRegistry'
import { KlerosRecipientRegistry as KlerosRecipientRegistryContract } from '../../generated/FundingRoundFactory/KlerosRecipientRegistry'

import { RecipientRegistry } from '../../generated/schema'
import { RecipientRegistryType, getRecipientRegistryType } from './RecipientRegistryType'

export class RecipientRegistryCreateParams {
recipientRegistryAddress: Address
fundingRoundFactoryId: string
createdAt: string
}

class RecipientRegistryFactoryOptimistic {
static create(params: RecipientRegistryCreateParams): RecipientRegistry {
let recipientRegistryId = params.recipientRegistryAddress.toHexString()
let recipientRegistry = new RecipientRegistry(recipientRegistryId)

let optimisticRegistry = OptimisticRecipientRegistryContract.bind(params.recipientRegistryAddress)
recipientRegistry.baseDeposit = optimisticRegistry.baseDeposit()
recipientRegistry.challengePeriodDuration = optimisticRegistry.challengePeriodDuration()
recipientRegistry.owner = optimisticRegistry.owner()

recipientRegistry.controller = optimisticRegistry.controller()
recipientRegistry.maxRecipients = optimisticRegistry.maxRecipients()
recipientRegistry.fundingRoundFactory = params.fundingRoundFactoryId
recipientRegistry.createdAt = params.createdAt

return recipientRegistry
}
}

class RecipientRegistryFactorySimple {
static create(params: RecipientRegistryCreateParams): RecipientRegistry {
let recipientRegistryId = params.recipientRegistryAddress.toHexString()
let recipientRegistry = new RecipientRegistry(recipientRegistryId)

let simpleRegistry = SimpleRecipientRegistryContract.bind(params.recipientRegistryAddress)
recipientRegistry.owner = simpleRegistry.owner()
recipientRegistry.controller = simpleRegistry.controller()
recipientRegistry.maxRecipients = simpleRegistry.maxRecipients()
recipientRegistry.fundingRoundFactory = params.fundingRoundFactoryId
recipientRegistry.createdAt = params.createdAt

return recipientRegistry
}
}

class RecipientRegistryFactoryKleros {
static create(params: RecipientRegistryCreateParams): RecipientRegistry {
let recipientRegistryId = params.recipientRegistryAddress.toHexString()
let recipientRegistry = new RecipientRegistry(recipientRegistryId)

let klerosRegistry = KlerosRecipientRegistryContract.bind(params.recipientRegistryAddress)
recipientRegistry.controller = klerosRegistry.controller()
recipientRegistry.maxRecipients = klerosRegistry.maxRecipients()
recipientRegistry.fundingRoundFactory = params.fundingRoundFactoryId
recipientRegistry.createdAt = params.createdAt

return recipientRegistry
}
}

export class RecipientRegistryFactory {
static create(params: RecipientRegistryCreateParams): RecipientRegistry {
let type = getRecipientRegistryType(params.recipientRegistryAddress)
if( type == RecipientRegistryType.Optimistic ) {
return RecipientRegistryFactoryOptimistic.create(params)
} else if ( type == RecipientRegistryType.Kleros ) {
return RecipientRegistryFactoryKleros.create(params)
} else {
return RecipientRegistryFactorySimple.create(params)
}
}
}
24 changes: 24 additions & 0 deletions subgraph/src/recipientRegistry/RecipientRegistryTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { RecipientRegistryType, getRecipientRegistryType } from "./RecipientRegistryType";
import { Address } from '@graphprotocol/graph-ts'
import {
OptimisticRecipientRegistry as OptimisticRecipientRegistryTemplate,
SimpleRecipientRegistry as SimpleRecipientRegistryTemplate,
KlerosRecipientRegistry as KlerosRecipientRegistryTemplate,
} from '../../generated/templates'


export class RecipientRegistryTemplate {
static create(registryAddress: Address): void {
let type = getRecipientRegistryType(registryAddress)
switch( type ) {
case RecipientRegistryType.Kleros:
KlerosRecipientRegistryTemplate.create(registryAddress)
break;
case RecipientRegistryType.Optimistic:
OptimisticRecipientRegistryTemplate.create(registryAddress)
break;
default:
SimpleRecipientRegistryTemplate.create(registryAddress)
}
}
}
34 changes: 34 additions & 0 deletions subgraph/src/recipientRegistry/RecipientRegistryType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Address } from '@graphprotocol/graph-ts'
import { OptimisticRecipientRegistry as OptimisticRecipientRegistryContract} from '../../generated/FundingRoundFactory/OptimisticRecipientRegistry'
import { KlerosRecipientRegistry as KlerosRecipientRegistryContract } from '../../generated/FundingRoundFactory/KlerosRecipientRegistry'

export enum RecipientRegistryType {
Simple = 0,
Kleros,
Optimistic,
}

/**
* Determine the type of the registry given the registry address
* Currently, we check for availability of a method to determine the type
*
* TODO: future recipient registry contract should have a type property
*
* @param registryAddress the recipient registry address
* @returns RecipientRegistryType of Simple, Kleros, Optimistic
*/
export function getRecipientRegistryType(registryAddress: Address): RecipientRegistryType {
let klerosRegistry = KlerosRecipientRegistryContract.bind(registryAddress)
let tcr = klerosRegistry.try_tcr()
if( !tcr.reverted ) {
return RecipientRegistryType.Kleros
}

let optimisticRegistry = OptimisticRecipientRegistryContract.bind(registryAddress)
let challengePeriodDuration = optimisticRegistry.try_challengePeriodDuration()
if( !challengePeriodDuration.reverted ) {
return RecipientRegistryType.Optimistic
}

return RecipientRegistryType.Simple
}
4 changes: 4 additions & 0 deletions subgraph/subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ dataSources:
file: ./abis/MACIFactory.json
- name: OptimisticRecipientRegistry
file: ./abis/OptimisticRecipientRegistry.json
- name: SimpleRecipientRegistry
file: ./abis/SimpleRecipientRegistry.json
- name: KlerosRecipientRegistry
file: ./abis/KlerosRecipientRegistry.json
- name: BrightIdUserRegistry
file: ./abis/BrightIdUserRegistry.json
eventHandlers:
Expand Down

0 comments on commit 031b394

Please sign in to comment.