diff --git a/silkaj/tx.py b/silkaj/tx.py index 00f6d8135a04bc644a8d1b061e53d3175d501cae..432d672a3558b82496fbb4ecb0b78569a4e41e45 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -17,12 +17,19 @@ import math import shlex from re import compile, search -from typing import List +from typing import List, Tuple import click from duniterpy.api.bma.tx import process -from duniterpy.documents import BlockID, Transaction -from duniterpy.documents.transaction import OutputSource, SIGParameter, Unlock +from duniterpy.documents import ( + BlockID, + InputSource, + OutputSource, + SIGParameter, + Transaction, + Unlock, +) +from duniterpy.key import SigningKey from tabulate import tabulate from silkaj import auth @@ -114,15 +121,15 @@ relative reference and recipients’ pubkeys", "--yes", "-y", is_flag=True, help="Assume yes. Do not prompt confirmation" ) def send_transaction( - amounts, - amountsud, - allsources, - recipients, - file_path, - comment, - outputbackchange, - yes, -): + amounts: List[float], + amountsud: List[float], + allsources: bool, + recipients: List[str], + file_path: str, + comment: str, + outputbackchange: str, + yes: bool, +) -> None: if file_path: tx_amounts, recipients = parse_file_containing_amounts_recipients(file_path) else: @@ -186,7 +193,9 @@ No transaction sent." ) -def parse_file_containing_amounts_recipients(file_path: str) -> List: +def parse_file_containing_amounts_recipients( + file_path: str, +) -> Tuple[List[int], List[str]]: """ Parse file in a specific format Comments are ignored @@ -203,8 +212,8 @@ def parse_file_containing_amounts_recipients(file_path: str) -> List: reference = "" amounts, recipients = [], [] with open(file_path) as file: - for n, line in enumerate(file): - line = shlex.split(line, True) + for n, raw_line in enumerate(file): + line = shlex.split(raw_line, True) if line: if n == 0: reference = line[0] @@ -233,7 +242,9 @@ def parse_file_containing_amounts_recipients(file_path: str) -> List: return tx_amounts, recipients -def transaction_amount(amounts, UDs_amounts, outputAddresses): +def transaction_amount( + amounts: List[float], UDs_amounts: List[float], outputAddresses: List[str] +) -> List[int]: """ Check that the number of passed amounts(UD) and recipients are the same Returns a list of amounts. @@ -255,7 +266,7 @@ def transaction_amount(amounts, UDs_amounts, outputAddresses): return amounts_list -def compute_amounts(amounts, multiplicator): +def compute_amounts(amounts: List[float], multiplicator: float) -> List[int]: """ Computes the amounts(UD) and returns a list. Multiplicator should be either CENT_MULT_TO_UNIT or UD_Value. @@ -275,8 +286,12 @@ def compute_amounts(amounts, multiplicator): def check_transaction_values( - comment, outputAddresses, outputBackChange, enough_source, issuer_pubkey -): + comment: str, + outputAddresses: List[str], + outputBackChange: str, + enough_source: bool, + issuer_pubkey: str, +) -> str: """ Check the comment format Check the pubkeys and the checksums of the recipients and the outputbackchange @@ -304,13 +319,13 @@ def check_transaction_values( def gen_confirmation_table( - issuer_pubkey, - pubkey_amount, - tx_amounts, - outputAddresses, - outputBackChange, - comment, -): + issuer_pubkey: str, + pubkey_amount: int, + tx_amounts: List[int], + outputAddresses: List[str], + outputBackChange: str, + comment: str, +) -> List[List]: """ Generate transaction confirmation """ @@ -318,7 +333,7 @@ def gen_confirmation_table( currency_symbol = tools.get_currency_symbol() ud_value = money.get_ud_value() total_tx_amount = sum(tx_amounts) - tx = list() + tx = list() # type: List[List[str]] # display account situation tui.display_amount( tx, @@ -353,7 +368,9 @@ def gen_confirmation_table( return tx -def get_list_input_for_transaction(pubkey, TXamount, outputs_number): +def get_list_input_for_transaction( + pubkey: str, TXamount: int, outputs_number: int +) -> Tuple[List[InputSource], int, bool]: listinput, amount = money.get_sources(pubkey) maxInputsNumber = max_inputs_number(outputs_number, NBR_ISSUERS) # generate final list source @@ -386,13 +403,13 @@ def get_list_input_for_transaction(pubkey, TXamount, outputs_number): def handle_intermediaries_transactions( - key, - issuers, - tx_amounts, - outputAddresses, - Comment="", - OutputbackChange=None, -): + key: SigningKey, + issuers: str, + tx_amounts: List[int], + outputAddresses: List[str], + Comment: str = "", + OutputbackChange: str = None, +) -> None: while True: # consider there is always one backchange output, hence +1 listinput_and_amount = get_list_input_for_transaction( @@ -423,7 +440,7 @@ def handle_intermediaries_transactions( break -def max_inputs_number(outputs_number, issuers_number): +def max_inputs_number(outputs_number: int, issuers_number: int) -> int: """ returns the maximum number of inputs. This function does not take care of backchange line. @@ -435,14 +452,14 @@ def max_inputs_number(outputs_number, issuers_number): def generate_and_send_transaction( - key, - issuers, - tx_amounts, - listinput_and_amount, - outputAddresses, - Comment, - OutputbackChange=None, -): + key: SigningKey, + issuers: str, + tx_amounts: List[int], + listinput_and_amount: Tuple[List[InputSource], int, bool], + outputAddresses: List[str], + Comment: str, + OutputbackChange: str = None, +) -> None: """ Display sent transaction Generate, sign, and send transaction document @@ -469,7 +486,7 @@ def generate_and_send_transaction( nt.send_document(process, transaction) -def display_sent_tx(outputAddress, amount): +def display_sent_tx(outputAddress: str, amount: int) -> None: print( " - To: ", tui.gen_pubkey_checksum(outputAddress), @@ -479,13 +496,13 @@ def display_sent_tx(outputAddress, amount): def generate_transaction_document( - issuers, - tx_amounts, - listinput_and_amount, - outputAddresses, - Comment="", - OutputbackChange=None, -): + issuers: str, + tx_amounts: List[int], + listinput_and_amount: Tuple[List[InputSource], int, bool], + outputAddresses: List[str], + Comment: str = "", + OutputbackChange: str = None, +) -> Transaction: listinput = listinput_and_amount[0] totalAmountInput = listinput_and_amount[1] @@ -508,7 +525,7 @@ def generate_transaction_document( # Generate output ################ - listoutput = [] + listoutput = [] # type: List[OutputSource] for tx_amount, outputAddress in zip(tx_amounts, outputAddresses): generate_output(listoutput, curentUnitBase, tx_amount, outputAddress) @@ -534,14 +551,16 @@ def generate_transaction_document( ) -def generate_unlocks(listinput): +def generate_unlocks(listinput: List[InputSource]) -> List[Unlock]: unlocks = list() for i in range(0, len(listinput)): unlocks.append(Unlock(index=i, parameters=[SIGParameter(0)])) return unlocks -def generate_output(listoutput, unitbase, rest, recipient_address): +def generate_output( + listoutput: List[OutputSource], unitbase: int, rest: int, recipient_address: str +) -> None: while rest > 0: outputAmount = truncBase(rest, unitbase) rest -= outputAmount @@ -549,7 +568,7 @@ def generate_output(listoutput, unitbase, rest, recipient_address): outputAmount = int(outputAmount / math.pow(10, unitbase)) listoutput.append( OutputSource( - amount=str(outputAmount), + amount=outputAmount, base=unitbase, condition=f"SIG({recipient_address})", ) @@ -557,7 +576,7 @@ def generate_output(listoutput, unitbase, rest, recipient_address): unitbase = unitbase - 1 -def checkComment(comment): +def checkComment(comment: str) -> None: if len(comment) > MAX_COMMENT_LENGTH: tools.message_exit("Error: Comment is too long") regex = compile( @@ -568,8 +587,8 @@ def checkComment(comment): tools.message_exit("Error: the format of the comment is invalid") -def truncBase(amount, base): - pow = math.pow(10, base) +def truncBase(amount: int, base: int) -> int: + pow = int(math.pow(10, base)) if amount < pow: return 0 return math.trunc(amount / pow) * pow diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 60835c23a0fd171b9d52c5c71fb01761f95e263e..2b940ea0ed6b39b200177cb9bde7d89882446978 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -227,17 +227,17 @@ result1 = Transaction( ], outputs=[ OutputSource( - amount=str(1000), + amount=1000, base=0, condition="SIG(DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw)", ), OutputSource( - amount=str(4000), + amount=4000, base=0, condition="SIG(4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw)", ), OutputSource( - amount=str(5000), + amount=5000, base=0, condition="SIG(BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh)", ), @@ -1458,7 +1458,7 @@ def test_generate_unlocks(listinput, expected): "2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY", [ OutputSource( - amount="500", + amount=500, base=0, condition="SIG(2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY)", ) @@ -1471,17 +1471,17 @@ def test_generate_unlocks(listinput, expected): "2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY", [ OutputSource( - amount="3", + amount=3, base=2, condition="SIG(2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY)", ), OutputSource( - amount="1", + amount=1, base=1, condition="SIG(2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY)", ), OutputSource( - amount="4", + amount=4, base=0, condition="SIG(2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY)", ), @@ -1490,7 +1490,7 @@ def test_generate_unlocks(listinput, expected): ( [ OutputSource( - amount="100", + amount=100, base=0, condition="SIG(2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY)", ) @@ -1500,12 +1500,12 @@ def test_generate_unlocks(listinput, expected): "2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY", [ OutputSource( - amount="100", + amount=100, base=0, condition="SIG(2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY)", ), OutputSource( - amount="500", + amount=500, base=0, condition="SIG(2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY)", ),