-
Notifications
You must be signed in to change notification settings - Fork 44
/
balance.py
175 lines (155 loc) · 5.02 KB
/
balance.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
import discord, json, requests, pymysql.cursors
from cogs.utils import rpc
from discord.ext import commands
#result_set = database response with parameters from query
#db_bal = nomenclature for result_set["balance"]
#author = author from message context, identical to user in database
#wallet_bal = nomenclature for wallet reponse
class rpc:
def listtransactions(params,count):
port = "11311"
rpc_user = 'srf2UUR0'
rpc_pass = 'srf2UUR0XomxYkWw'
serverURL = 'http://localhost:'+port
headers = {'content-type': 'application/json'}
payload = json.dumps({"method": "listtransactions", "params": [params,count], "jsonrpc": "2.0"})
response = requests.get(serverURL, headers=headers, data=payload, auth=(rpc_user,rpc_pass))
return(response.json()['result'])
class Balance:
def __init__(self, bot):
self.bot = bot
#//Establish connection to db//
self.connection = pymysql.connect(
host='localhost',
user='root',
password='',
db='netcoin')
self.cursor = self.connection.cursor(pymysql.cursors.DictCursor)
def make_user(self, author):
print(author)
to_exec("""
INSERT INTO db(user,balance)
VALUES('%s','%s')
""")
self.cursor.execute(to_exec, str(author), '0')
self.connection.commit()
return
def check_for_user(self, author):
try:
to_exec("""
SELECT user
FROM db
WHERE user
LIKE '%s'
""")
self.cursor.execute(to_exec, str(author))
result_set = self.cursor.fetchone()
except Exception as e:
print("Error in SQL query: ",str(e))
return
if result_set == None:
self.make_user(author)
return
def update_db(self, author, db_bal, lastblockhash):
try:
to_exec("""
UPDATE db
SET balance='%s', lastblockhash='%s'
WHERE user
LIKE '%s'
""")
self.cursor.execute(to_exec, db_bal,lastblockhash,str(author))
self.connection.commit()
except Exception as e:
print("Error: "+str(e))
return
async def do_embed(self, author, db_bal):
embed = discord.Embed(colour=discord.Colour.red())
embed.add_field(name="User", value=author)
embed.add_field(name="Balance (NET)", value="%.8f" % round(float(db_bal),8))
embed.set_footer(text="Sponsored by altcointrain.com - Choo!!! Choo!!!")
try:
await self.bot.say(embed=embed)
except discord.HTTPException:
await self.bot.say("I need the `Embed links` permission to send this")
return
async def parse_part_bal(self,result_set,author):
params = author
count = 1000
get_transactions = rpc.listtransactions(params,count)
print(len(get_transactions))
i = len(get_transactions)-1
new_balance = float(result_set["balance"])
lastblockhash = get_transactions[i]["blockhash"]
print("LBH: ",lastblockhash)
if lastblockhash == result_set["lastblockhash"]:
db_bal = result_set["balance"]
await self.do_embed(author, db_bal)
return
else:
while i <= len(get_transactions):
if get_transactions[i]["blockhash"] != result_set["lastblockhash"]:
new_balance += float(get_transactions[i]["amount"])
i -= 1
else:
new_balance += float(get_transactions[i]["amount"])
break
db_bal = new_balance
self.update_db(author, db_bal, lastblockhash)
await self.do_embed(author, db_bal)
async def parse_whole_bal(self,result_set,author):
params = author
user = params
count = 1000
get_transactions = rpc.listtransactions(params,count)
print(len(get_transactions))
i = len(get_transactions)-1
if len(get_transactions) == 0:
print("0 transactions found for "+author+", balance must be 0")
db_bal = 0
await self.do_embed(author, db_bal)
else:
new_balance = 0
lastblockhash = get_transactions[i]["blockhash"]
firstblockhash = get_transactions[0]["blockhash"]
print("FBH: ",firstblockhash)
print("LBH: ",lastblockhash)
while i <= len(get_transactions)-1:
if get_transactions[i]["blockhash"] != firstblockhash:
new_balance += float(get_transactions[i]["amount"])
i -= 1
print("New Balance: ",new_balance)
else:
new_balance += float(get_transactions[i]["amount"])
print("New Balance: ",new_balance)
break
db_bal = new_balance
self.update_db(author, db_bal, lastblockhash)
await self.do_embed(author, db_bal)
#Now update db with new balance
@commands.command(pass_context=True)
async def balance(self, ctx):
#//Set important variables//
author = str(ctx.message.author)
#//Check if user exists in db
self.check_for_user(author)
#//Execute and return SQL Query
try:
to_exec("""
SELECT balance, user, lastblockhash, tipped
FROM db
WHERE user
LIKE '%s'
""")
self.cursor.execute(to_exec,str(author))
result_set = self.cursor.fetchone()
except Exception as e:
print("Error in SQL query: ",str(e))
return
#//
if result_set["lastblockhash"] == "0":
await self.parse_whole_bal(result_set,author)
else:
await self.parse_part_bal(result_set,author)
def setup(bot):
bot.add_cog(Balance(bot))