How kybernetwork/kyberswap-dex-lib works
Main Components
a. Domain Entities and Interfaces
• The “pkg/entity” package defines the core domain objects (for instance, pool data structures and related types) that represent the simulation’s business logic.
b. Liquidity Sources and Pool Simulator Implementations
• The “pkg/liquidity-source” package is a huge folder containing multiple subdirectories each representing a different liquidity source (e.g., balancer-v1, curve, uniswap-v2, etc.). These modules are essentially various pool simulators implementing the DEX logic for different protocols.
• As “Pool Simulator Modules” showing that they all plug into the common simulation framework.
• Since the README explains how new simulators should be registered using go generate and msgpack registration, indicate that these modules are developed in a plug-and-play manner.
c. Serialization and Registration (Msgpack)
• The “pkg/msgpack” package contains the code and generation scripts necessary for marshalling and unmarshalling pool simulator instances.
• Registration process, including “go generate” which uses “generate_register_pool_types.go” to register simulator types.
• Interaction from the msgpack layer into the simulator interface and how the “AfterMsgpackUnmarshal()” hooks are used to perform pointer alias initialization.
d. Utility and Supporting Components
• “pkg/util” and “pkg/valueobject” provide helper functions for common tasks such as handling ABI definitions, big-number arithmetic, Ethereum address manipulations, and more.
• “pkg/pooltypes” includes shared types for pools.
• “pkg/source” aggregates external pool sources and contains adapters to interact with external DEX protocols.
• These should be represented as support layers that feed data and configurations into the simulation engine.
Relationships and Data Flow
• The simulation engine picks up the correct pool simulator by “unmarshalling” the data using msgpack; the msgpack layer (in “pkg/msgpack”) uses the registration created by “go generate” to decode the input into the concrete type.
• Once unmarshalled, the “AfterMsgpackUnmarshal()” method ensures any pointer dependencies (pointer aliases) are reset properly.
• The simulator then uses business logic from its module within “pkg/liquidity-source” to perform calculations and simulations.
• Utility packages provide shared functions used across these modules.
Important Architectural Patterns and Design Principles
• Modular Design: Separate packages for each liquidity source make the library easily extendible.
• Plugin/Registration Pattern: New pool simulator implementations can be plugged in via go generate and msgpack registration.
• Separation of Concerns: The architecture clearly separates entity definitions (domain), simulation logic (liquidity sources), serialization (msgpack), and utility/helper components.
• Extensibility: The README emphasizes that any changes in pointer dependencies should be handled in “AfterMsgpackUnmarshal()”, which is a callback pattern ensuring that unmarshalled simulators are in a correct runtime state.