Skip to content
Snippets Groups Projects
Commit 652eb059 authored by Moul's avatar Moul Committed by Mael
Browse files

[enh] #184: add generic f() to calculate amount in current base…

… from input or output source
- Use it in balance and tx commands
parent 31d51df2
No related branches found
No related tags found
No related merge requests found
...@@ -120,7 +120,7 @@ async def get_amount_from_pubkey(pubkey): ...@@ -120,7 +120,7 @@ async def get_amount_from_pubkey(pubkey):
totalAmountInput = 0 totalAmountInput = 0
for input in listinput: for input in listinput:
totalAmountInput += input.amount * 10 ** input.base totalAmountInput += amount_in_current_base(input)
return totalAmountInput, amount return totalAmountInput, amount
...@@ -133,7 +133,6 @@ async def get_sources(pubkey): ...@@ -133,7 +133,6 @@ async def get_sources(pubkey):
amount = 0 amount = 0
for source in sources["sources"]: for source in sources["sources"]:
if source["conditions"] == "SIG(" + pubkey + ")": if source["conditions"] == "SIG(" + pubkey + ")":
amount += source["amount"] * 10 ** source["base"]
listinput.append( listinput.append(
InputSource( InputSource(
amount=source["amount"], amount=source["amount"],
...@@ -143,6 +142,7 @@ async def get_sources(pubkey): ...@@ -143,6 +142,7 @@ async def get_sources(pubkey):
index=source["noffset"], index=source["noffset"],
) )
) )
amount += amount_in_current_base(listinput[-1])
# pending source # pending source
history = await client(tx.pending, pubkey) history = await client(tx.pending, pubkey)
...@@ -204,3 +204,10 @@ class UDValue(object): ...@@ -204,3 +204,10 @@ class UDValue(object):
NBlastUDblock = blockswithud["result"]["blocks"][-1] NBlastUDblock = blockswithud["result"]["blocks"][-1]
lastUDblock = await client(blockchain.block, NBlastUDblock) lastUDblock = await client(blockchain.block, NBlastUDblock)
return lastUDblock["dividend"] * 10 ** lastUDblock["unitbase"] 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
...@@ -26,7 +26,12 @@ from silkaj.crypto_tools import check_public_key ...@@ -26,7 +26,12 @@ from silkaj.crypto_tools import check_public_key
from silkaj.tools import message_exit, CurrencySymbol, coroutine from silkaj.tools import message_exit, CurrencySymbol, coroutine
from silkaj.auth import auth_method from silkaj.auth import auth_method
from silkaj.wot import get_uid_from_pubkey 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 silkaj.constants import NO_MATCHING_ID
from duniterpy.api.bma.tx import process from duniterpy.api.bma.tx import process
...@@ -326,8 +331,8 @@ async def get_list_input_for_transaction(pubkey, TXamount): ...@@ -326,8 +331,8 @@ async def get_list_input_for_transaction(pubkey, TXamount):
intermediatetransaction = False intermediatetransaction = False
for input in listinput: for input in listinput:
listinputfinal.append(input) listinputfinal.append(input)
totalAmountInput += input.amount * 10 ** input.base totalAmountInput += amount_in_current_base(input)
TXamount -= input.amount * 10 ** input.base TXamount -= amount_in_current_base(input)
# if more 40 sources, it's an intermediate transaction # if more 40 sources, it's an intermediate transaction
if len(listinputfinal) >= 40: if len(listinputfinal) >= 40:
intermediatetransaction = True intermediatetransaction = True
......
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