@ -19,6 +19,7 @@ from itertools import chain
from decimal import Decimal
from decimal import Decimal
from numbers import Integral
from numbers import Integral
from math import exp
from math import exp
from typing import Any , Dict , Optional , Tuple
from . configure import jm_single
from . configure import jm_single
@ -280,32 +281,28 @@ class UTXOManager(object):
' value ' : utxos [ s [ ' utxo ' ] ] [ 1 ] }
' value ' : utxos [ s [ ' utxo ' ] ] [ 1 ] }
for s in selected }
for s in selected }
def get_balance_by_mixdepth ( self , max_mixdepth = float ( ' Inf ' ) ,
def get_balance_at_mixdepth ( self , mixdepth : int ,
include_disabled = True , maxheight = None ) :
include_disabled : bool = True ,
""" By default this returns a dict of aggregated bitcoin
maxheight : Optional [ int ] = None ) - > int :
balance per mixdepth : { 0 : N sats , 1 : M sats , . . . } for all
""" By default this returns aggregated bitcoin balance at mixdepth.
currently available mixdepths .
If max_mixdepth is set it will return balances only up
to that mixdepth .
To get only enabled balance , set include_disabled = False .
To get only enabled balance , set include_disabled = False .
To get balances only with a certain number of confs , use maxheight .
To get balances only with a certain number of confs , use maxheight .
"""
"""
balance_dict = collections . defaultdict ( int )
utxomap = self . _utxo . get ( mixdepth )
for mixdepth , utxomap in self . _utxo . items ( ) :
if not utxomap :
if mixdepth > max_mixdepth :
return 0
continue
if not include_disabled :
if not include_disabled :
utxomap = { k : v for k , v in utxomap . items (
utxomap = { k : v for k , v in utxomap . items (
) if not self . is_disabled ( * k ) }
) if not self . is_disabled ( * k ) }
if maxheight is not None :
if maxheight is not None :
utxomap = { k : v for k , v in utxomap . items (
utxomap = { k : v for k , v in utxomap . items (
) if v [ 2 ] < = maxheight }
) if v [ 2 ] < = maxheight }
return sum ( x [ 1 ] for x in utxomap . values ( ) )
value = sum ( x [ 1 ] for x in utxomap . values ( ) )
balance_dict [ mixdepth ] = value
def get_utxos_at_mixdepth ( self , mixdepth : int ) - > \
return balance_dict
Dict [ Tuple [ bytes , int ] , Tuple [ Tuple , int , int ] ] :
utxomap = self . _utxo . get ( mixdepth )
def get_utxos_by_mixdepth ( self ) :
return deepcopy ( utxomap ) if utxomap else { }
return deepcopy ( self . _utxo )
def __eq__ ( self , o ) :
def __eq__ ( self , o ) :
return self . _utxo == o . _utxo and \
return self . _utxo == o . _utxo and \
@ -836,10 +833,19 @@ class BaseWallet(object):
confirmations , set maxheight to max acceptable blockheight .
confirmations , set maxheight to max acceptable blockheight .
returns : { mixdepth : value }
returns : { mixdepth : value }
"""
"""
balances = collections . defaultdict ( int )
for md in range ( self . mixdepth + 1 ) :
balances [ md ] = self . get_balance_at_mixdepth ( md , verbose = verbose ,
include_disabled = include_disabled , maxheight = maxheight )
return balances
def get_balance_at_mixdepth ( self , mixdepth ,
verbose : bool = True ,
include_disabled : bool = False ,
maxheight : Optional [ int ] = None ) - > int :
# TODO: verbose
# TODO: verbose
return self . _utxos . get_balance_by_mixdepth ( max_mixdepth = self . mixdepth ,
return self . _utxos . get_balance_at_mixdepth ( mixdepth ,
include_disabled = include_disabled ,
include_disabled = include_disabled , maxheight = maxheight )
maxheight = maxheight )
def get_utxos_by_mixdepth ( self , include_disabled = False , includeheight = False ) :
def get_utxos_by_mixdepth ( self , include_disabled = False , includeheight = False ) :
"""
"""
@ -850,25 +856,35 @@ class BaseWallet(object):
{ ' script ' : bytes , ' path ' : tuple , ' value ' : int } } }
{ ' script ' : bytes , ' path ' : tuple , ' value ' : int } } }
( if ` includeheight ` is True , adds key ' height ' : int )
( if ` includeheight ` is True , adds key ' height ' : int )
"""
"""
mix_utxos = self . _utxos . get_utxos_by_mixdepth ( )
script_utxos = collections . defaultdict ( dict )
script_utxos = collections . defaultdict ( dict )
for md , data in mix_utxos . items ( ) :
for md in range ( self . mixdepth + 1 ) :
if md > self . mixdepth :
script_utxos [ md ] = self . get_utxos_at_mixdepth ( md ,
continue
include_disabled = include_disabled , includeheight = includeheight )
return script_utxos
def get_utxos_at_mixdepth ( self , mixdepth : int ,
include_disabled : bool = False ,
includeheight : bool = False ) - > \
Dict [ Tuple [ bytes , int ] , Dict [ str , Any ] ] :
script_utxos = { }
if 0 < = mixdepth < = self . mixdepth :
data = self . _utxos . get_utxos_at_mixdepth ( mixdepth )
for utxo , ( path , value , height ) in data . items ( ) :
for utxo , ( path , value , height ) in data . items ( ) :
if not include_disabled and self . _utxos . is_disabled ( * utxo ) :
if not include_disabled and self . _utxos . is_disabled ( * utxo ) :
continue
continue
script = self . get_script_from_path ( path )
script = self . get_script_from_path ( path )
addr = self . get_address_from_path ( path )
addr = self . get_address_from_path ( path )
label = self . get_address_label ( addr )
label = self . get_address_label ( addr )
script_utxos [ md ] [ utxo ] = { ' script ' : script ,
script_utxo = {
' path ' : path ,
' script ' : script ,
' value ' : value ,
' path ' : path ,
' address ' : addr ,
' value ' : value ,
' label ' : label }
' address ' : addr ,
' label ' : label ,
}
if includeheight :
if includeheight :
script_utxos [ md ] [ utxo ] [ ' height ' ] = height
script_utxo [ ' height ' ] = height
script_utxos [ utxo ] = script_utxo
return script_utxos
return script_utxos