|
| 1 | +# Python Kraken SDK - Copilot Instructions |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +This is the **python-kraken-sdk** - a high-performance REST and WebSocket API |
| 6 | +client for Kraken Crypto Asset Exchange supporting both Spot and Futures |
| 7 | +trading. The SDK prioritizes performance, maintainability, reliability, |
| 8 | +modularization, and code reuse. |
| 9 | + |
| 10 | +## Core Principles |
| 11 | + |
| 12 | +### Code Quality Standards |
| 13 | + |
| 14 | +- **Performance First**: Optimize for speed and memory efficiency |
| 15 | +- **Maintainability**: Write clean, readable, self-documenting code |
| 16 | +- **Reliability**: Implement robust error handling and comprehensive testing |
| 17 | +- **Modularization**: Favor composition over inheritance, create reusable |
| 18 | + components |
| 19 | +- **DRY Principle**: Eliminate code duplication through shared utilities and |
| 20 | + base classes |
| 21 | + |
| 22 | +### Comment Philosophy |
| 23 | + |
| 24 | +- **Minimal Comments**: Code should be self-explanatory through clear naming and |
| 25 | + structure |
| 26 | +- **Only When Necessary**: Add comments only for non-obvious business logic, |
| 27 | + complex algorithms, or API-specific quirks |
| 28 | +- **No Obvious Comments**: Avoid stating what the code clearly does |
| 29 | +- **Focus on Why**: When commenting, explain the reasoning, not the mechanics |
| 30 | + |
| 31 | +## Project Structure |
| 32 | + |
| 33 | +``` |
| 34 | +src/kraken/ |
| 35 | +├── __init__.py # Main package exports |
| 36 | +├── cli.py # Command-line interface |
| 37 | +├── base_api/ # Core base classes and utilities |
| 38 | +│ └── __init__.py # SpotClient, FuturesClient, SpotAsyncClient, FuturesAsyncClient |
| 39 | +├── exceptions/ # Custom exception classes |
| 40 | +│ └── __init__.py # KrakenException hierarchy |
| 41 | +├── utils/ # Shared utilities and helpers |
| 42 | +├── spot/ # Spot trading API |
| 43 | +│ ├── __init__.py # Spot client exports |
| 44 | +│ ├── market.py # Market data client |
| 45 | +│ ├── trade.py # Trading operations client |
| 46 | +│ ├── user.py # User account client |
| 47 | +│ ├── funding.py # Funding operations client |
| 48 | +│ ├── earn.py # Earn/staking client |
| 49 | +│ ├── orderbook.py # Order book client |
| 50 | +│ ├── ws_client.py # WebSocket client implementation |
| 51 | +│ └── websocket/ # WebSocket infrastructure |
| 52 | +│ ├── __init__.py # SpotWSClientBase |
| 53 | +│ └── connectors.py # Connection management |
| 54 | +└── futures/ # Futures trading API |
| 55 | + ├── __init__.py # Futures client exports |
| 56 | + ├── market.py # Futures market data |
| 57 | + ├── trade.py # Futures trading |
| 58 | + ├── user.py # Futures user account |
| 59 | + ├── funding.py # Futures funding |
| 60 | + ├── ws_client.py # Futures WebSocket client |
| 61 | + └── websocket/ # Futures WebSocket infrastructure |
| 62 | +``` |
| 63 | + |
| 64 | +## Architecture Patterns |
| 65 | + |
| 66 | +### Client Hierarchy |
| 67 | + |
| 68 | +- **Base Classes**: `SpotClient`, `FuturesClient` (REST), `SpotAsyncClient`, |
| 69 | + `FuturesAsyncClient` (async REST) |
| 70 | +- **Specialized Clients**: Market, Trade, User, Funding, Earn (inherit from |
| 71 | + base) |
| 72 | +- **WebSocket Clients**: `SpotWSClient`, `FuturesWSClient` (extend async base |
| 73 | + classes) |
| 74 | + |
| 75 | +### Key Design Patterns |
| 76 | + |
| 77 | +- **Composition over Inheritance**: Favor utility functions and mixins |
| 78 | +- **Async/Await**: All WebSocket and async operations use proper async patterns |
| 79 | +- **Context Managers**: Support `async with` for resource management |
| 80 | +- **Type Hints**: Comprehensive typing with `typing` and `TYPE_CHECKING` |
| 81 | +- **Error Handling**: Custom exception hierarchy with specific error types |
| 82 | + |
| 83 | +## Development Guidelines |
| 84 | + |
| 85 | +### Code Style |
| 86 | + |
| 87 | +- **Python 3.11+**: Use modern Python features and syntax |
| 88 | +- **Type Annotations**: All functions must have complete type hints |
| 89 | +- **Docstrings**: Use Google-style docstrings for public APIs |
| 90 | +- **Naming**: Clear, descriptive names that eliminate need for comments |
| 91 | + |
| 92 | +### Performance Optimization |
| 93 | + |
| 94 | +- **Async Operations**: Use async/await for I/O operations |
| 95 | +- **Connection Pooling**: Reuse HTTP connections where possible |
| 96 | +- **Memory Efficiency**: Avoid unnecessary object creation in hot paths |
| 97 | +- **Caching**: Use `@lru_cache` for expensive computations |
| 98 | +- **Lazy Loading**: Initialize resources only when needed |
| 99 | + |
| 100 | +### Error Handling |
| 101 | + |
| 102 | +- **Custom Exceptions**: Use specific exception types from `kraken.exceptions` |
| 103 | +- **Graceful Degradation**: Handle network failures and API errors robustly |
| 104 | +- **Retry Logic**: Implement exponential backoff for transient failures |
| 105 | +- **Logging**: Use structured logging with appropriate levels |
| 106 | + |
| 107 | +### Testing Standards |
| 108 | + |
| 109 | +- **Comprehensive Coverage**: Aim for high test coverage |
| 110 | +- **Unit Tests**: Test individual components in isolation |
| 111 | +- **Integration Tests**: Test API interactions (with mocking when needed) |
| 112 | +- **Class-Based Organization**: Use pytest classes with shared fixtures and |
| 113 | + constants |
| 114 | +- **Helper Methods**: Create reusable assertion helpers to eliminate duplication |
| 115 | + |
| 116 | +### WebSocket Patterns |
| 117 | + |
| 118 | +- **Connection Management**: Proper connection lifecycle handling |
| 119 | +- **Subscription Management**: Track active subscriptions |
| 120 | +- **Message Routing**: Efficient message dispatch to handlers |
| 121 | +- **Reconnection Logic**: Automatic reconnection with backoff |
| 122 | + |
| 123 | +## Code Generation Guidelines |
| 124 | + |
| 125 | +### When Creating New Features |
| 126 | + |
| 127 | +1. **Extend Existing Patterns**: Follow established client patterns |
| 128 | +2. **Reuse Base Classes**: Inherit from appropriate base classes |
| 129 | +3. **Share Common Logic**: Extract reusable components |
| 130 | +4. **Maintain API Consistency**: Follow existing parameter and return patterns |
| 131 | +5. **Add Comprehensive Tests**: Include unit and integration tests |
| 132 | + |
| 133 | +### When Refactoring |
| 134 | + |
| 135 | +1. **Preserve Public APIs**: Maintain backward compatibility |
| 136 | +2. **Improve Performance**: Look for optimization opportunities |
| 137 | +3. **Reduce Complexity**: Simplify complex methods through decomposition |
| 138 | +4. **Enhance Type Safety**: Add or improve type annotations |
| 139 | +5. **Update Tests**: Ensure tests reflect changes |
| 140 | + |
| 141 | +### Specific Preferences |
| 142 | + |
| 143 | +- **Constants**: Use UPPER_CASE for module-level constants |
| 144 | +- **Private Methods**: Use single underscore prefix for internal methods |
| 145 | +- **Property Methods**: Use `@property` for computed attributes |
| 146 | +- **Context Managers**: Implement `__enter__`/`__exit__` or |
| 147 | + `__aenter__`/`__aexit__` when managing resources |
| 148 | + |
| 149 | +## API Design Philosophy |
| 150 | + |
| 151 | +- **Pythonic Interface**: Feel natural to Python developers |
| 152 | +- **Sensible Defaults**: Minimize required parameters |
| 153 | +- **Flexibility**: Support both high-level convenience and low-level control |
| 154 | +- **Performance**: Optimize for common use cases |
| 155 | +- **Documentation**: Self-documenting through clear parameter names and types |
| 156 | + |
| 157 | +## Testing Philosophy |
| 158 | + |
| 159 | +- **Fast Feedback**: Tests should run quickly |
| 160 | +- **Reliable**: Tests should not be flaky |
| 161 | +- **Isolated**: Each test should be independent |
| 162 | +- **Realistic**: Test real scenarios, not just edge cases |
| 163 | +- **Maintainable**: Tests should be easy to understand and modify |
| 164 | + |
| 165 | +Remember: The goal is to create a professional, high-performance SDK that |
| 166 | +developers love to use. Prioritize clarity, performance, and reliability in all |
| 167 | +code contributions. |
0 commit comments