π§ How the Odos Parser Works
The Odos parser in the-parsoor is a modular, multi-version system that extracts swap-related data from Odos's routers deployed across various chains. It identifies and parses three distinct contract types:
Each of these contract versions is parsed in isolation using dedicated logic and ABIs. Letβs explore the architecture and code patterns used in depth.
π Directory Structure
Copy src/lib/Odos/
ββ abis/ β ABIs for router versions
β ββ V1Router.json
β ββ V2Router.json
β ββ LimitOrderRouter.json
ββ parsers/ β Parsing logic per router version
β ββ RouterV1.ts
β ββ RouterV2.ts
β ββ LimitOrderRouter.ts
ββ contracts.ts β Deployment info, event hashes, and ABIs
ββ index.ts β Parser class implementing OpenRepβs parser interface π contracts.ts β The Router Registry
This file defines:
Contract addresses per chain
Events emitted by each router
Associated ABIs for decoding those events
Each router version is represented using an enum (CONTRACT_ENUM), and its event signatures are listed in EVENT_ENUM.
Why it matters:
This setup allows ProtocolHelper to dynamically decode logs just by checking the topic hash.
π§± index.ts β The Parser Entry Point
This is the class that integrates the parser into the OpenRep system.
Implements IProtocolParserExport
Registers a protocolIdentifier from config/protocols.ts
Uses ProtocolHelper.txnToIsListenerContract() to route the transaction to the correct sub-parser
Supports async behavior for future compatibility
This keeps all router-specific logic decoupled and modular.
π§ Parsing Logic Per Router
β
V2 Router β RouterV2.ts
Handles the latest swap logic and detects two kinds of events:
Swap β single-token swap
SwapMulti β multi-token route swap
Returned Action Types :
β
V1 Router β RouterV1.ts
Supports legacy Odos swaps. Event structure is more complex, containing outputs as a nested struct:
The parser differentiates between:
Single swap (1 input, 1 output)
Multi swap (multiple inputs/outputs)
Design Pattern:
Returned Action Types :
β
Limit Order Router β LimitOrderRouter.ts
Parses on-chain fill logs from limit orders submitted via Odos.
Event Types:
Returned Action Types :
Each order fill is mapped to a swap action with relevant:
π§ͺ Runtime Flow: How a Tx is Parsed
A transaction is passed to the Odos parser class (index.ts)
The class uses ProtocolHelper to determine which router the transaction was sent to
Once matched, the class calls the appropriate parseTransaction() from the correct router parser
The returned list of ITransactionAction[] is sent downstream to OpenRep scoring logic
β
Benefits of This Architecture
Each contract version has isolated logic and tests
Easy to add more routers or events
Shared logic for log decoding and routing
Adding v3 routers or new chain support is plug-and-play
Ensures all outputs conform to OpenRepβs score engine format
Last updated 9 months ago