-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathbanano_payout.py
More file actions
213 lines (174 loc) · 7.3 KB
/
banano_payout.py
File metadata and controls
213 lines (174 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
"""
BoTTube Banano Payout Processor
Cron script to process pending BAN withdrawals using bananopie.
Uses bananopie library for correct Ed25519-Blake2b block signing.
Constructs proper state blocks and submits via 'process' RPC.
Usage:
*/5 * * * * cd /root/bottube && python3 banano_payout.py >> /var/log/banano_payout.log 2>&1
"""
import json
import os
import sqlite3
import sys
import time
import urllib.request
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
DB_PATH = os.environ.get("BOTTUBE_DB", "/root/bottube/bottube.db")
KALIUM_API = "https://kaliumapi.appditto.com/api"
KALIUM_FALLBACK = "https://public.node.jungletv.live/rpc"
BANANO_SEED = os.environ.get("BANANO_SEED", "")
BAN_RAW_MULTIPLIER = 10**29
# Platform hot wallet is index 0
PLATFORM_WALLET_INDEX = 0
# Try importing bananopie
try:
from bananopie import RPC as BananoRPC, Wallet as BananoWallet, whole_to_raw, raw_to_whole
HAS_BANANOPIE = True
except ImportError:
HAS_BANANOPIE = False
def log(level, msg):
ts = time.strftime("%Y-%m-%d %H:%M:%S")
print(f"[{ts}] [{level}] {msg}", flush=True)
def _ban_rpc(action_data: dict) -> dict:
"""Send an RPC request with fallback."""
for api_url in [KALIUM_API, KALIUM_FALLBACK]:
try:
payload = json.dumps(action_data).encode()
req = urllib.request.Request(
api_url,
data=payload,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(req, timeout=30) as resp:
result = json.loads(resp.read())
if "error" not in result:
return result
if api_url == KALIUM_FALLBACK:
return result
except Exception as e:
if api_url == KALIUM_FALLBACK:
return {"error": str(e)}
return {"error": "all RPC endpoints failed"}
def process_withdrawals():
"""Process all pending BAN withdrawals."""
if not BANANO_SEED:
log("WARN", "BANANO_SEED not set, skipping")
return
if not HAS_BANANOPIE:
log("ERROR", "bananopie not installed. Install with: pip install bananopie")
return
db = sqlite3.connect(DB_PATH)
db.row_factory = sqlite3.Row
pending = db.execute(
"SELECT bt.*, bw.account_index FROM ban_transactions bt "
"LEFT JOIN ban_wallets bw ON bt.agent_id = bw.agent_id "
"WHERE bt.status = 'pending' AND bt.tx_type = 'withdrawal' "
"ORDER BY bt.created_at ASC LIMIT 10"
).fetchall()
if not pending:
db.close()
return
# Create bananopie wallet for platform hot wallet (index 0)
rpc = BananoRPC(KALIUM_API)
wallet = BananoWallet(rpc, seed=BANANO_SEED, index=PLATFORM_WALLET_INDEX)
platform_address = wallet.get_address()
log("INFO", f"Processing {len(pending)} pending withdrawals from {platform_address}")
# First, receive any pending deposits to ensure balance is available
try:
wallet.receive_all()
log("INFO", "Received all pending deposits")
except Exception as e:
log("WARN", f"receive_all failed (may be no pending): {e}")
# Check on-chain balance
try:
balance_info = rpc.get_account_balance(platform_address)
balance_raw = int(balance_info.get("balance", "0"))
balance_ban = balance_raw / BAN_RAW_MULTIPLIER
log("INFO", f"Platform balance: {balance_ban:.4f} BAN")
except Exception as e:
log("ERROR", f"Could not check balance: {e}")
db.close()
return
for tx in pending:
tx_id = tx["id"]
amount_ban = tx["amount_ban"]
reason = tx["reason"]
# Extract destination address from reason field
to_address = ""
if reason.startswith("withdraw_to_"):
to_address = reason[len("withdraw_to_"):]
if not to_address or not to_address.startswith("ban_"):
log("WARN", f"TX#{tx_id}: Invalid destination in reason: {reason}")
db.execute(
"UPDATE ban_transactions SET status = 'failed', processed_at = ? WHERE id = ?",
(time.time(), tx_id),
)
db.commit()
continue
# Check if we have enough balance
if balance_ban < amount_ban:
log("ERROR", f"TX#{tx_id}: Insufficient platform balance "
f"({balance_ban:.4f} < {amount_ban:.4f}). Stopping.")
break
log("INFO", f"TX#{tx_id}: Sending {amount_ban} BAN to {to_address}")
try:
# bananopie.Wallet.send() constructs a proper state block:
# 1. Fetches account_info for frontier + current balance
# 2. Computes new_balance = current_balance - amount_raw
# 3. Constructs state block with link = recipient public key
# 4. Signs with Ed25519-Blake2b (correct for Banano)
# 5. Computes PoW (locally or via RPC)
# 6. Submits via 'process' RPC
block_hash = wallet.send(to_address, str(amount_ban))
log("OK", f"TX#{tx_id}: Sent! Block: {block_hash}")
db.execute(
"UPDATE ban_transactions SET status = 'sent', block_hash = ?, processed_at = ? WHERE id = ?",
(str(block_hash), time.time(), tx_id),
)
balance_ban -= amount_ban
except Exception as e:
error_str = str(e)
log("ERROR", f"TX#{tx_id}: Failed - {error_str}")
# Mark as failed only for permanent errors
if any(kw in error_str.lower() for kw in ["insufficient", "invalid", "bad", "fork"]):
db.execute(
"UPDATE ban_transactions SET status = 'failed', processed_at = ? WHERE id = ?",
(time.time(), tx_id),
)
# Otherwise leave as pending for retry on next cron run
db.commit()
time.sleep(1) # Rate limit between sends
db.close()
log("INFO", "Payout processing complete")
def check_balance():
"""Quick balance check utility."""
if not BANANO_SEED:
print("BANANO_SEED not set")
return
if HAS_BANANOPIE:
rpc = BananoRPC(KALIUM_API)
wallet = BananoWallet(rpc, seed=BANANO_SEED, index=0)
address = wallet.get_address()
else:
import hashlib
seed_bytes = bytes.fromhex(BANANO_SEED)
index_bytes = (0).to_bytes(4, "big")
private_key = hashlib.blake2b(seed_bytes + index_bytes, digest_size=32).hexdigest()
resp = _ban_rpc({"action": "key_expand", "key": private_key})
address = resp.get("account", "unknown")
resp = _ban_rpc({"action": "account_balance", "account": address})
balance_raw = int(resp.get("balance", "0"))
receivable_raw = int(resp.get("receivable", resp.get("pending", "0")))
print(f"Platform wallet: {address}")
print(f"Balance: {balance_raw / BAN_RAW_MULTIPLIER:.4f} BAN")
print(f"Receivable: {receivable_raw / BAN_RAW_MULTIPLIER:.4f} BAN")
print(f"bananopie: {'available' if HAS_BANANOPIE else 'NOT INSTALLED'}")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "balance":
check_balance()
else:
process_withdrawals()