import { BigInt , ethereum , Value , Address , log } from '@graphprotocol/graph-ts' ; import { LendingPoolUpdated , ConfigurationAdminUpdated , LendingPoolConfiguratorUpdated , LendingPoolCollateralManagerUpdated , AddressSet , LendingRateOracleUpdated , PriceOracleUpdated , ProxyCreated , EmergencyAdminUpdated , } from '../../generated/templates/LendingPoolAddressesProvider/LendingPoolAddressesProvider' ; import { LendingPool as LendingPoolContract , LendingPoolConfigurator as LendingPoolConfiguratorContract , } from '../../generated/templates' ; import { createMapContractToPool , getOrInitPriceOracle } from '../helpers/initializers' ; import { Pool , PoolConfigurationHistoryItem } from '../../generated/schema' ; import { EventTypeRef , getHistoryId } from '../utils/id-generation' ; let POOL_COMPONENTS = [ 'lendingPoolConfigurator' , 'lendingPoolConfiguratorImpl' , 'lendingPool' , 'lendingPoolImpl' , 'configurationAdmin' , 'proxyPriceProvider' , 'lendingRateOracle' , 'lendingPoolCollateralManager' , 'emergencyAdmin' , 'ethereumAddress' , ] as string [ ] ; function saveAddressProvider ( lendingPool , timestamp , event ) { lendingPool . lastUpdateTimestamp = timestamp . toI32 ( ) ; lendingPool . save ( ) ; let configurationHistoryItem = new PoolConfigurationHistoryItem ( getHistoryId ( event , EventTypeRef . NoType ) ) ; for ( let i = 0 ; i < POOL_COMPONENTS . length ; i ++ ) { let param = POOL_COMPONENTS [ i ] ; let value = lendingPool . get ( param ) ; if ( ! value ) { return ; } configurationHistoryItem . set ( param , value as Value ) ; } configurationHistoryItem . timestamp = timestamp . toI32 ( ) ; configurationHistoryItem . pool = lendingPool . id ; configurationHistoryItem . save ( ) ; } function genericAddressProviderUpdate ( component , newAddress , event , createMapContract = true ) { if ( ! POOL_COMPONENTS . includes ( component ) ) { throw new Error ( 'wrong pool component name' + component ) ; } let poolAddress = event . address . toHexString ( ) ; let lendingPool = Pool . load ( poolAddress ) ; if ( lendingPool == null ) { log . error ( 'pool {} is not registered!' , [ poolAddress ] ) ; throw new Error ( 'pool' + poolAddress + 'is not registered!' ) ; } lendingPool . set ( component , Value . fromAddress ( newAddress ) ) ; if ( createMapContract ) { createMapContractToPool ( newAddress , lendingPool . id ) ; } saveAddressProvider ( lendingPool as Pool , event . block . timestamp , event ) ; } export function handleProxyCreated ( event ) { let newProxyAddress = event . params . newAddress ; let contactId = event . params . id . toString ( ) ; let poolComponent ; if ( contactId == 'LENDING_POOL_CONFIGURATOR' ) { poolComponent = 'lendingPoolConfigurator' ; LendingPoolConfiguratorContract . create ( newProxyAddress ) ; } else if ( contactId == 'LENDING_POOL' ) { poolComponent = 'lendingPool' ; LendingPoolContract . create ( newProxyAddress ) ; } else { return ; } genericAddressProviderUpdate ( poolComponent , newProxyAddress , event ) ; } export function handleAddressSet ( event ) { let mappedId = '' ; if ( event . params . id . toString ( ) == 'LENDING_POOL' ) { mappedId = 'lendingPool' ; } else if ( event . params . id . toString ( ) == 'LENDING_POOL_CONFIGURATOR' ) { mappedId = 'lendingPoolConfigurator' ; } else if ( event . params . id . toString ( ) == 'POOL_ADMIN' ) { mappedId = 'configurationAdmin' ; } else if ( event . params . id . toString ( ) == 'EMERGENCY_ADMIN' ) { mappedId = 'emergencyAdmin' ; } else if ( event . params . id . toString ( ) == 'COLLATERAL_MANAGER' ) { mappedId = 'lendingPoolCollateralManager' ; } else if ( event . params . id . toString ( ) == 'PRICE_ORACLE' ) { mappedId = 'proxyPriceProvider' ; } else if ( event . params . id . toString ( ) == 'LENDING_RATE_ORACLE' ) { mappedId = 'lendingRateOracle' ; } if ( mappedId != '' ) { genericAddressProviderUpdate ( mappedId , event . params . newAddress , event , false ) ; } else { log . error ( 'Address set: {} | Contract ID: {}' , [ event . params . newAddress . toHexString ( ) , event . params . id . toString ( ) , ] ) ; } } export function handlePriceOracleUpdated ( event ) { genericAddressProviderUpdate ( 'proxyPriceProvider' , event . params . newAddress , event , false ) ; let priceOracle = getOrInitPriceOracle ( ) ; priceOracle . proxyPriceProvider = event . params . newAddress ; priceOracle . save ( ) ; } export function handleLendingRateOracleUpdated ( event ) { genericAddressProviderUpdate ( 'lendingRateOracle' , event . params . newAddress , event , false ) ; } export function handleLendingPoolUpdated ( event ) { genericAddressProviderUpdate ( 'lendingPoolImpl' , event . params . newAddress , event , false ) ; } export function handleConfigurationAdminUpdated ( event ) { genericAddressProviderUpdate ( 'configurationAdmin' , event . params . newAddress , event , false ) ; } export function handleLendingPoolConfiguratorUpdated ( event ) { genericAddressProviderUpdate ( 'lendingPoolConfiguratorImpl' , event . params . newAddress , event , false ) ; } export function handleLendingPoolCollateralManagerUpdated ( event ) { genericAddressProviderUpdate ( 'lendingPoolCollateralManager' , event . params . newAddress , event , false ) ; } export function handleEmergencyAdminUpdated ( event ) { genericAddressProviderUpdate ( 'emergencyAdmin' , event . params . newAddress , event , false ) ; }