A modular service for monitoring staking deposits, executing profitable transactions, and claiming GovLst rewards.
This system is composed of four main components:
- Monitor: Tracks on-chain staking events and updates the database.
- Profitability Engine: Analyzes deposits for profitable actions and batches.
- Executor: Executes profitable transactions and manages the transaction queue.
- Database: Persists all state, events, and queue data (supports Supabase and JSON).
stateDiagram-v2
[*] --> Monitor
Monitor --> Database: Store events
Monitor --> ProfitabilityEngine: Notify new/updated deposits
ProfitabilityEngine --> Database: Update queue, analysis
ProfitabilityEngine --> Executor: Queue profitable actions
Executor --> Database: Update tx status
Executor --> [*]
sequenceDiagram
participant User
participant Monitor
participant Database
participant ProfitabilityEngine
participant Executor
participant Ethereum
Monitor->>Ethereum: Poll for events
Ethereum-->>Monitor: New deposit/withdrawal events
Monitor->>Database: Store events
Monitor->>ProfitabilityEngine: Notify deposit update
ProfitabilityEngine->>Database: Read deposits
ProfitabilityEngine->>ProfitabilityEngine: Analyze profitability
alt Profitable
ProfitabilityEngine->>Executor: Queue claim tx
Executor->>Database: Store tx
Executor->>Ethereum: Submit claim tx
Ethereum-->>Executor: Tx receipt
Executor->>Database: Update tx status
end
- Tracks on-chain events (deposits, withdrawals, delegatee changes)
- Groups events by transaction
- Maintains checkpoints for resilience
- Emits events to the profitability engine
- Analyzes deposits for claim profitability
- Optimizes batch size and timing
- Uses price feeds and gas estimates
- Queues profitable claims for execution
- Manages transaction queue (FIFO)
- Executes claim transactions (wallet or Defender relayer)
- Handles retries, confirmations, and tip management
- Stores deposits, events, checkpoints, queues, and claim history
- Supports Supabase (production) and JSON (testing)
Configuration is managed via environment variables and src/configuration/
.
- See
.env.example
for all options. - Key parameters: RPC_URL, STAKER_CONTRACT_ADDRESS, LST_ADDRESS, GOVLST_ADDRESSES, EXECUTOR_TYPE, DATABASE_TYPE, etc.
pnpm run build
pnpm run prod
Or run specific components:
COMPONENTS=monitor,profitability pnpm run prod
- Each component exposes health/status logs.
- Use
pnpm run health-check
for status. - Logs:
output.log
(info),errors.log
(errors)
deposits
: Tracks staking depositsprocessing_checkpoints
: Tracks component stategovlst_deposits
: Tracks GovLst-owned depositsgovlst_claim_history
: Records claim executionsprocessing_queue
: Manages analysis queuetransaction_queue
: Manages tx execution queue
- Prerequisites
- Installation
- Environment Setup
- Component Architecture
- Configuration Guide
- Running the Service
- Monitoring and Maintenance
- Troubleshooting
Before setting up the service, ensure you have:
- Node.js v18+ installed
- PNPM package manager installed (
npm install -g pnpm
) - Access to an Ethereum RPC endpoint (e.g., Alchemy, Infura)
- A wallet with sufficient funds for gas fees
- (Optional) OpenZeppelin Defender account for production deployments
- Clone the repository:
git clone <repository-url>
cd staker-bots
- Install dependencies:
pnpm install
- Create environment configuration:
cp .env.example .env
The service requires careful configuration of environment variables. Here's a detailed guide for each section:
# Required: RPC endpoint URL
RPC_URL=https://eth-mainnet.g.alchemy.com/v2/your-api-key
# Required: Chain ID (1 for mainnet, 11155111 for sepolia)
CHAIN_ID=1
# Required: Network name
NETWORK_NAME=mainnet
# Optional: Starting block number (defaults to latest if not set)
START_BLOCK=0
# Required: Staker contract address
STAKER_CONTRACT_ADDRESS=0x...
# Required: LST token contract address
LST_ADDRESS=0x...
# Required: GovLst contract addresses (comma-separated)
GOVLST_ADDRESSES=0x...,0x...
# Optional: Default delegatee address
DEFAULT_DELEGATEE=0x...
# Optional: Tip receiver address
TIP_RECEIVER=0x...
Choose ONE of these execution methods:
EXECUTOR_TYPE=wallet
EXECUTOR_PRIVATE_KEY=your_private_key_without_0x
EXECUTOR_TYPE=defender
DEFENDER_API_KEY=your_defender_api_key
DEFENDER_SECRET_KEY=your_defender_secret_key
PUBLIC_ADDRESS_DEFENDER=0x...
Choose ONE storage option:
DATABASE_TYPE=json
DATABASE_TYPE=supabase
SUPABASE_URL=your_supabase_url
SUPABASE_KEY=your_supabase_key
# Choose which components to run (all by default)
COMPONENTS=monitor,profitability,executor
# Polling and Intervals
POLL_INTERVAL=15
HEALTH_CHECK_INTERVAL=60
GOVLST_CLAIM_INTERVAL=1800
# Block Processing
MAX_BLOCK_RANGE=2000
CONFIRMATIONS=20
REORG_DEPTH=64
# Transaction Management
GOVLST_MAX_BATCH_SIZE=5
GOVLST_GAS_PRICE_BUFFER=30
GOVLST_MIN_PROFIT_MARGIN_PERCENT=10
The service consists of four main components that can be run independently or together:
- Purpose: Tracks on-chain events related to staking activities
- Key Features:
- Polls blockchain for new events
- Processes stake deposits/withdrawals
- Maintains processing checkpoints
- Handles network reorgs
- Configuration Focus:
POLL_INTERVAL
: How often to check for new blocksMAX_BLOCK_RANGE
: Maximum blocks to process at onceCONFIRMATIONS
: Required block confirmations
- Purpose: Analyzes deposits for profitable actions
- Key Features:
- Calculates potential rewards
- Estimates gas costs
- Determines profitability
- Queues profitable transactions
- Configuration Focus:
GOVLST_MIN_PROFIT_MARGIN_PERCENT
: Minimum profit thresholdGOVLST_GAS_PRICE_BUFFER
: Gas price safety marginPROFITABILITY_CHECK_INTERVAL
: Analysis frequency
- Purpose: Executes on-chain transactions
- Key Features:
- Manages transaction queue
- Handles gas optimization
- Supports multiple execution strategies
- Implements retry logic
- Configuration Focus:
EXECUTOR_TYPE
: Wallet or DefenderEXECUTOR_MIN_BALANCE
: Minimum balance thresholdEXECUTOR_MAX_PENDING_TXS
: Concurrent transaction limit
- Purpose: Manages GovLst reward claiming
- Key Features:
- Monitors unclaimed rewards
- Batches claim transactions
- Optimizes claim timing
- Tracks claim history
- Configuration Focus:
GOVLST_ADDRESSES
: Contract addresses to monitorGOVLST_CLAIM_INTERVAL
: Check frequencyGOVLST_MAX_BATCH_SIZE
: Maximum claims per transaction
- Build the TypeScript code:
pnpm run build
- Start all components:
pnpm run prod
Or run specific components:
# Set COMPONENTS in .env first
COMPONENTS=monitor,profitability pnpm run prod
The service implements automatic health checks for each component:
// Health check output format
{
monitor: {
isRunning: boolean,
lastBlock: number,
processingLag: number
},
profitability: {
isRunning: boolean,
queueSize: number,
lastCheck: string
},
executor: {
isRunning: boolean,
pendingTx: number,
balance: string
}
}
- All activities are logged to
output.log
- Errors are logged to
errors.log
- Log level can be configured via
LOG_LEVEL
For Supabase users:
- Regular pruning is automatic
- Manual cleanup can be triggered:
pnpm run db:cleanup
- RPC Connection Issues
# Check RPC connection
curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' $RPC_URL
- Insufficient Balance
# Check executor balance
pnpm run check-balance
- Transaction Failures
- Check gas price settings
- Verify contract allowances
- Review profitability thresholds
Enable detailed logging:
LOG_LEVEL=debug
- Report issues on GitHub
- Check logs in
errors.log
- Review transaction history in database
# Network
RPC_URL=https://ethereum-sepolia-rpc.publicnode.com
CHAIN_ID=11155111
NETWORK_NAME=sepolia
# Contracts
STAKER_CONTRACT_ADDRESS=0x8C97699cBEAB273e4b469E863721522c04349057
LST_ADDRESS=0xDfdEB974D0A564d7C25610e568c1D309220236BB
GOVLST_ADDRESSES=
# Executor (Local Wallet)
EXECUTOR_TYPE=wallet
EXECUTOR_PRIVATE_KEY=your_private_key_here
TIP_RECEIVER=0x...
# Database
DATABASE_TYPE=json
# Components
COMPONENTS=monitor,profitability,executor,govlst
# Performance (Development Settings)
POLL_INTERVAL=15
MAX_BLOCK_RANGE=500
CONFIRMATIONS=1
GOVLST_MAX_BATCH_SIZE=2
GOVLST_MIN_PROFIT_MARGIN_PERCENT=5
LOG_LEVEL=debug
# Network
RPC_URL=https://eth-mainnet.g.alchemy.com/v2/your-api-key
CHAIN_ID=1
NETWORK_NAME=mainnet
# Contracts
STAKER_CONTRACT_ADDRESS=0x...
LST_ADDRESS=0x...
GOVLST_ADDRESSES=0x...,0x...
# Executor (Defender)
EXECUTOR_TYPE=defender
DEFENDER_API_KEY=your_api_key
DEFENDER_SECRET_KEY=your_secret_key
PUBLIC_ADDRESS_DEFENDER=0x...
TIP_RECEIVER=0x...
# Database
DATABASE_TYPE=supabase
SUPABASE_URL=your_url
SUPABASE_KEY=your_key
# Components
COMPONENTS=monitor,profitability,executor,govlst
# Performance (Production Settings)
POLL_INTERVAL=13
MAX_BLOCK_RANGE=2000
CONFIRMATIONS=20
REORG_DEPTH=64
GOVLST_MAX_BATCH_SIZE=5
GOVLST_MIN_PROFIT_MARGIN_PERCENT=10
GOVLST_GAS_PRICE_BUFFER=30
LOG_LEVEL=info
Fine-tune gas settings for better transaction efficiency:
# Gas Price Management
GOVLST_GAS_PRICE_BUFFER=30
DEFENDER_MAX_FEE=100000000000
DEFENDER_PRIORITY_FEE=2000000000
# Batch Processing
GOVLST_MAX_BATCH_SIZE=5
EXECUTOR_MAX_PENDING_TXS=10
Configure health checks and monitoring:
# Health Check Settings
HEALTH_CHECK_INTERVAL=60
CIRCUIT_BREAKER_MAX_FAILED_TXS=3
CIRCUIT_BREAKER_COOLDOWN_PERIOD=1800
CIRCUIT_BREAKER_MIN_SUCCESS_RATE=0.8
# Monitoring Thresholds
EXECUTOR_MIN_BALANCE=0.1
DEFENDER_MIN_BALANCE=0.01
Optimize database performance:
# Database Performance
DATABASE_BATCH_TIMEOUT=3600
DATABASE_MAX_QUEUE_SIZE=1000
DATABASE_PRUNE_INTERVAL=86400
DATABASE_MAX_ARCHIVE_AGE=604800
# Block Processing
START_BLOCK=0
MAX_BLOCK_RANGE=2000
CONFIRMATIONS=20
REORG_DEPTH=64
# Polling
POLL_INTERVAL=13
MONITOR_HEALTH_CHECK_INTERVAL=60
# Profitability Thresholds
GOVLST_MIN_PROFIT_MARGIN_PERCENT=10
PROFITABILITY_CHECK_INTERVAL=300
PROFITABILITY_MAX_BATCH_SIZE=50
# Price Feed
COINMARKETCAP_API_KEY=your_api_key
# Wallet Configuration
EXECUTOR_MIN_BALANCE=0.1
EXECUTOR_MAX_PENDING_TXS=10
EXECUTOR_GAS_LIMIT_BUFFER=1.3
# Transaction Management
EXECUTOR_QUEUE_POLL_INTERVAL=60
EXECUTOR_RETRY_DELAY=60
EXECUTOR_MAX_RETRIES=3
# Reward Claiming
GOVLST_CLAIM_INTERVAL=1800
GOVLST_MAX_BATCH_SIZE=5
GOVLST_PAYOUT_AMOUNT=1000000000000000000
# Profitability
GOVLST_MIN_PROFIT_MARGIN_PERCENT=10
GOVLST_GAS_PRICE_BUFFER=30
-
Private Key Management
- Never commit private keys to version control
- Use environment variables or secure key management
- Consider using OpenZeppelin Defender for production
-
RPC Security
- Use authenticated RPC endpoints
- Implement rate limiting
- Monitor for suspicious activity
-
Database Security
- Use strong Supabase API keys
- Implement proper access controls
- Regular backup procedures
-
Pre-deployment
- Verify all contract addresses
- Test RPC connection
- Check executor balance
- Validate database connection
- Set appropriate gas limits
-
Deployment
- Set production environment variables
- Enable health monitoring
- Configure logging
- Set up alerts
-
Post-deployment
- Monitor initial transactions
- Verify profitability calculations
- Check component health status
- Review logs for errors
-
Regular Checks
# Check component health pnpm run health-check # Verify database state pnpm run db:status # Monitor gas usage pnpm run gas-report
-
Database Maintenance
# Prune old data pnpm run db:cleanup # Backup database pnpm run db:backup
-
Performance Optimization
# Analyze transaction history pnpm run analyze-tx # Optimize gas settings pnpm run optimize-gas
The service can be configured through the following parameters in config.ts
:
- Poll interval: How often to check for profitable opportunities (default: 15s)
- Gas price buffer: Additional buffer on gas price estimates (default: 20%)
- Minimum profit margin: Minimum expected profit to execute a transaction (default: 0.001 ETH)
- Maximum batch size: Maximum number of deposits to process in a batch (default: 10)
- Wallet minimum balance: Minimum wallet balance to maintain (default: 0.1 ETH)
- Maximum pending transactions: Maximum number of pending transactions (default: 5)
- Gas boost percentage: Percentage to boost gas price by (default: 10%)
- Concurrent transactions: Number of transactions to execute concurrently (default: 3)
- Claim interval: How often to check for claimable rewards (default: 1 hour)
- Minimum profit margin: Minimum profit required for claims (default: 0.01 ETH)
- Maximum batch size: Maximum deposits per claim (default: 10)
- Gas price buffer: Buffer for gas price volatility (default: 20%)
The service uses a JSON file database by default (staker-monitor-db.json
). This can be changed to use Supabase by modifying the database configuration in config.ts
.
- deposits: Tracks staking deposits
- processing_checkpoints: Tracks component processing state
- govlst_deposits: Tracks GovLst-owned deposits
- govlst_claim_history: Records GovLst claim executions
The GovLst Profitability Engine is a simple system for analyzing and optimizing reward claims from GovLst staking deposits. It implements a single-bin accumulation strategy that efficiently groups deposits to maximize profitability while minimizing gas costs.
- Single-bin accumulation strategy for optimal deposit grouping
- Real-time profitability analysis with gas cost estimation
- Batch processing of unclaimed rewards
- Automatic threshold optimization
- Resilient error handling and retry mechanisms
stateDiagram-v2
[*] --> Idle
Idle --> Analyzing: New deposits
Analyzing --> BinAccumulation: Process deposits
BinAccumulation --> ThresholdCheck: Add to bin
ThresholdCheck --> Queueing: Threshold met
ThresholdCheck --> BinAccumulation: Continue accumulating
Queueing --> Idle
The engine implements a novel single-bin accumulation strategy that:
- Maintains one active bin for collecting deposits
- Sorts deposits by reward amount in descending order
- Accumulates deposits until reaching optimal threshold
- Automatically queues full bins for execution
- Optimal Threshold: Minimum 1 token (1e18 wei)
- Gas Cost Buffer: Configurable safety margin for gas estimates
- Batch Size: 100 deposits per processing batch
- Profit Margin: Configurable minimum profit requirement
The engine calculates profitability using:
expectedProfit = totalRewards - (gasPrice * gasLimit * ethPrice) / tokenPrice;
Where:
totalRewards
: Sum of unclaimed rewards in the bingasPrice
: Current gas price with safety buffergasLimit
: Estimated gas required for claim (300,000)ethPrice
: ETH price in USDtokenPrice
: Reward token price in USD
The engine processes deposits in batches to optimize RPC calls:
- Reward Fetching: Processes 100 deposits per batch
- Gas Estimation: Cached with configurable update interval
- Threshold Checking: Real-time bin optimization
Implements a robust error handling system:
- Automatic retries for transient failures
- Exponential backoff for RPC calls
- Detailed error context and logging
- Graceful degradation on partial failures
-
Caching System
- Gas price caching with TTL
- Reward calculation memoization
- Batch processing of RPC calls
-
Smart Batching
- Parallel reward fetching
- Sorted deposit processing
- Optimal bin threshold calculation
interface ProfitabilityConfig {
rewardTokenAddress: string; // Reward token contract address
minProfitMargin: bigint; // Minimum profit threshold
gasPriceBuffer: number; // Gas price safety margin (%)
maxBatchSize: number; // Maximum deposits per batch
defaultTipReceiver: string; // Default tip receiver address
priceFeed: {
cacheDuration: number; // Price cache duration (ms)
};
}
const engine = new GovLstProfitabilityEngine(
govLstContract,
stakerContract,
provider,
{
rewardTokenAddress: '0x...',
minProfitMargin: BigInt(1e16), // 0.01 tokens
gasPriceBuffer: 20, // 20% buffer
maxBatchSize: 50,
defaultTipReceiver: '0x...',
priceFeed: {
cacheDuration: 300_000, // 5 minutes
},
},
);
// Start the engine
await engine.start();
// Analyze deposits
const analysis = await engine.analyzeAndGroupDeposits(deposits);
// Check if bin is ready
const isReady = await engine.isActiveBinReady();
The engine provides detailed monitoring capabilities:
- Real-time bin status
- Gas price trends
- Processing metrics
- Error rates and types
- Profitability statistics
-ProfitabilityError - // Base error class
GasEstimationError - // Gas calculation issues
BatchFetchError - // Batch processing failures
QueueProcessingError; // Queue operation errors
- Dynamic threshold adjustment based on market conditions
- Machine learning for gas price prediction
- Multi-bin optimization strategies
- Advanced profit maximization algorithms
- Enhanced monitoring and alerting systems