Skip to content

Keeper Bot

The keeper bot is an off-chain TypeScript service that automates vault operations.

Responsibilities

TaskFrequencyDescription
HarvestPeriodicClaim rewards, compound yield
RebalanceOn thresholdShift allocations to higher-yield protocols
MonitorContinuousTrack APYs, balances, health
AlertOn eventsTelegram notifications for significant events

Harvest Flow

The keeper uses the enhanced harvestWithSlippage() function for MEV protection:

  1. Query accrued rewards from each protocol
  2. Fetch Morpho rewards from Merkl API (https://api.merkl.xyz/v4/claim?user={strategyAddress}&chainId=1329)
  3. Calculate fair prices for reward token → USDC swaps using DEX quotes
  4. Apply slippage tolerance (typically 1%) to determine minAmountOut for each swap:
    minAmountOut = dexQuote * 0.99  // 1% slippage tolerance
  5. Call harvest functions:
    • strategy.harvestWithSlippage(takaraMinOut, morphoMinOut) for main harvest
    • strategy.claimMorphoRewards(tokens, amounts, proofs, minAmountsOut) for Merkl rewards
  6. Strategy executes:
    • Claims all rewards
    • Swaps to USDC with slippage protection (reverts if price is manipulated)
    • Redeposits compounded USDC
  7. Log yield amount and send Telegram summary

Slippage Calculation

The keeper calculates minimum output amounts off-chain to protect against MEV:

typescript
// Get fair price from Sailor DEX
const quote = await sailorDex.getQuote(rewardToken, usdc, rewardAmount);

// Apply 1% slippage tolerance
const minAmountOut = quote.outputAmount * 0.99;

// If actual output < minAmountOut, transaction reverts
await strategy.harvestWithSlippage(takaraMinOut, morphoMinOut);

This ensures that if a sandwich bot or price manipulator tries to extract value, the transaction fails instead of completing at a bad price.

Rebalance Logic

current_apys = [yei_apy, takara_apy, morpho_apy]
optimal_allocation = calculate_optimal(current_apys)
current_allocation = strategy.getAllocations()

if deviation(optimal, current) > threshold:
    strategy.setAllocations(optimal_allocation)
    strategy.rebalance()
    send_telegram_alert(old, new, reason)

The keeper compares current allocations against optimal allocations based on live APY data. If the deviation exceeds a configurable threshold, it triggers a rebalance.

MEV Protection

The keeper includes comprehensive MEV protection measures:

  • Slippage limits — all swaps require minAmountOut calculated off-chain from fair DEX quotes
  • Price validation — keeper fetches current prices before every harvest
  • Transaction reversion — if actual swap output < expected, transaction reverts (funds safe)
  • Private transactions — submits through private mempools when available
  • Timing randomization — avoids predictable harvest patterns
  • Gas optimization — batches operations when possible

How slippage protection works:

  1. Keeper queries Sailor DEX for current reward token → USDC prices
  2. Applies 1% slippage tolerance: minOut = quoteAmount * 0.99
  3. Passes minAmountOut to harvest function
  4. If sandwich bot manipulates price, actual output will be < minOut
  5. Transaction reverts, sandwich bot pays gas but gets nothing

This makes attacking Kana harvests unprofitable for MEV bots.

Telegram Alerts

The bot sends alerts for:

  • ✅ Successful harvests (with yield amount)
  • 🔄 Rebalance events (old → new allocations)
  • ⚠️ Errors or failed transactions
  • 📊 Daily summary reports
  • 🚨 Emergency events (large withdrawals, unusual APY changes)

Configuration

typescript
{
  harvestInterval: '4h',       // Harvest every 4 hours
  rebalanceThreshold: 100,     // 1% deviation triggers rebalance
  minHarvestAmount: '10',      // Min $10 yield to harvest (gas efficiency)
  telegramChatId: '...',
  rpcUrl: 'https://evm-rpc.sei-apis.com',
}

Tech Stack

  • Runtime: Node.js / TypeScript
  • Web3: ethers.js / viem
  • Scheduling: Node cron or setInterval
  • Alerts: Telegram Bot API
  • Monitoring: Structured logging

Built on SEI