forked from krruzic/trtlbotplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
124 lines (114 loc) · 4.35 KB
/
utils.py
File metadata and controls
124 lines (114 loc) · 4.35 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
import random
import sys
import os
import binascii
import json
from jsonrpc_requests import Server
from models import Transaction, TipJar
class TrtlServer(Server):
def dumps(self, data):
data['password'] = config['rpc_password']
return json.dumps(data)
config = json.load(open('config.json'))
if 'rpc_port' in config:
rpc_port = config['rpc_port']
else:
rpc_port = "8070"
rpc = TrtlServer("http://127.0.0.1:{}/json_rpc".format(rpc_port))
daemon = TrtlServer("http://127.0.0.1:11898/json_rpc")
CONFIRMED_TXS = []
def format_hash(hashrate):
i = 0
byteUnits = [" H", " KH", " MH", " GH", " TH", " PH"]
while (hashrate > 1000):
hashrate = hashrate / 1000
i = i+1
return str(round(hashrate,2)) + byteUnits[i]
def gen_paymentid(address):
rng = random.Random(address+config['token'])
results = []
length = 32
chunk_size = 65535
chunks = []
while length >= chunk_size:
chunks.append(rng.getrandbits(chunk_size * 8).to_bytes(chunk_size, sys.byteorder))
length -= chunk_size
if length:
chunks.append(rng.getrandbits(length * 8).to_bytes(length, sys.byteorder))
result = b''.join(chunks)
return "".join(map(chr, binascii.hexlify(result)))
def get_deposits(starting_height, session):
transactionData = rpc.getTransactions(firstBlockIndex=starting_height-10, blockCount=1000)
for item in transactionData['items']:
for tx in item['transactions']:
if tx['paymentId'] == '':
continue
if tx['transactionHash'] in CONFIRMED_TXS:
continue
if tx['unlockTime'] <= 3:
CONFIRMED_TXS.append({'transactionHash': tx['transactionHash'],'ready':True, 'pid': tx['paymentId']})
if tx['unlockTime'] > 3:
CONFIRMED_TXS.append({'transactionHash': tx['transactionHash'],'ready':False, 'pid': tx['paymentId']})
print("appended all txs to list... {}".format(len(CONFIRMED_TXS)))
for i,trs in enumerate(CONFIRMED_TXS):
print(trs)
processed = session.query(Transaction).filter(Transaction.tx == trs['transactionHash']).first()
amount = 0
if processed:
CONFIRMED_TXS.pop(i)
continue
data = rpc.getTransaction(transactionHash=trs['transactionHash'])
print("data from RPC:")
print(data)
if not trs['ready']:
if data['unlockTime'] != 0:
continue
else:
trs['ready'] = True
likestring = data['transaction']['paymentId'][0:58]
print(likestring)
balance = session.query(TipJar).filter(TipJar.paymentid.contains(likestring)).first()
if not balance:
print("user does not exist!")
continue
for transfer in data['transaction']['transfers']:
print("updating user balance!")
address = transfer['address']
amount = transfer['amount']
change = 0
if address in rpc.getAddresses()['addresses']:
print("deposit of {}".format(amount))
change += amount
elif address != "" and trs['pid'] == (balance.paymentid[0:58] + balance.withdraw): # money leaving tipjar, remove from user's balance
print("withdrawal of {}".format(amount))
change -= (amount+data['transaction']['fee'])
try:
balance.amount += change
except:
balance.amount = change
print("new balance: {}".format(balance.amount))
session.commit()
nt = Transaction(trs['transactionHash'])
CONFIRMED_TXS.pop(i)
yield nt
def get_fee(amount):
if amount < 10000000:
return 10
elif amount > 10000000 and amount < 30000000:
return 100
elif amount > 30000000:
return 300
def build_transfer(address, amount, self_address, balance):
print("SEND PID: {}".format(balance.paymentid[0:58] + balance.withdraw))
params = {
'fee': get_fee(amount),
'paymentId': balance.paymentid[0:58] + balance.withdraw,
'anonymity': 3,
'transfers': [
{
'amount': amount,
'address': address
}
]
}
return params