import { Address , BigInt , Bytes , log } from '@graphprotocol/graph-ts' ; import { Repo as RepoEntity , Version as VersionEntity , } from '../../generated/schema' ; import { NewVersion as NewVersionEvent , Repo as RepoContract , } from '../../generated/templates/Repo/Repo' ; import { ZERO_ADDR } from '../helpers/constants' ; export function handleNewVersion ( event ) { const repoAddress = event . address ; const repo = RepoEntity . load ( repoAddress . toHexString ( ) ) ; if ( repo !== null ) { const semanticVersion = event . params . semanticVersion . toString ( ) ; const version = loadOrCreateVersion ( repoAddress , semanticVersion , event . params . versionId ) ; const currentVersions = repo . versions ; currentVersions . push ( version . id ) ; repo . versions = currentVersions ; repo . save ( ) ; version . save ( ) ; } } function buildVersionId ( repoId , semanticVersion ) { return repoId . concat ( '-' ) . concat ( semanticVersion ) ; } function loadOrCreateVersion ( repo , semanticVersion , versionContractId ) { const versionId = buildVersionId ( repo . toHexString ( ) , semanticVersion ) ; let version = VersionEntity . load ( versionId ) ; if ( version === null ) { version = new VersionEntity ( versionId ) ; version . semanticVersion = semanticVersion ; version . codeAddress = Bytes . fromHexString ( ZERO_ADDR ) as Bytes ; version . contentUri = '' ; const repoContract = RepoContract . bind ( repo ) ; const callVersionResult = repoContract . try_getByVersionId ( versionContractId ) ; if ( callVersionResult . reverted ) { log . info ( 'get repo version by id reverted' , [ ] ) ; } else { const versionData = callVersionResult . value ; version . codeAddress = versionData . value1 ; version . contentUri = versionData . value2 . toString ( ) ; } } return version ! ; } function buildVersionId ( repoId , semanticVersion ) { return repoId . concat ( '-' ) . concat ( semanticVersion ) ; } function loadOrCreateVersion ( repo , semanticVersion , versionContractId ) { const versionId = buildVersionId ( repo . toHexString ( ) , semanticVersion ) ; let version = VersionEntity . load ( versionId ) ; if ( version === null ) { version = new VersionEntity ( versionId ) ; version . semanticVersion = semanticVersion ; version . codeAddress = Bytes . fromHexString ( ZERO_ADDR ) as Bytes ; version . contentUri = '' ; const repoContract = RepoContract . bind ( repo ) ; const callVersionResult = repoContract . try_getByVersionId ( versionContractId ) ; if ( callVersionResult . reverted ) { log . info ( 'get repo version by id reverted' , [ ] ) ; } else { const versionData = callVersionResult . value ; version . codeAddress = versionData . value1 ; version . contentUri = versionData . value2 . toString ( ) ; } } return version ! ; }