Skip to content

Commit

Permalink
Instantiate processors.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariacarmina committed Dec 17, 2024
1 parent 716ddd1 commit e15905a
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/components/Indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ export class OceanIndexer {
EVENTS.ORDER_STARTED,
EVENTS.ORDER_REUSED,
EVENTS.DISPENSER_ACTIVATED,
EVENTS.DISPENSER_DEACTIVATED
EVENTS.DISPENSER_DEACTIVATED,
EVENTS.EXCHANGE_ACTIVATED,
EVENTS.EXCHANGE_DEACTIVATED
].includes(event.method)
) {
// will emit the metadata created/updated event and advertise it to the other peers (on create only)
Expand Down
90 changes: 88 additions & 2 deletions src/components/Indexer/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import {
findServiceIdByDatatoken,
getDtContract,
getPricingStatsForDddo,
wasNFTDeployedByOurFactory
wasNFTDeployedByOurFactory,
getPricesByDt,
doesDispenserAlreadyExist,
doesFreAlreadyExist
} from './utils.js'
import { INDEXER_LOGGER } from '../../utils/logging/common.js'
import { Purgatory } from './purgatory.js'
Expand All @@ -41,7 +44,6 @@ 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, doesFreAlreadyExist } from './utils.js'
class BaseEventProcessor {
protected networkId: number

Expand Down Expand Up @@ -1132,3 +1134,87 @@ export class ExchangeActivatedEventProcessor extends BaseEventProcessor {
}
}
}

export class ExchangeDeactivatedEventProcessor extends BaseEventProcessor {
async processEvent(
event: ethers.Log,
chainId: number,
signer: Signer,
provider: JsonRpcApiProvider
): Promise<any> {
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 ExchangeDeactivated 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]
) {
const price = doesFreAlreadyExist(exchangeId, stat.prices)[1]
const index = stat.prices.indexOf(price)
stat.prices.splice(index, 1)
break
} else if (!doesFreAlreadyExist(exchangeId, stat.prices)[0]) {
INDEXER_LOGGER.logMessage(
`Detected ExchangeDeactivated changed for ${event.address}, but exchange ${exchangeId} does not exist in the DDO pricing.`
)
break
}
}
} else {
INDEXER_LOGGER.logMessage(
`[ExchangeDeactivated] - No stats were found on the ddo`
)
const serviceIdToFind = findServiceIdByDatatoken(ddo, datatokenAddress)
if (serviceIdToFind === '') {
INDEXER_LOGGER.logMessage(
`[ExchangeDeactivated] - This datatoken does not contain this service. Invalid service id!`
)
return
}
ddo.indexedMetadata.stats.push({
datatokenAddress: datatokenAddress,

Check warning on line 1206 in src/components/Indexer/processor.ts

View workflow job for this annotation

GitHub Actions / lint

Expected property shorthand
name: await datatokenContract.name(),
serviceId: serviceIdToFind,
orders: 0,
prices: getPricesByDt(datatokenContract, signer)
})
}

const savedDDO = this.createOrUpdateDDO(ddo, EVENTS.EXCHANGE_DEACTIVATED)
return savedDDO
} catch (err) {
INDEXER_LOGGER.log(LOG_LEVELS_STR.LEVEL_ERROR, `Error retrieving DDO: ${err}`, true)
}
}
}
41 changes: 40 additions & 1 deletion src/components/Indexer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
MetadataEventProcessor,
MetadataStateEventProcessor,
OrderReusedEventProcessor,
OrderStartedEventProcessor
OrderStartedEventProcessor,
ExchangeActivatedEventProcessor,
ExchangeDeactivatedEventProcessor
} from './processor.js'
import { INDEXER_LOGGER } from '../../utils/logging/common.js'
import { fetchEventFromTransaction } from '../../utils/util.js'
Expand All @@ -34,6 +36,9 @@ let orderReusedEventProcessor: OrderReusedEventProcessor
let orderStartedEventProcessor: OrderStartedEventProcessor
let dispenserActivatedEventProcessor: DispenserActivatedEventProcessor
let dispenserDeactivatedEventProcessor: DispenserDeactivatedEventProcessor
let exchangeActivatedEventProcessor: ExchangeActivatedEventProcessor
let exchangeDeactivatedEventProcessor: ExchangeDeactivatedEventProcessor


Check failure on line 42 in src/components/Indexer/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
function getMetadataEventProcessor(chainId: number): MetadataEventProcessor {
if (!metadataEventProccessor) {
Expand Down Expand Up @@ -81,6 +86,24 @@ function getDispenserDeactivatedEventProcessor(
return dispenserDeactivatedEventProcessor
}

function getExchangeActivatedEventProcessor(
chainId: number
): ExchangeActivatedEventProcessor {
if(!exchangeActivatedEventProcessor) {

Check failure on line 92 in src/components/Indexer/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
exchangeActivatedEventProcessor = new ExchangeActivatedEventProcessor(chainId)
}
return exchangeActivatedEventProcessor
}

function getExchangeDeactivatedEventProcessor(
chainId: number
): ExchangeDeactivatedEventProcessor {
if(!exchangeDeactivatedEventProcessor) {

Check failure on line 101 in src/components/Indexer/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
exchangeDeactivatedEventProcessor = new ExchangeDeactivatedEventProcessor(chainId)
}
return exchangeDeactivatedEventProcessor
}

export const getContractAddress = (chainId: number, contractName: string): string => {
const addressFile = getOceanArtifactsAdressesByChainId(chainId)
if (addressFile && contractName in addressFile) {
Expand Down Expand Up @@ -267,6 +290,22 @@ export const processChunkLogs = async (
signer,
provider
)
} else if (event.type === EVENTS.EXCHANGE_ACTIVATED) {
const processor = getExchangeActivatedEventProcessor(chainId)
storeEvents[event.type] = await processor.processEvent(
log,
chainId,
signer,
provider
)
} else if (event.type === EVENTS.EXCHANGE_DEACTIVATED) {
const processor = getExchangeDeactivatedEventProcessor(chainId)
storeEvents[event.type] = await processor.processEvent(
log,
chainId,
signer,
provider
)
}
}
}
Expand Down

0 comments on commit e15905a

Please sign in to comment.