-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrail.py
305 lines (259 loc) · 10.7 KB
/
rail.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import threading
import datetime
import requests
import time
import traceback
import os
import configparser
from requests.auth import HTTPBasicAuth
from db import get_paid, new_status, get_new, get_processing, get_errored, record_tx
from btc_wallet import bstartd, bstopd, btx_many
from ltc_wallet import lstartd, lstopd, ltx_many
processing = dict()
processing_ltc = dict()
current_orders = list()
executing = {
'btc': dict(),
'ltc': dict()
}
wallet_btc = '/home/bitrail/.electrum/wallets/segwit'
wallet_ltc = '/home/bitrail/.electrum-ltc/wallets/segwit'
rail_config = configparser.ConfigParser()
rail_config.read('config.ini')
#fees total per tx
fees = {
'btc': 1200,
'ltc': 1200
}
rates = {
'btcusd': 0,
'btceur': 0,
'ltcbtc': 0,
}
pay_for_all = False
def rate_check():
threading.Timer(180.0, rate_check).start()
print('refreshing rates...')
btcusd_get = requests.get('https://www.bitstamp.net/api/v2/ticker/btcusd')
time.sleep(1)
btceur_get = requests.get('https://www.bitstamp.net/api/v2/ticker/btceur')
time.sleep(1)
ltcbtc_get = requests.get('https://www.bitstamp.net/api/v2/ticker/ltcbtc')
if (btcusd_get.status_code == 200) and (btceur_get.status_code == 200) and (ltcbtc_get.status_code == 200):
print('renewing BTC/USD:' + "{:.2f}".format(rates['btcusd']) + ' -> ' + str(btcusd_get.json()['last']))
rates['btcusd'] = float(btcusd_get.json()['last'])
print('renewing BTC/EUR:' + "{:.2f}".format(rates['btceur']) + ' -> ' + str(btceur_get.json()['last']))
rates['btceur'] = float(btceur_get.json()['last'])
print('renewing LTC/BTC:' + "{:.8f}".format(rates['ltcbtc']) + ' -> ' + str(ltcbtc_get.json()['last']))
rates['ltcbtc'] = float(ltcbtc_get.json()['last'])
print('new rates saved!')
else:
print('error saving rates!')
print("updating fees")
# c-lightning fee estimate
fees_get = requests.get('http://145.239.239.40:5000/rateinfo',
auth=HTTPBasicAuth('ohr7zoh8Ogei7ze', 'Ophu0shohX3zie4'))
if fees_get.status_code == 200:
newfees = fees_get.json()
btc_fee = int(newfees['btc']['onchain_fee_estimates']['opening_channel_satoshis'])
try:
ltc_fee = int(newfees['ltc']['onchain_fee_estimates']['opening_channel_satoshis'])
except Exception as e:
ltc_fee = int(newfees['ltc']['perkb']['min_acceptable'])
print('btc fee: ' + str(btc_fee))
print('ltc fee: ' + str(ltc_fee))
print('full: ' + str(newfees))
fees['btc'] = int(btc_fee / 2.7)
fees['btc_full'] = newfees['btc']
fees['ltc'] = int(ltc_fee / 2.7)
fees['ltc_full'] = newfees['ltc']
else:
print('fee estimation failed: code + ' + str(fees_get.status_code))
def txcheck():
threading.Timer(9.0, txcheck).start()
invoices = get_paid()
inv_count = 0
for inv in invoices:
inv_count += 1
addr = inv[2]
satoshi = inv[3]
charge_id = inv[4]
status = inv[5]
dtime = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
amount = satoshi / 100000000
if addr.startswith("L") or addr.startswith("M") or addr.startswith("ltc1"):
print(dtime + ': (LTC) found ' + status + ' invoice ' + charge_id + ', sat: ' + str(satoshi))
print(charge_id + 'mark as in progress')
order_data = {
'address': addr,
'amount_ltc': amount
}
processing_ltc[charge_id] = order_data
new_status(charge_id, 'processing')
else:
print(dtime+': found ' + status + ' invoice ' + charge_id + ', sat: ' + str(satoshi))
print(charge_id + 'mark as in progress')
order_data = {
'address': addr,
'amount_btc': amount
}
processing[charge_id] = order_data
new_status(charge_id, 'processing')
for order in current_orders:
if order['charge_id'] == charge_id:
if order['fast']:
global pay_for_all
pay_for_all = True
print('========= stats =========')
print('paid orders: ' + str(inv_count))
newlist = get_new()
print('new orders: ' + str(len(newlist)))
processinglist = get_processing()
print('orders in process: ' + str(len(processinglist)))
errorlist = get_errored()
print('orders with error: ' + str(len(errorlist)))
print('========== queue ========')
for inv in processing:
inv_count += 1
order_data = processing[inv]
amount = '%.8f' % float(order_data['amount_btc'])
print(amount + ' btc >> ' + order_data['address'])
for inv in processing_ltc:
inv_count += 1
order_data = processing_ltc[inv]
amount = '%.8f' % float(order_data['amount_ltc'])
print(amount + ' ltc >> ' + order_data['address'])
print('=========== * * * ========')
def batch():
threading.Timer(70.0, batch).start()
inv_count = 0
btc_addr_str = '['
rcpts = 0
ids = list()
print('starting bitcoin batching..')
receiver_dict_optimized = dict()
for inv in processing:
inv_count += 1
rcpts += 1
order_data = processing[inv]
ids.append(inv)
try:
receiver_dict_optimized[order_data['address']] += order_data['amount_btc']
except KeyError as e:
receiver_dict_optimized[order_data['address']] = order_data['amount_btc']
final_output_count = 0
for receiver in receiver_dict_optimized:
final_output_count += 1
btc_addr_str = btc_addr_str + '["' + receiver + '", ' + "{:.8f}".format(receiver_dict_optimized[receiver]) + '],'
btc_addr_str = btc_addr_str[:-1] + ']'
# print('chargepool: ' + str(btcchargepool))
total_kb = (100 + (final_output_count * 50)) / 1000
fee_urgent = fees['btc_full']['perkb']['normal']
print('BTC FEE:')
print('per client: ' + str(fees['btc']))
print('for tx (est. normal): ' + "{:.1f}".format(fee_urgent*total_kb) + ', absolute fee: ' + "{:.8f}".format(round(int(fee_urgent*total_kb)/100000000,8)))
print('current kb size: ' + str(total_kb))
print('currently collected from ' + str(inv_count) + ' calls: ' + str(inv_count*fees['btc']))
print('real fee to pay currently (with merged outputs): ' + str(final_output_count*fees['btc']))
print('current output count: ' + str(final_output_count))
global pay_for_all
if (final_output_count*fees['btc'] > fee_urgent*total_kb) or pay_for_all:
pay_for_all = False
print('we want to send BTC: ' + btc_addr_str)
network_fee = round(int(fee_urgent*total_kb)/100000000,8)
paymentfee = network_fee
try:
dtime = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
for id in ids:
executing['btc'][id] = dtime
processing.pop(id)
btc_txstatus = btx_many(btc_addr_str, wallet_btc, "{:.8f}".format(paymentfee))
status = btc_txstatus.decode('utf-8')
print("BTC tx status:\n" + str(status))
print("fee paid: " + "{:.8f}".format(paymentfee))
os.system("echo '" + dtime + " TX BTC OK:" + str(status) + "' >> /tmp/tx.log")
record_tx(dtime, status, btc_addr_str, 'ok', 'BTC')
for id in ids:
new_status(id, 'completed')
executing['btc'].pop(id)
except Exception as e:
error = traceback.format_exc()
print('error during btc payment' + ": " + error)
record_tx(dtime, 'none txid', btc_addr_str, 'error', 'BTC')
i = 0
for id in ids:
new_status(id, 'error')
executing['btc'].pop(id)
ids.pop(i)
i+=1
else:
print('fees not enough collected')
def batch_ltc():
threading.Timer(6000.0, batch_ltc).start()
inv_count = 0
ltc_addr_str = '['
rcpts = 0
ids = list()
print('starting litecoin batching..')
receiver_dict_optimized = dict()
for inv in processing_ltc:
inv_count += 1
rcpts += 1
order_data = processing_ltc[inv]
ids.append(inv)
try:
receiver_dict_optimized[order_data['address']] += order_data['amount_ltc']
except KeyError as e:
receiver_dict_optimized[order_data['address']] = order_data['amount_ltc']
###### JOIN OUTPUTS
for receiver in receiver_dict_optimized:
ltc_addr_str = ltc_addr_str + '["' + receiver + '", ' + "{:.8f}".format(receiver_dict_optimized[receiver]) + '],'
ltc_addr_str = ltc_addr_str[:-1] + ']'
total_kb = (100 + (inv_count * 50)) / 1000
try:
fee_urgent = fees['ltc_full']['perkb']['urgent']
except Exception as e:
fee_urgent = 30000
#print('LTC FEE (lites):')
#print('per client: ' + str(fees['ltc']))
#print('for tx (est. urgent): ' + str(fee_urgent * total_kb))
#print('currently collected from ' + str(inv_count) + ' users: ' + str(inv_count * fees['ltc']))
# multiply by 10 to make txs less frequently
if inv_count * fees['ltc'] > 3*fee_urgent*total_kb:
print('we want to send LTC: ' + ltc_addr_str)
total_kb = (100 + (inv_count * 50)) / 1000
network_fee = round(int(fee_urgent*total_kb) / 100000000, 8)
paymentfee = network_fee
try:
dtime = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
for id in ids:
executing['ltc'][id] = dtime
processing_ltc.pop(id)
ltc_txstatus = ltx_many(ltc_addr_str, wallet_ltc, "{:.8f}".format(paymentfee))
status = ltc_txstatus.decode('utf-8')
print("LTC tx status:\n" + str(status))
print("fee paid: " + "{:.8f}".format(paymentfee))
os.system("echo '"+dtime+" TX LTC OK:" + str(status) + "' >> /tmp/tx.log")
record_tx(dtime, status, ltc_addr_str, 'ok', 'LTC')
for id in ids:
new_status(id, 'completed')
executing['ltc'].pop(id)
except Exception as e:
error = traceback.format_exc()
print('error during ltc payment' + ": " + error)
os.system("echo '" + dtime + " TX ERROR:" + error + "' >> /root/tx.log")
record_tx(dtime, 'none txid', ltc_addr_str, 'error', 'BTC')
i = 0
for id in ids:
new_status(id, 'error')
executing['ltc'].pop(id)
ids.pop(i)
i+=1
else:
print('no LTC transactions to be processed, batching canceled')
rate_check()
bstartd()
lstartd()
txcheck()
batch()
batch_ltc()