|
| 1 | +# Revenue Claim Cron Job |
| 2 | + |
| 3 | +This directory contains scripts for automatically claiming earned streaming funds for creators once they reach a certain threshold. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Revenue Claim Cron Job monitors the SubStream Protocol contract and automatically calls the `collect` function when a creator's accumulated revenue exceeds the configured threshold (e.g., > 100 USDC). |
| 8 | + |
| 9 | +## Files |
| 10 | + |
| 11 | +### 1. `revenue_claim_cron_job.py` (Recommended) |
| 12 | +A Python script that uses the Stellar SDK to interact with the SubStream contract. This is the most user-friendly option. |
| 13 | + |
| 14 | +### 2. `soroban_revenue_claimer.py` |
| 15 | +An advanced Python script that uses Soroban RPC directly for more precise contract interaction. Suitable for production use. |
| 16 | + |
| 17 | +### 3. `revenue_claim_cron_job.rs` |
| 18 | +A Rust implementation (requires additional dependencies and setup). |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +### 1. Install Dependencies |
| 23 | + |
| 24 | +```bash |
| 25 | +# For Python scripts |
| 26 | +pip install -r requirements.txt |
| 27 | + |
| 28 | +# Or install manually |
| 29 | +pip install stellar-sdk soroban-rpc requests python-dotenv |
| 30 | +``` |
| 31 | + |
| 32 | +### 2. Configure Environment |
| 33 | + |
| 34 | +Copy the example environment file: |
| 35 | +```bash |
| 36 | +cp .env.example .env |
| 37 | +``` |
| 38 | + |
| 39 | +Edit `.env` with your configuration: |
| 40 | +```bash |
| 41 | +# Required: Your creator address |
| 42 | +CREATOR_ADDRESS=GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 43 | + |
| 44 | +# Optional: Revenue threshold in USDC (default: 100) |
| 45 | +REVENUE_THRESHOLD=100 |
| 46 | + |
| 47 | +# Optional: Check interval in seconds (default: 300 = 5 minutes) |
| 48 | +CHECK_INTERVAL=300 |
| 49 | + |
| 50 | +# Optional: Your private key for signing transactions |
| 51 | +PRIVATE_KEY=YOUR_PRIVATE_KEY_HERE |
| 52 | +``` |
| 53 | + |
| 54 | +### 3. Run the Script |
| 55 | + |
| 56 | +#### Option A: Basic Python Script |
| 57 | +```bash |
| 58 | +export CREATOR_ADDRESS="your_creator_address" |
| 59 | +export PRIVATE_KEY="your_private_key" |
| 60 | +python revenue_claim_cron_job.py |
| 61 | +``` |
| 62 | + |
| 63 | +#### Option B: Advanced Soroban Script |
| 64 | +```bash |
| 65 | +export CREATOR_ADDRESS="your_creator_address" |
| 66 | +export PRIVATE_KEY="your_private_key" |
| 67 | +python soroban_revenue_claimer.py |
| 68 | +``` |
| 69 | + |
| 70 | +#### Option C: Command Line Arguments |
| 71 | +```bash |
| 72 | +python revenue_claim_cron_job.py \ |
| 73 | + --creator GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \ |
| 74 | + --threshold 100 \ |
| 75 | + --interval 300 \ |
| 76 | + --private-key YOUR_PRIVATE_KEY |
| 77 | +``` |
| 78 | + |
| 79 | +## Configuration Options |
| 80 | + |
| 81 | +| Environment Variable | Description | Default | |
| 82 | +|----------------------|-------------|---------| |
| 83 | +| `CREATOR_ADDRESS` | Your Stellar creator address | Required | |
| 84 | +| `PRIVATE_KEY` | Your private key for signing transactions | Required | |
| 85 | +| `REVENUE_THRESHOLD` | Revenue threshold in USDC | 100 | |
| 86 | +| `CHECK_INTERVAL` | Check interval in seconds | 300 (5 minutes) | |
| 87 | +| `NETWORK_URL` | Stellar network URL | Testnet | |
| 88 | +| `CONTRACT_ADDRESS` | SubStream contract address | Testnet contract | |
| 89 | +| `USDC_ADDRESS` | USDC token address | Testnet USDC | |
| 90 | + |
| 91 | +## Monitoring Mode |
| 92 | + |
| 93 | +To run in monitoring mode (without actually claiming revenue): |
| 94 | + |
| 95 | +```bash |
| 96 | +python revenue_claim_cron_job.py --monitor-only |
| 97 | +``` |
| 98 | + |
| 99 | +This will check for pending revenue and log what would be claimed without submitting transactions. |
| 100 | + |
| 101 | +## Production Deployment |
| 102 | + |
| 103 | +### Using systemd |
| 104 | + |
| 105 | +Create a systemd service file `/etc/systemd/system/substream-revenue-claimer.service`: |
| 106 | + |
| 107 | +```ini |
| 108 | +[Unit] |
| 109 | +Description=SubStream Revenue Claimer |
| 110 | +After=network.target |
| 111 | + |
| 112 | +[Service] |
| 113 | +Type=simple |
| 114 | +User=your-username |
| 115 | +WorkingDirectory=/path/to/SubStream-Protocol-Contracts/scripts |
| 116 | +Environment=CREATOR_ADDRESS=your_creator_address |
| 117 | +Environment=PRIVATE_KEY=your_private_key |
| 118 | +Environment=REVENUE_THRESHOLD=100 |
| 119 | +Environment=CHECK_INTERVAL=300 |
| 120 | +ExecStart=/usr/bin/python3 soroban_revenue_claimer.py |
| 121 | +Restart=always |
| 122 | +RestartSec=10 |
| 123 | + |
| 124 | +[Install] |
| 125 | +WantedBy=multi-user.target |
| 126 | +``` |
| 127 | + |
| 128 | +Enable and start the service: |
| 129 | +```bash |
| 130 | +sudo systemctl enable substream-revenue-claimer |
| 131 | +sudo systemctl start substream-revenue-claimer |
| 132 | +``` |
| 133 | + |
| 134 | +### Using Docker |
| 135 | + |
| 136 | +Create a `Dockerfile`: |
| 137 | + |
| 138 | +```dockerfile |
| 139 | +FROM python:3.11-slim |
| 140 | + |
| 141 | +WORKDIR /app |
| 142 | +COPY requirements.txt . |
| 143 | +RUN pip install -r requirements.txt |
| 144 | + |
| 145 | +COPY . . |
| 146 | +CMD ["python", "soroban_revenue_claimer.py"] |
| 147 | +``` |
| 148 | + |
| 149 | +Build and run: |
| 150 | +```bash |
| 151 | +docker build -t substream-revenue-claimer . |
| 152 | +docker run -d \ |
| 153 | + --name revenue-claimer \ |
| 154 | + --restart unless-stopped \ |
| 155 | + -e CREATOR_ADDRESS="your_creator_address" \ |
| 156 | + -e PRIVATE_KEY="your_private_key" \ |
| 157 | + -e REVENUE_THRESHOLD="100" \ |
| 158 | + -e CHECK_INTERVAL="300" \ |
| 159 | + substream-revenue-claimer |
| 160 | +``` |
| 161 | + |
| 162 | +## Security Considerations |
| 163 | + |
| 164 | +1. **Private Key Security**: Never commit private keys to version control. Use environment variables or secure key management systems. |
| 165 | + |
| 166 | +2. **Hardware Wallets**: For production use, consider using hardware wallets or secure key management services. |
| 167 | + |
| 168 | +3. **Network Security**: Ensure your server is properly secured with firewalls and regular updates. |
| 169 | + |
| 170 | +4. **Monitoring**: Set up monitoring and alerts for the cron job to ensure it's running properly. |
| 171 | + |
| 172 | +## Troubleshooting |
| 173 | + |
| 174 | +### Common Issues |
| 175 | + |
| 176 | +1. **"Account not found"**: Ensure the creator address is correct and the account exists on the network. |
| 177 | + |
| 178 | +2. **"Insufficient fee"**: Increase the base fee in the transaction builder. |
| 179 | + |
| 180 | +3. **"Transaction failed"**: Check the transaction result XDR for specific error details. |
| 181 | + |
| 182 | +4. **"Connection timeout"**: Check network connectivity and RPC endpoint availability. |
| 183 | + |
| 184 | +### Debug Mode |
| 185 | + |
| 186 | +Enable debug logging: |
| 187 | +```bash |
| 188 | +export RUST_LOG=debug # For Rust script |
| 189 | +export PYTHONPATH=. # For Python scripts |
| 190 | +python -v revenue_claim_cron_job.py |
| 191 | +``` |
| 192 | + |
| 193 | +## How It Works |
| 194 | + |
| 195 | +1. **Monitoring**: The script periodically checks all subscriptions where the creator is a recipient. |
| 196 | + |
| 197 | +2. **Simulation**: For each subscription, it simulates the `collect` function to determine how much revenue can be claimed. |
| 198 | + |
| 199 | +3. **Threshold Check**: It compares the claimable amount against the configured threshold. |
| 200 | + |
| 201 | +4. **Transaction Submission**: If above threshold, it submits a transaction calling the `collect` function. |
| 202 | + |
| 203 | +5. **Confirmation**: It waits for transaction confirmation and logs the result. |
| 204 | + |
| 205 | +## Contract Integration |
| 206 | + |
| 207 | +The script integrates with the SubStream Protocol contract's `collect` function: |
| 208 | + |
| 209 | +```rust |
| 210 | +pub fn collect(env: Env, subscriber: Address, creator: Address) { |
| 211 | + distribute_and_collect(&env, &subscriber, &creator, Some(&creator)); |
| 212 | +} |
| 213 | +``` |
| 214 | + |
| 215 | +This function: |
| 216 | +- Calculates the amount to collect based on streaming duration and rates |
| 217 | +- Handles discounted pricing for long-term subscribers |
| 218 | +- Transfers the collected amount to the creator |
| 219 | +- Updates the subscription state |
| 220 | + |
| 221 | +## Contributing |
| 222 | + |
| 223 | +When contributing to the revenue claimer: |
| 224 | + |
| 225 | +1. Test thoroughly on testnet before mainnet deployment |
| 226 | +2. Ensure proper error handling and logging |
| 227 | +3. Follow security best practices for key management |
| 228 | +4. Add comprehensive tests for new features |
| 229 | + |
| 230 | +## License |
| 231 | + |
| 232 | +This code is part of the SubStream Protocol project and follows the same license terms. |
0 commit comments