-
Notifications
You must be signed in to change notification settings - Fork 22
index escrow events #1378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
index escrow events #1378
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5c882c3
index escrow events
giurgiur99 910cce6
fix pr comments
giurgiur99 371995d
test fix
giurgiur99 bb9dbf4
offset & size
giurgiur99 2778009
add docs
giurgiur99 ae8335f
check type, strip backticks
giurgiur99 39d6291
address pr comments
giurgiur99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/components/Indexer/processors/EscrowEventProcessor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { ethers, Signer, FallbackProvider, Interface } from 'ethers' | ||
| import { INDEXER_LOGGER } from '../../../utils/logging/common.js' | ||
| import { LOG_LEVELS_STR } from '../../../utils/logging/Logger.js' | ||
| import { BaseEventProcessor } from './BaseProcessor.js' | ||
| import { getContractAddress } from '../utils.js' | ||
| import { EVENTS } from '../../../utils/constants.js' | ||
| import { EscrowEvent } from '../../../@types/Escrow.js' | ||
| import { OceanNodeConfig } from '../../../@types/OceanNode.js' | ||
| import EscrowJson from '@oceanprotocol/contracts/artifacts/contracts/escrow/Escrow.sol/Escrow.json' with { type: 'json' } | ||
|
|
||
| const escrowInterface = new Interface(EscrowJson.abi) | ||
|
|
||
| const addr = (v: any): string => v?.toString().toLowerCase() | ||
| const num = (v: any): string => v?.toString() | ||
|
|
||
| export class EscrowEventProcessor extends BaseEventProcessor { | ||
| private readonly escrowAddress: string | ||
|
|
||
| constructor(chainId: number, config: OceanNodeConfig) { | ||
| super(chainId, config) | ||
| this.escrowAddress = getContractAddress(chainId, 'Escrow') | ||
| } | ||
|
|
||
| async processEvent( | ||
| event: ethers.Log, | ||
| chainId: number, | ||
| signer: Signer, | ||
| provider: FallbackProvider, | ||
| eventName?: string | ||
| ): Promise<any> { | ||
| try { | ||
| if ( | ||
| !this.escrowAddress || | ||
| event.address.toLowerCase() !== this.escrowAddress.toLowerCase() | ||
| ) { | ||
| return null | ||
| } | ||
|
|
||
| const decoded = escrowInterface.parseLog({ | ||
| topics: Array.from(event.topics), | ||
| data: event.data | ||
| }) | ||
| if (!decoded) return null | ||
|
|
||
| const { args } = decoded | ||
| const record: EscrowEvent = { | ||
| id: `${event.transactionHash}-${event.index}`, | ||
| eventType: eventName, | ||
| chainId, | ||
| contract: event.address.toLowerCase(), | ||
| block: event.blockNumber, | ||
| txHash: event.transactionHash | ||
| } | ||
|
|
||
| switch (eventName) { | ||
| case EVENTS.ESCROW_AUTH: | ||
| record.payer = addr(args.payer) | ||
| record.payee = addr(args.payee) | ||
| record.maxLockedAmount = num(args.maxLockedAmount) | ||
| record.maxLockSeconds = num(args.maxLockSeconds) | ||
| record.maxLockCounts = num(args.maxLockCounts) | ||
| break | ||
| case EVENTS.ESCROW_LOCK: | ||
| record.payer = addr(args.payer) | ||
| record.payee = addr(args.payee) | ||
| record.jobId = num(args.jobId) | ||
| record.amount = num(args.amount) | ||
| record.expiry = num(args.expiry) | ||
| record.token = addr(args.token) | ||
| break | ||
| case EVENTS.ESCROW_CLAIMED: | ||
| record.payee = addr(args.payee) | ||
| record.jobId = num(args.jobId) | ||
| record.token = addr(args.token) | ||
| record.payer = addr(args.payer) | ||
| record.amount = num(args.amount) | ||
| record.proof = args.proof?.toString() | ||
| break | ||
| case EVENTS.ESCROW_CANCELED: | ||
| record.payee = addr(args.payee) | ||
| record.jobId = num(args.jobId) | ||
| record.token = addr(args.token) | ||
| record.payer = addr(args.payer) | ||
| record.amount = num(args.amount) | ||
| break | ||
| case EVENTS.ESCROW_DEPOSIT: | ||
| case EVENTS.ESCROW_WITHDRAW: | ||
| record.payer = addr(args.payer) | ||
| record.token = addr(args.token) | ||
| record.amount = num(args.amount) | ||
| break | ||
| default: | ||
| return null | ||
| } | ||
|
|
||
| const { escrow } = await this.getDatabase() | ||
| if (!escrow) return null | ||
| const result = await escrow.create(record) | ||
| INDEXER_LOGGER.logMessage( | ||
| `[Escrow] ${eventName} indexed for tx ${event.transactionHash} on chain ${chainId}` | ||
| ) | ||
| return result | ||
| } catch (err) { | ||
| INDEXER_LOGGER.log( | ||
| LOG_LEVELS_STR.LEVEL_ERROR, | ||
| `Error processing Escrow ${eventName} event: ${err.message}`, | ||
| true | ||
| ) | ||
| return null | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { CommandHandler } from './handler.js' | ||
| import { GetEscrowEventsCommand } from '../../../@types/commands.js' | ||
| import { P2PCommandResponse } from '../../../@types/OceanNode.js' | ||
| import { Readable } from 'stream' | ||
| import { | ||
| ValidateParams, | ||
| validateCommandParameters | ||
| } from '../../httpRoutes/validateCommands.js' | ||
| import { CORE_LOGGER } from '../../../utils/logging/common.js' | ||
|
|
||
| export class EscrowEventsHandler extends CommandHandler { | ||
| validate(command: GetEscrowEventsCommand): ValidateParams { | ||
| return validateCommandParameters(command, []) | ||
| } | ||
|
|
||
| async handle(task: GetEscrowEventsCommand): Promise<P2PCommandResponse> { | ||
| const validationResponse = await this.verifyParamsAndRateLimits(task) | ||
| if (this.shouldDenyTaskHandling(validationResponse)) { | ||
| return validationResponse | ||
| } | ||
| try { | ||
| const database = await this.getOceanNode().getDatabase() | ||
| if (!database || !database.escrow) { | ||
| CORE_LOGGER.error('Escrow database is not available') | ||
| return { | ||
| stream: null, | ||
| status: { httpStatus: 503, error: 'Escrow database is not available' } | ||
| } | ||
| } | ||
|
|
||
| const filters: Record<string, any> = { | ||
| chainId: task.chainId, | ||
| eventType: task.eventType, | ||
| payer: task.payer ? task.payer.toLowerCase() : undefined, | ||
| payee: task.payee ? task.payee.toLowerCase() : undefined, | ||
| token: task.token ? task.token.toLowerCase() : undefined, | ||
| jobId: task.jobId, | ||
| txHash: task.txId | ||
| } | ||
|
|
||
| let result = await database.escrow.search( | ||
| filters, | ||
| task.maxResultsPerPage, | ||
| task.pageNumber | ||
| ) | ||
| if (!result) { | ||
| result = [] | ||
| } | ||
| return { | ||
| stream: Readable.from(JSON.stringify(result)), | ||
| status: { httpStatus: 200 } | ||
| } | ||
|
giurgiur99 marked this conversation as resolved.
|
||
| } catch (error) { | ||
| CORE_LOGGER.error(`Error in EscrowEventsHandler: ${error.message}`) | ||
| return { | ||
| stream: null, | ||
| status: { httpStatus: 500, error: 'Unknown error: ' + error.message } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.