From 2e0036d424ebd1282edebb01a3333d0d9b7b4c03 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Mon, 25 Apr 2022 19:14:49 +0200 Subject: [PATCH] [mod] #397: Get rid of UDValue singleton --- silkaj/money.py | 25 +++++++------------------ silkaj/tx.py | 6 +++--- silkaj/tx_history.py | 4 ++-- tests/patched/money.py | 2 +- tests/test_tx.py | 6 +++--- tests/test_tx_file.py | 4 ++-- tests/test_unit_tx.py | 6 +++--- 7 files changed, 21 insertions(+), 32 deletions(-) diff --git a/silkaj/money.py b/silkaj/money.py index 1236805f..3462d13c 100644 --- a/silkaj/money.py +++ b/silkaj/money.py @@ -79,7 +79,7 @@ def show_amount_from_pubkey(label, inputs_balance): totalAmountInput = inputs_balance[0] balance = inputs_balance[1] currency_symbol = get_currency_symbol() - ud_value = UDValue().ud_value + ud_value = get_ud_value() average, monetary_mass = get_average() member = False @@ -184,23 +184,12 @@ def get_sources(pubkey): return listinput, amount -class UDValue: - __instance = None - - def __new__(cls): - if UDValue.__instance is None: - UDValue.__instance = object.__new__(cls) - return UDValue.__instance - - def __init__(self): - self.ud_value = self.get_ud_value() - - def get_ud_value(self): - client = ClientInstance().client - blockswithud = client(blockchain.ud) - NBlastUDblock = blockswithud["result"]["blocks"][-1] - lastUDblock = client(blockchain.block, NBlastUDblock) - return lastUDblock["dividend"] * 10 ** lastUDblock["unitbase"] +def get_ud_value(): + client = ClientInstance().client + blockswithud = client(blockchain.ud) + NBlastUDblock = blockswithud["result"]["blocks"][-1] + lastUDblock = client(blockchain.block, NBlastUDblock) + return lastUDblock["dividend"] * 10 ** lastUDblock["unitbase"] def amount_in_current_base(source): diff --git a/silkaj/tx.py b/silkaj/tx.py index 9cdc8d9e..a5bc63a3 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -224,7 +224,7 @@ def parse_file_containing_amounts_recipients(file_path: str) -> List: if reference == "ABSOLUTE": reference_mult = CENT_MULT_TO_UNIT else: - reference_mult = money.UDValue().ud_value + reference_mult = money.get_ud_value() tx_amounts = compute_amounts(amounts, reference_mult) return tx_amounts, recipients @@ -239,7 +239,7 @@ def transaction_amount(amounts, UDs_amounts, outputAddresses): if amounts: amounts_list = compute_amounts(amounts, CENT_MULT_TO_UNIT) elif UDs_amounts: - UD_value = money.UDValue().ud_value + UD_value = money.get_ud_value() amounts_list = compute_amounts(UDs_amounts, UD_value) if len(amounts_list) != len(outputAddresses) and len(amounts_list) != 1: tools.message_exit( @@ -313,7 +313,7 @@ def gen_confirmation_table( """ currency_symbol = tools.get_currency_symbol() - ud_value = money.UDValue().ud_value + ud_value = money.get_ud_value() total_tx_amount = sum(tx_amounts) tx = list() # display account situation diff --git a/silkaj/tx_history.py b/silkaj/tx_history.py index a8bdcd58..274e4796 100644 --- a/silkaj/tx_history.py +++ b/silkaj/tx_history.py @@ -26,7 +26,7 @@ from silkaj import wot from silkaj import wot_tools as wt from silkaj.constants import ALL, ALL_DIGITAL from silkaj.crypto_tools import check_pubkey_format, validate_checksum -from silkaj.money import UDValue, amount_in_current_base, get_amount_from_pubkey +from silkaj.money import amount_in_current_base, get_amount_from_pubkey, get_ud_value from silkaj.network_tools import ClientInstance from silkaj.tools import get_currency_symbol from silkaj.tui import display_pubkey_and_checksum @@ -41,7 +41,7 @@ def transaction_history(pubkey, uids, full_pubkey): pubkey = validate_checksum(pubkey) client = ClientInstance().client - ud_value = UDValue().ud_value + ud_value = get_ud_value() currency_symbol = get_urrency_symbol() header = generate_header(pubkey, currency_symbol, ud_value) diff --git a/tests/patched/money.py b/tests/patched/money.py index ba418538..32d0f815 100644 --- a/tests/patched/money.py +++ b/tests/patched/money.py @@ -40,7 +40,7 @@ from silkaj.money import amount_in_current_base from silkaj.tx import MAX_INPUTS_PER_TX -def patched_ud_value(self): +def patched_get_ud_value(): return mock_ud_value diff --git a/tests/test_tx.py b/tests/test_tx.py index 94464efc..0bc7ac82 100644 --- a/tests/test_tx.py +++ b/tests/test_tx.py @@ -21,7 +21,7 @@ from click import pass_context from click.testing import CliRunner from patched.auth import patched_auth_method -from patched.money import patched_get_sources, patched_ud_value +from patched.money import patched_get_sources, patched_get_ud_value from patched.test_constants import mock_ud_value from patched.tx import patched_gen_confirmation_table from silkaj import auth, money, tx @@ -50,7 +50,7 @@ def test_transaction_amount(monkeypatch): """test passed amounts passed tx command float ≠100 does not give the exact value""" - monkeypatch.setattr(money.UDValue, "get_ud_value", patched_ud_value) + monkeypatch.setattr(money, "get_ud_value", patched_get_ud_value) trials = ( # tests for --amount (unit) ([141.89], None, ["A"], [14189]), @@ -111,7 +111,7 @@ def test_transaction_amount_errors( amounts, UDs_amounts, outputAddresses, expected, capsys, monkeypatch ): # patched functions - monkeypatch.setattr(money.UDValue, "get_ud_value", patched_ud_value) + monkeypatch.setattr(money, "get_ud_value", patched_get_ud_value) def too_little_amount(amounts, multiplicator): for amount in amounts: diff --git a/tests/test_tx_file.py b/tests/test_tx_file.py index 628a7d1e..48b00a35 100644 --- a/tests/test_tx_file.py +++ b/tests/test_tx_file.py @@ -17,12 +17,12 @@ import pytest from click.testing import CliRunner from silkaj.constants import CENT_MULT_TO_UNIT -from silkaj.money import UDValue +from silkaj.money import get_ud_value from silkaj.tx import parse_file_containing_amounts_recipients FILE_PATH = "recipients.txt" -ud_value = UDValue().ud_value +ud_value = get_ud_value() @pytest.mark.parametrize( diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 4189e44a..2d3620e2 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -28,7 +28,7 @@ from duniterpy.documents.transaction import ( from patched.auth import patched_auth_method from patched.blockchain_tools import fake_block_id, patched_get_head_block -from patched.money import patched_get_sources, patched_ud_value +from patched.money import patched_get_sources, patched_get_ud_value from patched.test_constants import mock_ud_value from patched.tools import patched_get_currency_symbol from patched.tx import ( @@ -123,7 +123,7 @@ def test_gen_confirmation_table( ): # patched functions monkeypatch.setattr(wot_tools, "is_member", patched_is_member) - monkeypatch.setattr(money.UDValue, "get_ud_value", patched_ud_value) + monkeypatch.setattr(money, "get_ud_value", patched_get_ud_value) monkeypatch.setattr(tools, "get_currency_symbol", patched_get_currency_symbol) # creating expected list @@ -1032,7 +1032,7 @@ def test_send_transaction( patched_handle_intermediaries_transactions, ) monkeypatch.setattr(money, "get_sources", patched_get_sources) - monkeypatch.setattr(money.UDValue, "get_ud_value", patched_ud_value) + monkeypatch.setattr(money, "get_ud_value", patched_get_ud_value) # reset patched_get_sources patched_get_sources.counter = 0 -- GitLab