From 9ee01aa5e11687cf4ea026e9044c10244444b653 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 23 May 2025 15:35:23 +0000 Subject: [PATCH] proto.eth.tw: support `minconf` --- mmgen/proto/eth/rpc.py | 8 ++++++++ mmgen/proto/eth/tw/bal.py | 3 ++- mmgen/proto/eth/tw/unspent.py | 6 ++++-- mmgen/tw/addresses.py | 7 ++++--- mmgen/tw/unspent.py | 2 +- mmgen/tw/view.py | 1 + test/cmdtest_d/ethdev.py | 10 +++++----- 7 files changed, 25 insertions(+), 12 deletions(-) diff --git a/mmgen/proto/eth/rpc.py b/mmgen/proto/eth/rpc.py index 3c3c296c..4882953a 100755 --- a/mmgen/proto/eth/rpc.py +++ b/mmgen/proto/eth/rpc.py @@ -92,6 +92,14 @@ class EthereumRPCClient(RPCClient, metaclass=AsyncInit): def make_host_path(self, wallet): return '' + def get_block_from_minconf(self, minconf): + assert minconf - 1 <= self.blockcount, ( + f'{minconf}: illegal value for ‘minconf’ (exceeds block count)') + return ( + 'pending' if minconf == 0 else + 'latest' if minconf == 1 else + hex(self.blockcount - (minconf - 1))) + rpcmethods = ( 'eth_blockNumber', 'eth_call', diff --git a/mmgen/proto/eth/tw/bal.py b/mmgen/proto/eth/tw/bal.py index 1a94cba0..a52119ba 100755 --- a/mmgen/proto/eth/tw/bal.py +++ b/mmgen/proto/eth/tw/bal.py @@ -36,6 +36,7 @@ class EthereumTwGetBalance(TwGetBalance): async def create_data(self): in_data = self.twctl.mmid_ordered_dict + block = self.twctl.rpc.get_block_from_minconf(self.minconf) for d in in_data: if d.type == 'mmgen': label = d.obj.sid @@ -44,7 +45,7 @@ class EthereumTwGetBalance(TwGetBalance): else: label = 'Non-MMGen' - amt = await self.twctl.get_balance(in_data[d]['addr']) + amt = await self.twctl.get_balance(in_data[d]['addr'], block=block) self.data['TOTAL']['ge_minconf'] += amt self.data[label]['ge_minconf'] += amt diff --git a/mmgen/proto/eth/tw/unspent.py b/mmgen/proto/eth/tw/unspent.py index 4ae7f359..346772ad 100755 --- a/mmgen/proto/eth/tw/unspent.py +++ b/mmgen/proto/eth/tw/unspent.py @@ -102,13 +102,15 @@ class EthereumTwUnspentOutputs(EthereumTwView, TwUnspentOutputs): async def get_rpc_data(self): wl = self.twctl.sorted_list + minconf = int(self.minconf) + block = self.twctl.rpc.get_block_from_minconf(minconf) if self.addrs: wl = [d for d in wl if d['addr'] in self.addrs] return [{ 'account': TwLabel(self.proto, d['mmid']+' '+d['comment']), 'address': d['addr'], - 'amt': await self.twctl.get_balance(d['addr']), - 'confirmations': 0, # TODO + 'amt': await self.twctl.get_balance(d['addr'], block=block), + 'confirmations': minconf, } for d in wl] class EthereumTokenTwUnspentOutputs(EthereumTwUnspentOutputs): diff --git a/mmgen/tw/addresses.py b/mmgen/tw/addresses.py index 284e5348..f1d068a4 100755 --- a/mmgen/tw/addresses.py +++ b/mmgen/tw/addresses.py @@ -131,19 +131,20 @@ class TwAddresses(TwView): async def get_rpc_data(self): self.total = self.proto.coin_amt('0') - self.minconf = None addrs = {} used_addrs = self.twctl.used_addrs + minconf = int(self.minconf) + block = self.twctl.rpc.get_block_from_minconf(minconf) for e in await self.twctl.get_label_addr_pairs(): - bal = await self.twctl.get_balance(e.coinaddr) + bal = await self.twctl.get_balance(e.coinaddr, block=block) addrs[e.label.mmid] = { 'addr': e.coinaddr, 'amt': bal, 'recvd': bal, # current bal only, CF btc.tw.addresses.get_rpc_data() 'is_used': bool(bal) or e.coinaddr in used_addrs, - 'confs': 0, + 'confs': minconf, 'lbl': e.label} self.total += bal diff --git a/mmgen/tw/unspent.py b/mmgen/tw/unspent.py index ca81662d..7e69238a 100755 --- a/mmgen/tw/unspent.py +++ b/mmgen/tw/unspent.py @@ -72,7 +72,7 @@ class TwUnspentOutputs(TwView): async def __init__(self, cfg, proto, *, minconf=1, addrs=[]): await super().__init__(cfg, proto) - self.minconf = minconf + self.minconf = NonNegativeInt(minconf) self.addrs = addrs from ..cfg import gc self.min_cols = gc.min_screen_width diff --git a/mmgen/tw/view.py b/mmgen/tw/view.py index 1ed31a6b..97540ee9 100755 --- a/mmgen/tw/view.py +++ b/mmgen/tw/view.py @@ -86,6 +86,7 @@ class TwView(MMGenObject, metaclass=AsyncInit): reverse = False group = False use_cached = False + minconf = 1 txid_w = 64 sort_key = 'age' display_hdr = () diff --git a/test/cmdtest_d/ethdev.py b/test/cmdtest_d/ethdev.py index 7ed0fe6d..d5a52afe 100755 --- a/test/cmdtest_d/ethdev.py +++ b/test/cmdtest_d/ethdev.py @@ -616,7 +616,7 @@ class CmdTestEthdev(CmdTestEthdevMethods, CmdTestBase, CmdTestShared): ('token_bal6', 'the token balance'), ('listaddresses1', 'listaddresses'), - ('listaddresses2', 'listaddresses minconf=999999999 (ignored)'), + ('listaddresses2', 'listaddresses minconf=3'), ('listaddresses3', 'listaddresses sort=age (ignored)'), ('listaddresses4', 'listaddresses showempty=1 sort=age (ignored)'), @@ -663,8 +663,8 @@ class CmdTestEthdev(CmdTestEthdevMethods, CmdTestBase, CmdTestShared): ('twview1', 'twview'), ('twview2', 'twview wide=1'), ('twview3', 'twview wide=1 sort=age (ignored)'), - ('twview4', 'twview wide=1 minconf=999999999 (ignored)'), - ('twview5', 'twview wide=1 minconf=0 (ignored)'), + ('twview4', 'twview wide=1 minconf=15'), + ('twview5', 'twview wide=1 minconf=0'), ('token_twview1', 'twview --token=mm1'), ('token_twview2', 'twview --token=mm1 wide=1'), ('token_twview3', 'twview --token=mm1 wide=1 sort=age (ignored)'), @@ -1527,7 +1527,7 @@ class CmdTestEthdev(CmdTestEthdevMethods, CmdTestBase, CmdTestShared): def listaddresses1(self): return self.listaddresses() def listaddresses2(self): - return self.listaddresses(tool_args=['minconf=999999999']) + return self.listaddresses(tool_args=['minconf=3']) def listaddresses3(self): return self.listaddresses(tool_args=['sort=amt', 'reverse=1']) def listaddresses4(self): @@ -1619,7 +1619,7 @@ class CmdTestEthdev(CmdTestEthdevMethods, CmdTestBase, CmdTestShared): def twview3(self): return self.twview(tool_args=['wide=1', 'sort=age']) def twview4(self): - return self.twview(tool_args=['wide=1', 'minconf=999999999']) + return self.twview(tool_args=['wide=1', 'minconf=15'], expect_str=r'E:1\D.*\D100\D') def twview5(self): return self.twview(tool_args=['wide=1', 'minconf=0']) def twview6(self):