import { Address , Bytes } from '@graphprotocol/graph-ts' ; import { Registry as RegistryEntity , Repo as RepoEntity , } from '../../generated/schema' ; import { Repo as RepoTemplate } from '../../generated/templates' ; import { NewRepo as NewRepoEvent } from '../../generated/templates/Registry/APMRegistry' ; import { APM_REGISTRY_NODE , OPEN_REGISTRY_NODE , HATCH_REGISTRY_NODE , HIVE_REGISTRY_NODE , } from '../helpers/constants' ; export function handleNewRepo ( event ) { const registryAddress = event . address ; const registry = loadOrCreateRegistry ( registryAddress , Bytes . fromUTF8 ( '0' ) as Bytes ) ; const repoAddress = event . params . repo ; const repo = loadOrCreateRepo ( repoAddress , event . params . name , event . params . id ) ; const currentRepos = registry . repos ; currentRepos . push ( repo . id ) ; registry . repos = currentRepos ; registry . repoCount = registry . repoCount + 1 ; registry . save ( ) ; repo . save ( ) ; RepoTemplate . create ( repoAddress ) ; } export function loadOrCreateRegistry ( registryAddress , node ) { const registryId = registryAddress . toHexString ( ) ; let registry = RegistryEntity . load ( registryId ) ; if ( registry === null ) { registry = new RegistryEntity ( registryId ) ; let name = '' ; if ( node . toHex ( ) == APM_REGISTRY_NODE ) { name = 'aragonpm.eth' ; } else if ( node . toHex ( ) == OPEN_REGISTRY_NODE ) { name = 'open.aragonpm.eth' ; } else if ( node . toHex ( ) == HATCH_REGISTRY_NODE ) { name = 'hatch.aragonpm.eth' ; } else if ( node . toHex ( ) == HIVE_REGISTRY_NODE ) { name = '1hive.aragonpm.eth' ; } registry . address = registryAddress ; registry . name = name ; registry . node = node ; registry . repoCount = 0 ; registry . repos = [ ] ; } return registry ! ; } export function loadOrCreateRepo ( repoAddress , name , node ) { const repoId = repoAddress . toHexString ( ) ; let repo = RepoEntity . load ( repoId ) ; if ( repo === null ) { repo = new RepoEntity ( repoId ) ; repo . address = repoAddress ; repo . name = name ; repo . node = node ; repo . versions = [ ] ; repo . appCount = 0 ; } return repo ! ; }