Skip to content

Commit

Permalink
Monitor DispenserDeactivated.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariacarmina committed Dec 16, 2024
1 parent b197bd0 commit d37ba24
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/components/Indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ export class OceanIndexer {
EVENTS.METADATA_STATE,
EVENTS.ORDER_STARTED,
EVENTS.ORDER_REUSED,
EVENTS.DISPENSER_ACTIVATED
EVENTS.DISPENSER_ACTIVATED,
EVENTS.DISPENSER_DEACTIVATED
].includes(event.method)
) {
// will emit the metadata created/updated event and advertise it to the other peers (on create only)
Expand Down
86 changes: 84 additions & 2 deletions src/components/Indexer/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,15 +929,15 @@ export class DispenserActivatedEventProcessor extends BaseEventProcessor {
for (const stat of ddo.indexedMetadata.stats) {
if (
stat.datatokenAddress.toLowerCase() === datatokenAddress.toLowerCase() &&
!doesDispenserAlreadyExist(event.address, stat.prices)
!doesDispenserAlreadyExist(event.address, stat.prices)[0]
) {
stat.prices.push({
type: 'dispenser',
price: '0',
contract: event.address
})
break
} else if (doesDispenserAlreadyExist(event.address, stat.prices)) {
} else if (doesDispenserAlreadyExist(event.address, stat.prices)[0]) {
break
}
}
Expand Down Expand Up @@ -966,3 +966,85 @@ export class DispenserActivatedEventProcessor extends BaseEventProcessor {
}
}
}

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

Check warning on line 1036 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.DISPENSER_DEACTIVATED)
return savedDDO
} catch (err) {
INDEXER_LOGGER.log(LOG_LEVELS_STR.LEVEL_ERROR, `Error retrieving DDO: ${err}`, true)
}
}
}
25 changes: 22 additions & 3 deletions src/components/Indexer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { BlocksEvents, NetworkEvent, ProcessingEvents } from '../../@types/blockchain.js'
import {
DispenserActivatedEventProcessor,
DispenserDeactivatedEventProcessor,
MetadataEventProcessor,
MetadataStateEventProcessor,
OrderReusedEventProcessor,
Expand All @@ -32,6 +33,7 @@ let metadataStateEventProcessor: MetadataStateEventProcessor
let orderReusedEventProcessor: OrderReusedEventProcessor
let orderStartedEventProcessor: OrderStartedEventProcessor
let dispenserActivatedEventProcessor: DispenserActivatedEventProcessor
let dispenserDeactivatedEventProcessor: DispenserDeactivatedEventProcessor

function getMetadataEventProcessor(chainId: number): MetadataEventProcessor {
if (!metadataEventProccessor) {
Expand Down Expand Up @@ -70,6 +72,15 @@ function getDispenserActivatedEventProcessor(
return dispenserActivatedEventProcessor
}

function getDispenserDeactivatedEventProcessor(
chainId: number
): DispenserDeactivatedEventProcessor {
if (!dispenserDeactivatedEventProcessor) {
dispenserDeactivatedEventProcessor = new DispenserDeactivatedEventProcessor(chainId)
}
return dispenserDeactivatedEventProcessor
}

export const getContractAddress = (chainId: number, contractName: string): string => {
const addressFile = getOceanArtifactsAdressesByChainId(chainId)
if (addressFile && contractName in addressFile) {
Expand Down Expand Up @@ -248,6 +259,14 @@ export const processChunkLogs = async (
signer,
provider
)
} else if (event.type === EVENTS.DISPENSER_DEACTIVATED) {
const processor = getDispenserDeactivatedEventProcessor(chainId)
storeEvents[event.type] = await processor.processEvent(
log,
chainId,
signer,
provider
)
}
}
}
Expand Down Expand Up @@ -367,13 +386,13 @@ export function findServiceIdByDatatoken(ddo: any, datatokenAddress: string): st
export function doesDispenserAlreadyExist(
dispenserAddress: string,
prices: Price[]
): boolean {
): [boolean, Price?] {
for (const price of prices) {
if (dispenserAddress.toLowerCase() === price.contract.toLowerCase()) {
return true
return [true, price]
}
}
return false
return [false, null]
}

export async function getPricesByDt(
Expand Down
3 changes: 2 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export const EVENTS = {
EXCHANGE_CREATED: 'ExchangeCreated',
EXCHANGE_RATE_CHANGED: 'ExchangeRateChanged',
DISPENSER_CREATED: 'DispenserCreated',
DISPENSER_ACTIVATED: 'DispenserActivated'
DISPENSER_ACTIVATED: 'DispenserActivated',
DISPENSER_DEACTIVATED: 'DispenserDeactivated'
}

export const INDEXER_CRAWLING_EVENTS = {
Expand Down

0 comments on commit d37ba24

Please sign in to comment.