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
- Purpose: Analyzes deposits for profitable actions
- Key Features:
- Calculates potential rewards
- Dynamic profit margin scaling
- Simulation-based gas estimation
- Determines profitability
- Queues profitable transactions
- Configuration Focus:
GOVLST_MIN_PROFIT_MARGIN_PERCENT
: Base profit marginGOVLST_GAS_PRICE_BUFFER
: Gas price safety marginPROFITABILITY_CHECK_INTERVAL
: Analysis frequencyINCLUDE_GAS_COST
: Include gas in profitabilityTENDERLY_USE_SIMULATE
: Enable simulation
- 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
- Dynamic profit margin scaling
- Simulation-based gas estimation
- Determines profitability
- Queues profitable transactions
- Configuration Focus:
GOVLST_MIN_PROFIT_MARGIN_PERCENT
: Base profit marginGOVLST_GAS_PRICE_BUFFER
: Gas price safety marginPROFITABILITY_CHECK_INTERVAL
: Analysis frequencyINCLUDE_GAS_COST
: Include gas in profitabilityTENDERLY_USE_SIMULATE
: Enable simulation
- Purpose: Executes on-chain transactions
- Key Features:
- Manages transaction queue
- Transaction validation
- Simulation-based gas estimation
- 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 limitTENDERLY_ACCESS_KEY
: Simulation API key
- Purpose: Manages GovLst reward claiming
- Key Features:
- Monitors unclaimed rewards
- Dynamic batch optimization
- Optimizes claim timing
- Tracks claim history
- Simulation-aware execution
- Configuration Focus:
GOVLST_ADDRESSES
: Contract addresses to monitorGOVLST_CLAIM_INTERVAL
: Check frequencyGOVLST_MAX_BATCH_SIZE
: Maximum claims per transactionGOVLST_PROFIT_SCALING
: Profit margin scaling
- 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
The system supports transaction simulation using Tenderly's API before sending transactions to the blockchain. This helps prevent wasted gas on transactions that would fail and provides more accurate gas estimates.
# Tenderly Configuration
TENDERLY_USE_SIMULATE=true
TENDERLY_ACCESS_KEY=your_tenderly_access_key
TENDERLY_ACCOUNT_NAME=your_tenderly_account_name
TENDERLY_PROJECT_NAME=your_tenderly_project_name
TENDERLY_NETWORK_ID=1 # Default is 1 for mainnet
Benefits of transaction simulation:
- Prevents failed transactions by simulating them before sending
- Provides more accurate gas estimates for profitability calculations
- Offers detailed transaction execution traces for debugging
- Falls back gracefully to standard estimation if simulation is unavailable
# 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
INCLUDE_GAS_COST=true
LOG_LEVEL=debug
# Simulation (Optional)
TENDERLY_USE_SIMULATE=false
# 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
INCLUDE_GAS_COST=true
LOG_LEVEL=info
# Simulation
TENDERLY_USE_SIMULATE=true
TENDERLY_ACCESS_KEY=your_key
TENDERLY_ACCOUNT_NAME=your_account
TENDERLY_PROJECT_NAME=your_project
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
# Simulation Settings
TENDERLY_USE_SIMULATE=true
TENDERLY_SIMULATION_TIMEOUT=30000
Configure profitability calculation parameters:
# Base Settings
GOVLST_MIN_PROFIT_MARGIN_PERCENT=10
INCLUDE_GAS_COST=true
# Dynamic Scaling
GOVLST_PROFIT_SCALING_FACTOR=0.05
GOVLST_MAX_PROFIT_MARGIN_PERCENT=15
GOVLST_MIN_BATCH_SIZE=2
# Simulation
TENDERLY_USE_SIMULATE=true
TENDERLY_GAS_PRICE_MULTIPLIER=1.1
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
SIMULATION_SUCCESS_RATE_THRESHOLD=0.9
# 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
INCLUDE_GAS_COST=true
# Dynamic Scaling
GOVLST_PROFIT_SCALING_FACTOR=0.05
GOVLST_MAX_PROFIT_MARGIN_PERCENT=15
# Price Feed
COINMARKETCAP_API_KEY=your_api_key
# Simulation
TENDERLY_USE_SIMULATE=true
TENDERLY_ACCESS_KEY=your_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
# Simulation
TENDERLY_USE_SIMULATE=true
TENDERLY_SIMULATION_TIMEOUT=30000
TENDERLY_GAS_PRICE_MULTIPLIER=1.1
# 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
INCLUDE_GAS_COST=true
# Dynamic Scaling
GOVLST_PROFIT_SCALING_FACTOR=0.05
GOVLST_MAX_PROFIT_MARGIN_PERCENT=15
-
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
-
Simulation Security
- Use dedicated API keys for simulation
- Monitor simulation usage
- Implement fallback mechanisms
-
Pre-deployment
- Verify all contract addresses
- Test RPC connection
- Check executor balance
- Validate database connection
- Set appropriate gas limits
- Configure simulation settings
-
Deployment
- Set production environment variables
- Enable health monitoring
- Configure logging
- Set up alerts
- Test simulation integration
-
Post-deployment
- Monitor initial transactions
- Verify profitability calculations
- Check component health status
- Review logs for errors
- Monitor simulation success rate
-
Regular Checks
# Check component health pnpm run health-check # Verify database state pnpm run db:status # Monitor gas usage pnpm run gas-report # Check simulation stats pnpm run simulation-stats
-
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 # Review simulation performance pnpm run analyze-simulations
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
The executor module is responsible for executing transactions in the staker-bots system.
The swap strategy allows automatic conversion of tokens to ETH using Uniswap V2. This is useful for converting accumulated reward tokens into ETH.
Copy the environment variables from strategies/swap.env.example
to your .env
file and adjust as needed:
# Enable/disable automatic token swapping to ETH
EXECUTOR_SWAP_TO_ETH=false
# Uniswap V2 Router address (mainnet)
UNISWAP_ROUTER_ADDRESS=0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
# Maximum allowed slippage in percentage (e.g., 0.5 for 0.5%)
SWAP_SLIPPAGE_TOLERANCE=0.5
# Number of minutes until the swap transaction expires
SWAP_DEADLINE_MINUTES=10
# Minimum amount of tokens required to trigger a swap (in wei)
SWAP_MIN_AMOUNT_IN=1000000000000000000 # 1 token
# Maximum amount of tokens to swap in a single transaction (in wei)
SWAP_MAX_AMOUNT_IN=1000000000000000000000 # 1000 tokens
# Number of decimals for the token being swapped
SWAP_TOKEN_DECIMALS=18
- Automatic token to ETH swaps via Uniswap V2
- Configurable slippage tolerance
- Minimum and maximum swap amounts
- Transaction deadline protection
- Uses OpenZeppelin Defender Relayer for secure transaction execution
- Automatic approval handling
The swap strategy will automatically:
- Check if token balance exceeds minimum amount
- Get optimal swap path and quote from Uniswap
- Apply slippage tolerance to protect against price movement
- Execute swap with deadline protection
- Handle token approvals if needed
The executor now features automatic token swapping to ETH when ETH balance is low. This ensures that the executor always has enough ETH for transaction fees.
- After each transaction (successful or failed), the executor checks the wallet's ETH balance
- If the balance is below 0.1 ETH, it automatically swaps some reward tokens for ETH
- The swap maintains a minimum token balance (max of 2200 tokens or contract's payout amount)
- Only half of excess tokens above the minimum are swapped to avoid depleting token reserves
- The swap uses Uniswap V2 with configurable slippage protection (default 0.5%)
- Maintains sufficient ETH for gas fees without manual intervention
- Preserves minimum token balance for payout requirements
- Handles token approvals and transaction confirmations
- Detailed logging for monitoring and troubleshooting
- Graceful error handling with full database logging