Skip to content
Snippets Groups Projects
Commit 2e0036d4 authored by Moul's avatar Moul
Browse files

[mod] #397: Get rid of UDValue singleton

parent cb2b5e2d
No related branches found
No related tags found
No related merge requests found
Pipeline #15354 passed
......@@ -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):
......
......@@ -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
......
......@@ -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)
......
......@@ -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
......
......@@ -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:
......
......@@ -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(
......
......@@ -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
......
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