Skip to content

Commit

Permalink
Implemented an util function which calculates prices for a datatoken.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariacarmina committed Dec 16, 2024
1 parent 1be249e commit 58d4491
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/@types/DDO/IndexedMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export interface Stats {
datatokenAddress: string
name: string
serviceId: string
orders: number
prices: Price[]
orders?: number
prices?: Price[]
}

export interface IndexedMetadata {
Expand Down
5 changes: 3 additions & 2 deletions src/components/Indexer/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

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 @@ -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(
Expand Down Expand Up @@ -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)
})
}

Expand Down
59 changes: 59 additions & 0 deletions src/components/Indexer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -344,6 +345,64 @@ export function findServiceIdByDatatoken(ddo: any, datatokenAddress: string): st
return serviceIdToFind
}

export async function getPricesForDt(
datatoken: ethers.Contract,
signer: Signer
): Promise<Price[]> {
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<any> {
if (!ddo.indexedMetadata) {
ddo.indexedMetadata = {}
Expand Down

0 comments on commit 58d4491

Please sign in to comment.