Skip to content

Commit 6c4fbdf

Browse files
Merge pull request #91 from Didi-vi/protocol_chain
Implement Revenue_Claim_Cron_Job for automatic revenue claiming
2 parents 4505ed1 + 9322f2d commit 6c4fbdf

File tree

8 files changed

+1670
-0
lines changed

8 files changed

+1670
-0
lines changed

scripts/.env.example

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Revenue Claim Cron Job Configuration
2+
3+
# Required: Creator's Stellar address
4+
CREATOR_ADDRESS=GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF
5+
6+
# Optional: Revenue threshold in USDC (default: 100)
7+
REVENUE_THRESHOLD=100
8+
9+
# Optional: Check interval in seconds (default: 300 = 5 minutes)
10+
CHECK_INTERVAL=300
11+
12+
# Optional: Stellar network URL (default: testnet)
13+
NETWORK_URL=https://horizon-testnet.stellar.org
14+
15+
# Optional: SubStream contract address (default: testnet contract)
16+
CONTRACT_ADDRESS=CAOUX2FZ65IDC4F2X7LJJ2SVF23A35CCTZB7KVVN475JCLKTTU4CEY6L
17+
18+
# Optional: Creator's private key for signing transactions
19+
# WARNING: Keep this secure! Use a hardware wallet or secure key management in production
20+
PRIVATE_KEY=YOUR_PRIVATE_KEY_HERE
21+
22+
# Optional: USDC token address (testnet default)
23+
USDC_ADDRESS=GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5

scripts/README.md

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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.

scripts/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
stellar-sdk>=9.0.0
2+
soroban-rpc>=0.8.0
3+
requests>=2.31.0
4+
python-dotenv>=1.0.0
5+
aiohttp>=3.8.0

0 commit comments

Comments
 (0)