Skip to content

Commit 4ec2a1c

Browse files
committed
BANANO-ify and more
- Rewrite DB layer to use peewee ORM with a pooled postgres database - Use eventlet green-threads instead of pre-fork strategy for concurrency - Some other minor things, like adding use_peers to work_generate
1 parent 327e294 commit 4ec2a1c

8 files changed

Lines changed: 356 additions & 528 deletions

File tree

‎README.md‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
NOS Telegram Tip Bot
1+
# BANANO Telegram Tip Bot
22

33
Commands
44

5-
Thank you for using my services @NOSTipBot! Below is a list of commands, and a description of how you can interact with me:
5+
- .tip
6+
- .help
7+
- .register
8+
- .account
9+
- .withdraw
610

7-
• !help: The NOSTipBot will respond to your DM with a list of commands and their functions. If you forget something, use this to get a hint of how to do it!n
11+
# Install
812

9-
• !register: Creates a fresh NOS (XNOS) account address specifically for you. This is used to store your tips. Make sure to withdraw to a private wallet, as the tip bot is not meant to be a long term storage device for NOS (XNOS).
13+
`cp config/example_config.ini config/webhooks.ini`
1014

11-
• !balance: This shows you how much funds are in your your account.
15+
Specify postgres user/schema/port/host
1216

13-
• !tip: Tips are sent directly to @username on telegram. Tag @NOSTipBot and mention !tip <amount> <@username>. EXAMPLE: @NOSTipBot !tip 1 @user will send a 1 NOS (XNOS) tip to another user.
17+
Copy tipbot.service example to systemd
1418

15-
• !account: Returns the account number. You can use this to deposit more NOS (XNOS) to tip from your personal wallet.
16-
17-
• !withdraw: Proper usage is !withdraw nos_12345. This will send the full balance of your tip account to another external NOS (XNOS) account. Optional: You can include an amount to withdraw by sending !withdraw <amount> <address>. Example: !withdraw 1 nos_123 would withdraw 1 Nos to account nos_123
19+
Run with systemd

‎modules/currency.py‎

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import nano
99
import requests
1010

11-
from . import db, social
11+
import db
12+
import social
1213

1314
# Read config and parse constants
1415
config = configparser.ConfigParser()
@@ -20,23 +21,23 @@
2021

2122
# Connect to Nano node
2223
rpc = nano.rpc.Client(NODE_IP)
23-
raw_denominator = 10**10
24+
raw_denominator = 10**29
2425

2526

2627
def receive_pending(sender_account):
2728
"""
2829
Check to see if the account has any pending blocks and process them
2930
"""
3031
try:
31-
logging.info("{}: in receive pending".format(datetime.now()))
32+
logging.info("{}: in receive pending".format(datetime.utcnow()))
3233
pending_blocks = rpc.pending(account='{}'.format(sender_account))
3334
logging.info("pending blocks: {}".format(pending_blocks))
3435
if len(pending_blocks) > 0:
3536
for block in pending_blocks:
3637
work = get_pow(sender_account)
3738
if work == '':
3839
logging.info("{}: processing without pow".format(
39-
datetime.now()))
40+
datetime.utcnow()))
4041
receive_data = {
4142
'action': "receive",
4243
'wallet': WALLET,
@@ -45,7 +46,7 @@ def receive_pending(sender_account):
4546
}
4647
else:
4748
logging.info("{}: processing with pow".format(
48-
datetime.now()))
49+
datetime.utcnow()))
4950
receive_data = {
5051
'action': "receive",
5152
'wallet': WALLET,
@@ -56,10 +57,10 @@ def receive_pending(sender_account):
5657
receive_json = json.dumps(receive_data)
5758
requests.post('{}'.format(NODE_IP), data=receive_json)
5859
logging.info("{}: block {} received".format(
59-
datetime.now(), block))
60+
datetime.utcnow(), block))
6061

6162
else:
62-
logging.info('{}: No blocks to receive.'.format(datetime.now()))
63+
logging.info('{}: No blocks to receive.'.format(datetime.utcnow()))
6364

6465
except Exception as e:
6566
logging.info("Receive Pending Error: {}".format(e))
@@ -72,25 +73,25 @@ def get_pow(sender_account):
7273
"""
7374
Retrieves the frontier (hash of previous transaction) of the provided account and generates work for the next block.
7475
"""
75-
logging.info("{}: in get_pow".format(datetime.now()))
76+
logging.info("{}: in get_pow".format(datetime.utcnow()))
7677
try:
7778
account_frontiers = rpc.accounts_frontiers([sender_account])
7879
frontier_hash = account_frontiers[sender_account]
7980
except Exception as e:
8081
logging.info("{}: Error checking frontier: {}".format(
81-
datetime.now(), e))
82+
datetime.utcnow(), e))
8283
return ''
8384
logging.info("account_frontiers: {}".format(account_frontiers))
8485

8586
work = ''
86-
logging.info("{}: hash: {}".format(datetime.now(), frontier_hash))
87+
logging.info("{}: hash: {}".format(datetime.utcnow(), frontier_hash))
8788
while work == '':
8889
try:
89-
work = rpc.work_generate(frontier_hash)
90-
logging.info("{}: Work generated: {}".format(datetime.now(), work))
90+
work = rpc.work_generate(frontier_hash, use_peers=True)
91+
logging.info("{}: Work generated: {}".format(datetime.utcnow(), work))
9192
except Exception as e:
9293
logging.info("{}: ERROR GENERATING WORK: {}".format(
93-
datetime.now(), e))
94+
datetime.utcnow(), e))
9495
pass
9596

9697
return work
@@ -101,42 +102,36 @@ def send_tip(message, users_to_tip, tip_index):
101102
Process tip for specified user
102103
"""
103104
logging.info("{}: sending tip to {}".format(
104-
datetime.now(), users_to_tip[tip_index]['receiver_screen_name']))
105+
datetime.utcnow(), users_to_tip[tip_index]['receiver_screen_name']))
105106
if str(users_to_tip[tip_index]['receiver_id']) == str(
106107
message['sender_id']):
107-
self_tip_text = "Self tipping is not allowed. Please use this bot to spread the NOS to other users!"
108+
self_tip_text = "Self tipping is not allowed. Please use this bot to tip BANANO to other users!"
108109
social.send_reply(message, self_tip_text)
109110

110-
logging.info("{}: User tried to tip themself").format(datetime.now())
111+
logging.info("{}: User tried to tip themself").format(datetime.utcnow())
111112
return
112113

113114
# Check if the receiver has an account
114-
receiver_account_get = "SELECT account FROM users where user_id = %s"
115-
arguments = (int(users_to_tip[tip_index]['receiver_id']))
116-
receiver_account_data = db.get_db_data(receiver_account_get, arguments)
117-
118-
119-
# If they don't, create an account for them
120-
if not receiver_account_data:
115+
try:
116+
user = db.User.select().where(db.User.user_id == int(users_to_tip[tip_index]['receiver_id'])).get()
117+
users_to_tip[tip_index]['receiver_account'] = user.account
118+
except db.User.DoesNotExist:
119+
# If they don't, create an account for them
121120
users_to_tip[tip_index]['receiver_account'] = rpc.account_create(
122121
wallet="{}".format(WALLET), work=True)
123-
create_receiver_account = (
124-
"INSERT INTO users (user_id, user_name, account, register) "
125-
"VALUES({}, '{}', '{}',0)".format(
126-
users_to_tip[tip_index]['receiver_id'],
127-
users_to_tip[tip_index]['receiver_screen_name'],
128-
users_to_tip[tip_index]['receiver_account']))
129-
db.set_db_data(create_receiver_account)
122+
user = db.User(
123+
user_id = int(users_to_tip[tip_index]['receiver_id']),
124+
user_name = users_to_tip[tip_index]['receiver_screen_name'],
125+
account = users_to_tip[tip_index]['receiver_account'],
126+
register=0
127+
)
128+
user.save()
130129
logging.info(
131130
"{}: Sender sent to a new receiving account. Created account {}".
132-
format(datetime.now(),
131+
format(datetime.utcnow(),
133132
users_to_tip[tip_index]['receiver_account']))
134-
135-
else:
136-
users_to_tip[tip_index]['receiver_account'] = receiver_account_data[0][
137-
0]
138-
139133
# Send the tip
134+
140135
message['tip_id'] = "{}{}".format(message['id'], tip_index)
141136

142137
work = get_pow(message['sender_account'])
@@ -185,19 +180,17 @@ def send_tip(message, users_to_tip, tip_index):
185180

186181
# Send a DM to the receiver
187182
receiver_tip_text = (
188-
"@{} just sent you a {} NOS tip! Reply to this DM with !balance to see your new balance. If you have not "
189-
"registered an account, send a reply with !register to get started, or !help to see a list of "
190-
"commands! Learn more about NOS (XNOS) & Nollar at https://nos.cash/"
191-
.format(message['sender_screen_name'], message['tip_amount_text'],
192-
users_to_tip[tip_index]['balance']))
183+
"@{0} just sent you a {1} BANANO tip! Reply to this DM with .balance to see your new balance. If you have not "
184+
"registered an account, send a reply with .register to get started, or .help to see a list of "
185+
"commands! Learn more about BANANO at https://banano.cc"
186+
.format(message['sender_screen_name'], message['tip_amount_text']))
193187
social.send_dm(users_to_tip[tip_index]['receiver_id'],
194188
receiver_tip_text)
195-
196189
except Exception as e:
197190
logging.info(
198191
"{}: ERROR IN RECEIVING NEW TIP - POSSIBLE NEW ACCOUNT NOT REGISTERED WITH DPOW: {}"
199-
.format(datetime.now(), e))
192+
.format(datetime.utcnow(), e))
200193

201194
logging.info("{}: tip sent to {} via hash {}".format(
202-
datetime.now(), users_to_tip[tip_index]['receiver_screen_name'],
195+
datetime.utcnow(), users_to_tip[tip_index]['receiver_screen_name'],
203196
message['send_hash']))

0 commit comments

Comments
 (0)