Skip to content

Commit

Permalink
Monitor ExchangeActivated. Added token address to all dispensers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariacarmina committed Dec 17, 2024
1 parent d37ba24 commit 716ddd1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
88 changes: 86 additions & 2 deletions src/components/Indexer/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Check failure on line 44 in src/components/Indexer/processor.ts

View workflow job for this annotation

GitHub Actions / lint

'./utils.js' imported multiple times
class BaseEventProcessor {
protected networkId: number

Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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<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 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

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

View workflow job for this annotation

GitHub Actions / lint

Expected property shorthand
})
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,

Check warning on line 1120 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_ACTIVATED)
return savedDDO
} catch (err) {
INDEXER_LOGGER.log(LOG_LEVELS_STR.LEVEL_ERROR, `Error retrieving DDO: ${err}`, true)
}
}
}
15 changes: 14 additions & 1 deletion src/components/Indexer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
})
})
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 716ddd1

Please sign in to comment.