Skip to main content

Architecture Overview

Technical overview of LazaiTrader's system architecture.


System Components

LazaiTrader is built on Cloudflare Workers with smart contracts on multiple EVM chains.

┌─────────────────────────────────────────────────────────────────────────┐
│ Users │
│ (Telegram) │
└─────────────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────────────┐
│ Telegram Bot API │
└─────────────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────────────┐
│ Cloudflare Workers Infrastructure │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ lt_tg │ │ lt_trader │ │ lt_balance │ │
│ │ Main Bot │ │ _queue │ │ _tracker │ │
│ │ │ │ (Producer) │ │ (Scheduled) │ │
│ └──────┬───────┘ └──────┬───────┘ └──────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌────────────────────────────────────┐ │
│ │ Cloudflare Queue │ │
│ │ "lt-trading-queue" │ │
│ └─────────────┬──────────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ lt_trader │ │ lt_tg_start │ │lt_tg_deposit │ │
│ │ _execute │ │(Registration)│ │(SCW Deploy) │ │
│ │ (Consumer) │ └──────────────┘ └──────────────┘ │
│ └──────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ Cloudflare D1 Database │ │
│ └─────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────────────┐
│ Blockchain Layer │
│ │
│ ┌────────────────────┐ ┌────────────────────┐ │
│ │ LazaiWalletFactory │ │ User SCWs │ │
│ │ (Deterministic) │ │ (Per User) │ │
│ └────────────────────┘ └────────────────────┘ │
│ │
│ ┌────────────────────┐ │
│ │ Whitelisted DEXs │ │
│ │ (HerculesDEX,etc) │ │
│ └────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘

Cloudflare Workers

Main Bot (lt_tg)

Purpose: Entry point for all Telegram interactions

Responsibilities:

  • Handle webhook from Telegram
  • Route commands to appropriate handlers
  • Display responses to users
  • Coordinate with microservices

Key Files:

  • worker.js - Main entry point
  • helper.strategyhandlers.js - Strategy management
  • helper.withdrawalhandlers.js - Withdrawal handling

Trading Queue Producer (lt_trader_queue)

Purpose: Monitor prices and queue trades

Trigger: Cron schedule (every 1 minute)

Responsibilities:

  • Fetch current prices from APIs
  • Check user trigger conditions
  • Queue trades for execution
  • Cache prices in database

Trading Execution Consumer (lt_trader_execute)

Purpose: Execute queued trades

Trigger: Cloudflare Queue messages

Responsibilities:

  • Re-validate trigger conditions
  • Calculate trade amounts
  • Execute trades via SCW
  • Record trades in database
  • Send notifications

Balance Tracker (lt_balance_tracker)

Purpose: Track user balances

Trigger: Cron schedule (every 5 minutes)

Responsibilities:

  • Fetch on-chain balances
  • Detect deposits
  • Update USDC valuations
  • Maintain balance history

Microservices

WorkerPurposeBinding Type
lt_tg_startUser registrationService Binding
lt_tg_depositSCW deploymentService Binding
lt_tg_withdrawalToken withdrawalsService Binding
lt_tg_balanceBalance fetchingService Binding
lt_tg_chartPerformance chartsService Binding

Data Flow

User Registration Flow

User → /start → lt_tg → lt_tg_start → D1 Database


Create Users record
Create RegistrationSession

Deposit Flow

User → /deposit → lt_tg → lt_tg_deposit → Blockchain


Deploy SCW via Factory
Update Users.SCWAddress

Trading Flow

Cron (1 min) → lt_trader_queue → Check prices

▼ (trigger met)
Cloudflare Queue


lt_trader_execute → SCW.executeTrade() → DEX


Record in D1 → Notify user

Withdrawal Flow

User → /withdraw → lt_tg → lt_tg_withdrawal → SCW.withdrawAll()


Funds sent to user's EOA
Record in Withdrawals table

Database Schema

Core Tables

TablePurpose
UsersRegistered users
ChainsSupported blockchains
TokensERC20 tokens
TradingPairsTrading pair configs
UserTradingConfigsUser strategies

Trading Tables

TablePurpose
TradesExecuted trades
TradeMetricsAdditional trade data
PriceHistoryHistorical prices
CachedPricesLatest prices

Financial Tables

TablePurpose
UserBalancesBalance history
DepositTransactionsDetected deposits
WithdrawalsWithdrawal records
SCWDeploymentsSCW addresses

Smart Contracts

LazaiWalletFactory

Purpose: Deploy user SCWs deterministically

Key Functions:

  • createWallet(owner) - Deploy new SCW
  • computeWalletAddress(owner) - Predict address
  • setDEXWhitelist(dex, status) - Manage whitelist

LazaiTradingWallet

Purpose: User's non-custodial trading wallet

Key Functions:

  • executeTrade(dex, data) - Execute swap
  • withdrawAllTokens(token) - Withdraw to owner
  • approveToken(token, dex, amount) - Approve DEX

External Integrations

Price APIs

ProviderPriorityUse Case
Binance1Major pairs
CoinGecko2Fallback
Coinbase3Additional source
DEXScreener4DEX-specific

Telegram Bot API

  • Webhook for incoming messages
  • sendMessage for responses
  • Inline keyboards for UX

Blockchain RPC

ChainRPC Provider
MetisMetis official
ZircuitZircuit official

Shared Utilities

priceHelper.js

  • Token symbol normalization
  • Price fetching with caching
  • Multi-source fallback

priceParser.js

  • Parse API responses
  • Handle different formats
  • Error handling

tokenMappings.json

  • Symbol normalization map
  • Stablecoin list
  • Chart colors

Security Architecture

Worker Level

  • Service bindings (internal only)
  • Secret management (Wrangler)
  • Input validation

Contract Level

  • Immutable owner
  • DEX whitelist
  • Reentrancy guards

Data Level

  • D1 database isolation
  • No sensitive data in logs
  • Minimal data exposure

Further Reading