Architecture
System Architecture
The Opinion CLOB SDK implements a hybrid architecture that integrates off-chain order matching with on-chain settlement. This Python client provides programmatic access to the Opinion prediction market infrastructure deployed on BNB Chain.
High-Level Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Your Application β
βββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β opinion_clob_sdk.Client β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β API Layer (opinion_api) β β
β β - Market data queries β β
β β - Order submission β β
β β - Position tracking β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Chain Layer (Web3) β β
β β - Smart contract interactions β β
β β - Token operations (split/merge/redeem) β β
β β - Transaction signing β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Order Utils (EIP712) β β
β β - Order building β β
β β - Cryptographic signing β β
β β - Gnosis Safe integration β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββ¬βββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββ
β β
βΌ βΌ
βββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
β Opinion CLOB API β β BNB Chain (BSC) β
β - Order matching β β - ConditionalTokens β
β - Market data β β - USDT (Collateral) β
β - User positions β β - Gnosis Safe β
βββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββCore Components
1. Client (sdk.py)
sdk.py)The Client class serves as the primary interface, orchestrating interactions across API and blockchain layers.
Responsibilities:
API connection management and authentication
Method exposure for market data, trading, and contract operations
Market data and quote token caching with configurable TTL
Coordination between HTTP requests and Web3 transactions
Key Configuration:
2. API Layer (opinion_api)
opinion_api)Auto-generated OpenAPI client managing HTTP communication with the Opinion CLOB backend.
Capabilities:
RESTful API invocation with type-safe request/response models
Request serialization and response deserialization
HTTP status code handling and error propagation
Bearer token authentication (API key-based)
Endpoints Covered:
Market discovery and details
Orderbook snapshots
Price data and candles
Order placement and cancellation
Position and balance queries
Trade history
3. Chain Layer (chain/contract_caller.py)
chain/contract_caller.py)Web3 integration layer enabling direct blockchain interactions via the web3.py library.
Smart Contracts Integrated:
ConditionalTokens
0xAD1a38cEc043e70E83a3eC30443dB285ED10D774
Core prediction market contract for splitting/merging positions
MultiSend
0x998739BFdAAdde7C933B942a68053933098f9EDa
Gnosis Safe batch transaction helper
USDT
Native BNB Chain USDT
Collateral token for all markets
Operations:
split()- Convert USDT β outcome tokens (YES/NO)merge()- Convert outcome tokens β USDTredeem()- Claim winnings from resolved marketsenable_trading()- Approve token allowances
4. Order Utils (chain/py_order_utils/)
chain/py_order_utils/)Order construction and signing module implementing EIP712 typed structured data signatures.
Components:
OrderBuilder - Constructs valid order objects with all required fields
Signer - Signs orders using private key (EOA) or Gnosis Safe
Model Classes - Type-safe order representations
EIP712 Signing Process:
5. Gnosis Safe Integration (chain/safe/)
chain/safe/)Support for multi-signature wallets using Gnosis Safe v1.3.0 contracts.
Key Concepts:
Signer Address - The private key that signs orders (can be a Safe owner)
Multi-Sig Address - The Safe contract holding your funds
Signature Type 2 - Indicates Gnosis Safe signature scheme
Data Flow Patterns
Pattern 1: Market Data Query (Gas-Free)
Characteristics:
Pure API interaction, no blockchain transaction
Zero gas cost
Response latency: 50-150ms (cached: <1ms)
Configurable TTL-based caching
Pattern 2: Order Placement (Gas-Free via CLOB)
Characteristics:
Gas abstraction: backend covers settlement costs
Cryptographic proof of authorization via ECDSA signature
Order matching latency: 200-500ms
On-chain settlement batched asynchronously
Pattern 3: Smart Contract Operation (Gas Required)
Characteristics:
Gas payment required (BNB native token)
Direct smart contract invocation
Block confirmation time: ~3 seconds (BSC)
Finality: ~10 blocks (~15 seconds recommended)
State changes immutable post-confirmation
Authentication & Security
Blockchain Signing
Two-key system for enhanced security:
Private Key (Signer)
Signs orders and transactions
Can be a hot wallet for automated trading
Never leaves your application
Multi-Sig Address (Funder)
Holds your USDT and outcome tokens
Gnosis Safe v1.3.0 create via GnosisSafeProxyFactory(0xa6B71E26C5e0845f74c812102Ca7114b6a896AB2) on BNB Chain
Requires approval for token operations
Example Configuration:
Precision and Number Handling
Token Decimals
All tokens use 18 decimal places (Wei standard):
Price Representation
Prices are quoted as decimal strings representing probability:
Valid Range: 0.01 to 0.99 (1% to 99%)
Amount Specifications
Orders can specify amounts in two ways:
Caching Strategy
The SDK implements intelligent caching for frequently accessed data:
Market Cache
Rationale: Market metadata rarely changes, reducing API load.
Quote Token Cache
Rationale: Supported currencies (USDT) are static configuration.
Real-Time Data
Never cached:
Orderbook snapshots
Latest prices
User positions
Order status
Error Handling Architecture
API Errors
Structured response format:
Common Error Codes:
0- Success400- Invalid request parameters500- Internal server error
Chain Errors
Handling Strategy:
Performance Benchmarks
API Response Times
Get markets
50-150ms
Cached: <10ms
Get orderbook
100-300ms
Market depth
Place order
200-500ms
Signature verification
Get positions
100-200ms
Position count
Blockchain Transactions
Split
2 block (~3s)
10 blocks (~15s)
Merge
2 block (~3s)
10 blocks (~15s)
Redeem
2 block (~3s)
10 blocks (~15s)
Approve
2 block (~3s)
10 blocks (~15s)
Note: BNB Chain (BSC) has ~1.5 second block time and recommends waiting 10 blocks for finality.
Python Compatibility
Supported: Python 3.8, 3.9, 3.10, 3.11, 3.12
Recommended: Python 3.10+ for best performance and type checking
Extensibility
Custom RPC Providers
The SDK supports any Web3-compatible RPC provider:
Next Steps
Client - Detailed setup guide
Order - Understanding market/limit orders
Gas Operations - When you need BNB
Precision - Working with Wei units
Last updated