diff --git a/src/components/Indexer/processor.ts b/src/components/Indexer/processor.ts index 0fbf6cd14..7dc5d155e 100644 --- a/src/components/Indexer/processor.ts +++ b/src/components/Indexer/processor.ts @@ -41,7 +41,7 @@ import { create256Hash } from '../../utils/crypt.js' import { URLUtils } from '../../utils/url.js' import { makeDid } from '../core/utils/validateDdoHandler.js' import { PolicyServer } from '../policyServer/index.js' -import { getPricesByDt, doesDispenserAlreadyExist } from './utils.js' +import { getPricesByDt, doesDispenserAlreadyExist, doesFreAlreadyExist } from './utils.js' class BaseEventProcessor { protected networkId: number @@ -934,7 +934,8 @@ export class DispenserActivatedEventProcessor extends BaseEventProcessor { stat.prices.push({ type: 'dispenser', price: '0', - contract: event.address + contract: event.address, + token: datatokenAddress }) break } else if (doesDispenserAlreadyExist(event.address, stat.prices)[0]) { @@ -1048,3 +1049,86 @@ export class DispenserDeactivatedEventProcessor extends BaseEventProcessor { } } } + +export class ExchangeActivatedEventProcessor extends BaseEventProcessor { + async processEvent( + event: ethers.Log, + chainId: number, + signer: Signer, + provider: JsonRpcApiProvider + ): Promise { + const decodedEventData = await this.getEventData( + provider, + event.transactionHash, + FixedRateExchange.abi + ) + const exchangeId = ethers.toUtf8Bytes(decodedEventData.args[0].toString()) + const freContract = new ethers.Contract(event.address, FixedRateExchange.abi, signer) + const exchange = await freContract.getExchange(exchangeId) + const datatokenAddress = exchange[1] + const datatokenContract = getDtContract(signer, datatokenAddress) + const nftAddress = await datatokenContract.getERC721Address() + const did = + 'did:op:' + + createHash('sha256') + .update(getAddress(nftAddress) + chainId.toString(10)) + .digest('hex') + try { + const { ddo: ddoDatabase } = await getDatabase() + const ddo = await ddoDatabase.retrieve(did) + if (!ddo) { + INDEXER_LOGGER.logMessage( + `Detected ExchangeActivated changed for ${did}, but it does not exists.` + ) + return + } + if (!ddo.indexedMetadata) { + ddo.indexedMetadata = {} + } + + if (!Array.isArray(ddo.indexedMetadata.stats)) { + ddo.indexedMetadata.stats = [] + } + if (ddo.indexedMetadata.stats.length !== 0) { + for (const stat of ddo.indexedMetadata.stats) { + if ( + stat.datatokenAddress.toLowerCase() === datatokenAddress.toLowerCase() && + !doesFreAlreadyExist(exchangeId, stat.prices)[0] + ) { + stat.prices.push({ + type: 'fixedrate', + price: exchange[5], + contract: event.address, + token: exchange[3], + exchangeId: exchangeId + }) + break + } else if (doesDispenserAlreadyExist(event.address, stat.prices)[0]) { + break + } + } + } else { + INDEXER_LOGGER.logMessage(`[ExchangeActivated] - No stats were found on the ddo`) + const serviceIdToFind = findServiceIdByDatatoken(ddo, datatokenAddress) + if (serviceIdToFind === '') { + INDEXER_LOGGER.logMessage( + `[ExchangeActivated] - This datatoken does not contain this service. Invalid service id!` + ) + return + } + ddo.indexedMetadata.stats.push({ + datatokenAddress: datatokenAddress, + name: await datatokenContract.name(), + serviceId: serviceIdToFind, + orders: 0, + prices: getPricesByDt(datatokenContract, signer) + }) + } + + const savedDDO = this.createOrUpdateDDO(ddo, EVENTS.EXCHANGE_ACTIVATED) + return savedDDO + } catch (err) { + INDEXER_LOGGER.log(LOG_LEVELS_STR.LEVEL_ERROR, `Error retrieving DDO: ${err}`, true) + } + } +} diff --git a/src/components/Indexer/utils.ts b/src/components/Indexer/utils.ts index 492871fdb..66e1bf008 100644 --- a/src/components/Indexer/utils.ts +++ b/src/components/Indexer/utils.ts @@ -395,6 +395,18 @@ export function doesDispenserAlreadyExist( return [false, null] } +export function doesFreAlreadyExist( + exchangeId: ethers.BytesLike, + prices: Price[] +): [boolean, Price?] { + for (const price of prices) { + if (exchangeId === price.exchangeId) { + return [true, price] + } + } + return [false, null] +} + export async function getPricesByDt( datatoken: ethers.Contract, signer: Signer @@ -504,7 +516,8 @@ export async function getPricingStatsForDddo(ddo: any, signer: Signer): Promise< prices: prices.push({ type: 'dispenser', price: '0', - contract: dispenser + contract: dispenser, + token: service.datatokenAddress }) }) } diff --git a/src/utils/constants.ts b/src/utils/constants.ts index f17af24b5..4c2eb6987 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -80,7 +80,9 @@ export const EVENTS = { EXCHANGE_RATE_CHANGED: 'ExchangeRateChanged', DISPENSER_CREATED: 'DispenserCreated', DISPENSER_ACTIVATED: 'DispenserActivated', - DISPENSER_DEACTIVATED: 'DispenserDeactivated' + DISPENSER_DEACTIVATED: 'DispenserDeactivated', + EXCHANGE_ACTIVATED: 'ExchangeActivated', + EXCHANGE_DEACTIVATED: 'ExchangeDeactivated' } export const INDEXER_CRAWLING_EVENTS = {