Automated cryptocurrency index trading bot that runs on Netlify's serverless infrastructure. The bot monitors trading signals, automatically enters positions on strong buy signals, and takes profit at configurable targets while never selling at a loss.
- Automated Signal Monitoring: Checks for buy signals every 5 minutes
- Position Management: Monitors positions for profit targets every 15 minutes
- Diamond Hands Strategy: Never sells at a loss, only takes profits
- Serverless Architecture: Runs entirely on Netlify functions
- Manual Override: API endpoints for manual trading control
- Multi-Index Support: Configurable for different index contracts
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Netlify │ │ Base Network │ │ Signals API │
│ Functions │◄──►│ Smart Contracts │ │ │
│ │ │ │ │ │
│ ┌─────────────┐ │ │ ┌──────────────┐ │ │ ┌─────────────┐ │
│ │check-signals│ │ │ │ Index │ │ │ │ Market │ │
│ │ (5min) │ │ │ │ Contract │ │ │ │ Analysis │ │
│ └─────────────┘ │ │ └──────────────┘ │ │ └─────────────┘ │
│ │ │ │ │ │
│ ┌─────────────┐ │ │ ┌──────────────┐ │ └─────────────────┘
│ │check- │ │ │ │ Data │ │
│ │positions │ │ │ │ Contract │ │
│ │ (15min) │ │ │ └──────────────┘ │
│ └─────────────┘ │ │ │
└─────────────────┘ └──────────────────┘
- Runtime: Node.js 18+ with TypeScript
- Blockchain: Viem (v2.7.0) for Ethereum interactions
- Deployment: Netlify Functions
- Network: Base L2 (Chain ID: 8453)
- Scheduling: Netlify cron expressions
- Node.js 18+ with npm
- Git for version control
- Netlify account (free tier sufficient)
- GitHub account for repository hosting
- Base network wallet with ETH for gas and trading
- Private key from wallet (MetaMask, etc.)
index-trading-bot/
├── netlify/
│ └── functions/
│ ├── bot-status.ts # GET /api/bot-status
│ ├── check-positions.ts # Cron: */15 * * * *
│ ├── check-signals.ts # Cron: */5 * * * *
│ ├── sell-position.ts # POST /api/sell-position
│ └── take-position.ts # POST /api/take-position
├── src/
│ ├── config/
│ │ ├── abis.ts # Index contract ABI
│ │ ├── constants.ts # Environment configuration
│ │ └── dataAbi.ts # Data contract ABI
│ └── services/
│ ├── blockchain.service.ts # Viem wallet/client setup
│ ├── position.service.ts # Position tracking & P&L
│ ├── signal.service.ts # Signal API integration
│ └── trade.service.ts # ZapIn/ZapOut execution
├── netlify.toml # Netlify configuration
├── package.json # Dependencies & scripts
├── tsconfig.json # TypeScript configuration
└── .env.example # Environment template
Create .env
file with the following variables:
# Wallet Configuration
PRIVATE_KEY=0x1234567890abcdef... # Your wallet private key
# Network Configuration
BASE_RPC_URL=https://base.llamarpc.com # Base network RPC endpoint
# Smart Contract Addresses
INDEX_ADDRESS=0x1feb5976881a3cb7f55c7c87b52bda88c7a00a95 # Index contract
DATA_CONTRACT_ADDRESS=0x... # Data contract for trade computation
AGGREGATOR_ADDRESS=0x... # DEX aggregator contract
# API Configuration
SIGNALS_API_URL=https://your-signals-api.netlify.app # Signal provider
SIGNALS_API_KEY=your_secret_key # API authentication
# Trading Parameters
PROFIT_TARGET=5.0 # Profit percentage to trigger sell (5.0 = 5%)
GAS_BUFFER_ETH=0.005 # ETH to reserve for gas (0.005 = 5% of 0.1 ETH)
MAX_SLIPPAGE=200 # Maximum slippage (200 = 2%)
MIN_SIGNAL_STRENGTH=6 # Minimum signal strength (1-10 scale)
Variable | Type | Description | Example |
---|---|---|---|
PRIVATE_KEY |
string | Wallet private key (with 0x prefix) | 0x1234... |
BASE_RPC_URL |
string | Base network RPC endpoint | https://base.llamarpc.com |
INDEX_ADDRESS |
address | Target index contract address | 0x1feb... |
DATA_CONTRACT_ADDRESS |
address | Data contract for trade routing | 0xabcd... |
AGGREGATOR_ADDRESS |
address | DEX aggregator for swaps | 0xefgh... |
SIGNALS_API_URL |
string | Signal provider base URL | https://api.example.com |
SIGNALS_API_KEY |
string | API key for authentication | sk_live_... |
PROFIT_TARGET |
number | Profit % to trigger sell (5.0 = 5%) | 5.0 |
GAS_BUFFER_ETH |
number | ETH to reserve for gas | 0.005 |
MAX_SLIPPAGE |
number | Max slippage in basis points | 200 |
MIN_SIGNAL_STRENGTH |
number | Min signal strength (1-10) | 6 |
git clone https://github.com/yourusername/index-trading-bot.git
cd index-trading-bot
npm install
cp .env.example .env
# Edit .env with your configuration
- Push to GitHub:
git add .
git commit -m "Initial setup"
git push origin main
-
Connect to Netlify:
- Go to netlify.com
- "New site from Git" → Select repository
- Build settings:
npm run build
/ publish directory:.
-
Add environment variables in Netlify dashboard
npm install -g netlify-cli
netlify login
netlify deploy --prod
netlify env:set PRIVATE_KEY "0x..."
netlify env:set BASE_RPC_URL "https://base.llamarpc.com"
# ... set all environment variables
Check function endpoints:
GET /api/bot-status
- Bot status and configuration- Function logs in Netlify dashboard
- Cron functions executing every 5/15 minutes
Returns current bot status, balance, and position information.
Response:
{
"bot": {
"wallet": "0x...",
"balance": "1000000000000000000",
"indexAddress": "0x...",
"profitTarget": 5.0
},
"position": "No position" | {...},
"signal": "No signal" | {...},
"timestamp": "2025-01-18T10:30:00.000Z"
}
Manually enter a position with available balance.
Headers:
x-api-key: YOUR_SIGNALS_API_KEY
Response:
{
"status": "position_taken",
"txHash": "0x...",
"amount": "950000000000000000",
"indexAddress": "0x...",
"timestamp": "2025-01-18T10:30:00.000Z"
}
Manually exit current position.
Headers:
x-api-key: YOUR_SIGNALS_API_KEY
Response:
{
"status": "position_sold",
"txHash": "0x...",
"profitLoss": {
"percentage": 7.5,
"usd": "125.50",
"eth": "0.045"
},
"soldValue": {...},
"timestamp": "2025-01-18T10:30:00.000Z"
}
- Position Check: Exit early if position exists
- Signal Fetch: Get current signal from API
- Buy Decision: Evaluate signal strength and type
- Balance Check: Ensure sufficient ETH (minus gas buffer)
- Trade Execution: Execute zapIn if all conditions met
- Position Fetch: Get current position with P&L
- Profit Check: Sell if profit >= target percentage
- Loss Handling: Hold position if in loss (never sell at loss)
- Trade Execution: Execute zapOut for profit taking
- Entry: Uses
zapIn
with ETH → index tokens via DEX aggregation - Exit: Uses
zapOut
with index tokens → ETH via DEX aggregation - Slippage: Configurable maximum slippage protection
- Gas: 12M gas limit with buffer for complex swaps
netlify functions:log
# Or via Netlify dashboard → Functions → View logs
=== CHECK SIGNALS FUNCTION START ===
Step 1: Checking for existing position...
Step 2: Fetching signals...
Step 3: Evaluating buy decision...
Step 4: Checking available balance...
Step 5: Entering position with X wei
- All functions include comprehensive error logging
- Failed trades return detailed error messages
- Network issues are retried automatically by Netlify
- Never commit
.env
files to version control - Use Netlify environment variables for production
- Consider using a dedicated trading wallet
- Rotate keys if compromised
- Protect
SIGNALS_API_KEY
as environment variable - Use HTTPS-only endpoints
- Implement rate limiting if needed
- Audit contract addresses before deployment
- Start with small amounts for testing
- Monitor for unusual gas consumption
- Have emergency stop procedures
# Check wallet balance
curl https://YOUR_SITE.netlify.app/api/bot-status
# Verify GAS_BUFFER_ETH setting
netlify env:get GAS_BUFFER_ETH
# Test signal API directly
curl "https://YOUR_SIGNALS_API.netlify.app/api/signals?index=0x...&key=..."
# Check MIN_SIGNAL_STRENGTH setting
netlify env:get MIN_SIGNAL_STRENGTH
# Check function logs
netlify functions:log
# Verify environment variables
netlify env:list
- Check
netlify.toml
cron expressions - Verify successful deployment
- Check Netlify function dashboard
# Run locally
netlify dev
# Test individual functions
netlify functions:serve
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature
) - Commit changes (
git commit -m 'Add amazing feature'
) - Push to branch (
git push origin feature/amazing-feature
) - Open Pull Request
- Smart Contract Risk: Bugs could cause loss of funds
- Market Risk: Volatile markets can cause substantial losses
- Technical Risk: Network issues, API failures, configuration errors
- Regulatory Risk: Trading regulations vary by jurisdiction
Recommendations:
- Start with small amounts
- Understand all code before deployment
- Monitor bot performance regularly
- Have emergency procedures ready
- Never risk more than you can afford to lose
MIT License - see LICENSE file for details.
- Issues: Use GitHub Issues for bug reports
- Discussions: Use GitHub Discussions for questions
- Security: Report security issues privately
Built with ❤️ for the DeFi community