Skip to content
Snippets Groups Projects
Commit e22e607e authored by matograine's avatar matograine
Browse files

[mod] #213: import whole modules in tx.py

For unit test purposes, we need to do:

`from silkaj import module`

instead of

`from silkaj.module import function1, function2`

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