import { log } from '@graphprotocol/graph-ts' ; import { getChainlinkAggregator , getOrInitPriceOracle , getPriceOracleAsset , } from '../helpers/initializers' ; import { AddrChanged } from '../../generated/ChainlinkENSResolver/ChainlinkENSResolver' ; import { ChainlinkAggregator as ChainlinkAggregatorContract } from '../../generated/templates' ; import { ChainlinkENS } from '../../generated/schema' ; import { IExtendedPriceAggregator } from '../../generated/ChainlinkENSResolver/IExtendedPriceAggregator' ; import { formatUsdEthChainlinkPrice , zeroBI } from '../utils/converters' ; import { genericPriceUpdate , usdEthPriceUpdate } from '../helpers/price-updates' ; import { MOCK_USD_ADDRESS } from '../utils/constants' ; export function handleAddressesChanged ( event ) { let priceSource = event . params . a ; let node = event . params . node . toHexString ( ) ; let ens = ChainlinkENS . load ( node ) ; if ( ens ) { ens . aggregatorAddress = priceSource ; ens . save ( ) ; let oracleAssetAddress = ens . underlyingAddress ; let assetOracle = getPriceOracleAsset ( oracleAssetAddress . toHexString ( ) ) ; assetOracle . priceSource = priceSource ; let priceAggregatorInstance = IExtendedPriceAggregator . bind ( priceSource ) ; let latestAnswerCall = priceAggregatorInstance . try_latestAnswer ( ) ; if ( ! latestAnswerCall . reverted && latestAnswerCall . value . gt ( zeroBI ( ) ) ) { genericPriceUpdate ( assetOracle , latestAnswerCall . value , event ) ; if ( oracleAssetAddress . toHexString ( ) == MOCK_USD_ADDRESS ) { let priceOracle = getOrInitPriceOracle ( ) ; priceOracle . usdPriceEthFallbackRequired = assetOracle . isFallbackRequired ; priceOracle . usdPriceEthMainSource = priceSource ; usdEthPriceUpdate ( priceOracle , formatUsdEthChainlinkPrice ( latestAnswerCall . value ) , event ) ; } } else { log . error ( `Latest answer call failed on aggregator:: {} | for node:: {}` , [ priceSource . toHexString ( ) , node , ] ) ; return ; } ChainlinkAggregatorContract . create ( priceSource ) ; let chainlinkAggregator = getChainlinkAggregator ( priceSource . toHexString ( ) ) ; chainlinkAggregator . oracleAsset = oracleAssetAddress . toHexString ( ) ; chainlinkAggregator . save ( ) ; log . warning ( 'ENS updated: {}' , [ node ] ) ; } else { } }