forked from vivek0/WhatBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tipbot.py
319 lines (266 loc) · 10.5 KB
/
tipbot.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# -*- coding: utf-8 -*-
import MySQLdb
from block_io import BlockIo
import random
import string
import urllib
import json
import price
import requests
def getApiKey(name):
cur.execute("select aes_decrypt(coinapi,'%s') from whatdata where user = '%s'" %(key,name))
return cur.fetchall()[0][0]
def coincheck(name):
if not(userExists(name)):
return "Not registered. Use !register"
if not(apiExists(name)):
return "Api key is not added."
return ""
def coinAdd(text,name):
if not(userExists(name)):
return "Not registered. Use !register"
api = text[1]
cur.execute("update whatdata set coinapi = aes_encrypt('%s','%s') where user = '%s'" %(api,key,name))
db.commit()
return "Api key added."
def cancelBidAsk(text,name,cancelType):
stats = coincheck(name)
if(stats):
return stats
api_key = getApiKey(name)
oid = text[1]
data = {"apiKey":api_key,"orderID":oid}
header = {'content-type': 'application/json'}
url = "https://api.coinsecureis.cool/v0/auth/%s" %(cancelType)
post_data = requests.post(url,data = json.dumps(data),headers=header).json()
if "error" in post_data.keys():
return post_data["error"]
if type(post_data["result"][0]) == dict:
return "%s" %post_data["result"][0]["error"]
return "%s (%s) cancelled." %(cancelType.replace("cancel",""),oid)
def placeBidAsk(text,name,createType):
stats = coincheck(name)
if(stats):
return stats
api_key = getApiKey(name)
rate = int(float(text[1]) * 100)
vol = int(float(text[2]) * 100000000)
data = {"apiKey":api_key,"rate":rate,"vol":vol}
header = {'content-type': 'application/json'}
url = "https://api.coinsecureis.cool/v0/auth/%s" %(createType)
post_data = requests.post(url,data = json.dumps(data),headers=header).json()
if "error" in post_data.keys():
return post_data["error"]
if type(post_data["result"][0]) == dict:
return "%s" %post_data["result"][0]["error"]
return "%s created.\n ID : %s" %(createType.replace("create",""),post_data["result"][0])
def coinbal(name,baltype):
stats = coincheck(name)
if(stats):
return stats
api_key = getApiKey(name)
data = {"apiKey":api_key}
header = {'content-type': 'application/json'}
url = "https://api.coinsecureis.cool/v0/auth/%s" %(baltype)
post_data = requests.post(url,data = json.dumps(data),headers=header).json()
amount = float(post_data["result"][0])
if "fiat" in baltype:
amount /= 100
tag = "₹"
else:
amount /= 100000000
tag = "B"
if "error" in post_data.keys():
return post_data["error"]
return "Your coinsecure %s balance : %s%.2f" %(baltype.replace("balance",""),tag,amount)
def getCoinsecAddr(name):
stats = coincheck(name)
if(stats):
return stats
api_key = getApiKey(name)
data = {"apiKey":api_key}
header = {'content-type': 'application/json'}
post_data = requests.post("https://api.coinsecureis.cool/v0/auth/getcoinaddresses",data = json.dumps(data),headers=header).json()
if "error" in post_data.keys():
return post_data["error"]
return "Your coinsecure address : %s" %(post_data["result"][len(post_data["result"]) -1]["address"])
def rup(amount):
inrusd = float(price.iu)
value = amount * inrusd
usddoge = float(price.ud)
return int(usddoge * value)
def commands():
return("!register - Register accont.\n !balance - Check your balance. \n !tip - Tip others. \n !tag - Check your tag."
"\n !address - Check your address. \n !change - Change your tag. \n !market - check coin market. \n !convert - check n amount of coinA in coinB"
"\n\n Coinsecure commands : \n!coinapi - Add coinsecure api. \n !cbid - Create bid. \n !dbid - Cancel bid. \n !cask - Create ask. \n !dask - Cancel ask"
"\n!info - Check coinsecure market.\n !cbal - Check coin balance.\n !fbal - Check fiat balance.\n !addr - Get coinsecure address.")
def stats():
cur.execute("select * from whatdata")
data = cur.fetchall()
tmp = "Registered users : %d" %(len(data))
return tmp
def changeTag(text,name):
newTag = text[1]
if tagExists(newTag):
return "%s is taken" %(newTag)
else:
cur.execute("update whatdata set tag = '%s' where user = '%s'" %(newTag,name))
db.commit()
return "Tag changed"
def getUserTag(name):
if not(userExists(name)):
return "Not registered. Use !register"
else:
cur.execute("select tag from whatdata where user = '%s'" %(name))
return "Your tag : %s" %(cur.fetchall()[0][0])
def getUserAdd(name):
if not(userExists(name)):
return "Not registered. Use !register"
else:
cur.execute("select address from whatdata where user = '%s'" %(name))
return "Your address : %s" %(cur.fetchall()[0][0])
def tip(text,name):
ruppee = "off"
if not(userExists(name)):
return "Not registered. Use !register"
else:
try:
if "rs" in text[2]:
ruppee = "on"
text[2] = text[2].replace("rs","")
amount = int(text[2])
except ValueError:
return "Invalid amount : %r" %(text[2])
if amount <= 0:
return "Invalid amount : %r. should be > 1" %(text[2])
receiver = text[1]
print receiver
if not(tagExists(receiver)):
return "Tag doesn't exist"
cur_bal = balance(name,"yes")
if ruppee == "on":
amount = rup(amount)
if(amount > cur_bal):
return "You tried to tip %d but you only have %d" %(amount,cur_bal)
else:
cur.execute("update whatdata set balance = balance + %d where tag = '%s'" %(amount,receiver))
db.commit()
cur.execute("update whatdata set balance = balance - %d where user = '%s'" %(amount,name))
db.commit()
sender = getUserTag(name).replace("Your tag :","")
value = float(price.du)
inr = float(price.ui)
return "%s tipped Ð%d (₹%.2f) to %s" %(sender,amount,(amount*value) * inr ,receiver)
def getBal(name,x):
cur.execute("select balance,address from whatdata where user = '%s'" %(name))
fetch = cur.fetchall()
tmp_bal = fetch[0][0]
address = fetch[0][1]
bal_data = blockIo.get_address_balance(addresses="%s" %(address))
bal = int(float(bal_data["data"]["balances"][0]["available_balance"])) + tmp_bal
if x == "diff":
return bal - tmp_bal
elif x == "all":
return [tmp_bal,bal]
if x == "pending":
return int(float(bal_data["data"]["balances"][0]["pending_received_balance"]))
else:
return bal
def getTxData(txid,way):
if "in" in way:
page = urllib.urlopen("https://chain.so/api/v2/get_tx_inputs/DOGE/%s" %(txid))
else:
page = urllib.urlopen("https://chain.so/api/v2/get_tx_outputs/DOGE/%s" %(txid))
page_data = json.loads(page.read().decode('utf-8'))
if "in" in way:
return page_data["data"]["inputs"]
return page_data["data"]["outputs"]
def withdraw(text,name):
if not(userExists(name)):
return "Not registered. Use !register"
try:
amount = int(text[2])
except ValueError:
return "Invalid amount : %r" %(text[2])
if amount <= 0:
return "Invalid amount : %r. should be > 1" %(text[2])
address = text[1]
local_bal,cur_bal = getBal(name,"all")
if((amount + 1) > cur_bal):
return "Withdraw failed: insufficient balance."
else:
try:
data = blockIo.withdraw(amounts= "%d" %(amount), to_addresses="%s" %(address), pin="%s" %(pin))
except:
return "Withdraw failed."
if(data["status"] == "fail"):
return "Withdraw failed."
txid = data["data"]["txid"]
inout = getTxData(txid,"in")
for i in inout:
print("%d %s"%(int(float(i["value"])),i["address"]))
cur.execute("update whatdata set balance = balance + %d where address = '%s'" %(int(float(i["value"])),i["address"]))
db.commit()
inout = getTxData(txid,"out")
for i in inout:
print("%d %s"%(int(float(i["value"])),i["address"]))
cur.execute("update whatdata set balance = balance - %d where address = '%s'" %(int(float(i["value"])),i["address"]))
db.commit()
cur.execute("update whatdata set balance = balance - %d where user = '%s'" %((amount + 1),name))
db.commit()
return "Withdraw successfull.\n\n txid : %s" %(data["data"]["txid"])
def balance(name,val):
if not(userExists(name)):
return "Not registered. Use !register"
else:
print "here"
pen = getBal(name,"pending")
cur_bal = getBal(name,"")
print "got value"
if val == "yes":
return cur_bal
value = float(price.du)
inr = float(price.ui)
if not(pen):
return "Balance : Ð%d (₹%.2f)" %(cur_bal,(cur_bal*value)*inr)
else:
return "Balance : Ð%d (₹%.2f) (Pending --> Ð%d)" %(cur_bal,(cur_bal*value) * inr,pen)
def getTag():
tag = ''.join(random.choice(string.ascii_lowercase) for i in range(5))
cur.execute("select * from whatdata where tag = '%s'" %(tag))
if cur.fetchall():
getTag()
return tag
def tagExists(receiver):
cur.execute("select * from whatdata where tag = '%s'" %(receiver))
if cur.fetchall():
return True
return False
def userExists(name):
cur.execute("select * from whatdata where user = '%s'" %(name))
if cur.fetchall():
return True
return False
def apiExists(name):
cur.execute("select coinapi from whatdata where user = '%s' and coinapi is not NULL" %(name))
if cur.fetchall():
return True
return False
def register(name):
if userExists(name):
return "You are already registered."
else:
address = blockIo.get_new_address()["data"]["address"]
tag = getTag()
cur.execute("insert into whatdata(tag,user,address,balance) values('%s','%s','%s',0)" %(tag,name,address))
db.commit()
return "Your tipbot address : %s\nYour tag : %s\n\nUse !change to change your tag." %(address,tag)
def connectIO(pin,api):
version = 2
return BlockIo(api,pin,version)
db = MySQLdb.connect(host="",user="",passwd="",db="")
cur = db.cursor()
blockApi = "" #Block.io api key
pin = "" #Block.io pin
blockIo = connectIO(pin,blockApi)
key = "" #AES key