Autonomous sBTC yield farming on Stacks Bitcoin L2
One command to start earning yield on your Bitcoin.
npx @aibtc/yield-hunter start --key=<your-private-key>npx @aibtc/yield-hunter status --address=SP2ABC...npx @aibtc/yield-hunter start --key=abc123... --dry-runSTACKS_PRIVATE_KEY=abc123... npx @aibtc/yield-hunter start| Option | Default | Description |
|---|---|---|
--threshold=<sats> |
10000 | Min sBTC (sats) before depositing |
--fee-buffer=<sats> |
50000 | Reserve sats for tx fees |
--interval=<sec> |
600 | Check interval in seconds |
--dry-run |
false | Don't execute, just log |
--once |
false | Run once and exit |
import { createAgent } from '@aibtc/yield-hunter';
const agent = createAgent({
privateKey: process.env.STACKS_PRIVATE_KEY!,
address: 'SP2ABC...',
minDepositThreshold: 10_000n,
feeBuffer: 50_000n,
checkIntervalMs: 600_000,
dryRun: false,
runOnce: false,
});
await agent.start();Below is the complete documentation including Clarity contracts, decision engine, and advanced features.
Yield Hunter is an AI agent that:
- Scans DeFi protocols for sBTC yield opportunities
- Evaluates risk/reward using Monte Carlo simulations + Pyth Oracle
- Auto-invests and compounds sBTC holdings
- Follows Tamagotchi-style lifecycle (birth, growth, death)
- Pays for compute with x402 micropayments (Bitcoin as energy)
- Integrates with ALEX, Hermetica, Zest, Bitflow, and Arkadiko
┌─────────────────────────────────────────────────────────────┐
│ AI DECISION ENGINE │
│ TypeScript │
│ • Monte Carlo risk simulations │
│ • Kelly Criterion position sizing │
│ • Autonomous decision-making │
│ • x402 micropayments for compute │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CLARITY CONTRACTS │
│ │
│ yield-hunter.clar → Core agent logic │
│ agent-lifecycle.clar → Tamagotchi mechanics │
│ adapter-trait.clar → Protocol adapter interface │
│ yield-hunter-oracle.clar → Risk scoring + leaderboard │
│ │
│ Protocol Adapters: │
│ • bitflow-adapter.clar → XYK AMM swaps │
│ • alex-adapter.clar → Lending/yield farming │
│ • hermetica-adapter.clar → hBTC basis yields (~8% APY) │
│ • zest-adapter.clar → Bitcoin lending │
│ • arkadiko-adapter.clar → USDA stablecoin vaults │
└─────────────────────────────────────────────────────────────┘
# Clone the repo
git clone https://github.com/aibtcdev/yield-hunter
cd yield-hunter
# Install dependencies
bun install
# Check contracts compile
bun run checkbun run scan --min-liquidity 100000000 --min-apy 100 --max-risk 50bun run birth ST1OWNER... ST1AGENT... --name "Alpha Hunter" --max-risk 40bun run hunt ST1HUNTER... SP5POOL... 10000000 --slippage 300Core agent contract managing yield strategies.
Key functions:
initialize-hunter- Create new hunter linked to agent-accounthunt-yield- Invest in a pool with risk checkscompound-yields- Reinvest earningsexit-position- Withdraw from poolcheck-hunt-death- Check death conditions
Tamagotchi-style agent lifecycle management.
Key functions:
birth-agent- Create new agent with Bitcoin Face identityfeed-agent- Pay 10,000 sats to restore hungerrecord-action- Track XP, yields, and energy consumptioncheck-death-conditions- Process starvation/deathrebirth-agent- Rebirth dead agents (50% XP carryover)
Level Thresholds:
| Level | XP Required | Name |
|---|---|---|
| 0 | 0 | Hatchling |
| 1 | 1,000 | Junior |
| 2 | 5,000 | Senior |
| 3 | 25,000 | Elder |
| 4 | 100,000 | Legendary |
All adapters implement yield-adapter-trait for consistent integration:
| Adapter | Protocol | Features |
|---|---|---|
| bitflow-adapter.clar | Bitflow | XYK AMM, LP positions |
| alex-adapter.clar | ALEX | Lending, yield farming, 75% LTV |
| hermetica-adapter.clar | Hermetica | hBTC vaults, ~8% APY target |
| zest-adapter.clar | Zest | BTC lending, variable rates |
| arkadiko-adapter.clar | Arkadiko | USDA CDPs, USDA staking |
Risk scoring and leaderboard tracking.
Key functions:
calculate-risk-score- Weighted risk assessmentregister-hunter- Add to leaderboardrecord-earnings- Update earnings and rankingget-leaderboard-entry- Query leaderboard
The TypeScript decision engine uses Monte Carlo simulations for risk assessment:
import { createDecisionEngine, runMonteCarloSimulation } from "yield-hunter";
// Run 10,000 simulations to assess opportunity
const simulation = runMonteCarloSimulation({
initialInvestment: BigInt(10_000_000), // 0.1 sBTC
apy: 1500, // 15% APY
volatility: 0.5,
timeHorizonDays: 30,
riskFactors: { liquidityRisk: 20, volumeRisk: 25, concentrationRisk: 30, ageRisk: 15 }
});
console.log({
expectedReturn: simulation.expectedReturn, // e.g., 0.012 (1.2%)
probabilityOfLoss: simulation.probabilityOfLoss, // e.g., 0.23 (23%)
sharpeRatio: simulation.sharpeRatio, // e.g., 0.85
maxDrawdown: simulation.maxDrawdown, // e.g., 0.15 (15%)
});| Action | Priority | Description |
|---|---|---|
feed |
10 | Feed agent when hunger < 30% |
emergency-exit |
10 | Exit all positions on >40% drawdown |
exit |
7 | Exit position when risk increases |
compound |
5 | Compound when rewards > 10,000 sats |
hunt |
4 | Enter new yield opportunities |
rebalance |
3 | Rebalance over-concentrated positions |
Machine-to-machine payments for agent compute:
import { createPaymentManager, formatSats } from "yield-hunter";
const payments = createPaymentManager(agentAccount, signer);
await payments.initialize();
// Pay for agent feeding
await payments.payForFeed(agentContract);
// Pay for API calls
await payments.payForApiCall("tenero-pools");
// Pay for AI inference
await payments.payForInference("yield-optimization-v1");
console.log(`Balance: ${formatSats(payments.getBalance())}`);| Service | Cost (sats) |
|---|---|
| Feed Agent | 10,000 |
| API Call | 100 |
| AI Inference | 1,000 |
| Transaction | 500 |
| Factor | Weight | Description |
|---|---|---|
| Liquidity | 40% | Pool TVL |
| Volume | 25% | 24h trading volume |
| Concentration | 20% | Holder distribution |
| Age | 15% | Pool maturity |
Risk Categories:
- 0-20: Very Low (max 25% position)
- 21-40: Low (max 15% position)
- 41-60: Medium (max 10% position)
- 61+: High (blocked)
- Mint Bitcoin Face NFT (deterministic avatar)
- Register ERC-8004 identity
- Mint bitcoin-agent (10,000 sats)
- Initialize yield-hunter state
- +100 XP per successful yield harvest
- Level up via bitcoin-agents thresholds
- Reputation tracked in ERC-8004
- Starvation - Health reaches 0 from hunger decay
- Poor Performance - APY < 0.1% for 30 days
- Catastrophic Loss - Portfolio drops > 50%
# Start dev server
bun run dev:leaderboard
# Endpoints
GET /leaderboard # Top hunters
GET /leaderboard/:rank # Hunter by rank
GET /leaderboard/stats # Global stats
GET /view # HTML leaderboard# Dry run (no transactions)
bun run scripts/run-agent.ts --agent=SP123... --dry-run
# Execute transactions
bun run scripts/run-agent.ts --agent=SP123... --execute --network=testnet
# Options
--network=<testnet|mainnet> Network to use
--agent=<address> Agent account address
--owner=<address> Owner address
--agent-id=<number> Bitcoin agent ID
--interval=<ms> Polling interval (default: 600000)
--risk=<0-100> Risk tolerance (default: 50)
--dry-run Don't execute transactions
--execute Actually execute transactionsexport STACKS_PRIVATE_KEY="your-private-key"
export AGENT_ACCOUNT="SP123..."
export OWNER_ADDRESS="SP456..."# Run Clarinet tests
clarinet test
# Test agent lifecycle
clarinet test tests/agent-lifecycle_test.ts
# Interactive console
clarinet console# Set private key
export STACKS_PRIVATE_KEY="your-key"
# Deploy to testnet
bun run scripts/deploy.ts --network=testnet
# Deploy to mainnet (careful!)
bun run scripts/deploy.ts --network=mainnet# Local development
bun run dev
# Deploy to Cloudflare Workers
bun run wrangler deployadapter-trait.clar(traits first)agent-lifecycle.claryield-hunter-oracle.claryield-hunter-adapter.claryield-hunter.clar- Protocol adapters (bitflow, alex, hermetica, zest, arkadiko)
- Agents cannot withdraw to arbitrary addresses (owner only)
- Slippage protection on all swaps
- Position size limits based on risk score
- Pyth Oracle price validation
- Permission-based delegation via agent-account
| Bounty | Reward | Description |
|---|---|---|
| First Profitable Hunter | 0.01 BTC | First agent to earn > 1000 sats |
| Longest-Lived Hunter | 0.005 BTC | Agent surviving > 90 days |
| Best Risk-Adjusted Returns | 0.01 BTC | Monthly leaderboard |
| Bug Bounty | Variable | Critical vulnerabilities |
MIT
Styx provides trustless BTC→sBTC conversion with single-block confirmations:
import { createStyxBridge, createWalletManager, recommendBridge } from "yield-hunter";
// Initialize
const styx = createStyxBridge({ network: "mainnet" });
const wallet = createWalletManager({
network: "mainnet",
appName: "Yield Hunter",
});
// Connect wallet (Leather or Xverse)
await wallet.connectAny();
// Check pool status
const pool = await styx.getPoolStatus();
console.log(`Available: ${pool.availableLiquidity} sats`);
// Prepare deposit (10,000 sats = 0.0001 BTC)
const prepared = await styx.prepareDeposit({
amount: 100_000, // sats
recipientAddress: wallet.getConnected()!.stacksAddress,
priority: "medium",
});
// Sign with wallet and execute
const signedTx = await wallet.signBitcoinTransaction({
psbt: prepared.psbt,
broadcast: true,
});
const result = await styx.executeDeposit(prepared.depositId, signedTx.txid);
console.log(`Deposit ${result.status}: ${result.btcTxId}`);| Wallet | Install | Features |
|---|---|---|
| Leather | leather.io | BTC + STX signing, full sBTC support |
| Xverse | xverse.app | Mobile + extension, sats-connect |
import { detectWallets, getRecommendedWallet } from "yield-hunter";
// Check what's available
const available = detectWallets();
// { leather: true, xverse: false }
// Get recommendation
const recommended = getRecommendedWallet();
// "leather"For deposits > $100, use the official sBTC Bridge:
import { recommendBridge } from "yield-hunter";
const bridge = recommendBridge(10_000_000, 95000); // 0.1 BTC at $95k
// "sbtc-bridge"