diff --git a/silkaj/tx.py b/silkaj/tx.py index e1a9c2f6b499f4381fdb44e109995de0b537c530..5bac90ba886b2a0fe517c97e629ef35d871cca70 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -19,20 +19,20 @@ from re import compile, search import math from time import sleep from tabulate import tabulate -from click import command, option, FloatRange +from click import command, option, FloatRange, group -from silkaj.cli_tools import MutuallyExclusiveOption -from silkaj.network_tools import ClientInstance, HeadBlock -from silkaj.crypto_tools import check_public_key -from silkaj.tools import message_exit, CurrencySymbol, coroutine -from silkaj.auth import auth_method +from silkaj import cli_tools +from silkaj import network_tools as nt +from silkaj import crypto_tools as ct +from silkaj import tools +from silkaj import auth from silkaj import money +from silkaj import tui from silkaj.constants import ( SOURCES_PER_TX, MINIMAL_TX_AMOUNT, CENT_MULT_TO_UNIT, ) -from silkaj.tui import display_amount, display_pubkey from duniterpy.api.bma.tx import process from duniterpy.documents import BlockUID, Transaction @@ -49,7 +49,7 @@ from duniterpy.documents.transaction import OutputSource, Unlock, SIGParameter help="Quantitative amount(s):\n-a <amount>\nMinimum amount is {0}".format( MINIMAL_TX_AMOUNT ), - cls=MutuallyExclusiveOption, + cls=cli_tools.MutuallyExclusiveOption, mutually_exclusive=["amountsud", "allsources"], ) @option( @@ -59,14 +59,14 @@ from duniterpy.documents.transaction import OutputSource, Unlock, SIGParameter multiple=True, type=float, help="Relative amount(s):\n-d <amount_UD>", - cls=MutuallyExclusiveOption, + cls=cli_tools.MutuallyExclusiveOption, mutually_exclusive=["amounts", "allsources"], ) @option( "--allSources", is_flag=True, help="Send all sources to one recipient", - cls=MutuallyExclusiveOption, + cls=cli_tools.MutuallyExclusiveOption, mutually_exclusive=["amounts", "amountsud"], ) @option( @@ -86,7 +86,7 @@ Sending to many recipients is possible :\n\ help="Pubkey recipient to send the rest of the transaction: <pubkey[!checksum]>", ) @option("--yes", "-y", is_flag=True, help="Assume yes. Do not prompt confirmation") -@coroutine +@tools.coroutine async def send_transaction( amounts, amountsud, allsources, recipients, comment, outputbackchange, yes ): @@ -94,15 +94,15 @@ async def send_transaction( Main function """ if not (amounts or amountsud or allsources): - message_exit("Error : amount, amountUD or allSources is not set.") + tools.message_exit("Error : amount, amountUD or allSources is not set.") if not allsources: tx_amounts = await transaction_amount(amounts, amountsud, recipients) elif len(recipients) > 1: - message_exit( + tools.message_exit( "Error: the --allSources option can only be used with one recipient." ) - key = auth_method() + key = auth.auth_method() issuer_pubkey = key.pubkey pubkey_amount = await money.get_amount_from_pubkey(issuer_pubkey) @@ -139,7 +139,7 @@ async def send_transaction( key, issuer_pubkey, tx_amounts, recipients, comment, outputbackchange, ) else: - client = ClientInstance().client + client = nt.ClientInstance().client await client.close() @@ -154,11 +154,11 @@ async def transaction_amount(amounts, amountUDs, outputAddresses): # creating amounts list if amounts: if len(amounts) != len(outputAddresses) and len(amounts) != 1: - message_exit(error_message_number_recipients_amounts) + tools.message_exit(error_message_number_recipients_amounts) amounts_list = compute_amounts(amounts, CENT_MULT_TO_UNIT) elif amountUDs: if len(amountUDs) != len(outputAddresses) and len(amountUDs) != 1: - message_exit(error_message_number_recipients_amounts) + tools.message_exit(error_message_number_recipients_amounts) UD_value = await money.UDValue().ud_value amounts_list = compute_amounts(amountUDs, UD_value) # generating list for many issuers @@ -181,7 +181,7 @@ def compute_amounts(amounts, multiplicator): if (multiplicator != CENT_MULT_TO_UNIT) and ( computed_amount < (MINIMAL_TX_AMOUNT * CENT_MULT_TO_UNIT) ): - message_exit("Error: amount {0} is too low.".format(amount)) + tools.message_exit("Error: amount {0} is too low.".format(amount)) amounts_list.append(round(computed_amount)) return amounts_list @@ -191,14 +191,14 @@ def check_transaction_values( ): checkComment(comment) for outputAddress in outputAddresses: - if check_public_key(outputAddress, True) is False: - message_exit(outputAddress) + if ct.check_public_key(outputAddress, True) is False: + tools.message_exit(outputAddress) if outputBackChange: - outputBackChange = check_public_key(outputBackChange, True) - if check_public_key(outputBackChange, True) is False: - message_exit(outputBackChange) + outputBackChange = ct.check_public_key(outputBackChange, True) + if ct.check_public_key(outputBackChange, True) is False: + tools.message_exit(outputBackChange) if enough_source: - message_exit( + tools.message_exit( issuer_pubkey + " pubkey doesn’t have enough money for this transaction." ) @@ -215,32 +215,32 @@ async def transaction_confirmation( Generate transaction confirmation """ - currency_symbol = await CurrencySymbol().symbol + currency_symbol = await tools.CurrencySymbol().symbol ud_value = await money.UDValue().ud_value total_tx_amount = sum(tx_amounts) tx = list() # display account situation - display_amount( + tui.display_amount( tx, "pubkey's balance before tx", pubkey_amount, ud_value, currency_symbol, ) - display_amount( + tui.display_amount( tx, "total transaction amount", total_tx_amount, ud_value, currency_symbol, ) - display_amount( + tui.display_amount( tx, "pubkey's balance after tx", (pubkey_amount - total_tx_amount), ud_value, currency_symbol, ) - await display_pubkey(tx, "from", issuer_pubkey) + await tui.display_pubkey(tx, "from", issuer_pubkey) # display outputs and amounts for outputAddress, tx_amount in zip(outputAddresses, tx_amounts): - await display_pubkey(tx, "to", outputAddress) - display_amount(tx, "amount", tx_amount, ud_value, currency_symbol) + await tui.display_pubkey(tx, "to", outputAddress) + tui.display_amount(tx, "amount", tx_amount, ud_value, currency_symbol) # display last informations if outputBackChange: - await display_pubkey(tx, "Backchange", outputBackChange) + await tui.display_pubkey(tx, "Backchange", outputBackChange) tx.append(["comment", comment]) return tx @@ -263,14 +263,14 @@ async def get_list_input_for_transaction(pubkey, TXamount): if TXamount <= 0: break if TXamount > 0 and not intermediatetransaction: - message_exit("Error: you don't have enough money") + tools.message_exit("Error: you don't have enough money") return listinputfinal, totalAmountInput, intermediatetransaction async def handle_intermediaries_transactions( key, issuers, tx_amounts, outputAddresses, Comment="", OutputbackChange=None, ): - client = ClientInstance().client + client = nt.ClientInstance().client while True: listinput_and_amount = await get_list_input_for_transaction( issuers, sum(tx_amounts) @@ -326,7 +326,7 @@ async def generate_and_send_transaction( display_sent_tx(outputAddress, tx_amount) print(" - Total: " + str(sum(tx_amounts) / 100)) - client = ClientInstance().client + client = nt.ClientInstance().client transaction = await generate_transaction_document( issuers, tx_amounts, @@ -336,11 +336,12 @@ async def generate_and_send_transaction( OutputbackChange, ) transaction.sign([key]) + response = await client(process, transaction.signed_raw()) if response.status == 200: print("Transaction successfully sent.") else: - message_exit( + tools.message_exit( "Error while publishing transaction: {0}".format(await response.text()) ) @@ -362,7 +363,7 @@ async def generate_transaction_document( totalAmountInput = listinput_and_amount[1] total_tx_amount = sum(tx_amounts) - head_block = await HeadBlock().head_block + head_block = await nt.HeadBlock().head_block currency_name = head_block["currency"] blockstamp_current = BlockUID(head_block["number"], head_block["hash"]) curentUnitBase = head_block["unitbase"] @@ -431,12 +432,12 @@ def generate_output(listoutput, unitbase, rest, recipient_address): def checkComment(Comment): if len(Comment) > 255: - message_exit("Error: Comment is too long") + tools.message_exit("Error: Comment is too long") regex = compile( "^[0-9a-zA-Z\ \-\_\:\/\;\*\[\]\(\)\?\!\^\+\=\@\&\~\#\{\}\|\\\<\>\%\.]*$" ) if not search(regex, Comment): - message_exit("Error: the format of the comment is invalid") + tools.message_exit("Error: the format of the comment is invalid") def truncBase(amount, base):