From 652eb059f9039b6bb401d6c9e3e4b3e89a6f71d8 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Mon, 28 Jan 2019 15:49:56 +0100
Subject: [PATCH] =?UTF-8?q?[enh]=20#184:=20add=20generic=20f()=20to=20calc?=
 =?UTF-8?q?ulate=20amount=20in=20current=20base=E2=80=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

… from input or output source
- Use it in balance and tx commands
---
 silkaj/money.py | 11 +++++++++--
 silkaj/tx.py    | 11 ++++++++---
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/silkaj/money.py b/silkaj/money.py
index 2da4c89e..c032b7dc 100644
--- a/silkaj/money.py
+++ b/silkaj/money.py
@@ -120,7 +120,7 @@ async def get_amount_from_pubkey(pubkey):
 
     totalAmountInput = 0
     for input in listinput:
-        totalAmountInput += input.amount * 10 ** input.base
+        totalAmountInput += amount_in_current_base(input)
     return totalAmountInput, amount
 
 
@@ -133,7 +133,6 @@ async def get_sources(pubkey):
     amount = 0
     for source in sources["sources"]:
         if source["conditions"] == "SIG(" + pubkey + ")":
-            amount += source["amount"] * 10 ** source["base"]
             listinput.append(
                 InputSource(
                     amount=source["amount"],
@@ -143,6 +142,7 @@ async def get_sources(pubkey):
                     index=source["noffset"],
                 )
             )
+            amount += amount_in_current_base(listinput[-1])
 
     # pending source
     history = await client(tx.pending, pubkey)
@@ -204,3 +204,10 @@ class UDValue(object):
         NBlastUDblock = blockswithud["result"]["blocks"][-1]
         lastUDblock = await client(blockchain.block, NBlastUDblock)
         return lastUDblock["dividend"] * 10 ** lastUDblock["unitbase"]
+
+
+def amount_in_current_base(source):
+    """
+    Get amount in current base from input or output source
+    """
+    return source.amount * 10 ** source.base
diff --git a/silkaj/tx.py b/silkaj/tx.py
index d4dff047..48bf5753 100644
--- a/silkaj/tx.py
+++ b/silkaj/tx.py
@@ -26,7 +26,12 @@ from silkaj.crypto_tools import check_public_key
 from silkaj.tools import message_exit, CurrencySymbol, coroutine
 from silkaj.auth import auth_method
 from silkaj.wot import get_uid_from_pubkey
-from silkaj.money import get_sources, get_amount_from_pubkey, UDValue
+from silkaj.money import (
+    get_sources,
+    get_amount_from_pubkey,
+    UDValue,
+    amount_in_current_base,
+)
 from silkaj.constants import NO_MATCHING_ID
 
 from duniterpy.api.bma.tx import process
@@ -326,8 +331,8 @@ async def get_list_input_for_transaction(pubkey, TXamount):
     intermediatetransaction = False
     for input in listinput:
         listinputfinal.append(input)
-        totalAmountInput += input.amount * 10 ** input.base
-        TXamount -= input.amount * 10 ** input.base
+        totalAmountInput += amount_in_current_base(input)
+        TXamount -= amount_in_current_base(input)
         # if more 40 sources, it's an intermediate transaction
         if len(listinputfinal) >= 40:
             intermediatetransaction = True
-- 
GitLab