forked from delta1512/GRC-Wallet-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
147 lines (131 loc) · 5.79 KB
/
commands.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
from math import ceil
import random as r
import logging
import time
import io
import qrcode
from user import usr
import grcconf as g
import emotes as e
import wallet as w
import docs
def amt_filter(inp, userobj):
#if inp == 'all':
#return round(userobj.balance, 8)
try:
inp = float(inp)
if (inp < 0) or (inp <= g.MIN_TX) or (inp == float('inf')):
return None
else:
return round(inp, 8)
except:
return None
async def dump_cfg(price_fetcher):
block_height = await w.query('getblockcount', [])
block_hash = await w.query('getblockhash', [block_height])
if block_height < 5 or not isinstance(block_hash, str): # 5 is largest error return value
return '{}Could not access the Gridcoin client.'.format(e.ERROR)
return '''{}Bot is up. Configuration:```
Withdraw fee: {}
Transfer limit: {}
Required confirmations per withdraw: {}
Block height: {}
Latest hash: {}
Price (USD): {}```'''.format(e.ONLINE, g.tx_fee, g.MIN_TX, g.tx_timeout, block_height, block_hash, round(await price_fetcher.price(), 4))
async def new_user(uid):
try:
addr = await w.query('getnewaddress', [])
if not isinstance(addr, str):
logging.error('Failed to get address from GRC client')
raise RuntimeError('Error in communicating with client')
userobj = usr(uid, address=addr)
logging.info('New user registered successfully, UID: %s', uid)
return 0, '{}User account created successfully. Your address is `{}`'.format(e.GOOD, userobj.address), userobj
except (RuntimeError, ValueError) as E:
logging.error('New user registration failed with error: %s', E)
return 1, '{}Error: Something went wrong when attempting to make your user account.'.format(e.ERROR), None
async def fetch_balance(userobj, price_fetcher):
usrbal = userobj.balance
return '''{}Your balance for: `{}`
```{} GRC (${} USD)```'''.format(e.BAL, userobj.address, round(usrbal, 8), await price_fetcher.conv(usrbal))
async def donate(selection, amount, userobj):
amount = amt_filter(amount, userobj)
try:
selection = int(selection)-1
except ValueError:
return '{}Invalid selection.'.format(e.ERROR)
if amount is None:
return '{}Amount provided is invalid.'.format(e.ERROR)
if round(userobj.balance, 8) < amount:
return '{}Insufficient funds to donate. You have `{} GRC`'.format(e.ERROR, round(userobj.balance, 2))
if 0 <= selection < len(g.donation_accts):
acct_dict = g.donation_accts[selection]
address = acct_dict[list(acct_dict.keys())[0]]
return await userobj.donate(address, amount)
return '{}Invalid selection.'.format(e.ERROR)
async def rdonate(amount, userobj):
selection = r.randint(1, len(g.donation_accts))
reply = await donate(selection, amount, userobj)
if reply.startswith(e.GOOD):
acct_dict = g.donation_accts[selection-1]
return reply + '\n\nYou donated to: {}'.format(list(acct_dict.keys())[0])
return reply
def fetch_donation_addrs():
big_string = '{}Be generous! Below are possible donation options.```{}```\nTo donate, type `%donate [selection no.] [amount-GRC]`'
acc = ''
for count, acct in enumerate(g.donation_accts):
name = list(acct.keys())[0]
acc += '\n{}. {}'.format(str(count+1), name)
return big_string.format(e.GIVE, acc[1:])
async def withdraw(amount, addr, userobj):
amount = amt_filter(amount, userobj)
if amount is None or amount <= 0:
return '{}Amount provided is invalid.'.format(e.ERROR)
if amount-g.tx_fee-g.MIN_TX <= 0:
return '{}Invalid amount, withdraw an amount higher than the fee and minimum. (`{} GRC`)'.format(e.ERROR, g.tx_fee+g.MIN_TX)
if round(userobj.balance, 8) < amount:
return '{}Insufficient funds to withdraw. You have `{} GRC`'.format(e.ERROR, round(userobj.balance, 2))
return await userobj.withdraw(amount, addr)
def give(amount, current_usrobj, rec_usrobj, add_success_msg='', donation=False):
amount = amt_filter(amount, current_usrobj)
if current_usrobj.usrID == rec_usrobj.usrID:
return '{}Cannot give funds to yourself.'.format(e.CANNOT)
if amount != None:
if amount <= round(current_usrobj.balance, 8):
current_usrobj.balance -= amount
rec_usrobj.balance += amount
if donation:
current_usrobj.donations += amount
return '{}In-server transaction of `{} GRC` successful.{}'.format(e.GOOD, amount, add_success_msg)
else:
return '{}Insufficient funds to give. You have `{} GRC`'.format(e.ERROR, round(current_usrobj.balance, 8))
else:
return '{}Amount provided was invalid.'.format(e.ERROR)
def faucet(faucet_usr, current_usr):
nxtfct = current_usr.last_faucet+3600*g.FCT_REQ_LIM
ctime = round(time.time())
if faucet_usr.balance <= g.FCT_MAX:
return '{}Unfortunately the faucet balance is too low. Try again soon.'.format(e.DOWN)
elif ctime < nxtfct:
return '{}Request too recent. Faucet timeout is {} hours. Try again in: {}'.format(e.CANNOT, g.FCT_REQ_LIM,
time.strftime("%H Hours %M Minutes %S Seconds", time.gmtime(ceil(nxtfct-ctime))))
current_usr.last_faucet = ctime
return give(round(r.uniform(g.FCT_MIN, g.FCT_MAX), 8), faucet_usr, current_usr)
def get_qr(string):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=2)
qr.add_data(string)
qr.make(fit=True)
savedir = io.BytesIO()
img = qr.make_image(fill_color='#5c00b3', back_color='white')
img.save(savedir, format="PNG")
savedir.seek(0)
return savedir
def help_interface(query):
try:
return docs.help_dict[query]
except:
return docs.help_dict['default']