Skip to content
Snippets Groups Projects

Draft: initial user flow for creating a wallet

Merged Janssen, D.D. (Dylan, Student M-CS) requested to merge 2-implement_all_user_flows into main
4 files
+ 108
10
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 86
8
import ast
import os
from bitcoinlib.encoding import EncodingError
from bitcoinlib.keys import Address
from bitcoinlib.wallets import Wallet
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackContext
import database
from database import AppUser, AppState, AppWallet, AppWalletRequest, AppUserWallet
from util import generate_wallets, get_wallet_name
from util import generate_wallets, get_wallet_name, has_enough_funds, do_transaction
def generate_message(state_id, extra_menu=None):
@@ -26,6 +32,7 @@ def start(update: Update, context: CallbackContext):
token = params[1]
request = AppWalletRequest.get(AppWalletRequest.token == token)
user_wallet, created = AppUserWallet.get_or_create(user_id=user.id, wallet_id=request.wallet_id)
# TODO remove or True
if created or True:
request.delete_instance()
update.effective_message.reply_text("Succesfully joined the wallet")
@@ -44,7 +51,7 @@ def start(update: Update, context: CallbackContext):
update.effective_message.reply_text("You cannot join a wallet twice")
user.set_state(1)
user.set_variable({})
user.set_variables({})
reply_msg, reply_markup = generate_message(1)
update.effective_message.reply_text(reply_msg, reply_markup=reply_markup)
@@ -64,7 +71,11 @@ def send_a_transaction(update: Update, context: CallbackContext):
telegram_user = update.effective_user
user, created = AppUser.get_or_create(telegram_id=telegram_user.id, nickname=telegram_user.full_name)
if user.state_id.id != 6:
update.effective_message.reply_text("You cannot do this operation now")
update.effective_message.reply_text("You cannot do this operation right now")
else:
next_state = user.next_state()
reply_msg, reply_markup = generate_message(next_state)
update.effective_message.reply_text(reply_msg, reply_markup=reply_markup)
pass
@@ -77,24 +88,88 @@ def create_wallet(update: Update, context: CallbackContext):
def choose_wallet(update: Update, context: CallbackContext):
# TODO: some information about the wallet when choosing the wallet
# TODO: check if wallet is already created (choose wallet)
telegram_user = update.effective_user
user = AppUser.get(telegram_id=telegram_user.id, nickname=telegram_user.full_name)
user_wallets = AppUserWallet.select().where(AppUserWallet.user_id == user)
user.set_state(5)
menu = [[InlineKeyboardButton(uw.wallet_id.name, callback_data=get_wallet_name(uw.wallet_id.id, uw.user_id.id))] for uw in user_wallets]
menu = [[InlineKeyboardButton(uw.wallet_id.name, callback_data=uw.wallet_id.id)] for uw in user_wallets]
reply_msg, reply_markup = generate_message(5, menu)
update.effective_message.reply_text(reply_msg, reply_markup=reply_markup)
pass
def wallet_options(update: Update, context: CallbackContext, user: AppUser):
wallet_name = update.callback_query.data
user.set_variable("wallet_name", wallet_name)
wallet_id = update.callback_query.data
user.set_variable("wallet_id", wallet_id)
next_state = user.next_state()
reply_msg, reply_markup = generate_message(next_state)
update.effective_message.reply_text(reply_msg, reply_markup=reply_markup)
def transaction_address(update: Update, context: CallbackContext, user: AppUser):
msg = update.effective_message.text
try:
address = Address(msg, network=os.getenv("BTC_NETWORK"))
user.set_variable("address", msg)
next_state = user.next_state()
reply_msg, reply_markup = generate_message(next_state)
update.effective_message.reply_text(reply_msg, reply_markup=reply_markup)
except EncodingError:
update.effective_message.reply_text("This is not a valid address")
def transaction_amount(update: Update, context: CallbackContext, user: AppUser):
msg = update.effective_message.text
try:
amount = float(msg)
user.set_variable("amount", amount)
next_state = user.next_state()
reply_msg, reply_markup = generate_message(next_state)
update.effective_message.reply_text(reply_msg, reply_markup=reply_markup)
except ValueError:
pass
# update.effective_message.reply_text("This is not a number, try again")
def transaction_fee(update: Update, context: CallbackContext, user: AppUser):
cq = update.callback_query
cm = update.effective_message
success = False
if cq:
query = cq.data
if query == "low" or query == "normal" or query == "high":
user.set_variable("fee", query)
success = True
elif cm:
msg = cm.text
try:
amount = float(msg)
user.set_variable("fee", amount)
success = True
except ValueError:
pass
if success:
variables = user.variables()
t, has_funds, error = do_transaction(variables["wallet_id"], user.id, variables["address"], variables["amount"], variables["fee"])
if not has_funds:
update.effective_message.reply_text("The wallet does not have enough funds to do this transaction, please change the amount or fee")
elif error:
update.effective_message.reply_text("Something went wrong during the creating of the transaction please try again later")
else:
# TODO: send notification to the other co signers
pass
next_state = user.next_state()
reply_msg, reply_markup = generate_message(next_state)
update.effective_message.reply_text(reply_msg, reply_markup=reply_markup)
else:
update.effective_message.reply_text("Please send a number or use the buttons to identify the fee amount")
def enter_co_signers(update: Update, context: CallbackContext, user: AppUser):
msg = update.effective_message.text
try:
@@ -171,7 +246,10 @@ def message_handler(update: Update, context: CallbackContext):
2: enter_co_signers,
3: minimum_co_signers,
4: name_wallet,
5: wallet_options
5: wallet_options,
7: transaction_address,
8: transaction_amount,
9: transaction_fee
}
try:
f = HANDLERS[state_user]
@@ -186,5 +264,5 @@ COMMANDS = {
"create_a_wallet": create_wallet,
"send_a_transaction": send_a_transaction,
"back": back,
"choose_wallet": choose_wallet
"choose_wallet": choose_wallet,
}
Loading