def deposit_query(client, call): call.edit_message_text("Okay, almost done! Now generating invoice...") _, currency, amount = call.data.split("_") amount_sat = int(amount) amount_btc = bitcoins(amount_sat) # bitcart: convert satoshis to bitcoins user_id = call.from_user.id invoice, amount, _ = generate_invoice(user_id, currency, amount_btc, amount_sat, f"{secret_id(user_id)} top-up") send_qr( invoice["URI"], user_id, client, caption=f"Your invoice for {amount_sat} Satoshi ({amount:0.8f} {currency.upper()}):\n{invoice['address']}", )
def paylink_query(client, message): user_id = message.from_user.id _, link_type, currency, amount_sat = message.data.split("_") amount_sat = int(amount_sat) amount_btc = bitcoins(amount_sat) amount, currency_name = convert_amounts(currency, amount_btc) if link_type == "pr": invoice, _, _ = generate_invoice(user_id, currency, amount_btc, amount_sat, f"{secret_id(user_id)} paylink") invoice_link = invoice["URI"] elif link_type == "bot": bot_username = app.get_me().username invoice_link = f"https://t.me/{bot_username}?start={user_id}={amount_sat}" try: message.edit_message_text(f"Invoice for {amount_sat} Satoshi [{amount:.8f} {currency.upper()}]\n\nMessage to forward:") time.sleep(1) app.send_message( chat_id=user_id, text=f"Send me {currency_name.lower()} using this link: {invoice_link}", ) except BadRequest: pass
def withdraw(client, message): user_id = message.from_user.id currency = message.matches[0].group(1) address = message.matches[0].group(2) try: amount = int(message.matches[0].group(3)) except ValueError: return message.reply("Invalid amount specified", quote=False) coin_obj = instances.get(currency.lower()) if not coin_obj: return message.reply("Invalid currency", quote=False) user = get_user_data(user_id) if amount <= 0 or user["balance"] < amount: return message.reply("Not enough balance", quote=False) amount_to_send = bitcoins(amount) / coin_obj.rate("BTC") wallet_balance = coin_obj.balance()["confirmed"] if wallet_balance < amount_to_send: available_coins = [] for coin in instances: coin_balance = instances[coin].balance()["confirmed"] if coin_balance >= amount_to_send: available_coins.append(instances[coin].coin_name) wallet_balance_sat = int(round(wallet_balance * coin_obj.rate("BTC") * 100000000, 8)) return message.reply( f"Current {currency} wallet balance: {wallet_balance_sat}. \nIf you want to withdraw {amount} satoshis, you can do" f' so in any of these currencies: {", ".join(available_coins)}', quote=False, ) # bitcart: send to address in BTC try: tx_hash = coin_obj.pay_to(address, amount_to_send) # payment succeeded, we have tx hash change_balance(user_id, -amount, "withdraw", tx_hash) message.reply(f"Successfuly withdrawn. Tx id: {tx_hash}", quote=False) except Exception: error_line = traceback.format_exc().splitlines()[-1] message.reply(f"Error occured: \n<code>{error_line}</code>", quote=False)
def test_convertability(): assert bitcoins(satoshis(1)) == 1
def test_bitcoins(sats, expected): result = bitcoins(sats) assert isinstance(result, Decimal) assert result == Decimal(expected)