tw.ctl: add use_cached_balances attr

This commit is contained in:
The MMGen Project 2025-05-23 15:35:22 +00:00
commit ea7cb6f6f3
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 6 additions and 2 deletions

View file

@ -33,6 +33,6 @@ class EthereumTwView(TwView):
def gen_subheader(self, cw, color):
if self.disp_prec == 8:
yield 'Balances truncated to 8 decimal points'
if self.cfg.cached_balances:
if self.twctl.use_cached_balances:
from ....color import nocolor, yellow
yield (nocolor, yellow)[color]('WARNING: Using cached balances. These may be out of date!')

View file

@ -54,6 +54,7 @@ class TwCtl(MMGenObject, metaclass=AsyncInit):
aggressive_sync = False
importing = False
tw_fn = 'tracking-wallet.json'
use_cached_balances = False
def __new__(cls, cfg, proto, *args, **kwargs):
return MMGenObject.__new__(
@ -81,6 +82,9 @@ class TwCtl(MMGenObject, metaclass=AsyncInit):
self.desc = self.base_desc = f'{self.proto.name} tracking wallet'
self.cur_balances = {} # cache balances to prevent repeated lookups per program invocation
if cfg.cached_balances:
self.use_cached_balances = True
if not no_rpc:
self.rpc = await rpc_init(cfg, proto, ignore_wallet=rpc_ignore_wallet)
@ -182,7 +186,7 @@ class TwCtl(MMGenObject, metaclass=AsyncInit):
def get_cached_balance(self, addr, session_cache, data_root):
if addr in session_cache:
return self.proto.coin_amt(session_cache[addr])
if not self.cfg.cached_balances:
if not self.use_cached_balances:
return None
if addr in data_root and 'balance' in data_root[addr]:
return self.proto.coin_amt(data_root[addr]['balance'])