|
| 1 | +import { ethers, Signer, FallbackProvider, Interface } from 'ethers' |
| 2 | +import { INDEXER_LOGGER } from '../../../utils/logging/common.js' |
| 3 | +import { LOG_LEVELS_STR } from '../../../utils/logging/Logger.js' |
| 4 | +import { BaseEventProcessor } from './BaseProcessor.js' |
| 5 | +import { getContractAddress } from '../utils.js' |
| 6 | +import { EVENTS } from '../../../utils/constants.js' |
| 7 | +import { EscrowEvent } from '../../../@types/Escrow.js' |
| 8 | +import { OceanNodeConfig } from '../../../@types/OceanNode.js' |
| 9 | +import EscrowJson from '@oceanprotocol/contracts/artifacts/contracts/escrow/Escrow.sol/Escrow.json' with { type: 'json' } |
| 10 | + |
| 11 | +const escrowInterface = new Interface(EscrowJson.abi) |
| 12 | + |
| 13 | +const addr = (v: any): string => v?.toString().toLowerCase() |
| 14 | +const num = (v: any): string => v?.toString() |
| 15 | + |
| 16 | +export class EscrowEventProcessor extends BaseEventProcessor { |
| 17 | + private readonly escrowAddress: string |
| 18 | + |
| 19 | + constructor(chainId: number, config: OceanNodeConfig) { |
| 20 | + super(chainId, config) |
| 21 | + this.escrowAddress = getContractAddress(chainId, 'Escrow') |
| 22 | + } |
| 23 | + |
| 24 | + async processEvent( |
| 25 | + event: ethers.Log, |
| 26 | + chainId: number, |
| 27 | + signer: Signer, |
| 28 | + provider: FallbackProvider, |
| 29 | + eventName?: string |
| 30 | + ): Promise<any> { |
| 31 | + try { |
| 32 | + if ( |
| 33 | + !this.escrowAddress || |
| 34 | + event.address.toLowerCase() !== this.escrowAddress.toLowerCase() |
| 35 | + ) { |
| 36 | + return null |
| 37 | + } |
| 38 | + |
| 39 | + const decoded = escrowInterface.parseLog({ |
| 40 | + topics: Array.from(event.topics), |
| 41 | + data: event.data |
| 42 | + }) |
| 43 | + if (!decoded) return null |
| 44 | + |
| 45 | + const { args } = decoded |
| 46 | + if (!eventName) return null |
| 47 | + const record: EscrowEvent = { |
| 48 | + id: `${event.transactionHash}-${event.index}`, |
| 49 | + eventType: eventName, |
| 50 | + chainId, |
| 51 | + contract: event.address.toLowerCase(), |
| 52 | + block: event.blockNumber, |
| 53 | + txHash: event.transactionHash |
| 54 | + } |
| 55 | + |
| 56 | + switch (eventName) { |
| 57 | + case EVENTS.ESCROW_AUTH: |
| 58 | + record.payer = addr(args.payer) |
| 59 | + record.payee = addr(args.payee) |
| 60 | + record.maxLockedAmount = num(args.maxLockedAmount) |
| 61 | + record.maxLockSeconds = num(args.maxLockSeconds) |
| 62 | + record.maxLockCounts = num(args.maxLockCounts) |
| 63 | + break |
| 64 | + case EVENTS.ESCROW_LOCK: |
| 65 | + record.payer = addr(args.payer) |
| 66 | + record.payee = addr(args.payee) |
| 67 | + record.jobId = num(args.jobId) |
| 68 | + record.amount = num(args.amount) |
| 69 | + record.expiry = num(args.expiry) |
| 70 | + record.token = addr(args.token) |
| 71 | + break |
| 72 | + case EVENTS.ESCROW_CLAIMED: |
| 73 | + record.payee = addr(args.payee) |
| 74 | + record.jobId = num(args.jobId) |
| 75 | + record.token = addr(args.token) |
| 76 | + record.payer = addr(args.payer) |
| 77 | + record.amount = num(args.amount) |
| 78 | + record.proof = args.proof?.toString() |
| 79 | + break |
| 80 | + case EVENTS.ESCROW_CANCELED: |
| 81 | + record.payee = addr(args.payee) |
| 82 | + record.jobId = num(args.jobId) |
| 83 | + record.token = addr(args.token) |
| 84 | + record.payer = addr(args.payer) |
| 85 | + record.amount = num(args.amount) |
| 86 | + break |
| 87 | + case EVENTS.ESCROW_DEPOSIT: |
| 88 | + case EVENTS.ESCROW_WITHDRAW: |
| 89 | + record.payer = addr(args.payer) |
| 90 | + record.token = addr(args.token) |
| 91 | + record.amount = num(args.amount) |
| 92 | + break |
| 93 | + default: |
| 94 | + return null |
| 95 | + } |
| 96 | + |
| 97 | + const { escrow } = await this.getDatabase() |
| 98 | + if (!escrow) return null |
| 99 | + const result = await escrow.create(record) |
| 100 | + INDEXER_LOGGER.logMessage( |
| 101 | + `[Escrow] ${eventName} indexed for tx ${event.transactionHash} on chain ${chainId}` |
| 102 | + ) |
| 103 | + return result |
| 104 | + } catch (err) { |
| 105 | + INDEXER_LOGGER.log( |
| 106 | + LOG_LEVELS_STR.LEVEL_ERROR, |
| 107 | + `Error processing Escrow ${eventName} event: ${err.message}`, |
| 108 | + true |
| 109 | + ) |
| 110 | + return null |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments