diff --git a/src/@types/DDO/IndexedMetadata.ts b/src/@types/DDO/IndexedMetadata.ts index 8beb47612..403324e4e 100644 --- a/src/@types/DDO/IndexedMetadata.ts +++ b/src/@types/DDO/IndexedMetadata.ts @@ -10,8 +10,8 @@ export interface Stats { datatokenAddress: string name: string serviceId: string - orders: number - prices: Price[] + orders?: number + prices?: Price[] } export interface IndexedMetadata { diff --git a/src/components/Indexer/processor.ts b/src/components/Indexer/processor.ts index 1ec1d7c82..c5ab8a17a 100644 --- a/src/components/Indexer/processor.ts +++ b/src/components/Indexer/processor.ts @@ -41,6 +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 { getPricesForDt } from './utils.js' class BaseEventProcessor { protected networkId: number @@ -762,7 +763,7 @@ export class OrderStartedEventProcessor extends BaseEventProcessor { name: await datatokenContract.name(), serviceId: ddo.services[serviceIndex].id, orders: 1, - prices: [] // needs fixing, to retrieve pricing -> add util function for fres and dispensers + prices: getPricesForDt(datatokenContract, signer) }) } await orderDatabase.create( @@ -848,7 +849,7 @@ export class OrderReusedEventProcessor extends BaseEventProcessor { name: await datatokenContract.name(), serviceId: serviceIdToFind, orders: 1, - prices: [] // needs fixing, to retrieve pricing -> add util function for fres and dispensers + prices: getPricesForDt(datatokenContract, signer) }) } diff --git a/src/components/Indexer/utils.ts b/src/components/Indexer/utils.ts index 107e07915..9af679ff4 100644 --- a/src/components/Indexer/utils.ts +++ b/src/components/Indexer/utils.ts @@ -24,6 +24,7 @@ import { CommandStatus, JobStatus } from '../../@types/commands.js' import { create256Hash } from '../../utils/crypt.js' import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' assert { type: 'json' } import FixedRateExchange from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' assert { type: 'json' } +import { Price } from '../../@types/DDO/IndexedMetadata.js' let metadataEventProccessor: MetadataEventProcessor let metadataStateEventProcessor: MetadataStateEventProcessor @@ -344,6 +345,64 @@ export function findServiceIdByDatatoken(ddo: any, datatokenAddress: string): st return serviceIdToFind } +export async function getPricesForDt( + datatoken: ethers.Contract, + signer: Signer +): Promise { + let dispensers = [] + let fixedRates = [] + let prices: Price[] = [] + try { + dispensers = await datatoken.getDispensers() + } catch (e) { + INDEXER_LOGGER.error(`[GET PRICES] failure when retrieving dispensers: ${e}`) + } + try { + fixedRates = await datatoken.getFixedRates() + } catch (e) { + INDEXER_LOGGER.error( + `[GET PRICES] failure when retrieving fixed rate exchanges: ${e}` + ) + } + if (dispensers.length === 0 && fixedRates.length === 0) { + prices = [] + } else { + if (dispensers) { + for (const dispenser of dispensers) { + const dispenserContract = new ethers.Contract(dispenser, Dispenser.abi, signer) + if ((await dispenserContract.status())[0] === true) { + prices.push({ + type: 'dispenser', + price: '0', + contract: dispenser + }) + } + } + } + + if (fixedRates) { + for (const fixedRate of fixedRates) { + const fixedRateContract = new ethers.Contract( + fixedRate.address, + FixedRateExchange.abi, + signer + ) + const exchange = await fixedRateContract.getExchange(fixedRate.id) + if (exchange[6] === true) { + prices.push({ + type: 'fixedrate', + price: exchange[5], + token: exchange[3], + contract: fixedRate, + exchangeId: fixedRate.id + }) + } + } + } + } + return prices +} + export async function getPricingStatsForDddo(ddo: any, signer: Signer): Promise { if (!ddo.indexedMetadata) { ddo.indexedMetadata = {}