import { Address } from '@graphprotocol/graph-ts' ; import { TokenManager as TokenManagerContract } from '../generated/templates/TokenManager/TokenManager' ; import { TokenManager as TokenManagerEntity } from '../generated/schema' ; import { OrphanTokenManagers as OrphanTokenManagersEntity } from '../generated/schema' ; import * as aragon from './aragon/aragon' ; export function getTokenManagerId ( proxyAddress ) { return 'proxyAddress-' + proxyAddress . toHexString ( ) ; } export function getTokenManagerEntity ( proxyAddress ) { let tokenManagerId = getTokenManagerId ( proxyAddress ) ; let tokenManagerEntity = TokenManagerEntity . load ( tokenManagerId ) ; if ( ! tokenManagerEntity ) { let tokenManagerEntity = new TokenManagerEntity ( tokenManagerId ) ; let tokenManagerContract = TokenManagerContract . bind ( proxyAddress ) ; tokenManagerEntity . address = proxyAddress ; tokenManagerEntity . orgAddress = tokenManagerContract . kernel ( ) ; let tokenAddress = tokenManagerContract . token ( ) ; if ( tokenAddress . toHexString ( ) != '0x0000000000000000000000000000000000000000' ) { aragon . processToken ( tokenAddress ) ; } else { _registerOrphanTokenManager ( proxyAddress ) ; } tokenManagerEntity . save ( ) ; } return tokenManagerEntity ! ; } export function processOrphanTokenManagers ( ) { let registry = _getOrphanTokenManagersEntity ( ) ; let apps = registry . apps ; if ( apps . length > 0 ) { for ( let i = 0 ; i < apps . length ; i ++ ) { let proxyAddress = apps [ i ] as Address ; let tokenManagerContract = TokenManagerContract . bind ( proxyAddress ) ; let tokenAddress = tokenManagerContract . token ( ) ; if ( tokenAddress . toHexString ( ) != '0x0000000000000000000000000000000000000000' ) { _unregisterOrphanTokenManager ( proxyAddress ) ; aragon . processToken ( tokenAddress ) ; } } } } function _registerOrphanTokenManager ( appAddress ) { let registry = _getOrphanTokenManagersEntity ( ) ; let apps = registry . apps ; if ( ! apps . includes ( appAddress ) ) { apps . push ( appAddress ) ; registry . apps = apps ; registry . save ( ) ; } } function _unregisterOrphanTokenManager ( appAddress ) { let registry = _getOrphanTokenManagersEntity ( ) ; let apps = registry . apps ; if ( apps . includes ( appAddress ) ) { let idx = apps . indexOf ( appAddress ) ; apps . splice ( idx , 1 ) ; registry . apps = apps ; registry . save ( ) ; } } function _getOrphanTokenManagersEntity ( ) { let registryId = 'Singleton_OrphanTokenManagers' ; let registry = OrphanTokenManagersEntity . load ( registryId ) ; if ( ! registry ) { registry = new OrphanTokenManagersEntity ( registryId ) ; registry . apps = [ ] ; registry . save ( ) ; } return registry ! ; }