|
| 1 | +# Thirdweb Engine Core |
| 2 | + |
| 3 | +A high-performance, Rust-based blockchain transaction engine that provides robust infrastructure for Web3 applications. Engine Core handles smart contract interactions, Account Abstraction (ERC-4337), and transaction management with enterprise-grade reliability. |
| 4 | + |
| 5 | +## 🏗️ Architecture Overview |
| 6 | + |
| 7 | +Thirdweb Engine Core is built as a modular Rust workspace with the following key components: |
| 8 | + |
| 9 | +### Core Components |
| 10 | + |
| 11 | +- **`server/`** - Main HTTP API server (`thirdweb-engine`) |
| 12 | +- **`core/`** - Core blockchain functionality (`engine-core`) |
| 13 | +- **`aa-core/`** - Account Abstraction support (`engine-aa-core`) |
| 14 | +- **`executors/`** - Background job processors (`engine-executors`) |
| 15 | +- **`thirdweb-core/`** - Thirdweb-specific integrations |
| 16 | +- **`twmq/`** - Thirdweb Message Queue - Redis-backed job queue system |
| 17 | + |
| 18 | +### Key Features |
| 19 | + |
| 20 | +- ✅ **Account Abstraction (ERC-4337)** - Full support for UserOperations v0.6 and v0.7 |
| 21 | +- ✅ **Multi-chain Support** - Works with any EVM-compatible blockchain |
| 22 | +- ✅ **Smart Contract Interactions** - Read, write, and encode contract calls |
| 23 | +- ✅ **Background Job Processing** - Reliable webhook delivery and transaction confirmation |
| 24 | +- ✅ **Secure Wallet Management** - Integration with Thirdweb Vault for key management |
| 25 | +- ✅ **REST API** - OpenAPI-documented endpoints with Scalar documentation |
| 26 | +- ✅ **Enterprise Reliability** - Redis-backed job queues with retry logic and error handling |
| 27 | + |
| 28 | +## 🚀 Quick Start |
| 29 | + |
| 30 | +### Prerequisites |
| 31 | + |
| 32 | +- **Rust** (latest stable version) |
| 33 | +- **Redis** server running locally or accessible remotely |
| 34 | +- **Thirdweb API credentials** (secret key and client ID) |
| 35 | + |
| 36 | +### Environment Setup |
| 37 | + |
| 38 | +1. **Clone the repository:** |
| 39 | + |
| 40 | + ```bash |
| 41 | + git clone <repo-url> |
| 42 | + cd engine-core |
| 43 | + ``` |
| 44 | + |
| 45 | +2. **Install dependencies:** |
| 46 | + |
| 47 | + ```bash |
| 48 | + cargo build |
| 49 | + ``` |
| 50 | + |
| 51 | +3. **Set up Redis:** |
| 52 | + |
| 53 | + ```bash |
| 54 | + # Using Docker |
| 55 | + docker run -d --name redis -p 6379:6379 redis:7-alpine |
| 56 | + |
| 57 | + # Or install locally (macOS) |
| 58 | + brew install redis |
| 59 | + brew services start redis |
| 60 | + ``` |
| 61 | + |
| 62 | +### Configuration |
| 63 | + |
| 64 | +The server uses a layered configuration system with YAML files and environment variables. |
| 65 | + |
| 66 | +#### Configuration Files |
| 67 | + |
| 68 | +Create configuration files in the `server/configuration/` directory: |
| 69 | + |
| 70 | +**`server/configuration/server_local.yaml`:** |
| 71 | + |
| 72 | +```yaml |
| 73 | +thirdweb: |
| 74 | + secret: "your_thirdweb_secret_key" |
| 75 | + client_id: "your_thirdweb_client_id" |
| 76 | +``` |
| 77 | +
|
| 78 | +#### Or with Environment Variables |
| 79 | +
|
| 80 | +You can also override any configuration using environment variables with the prefix `APP__`: |
| 81 | + |
| 82 | +```bash |
| 83 | +export APP__THIRDWEB__SECRET="your_secret_key" |
| 84 | +export APP__THIRDWEB__CLIENT_ID="your_client_id" |
| 85 | +export APP__REDIS__URL="redis://localhost:6379" |
| 86 | +export APP__SERVER__PORT=3069 |
| 87 | +``` |
| 88 | + |
| 89 | +#### Required Configuration |
| 90 | + |
| 91 | +- **`thirdweb.secret`** - Your Thirdweb secret key |
| 92 | +- **`thirdweb.client_id`** - Your Thirdweb client ID |
| 93 | +- **`redis.url`** - Redis connection URL (default: `redis://localhost:6379`) |
| 94 | + |
| 95 | +### Running the Server |
| 96 | + |
| 97 | +1. **Start Redis** (if not already running): |
| 98 | + |
| 99 | + ```bash |
| 100 | + redis-server |
| 101 | + ``` |
| 102 | + |
| 103 | +2. **Run the server:** |
| 104 | + |
| 105 | + ```bash |
| 106 | + # Development mode with debug logging |
| 107 | + RUST_LOG=debug cargo run --bin thirdweb-engine |
| 108 | +
|
| 109 | + # Or build and run the optimized binary |
| 110 | + cargo build --release |
| 111 | + ./target/release/thirdweb-engine |
| 112 | + ``` |
| 113 | + |
| 114 | +3. **Verify the server is running:** |
| 115 | + ```bash |
| 116 | + curl http://localhost:3069/v1/api.json |
| 117 | + ``` |
| 118 | + |
| 119 | +The server will start on `http://localhost:3069` by default. |
| 120 | + |
| 121 | +## 📚 API Documentation |
| 122 | + |
| 123 | +Once the server is running, you can access: |
| 124 | + |
| 125 | +- **API Documentation**: `http://localhost:3069/v1/scalar` |
| 126 | +- **OpenAPI Spec**: `http://localhost:3069/v1/api.json` |
| 127 | + |
| 128 | +### Available Endpoints |
| 129 | + |
| 130 | +- `POST /v1/read/contract` - Read from smart contracts |
| 131 | +- `POST /v1/write/contract` - Write to smart contracts (with AA support) |
| 132 | +- `POST /v1/encode/contract` - Encode contract function calls |
| 133 | +- `POST /v1/write/transaction` - Send raw transactions |
| 134 | + |
| 135 | +## 🔧 Development |
| 136 | + |
| 137 | +### Running Tests |
| 138 | + |
| 139 | +```bash |
| 140 | +# Run all tests |
| 141 | +cargo test |
| 142 | +
|
| 143 | +# Run tests for a specific component |
| 144 | +cargo test -p twmq |
| 145 | +cargo test -p engine-core |
| 146 | +
|
| 147 | +# Run with Redis integration tests |
| 148 | +cargo nextest run -p twmq --profile ci |
| 149 | +``` |
| 150 | + |
| 151 | +### Code Structure |
| 152 | + |
| 153 | +``` |
| 154 | +├── server/ # Main HTTP server |
| 155 | +│ ├── src/ |
| 156 | +│ │ ├── http/ # REST API routes and handlers |
| 157 | +│ │ ├── queue/ # Queue management |
| 158 | +│ │ └── config.rs # Configuration management |
| 159 | +│ └── configuration/ # YAML config files |
| 160 | +├── core/ # Core blockchain functionality |
| 161 | +│ └── src/ |
| 162 | +│ ├── userop.rs # Account Abstraction support |
| 163 | +│ ├── chain.rs # Chain management |
| 164 | +│ └── error.rs # Error types |
| 165 | +├── twmq/ # Message queue system |
| 166 | +│ └── src/ |
| 167 | +│ ├── queue.rs # Queue implementation |
| 168 | +│ └── job.rs # Job processing |
| 169 | +├── executors/ # Background job handlers |
| 170 | +│ └── src/ |
| 171 | +│ ├── webhook/ # Webhook delivery |
| 172 | +│ └── external_bundler/ # AA bundler integration |
| 173 | +└── thirdweb-core/ # Thirdweb integrations |
| 174 | +``` |
| 175 | +
|
| 176 | +### Adding New Features |
| 177 | +
|
| 178 | +1. **New API Endpoints**: Add routes in `server/src/http/routes/` |
| 179 | +2. **Background Jobs**: Implement `DurableExecution` trait in `executors/` |
| 180 | +3. **Chain Support**: Extend `ThirdwebChainService` in `server/src/chains.rs` |
| 181 | +
|
| 182 | +## 🔍 Monitoring & Operations |
| 183 | +
|
| 184 | +### Queue Statistics |
| 185 | +
|
| 186 | +The system provides comprehensive queue monitoring for webhook delivery, transaction sending, and confirmation processing: |
| 187 | +
|
| 188 | +```rust |
| 189 | +// Queue statistics are available through the QueueManager |
| 190 | +let stats = queue_manager.get_stats().await?; |
| 191 | +println!("Pending jobs: {}", stats.webhook.pending); |
| 192 | +``` |
| 193 | + |
| 194 | +### Logging |
| 195 | + |
| 196 | +Configure logging levels using the `RUST_LOG` environment variable: |
| 197 | + |
| 198 | +```bash |
| 199 | +# Detailed debugging |
| 200 | +RUST_LOG="thirdweb_engine=debug,twmq=debug,engine_executors=debug" |
| 201 | + |
| 202 | +# Production logging |
| 203 | +RUST_LOG="thirdweb_engine=info,twmq=warn" |
| 204 | +``` |
| 205 | + |
| 206 | +### Health Checks |
| 207 | + |
| 208 | +The server provides graceful shutdown handling and can be monitored for: |
| 209 | + |
| 210 | +- HTTP server health |
| 211 | +- Redis connectivity |
| 212 | +- Background worker status |
| 213 | + |
| 214 | +## 🔒 Security Considerations |
| 215 | + |
| 216 | +- **Vault Integration**: All private keys are managed through Thirdweb Vault |
| 217 | +- **API Authentication**: Requests are authenticated using Thirdweb secret keys |
| 218 | +- **Network Security**: Configure appropriate CORS policies for production |
| 219 | +- **Redis Security**: Secure your Redis instance with authentication and network restrictions |
| 220 | + |
| 221 | +## 🌐 Production Deployment |
| 222 | + |
| 223 | +### Environment Configuration |
| 224 | + |
| 225 | +Set `APP_ENVIRONMENT=production` and create `server_production.yaml`: |
| 226 | + |
| 227 | +```yaml |
| 228 | +server: |
| 229 | + host: "0.0.0.0" |
| 230 | + port: 3069 |
| 231 | + |
| 232 | +redis: |
| 233 | + url: "redis://your-redis-cluster" |
| 234 | + |
| 235 | +queue: |
| 236 | + webhook_workers: 50 |
| 237 | + external_bundler_send_workers: 20 |
| 238 | + userop_confirm_workers: 10 |
| 239 | +``` |
| 240 | +
|
| 241 | +### Resource Requirements |
| 242 | +
|
| 243 | +- **CPU**: 2+ cores recommended |
| 244 | +- **Memory**: 1GB+ RAM (more for high-throughput scenarios) |
| 245 | +- **Storage**: Minimal (logs and temporary data only) |
| 246 | +- **Network**: Stable internet connection for blockchain RPC calls |
| 247 | +
|
| 248 | +### Scaling Considerations |
| 249 | +
|
| 250 | +- **Horizontal Scaling**: Multiple instances can share the same Redis backend |
| 251 | +- **Queue Workers**: Adjust worker counts based on throughput requirements |
| 252 | +- **Redis**: Consider Redis clustering for high availability |
| 253 | +
|
| 254 | +## 📦 Dependencies |
| 255 | +
|
| 256 | +### Key External Dependencies |
| 257 | +
|
| 258 | +- **Alloy** - Ethereum library for Rust |
| 259 | +- **Axum** - Web framework |
| 260 | +- **Redis** - Message queue backend |
| 261 | +- **Tokio** - Async runtime |
| 262 | +- **Thirdweb Vault SDK** - Secure wallet management |
| 263 | +
|
| 264 | +## 🤝 Contributing |
| 265 | +
|
| 266 | +1. Fork the repository |
| 267 | +2. Create a feature branch |
| 268 | +3. Make your changes |
| 269 | +4. Add tests where appropriate |
| 270 | +5. Run the test suite: `cargo test` |
| 271 | +6. Submit a pull request |
| 272 | + |
| 273 | +## 📄 License |
| 274 | + |
| 275 | +MIT |
| 276 | + |
| 277 | +## 🆘 Support |
| 278 | + |
| 279 | +For issues and questions: |
| 280 | + |
| 281 | +1. Check the API documentation at `/v1/scalar` |
| 282 | +2. Review server logs for error details |
| 283 | +3. Ensure Redis is running and accessible |
| 284 | +4. Verify Thirdweb credentials are valid |
| 285 | + |
| 286 | +--- |
| 287 | + |
| 288 | +**Built with ❤️ by the Thirdweb team** |
0 commit comments