From 43d9b7bb60645427e153a9313c481281cd44bd2c Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 8 May 2025 15:22:02 +0000 Subject: [PATCH] TwCtl.get_balance(): add `block` parameter --- mmgen/proto/btc/tw/ctl.py | 2 +- mmgen/proto/eth/contract.py | 6 +++--- mmgen/proto/eth/tw/ctl.py | 12 ++++++------ mmgen/tw/ctl.py | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/mmgen/proto/btc/tw/ctl.py b/mmgen/proto/btc/tw/ctl.py index a302649c..1af357dd 100755 --- a/mmgen/proto/btc/tw/ctl.py +++ b/mmgen/proto/btc/tw/ctl.py @@ -23,7 +23,7 @@ class BitcoinTwCtl(TwCtl): def upgrade_wallet_maybe(self): pass - async def rpc_get_balance(self, addr): + async def rpc_get_balance(self, addr, block='latest'): raise NotImplementedError('not implemented') @write_mode diff --git a/mmgen/proto/eth/contract.py b/mmgen/proto/eth/contract.py index c89d26af..13a9a972 100755 --- a/mmgen/proto/eth/contract.py +++ b/mmgen/proto/eth/contract.py @@ -59,7 +59,7 @@ class Contract: method_args = '', *, method = 'eth_call', - block = 'pending', # earliest, latest, safe, finalized + block = 'latest', # earliest, latest, safe, finalized, pending from_addr = None, data = None, toUnit = False): @@ -130,9 +130,9 @@ class Token(Contract): self.decimals = decimals self.base_unit = Decimal('10') ** -self.decimals - async def get_balance(self, acct_addr): + async def get_balance(self, acct_addr, block='latest'): return self.proto.coin_amt( - await self.do_call('balanceOf(address)', acct_addr.rjust(64, '0'), toUnit=True), + await self.do_call('balanceOf(address)', acct_addr.rjust(64, '0'), toUnit=True, block=block), from_decimal = True) async def get_name(self): diff --git a/mmgen/proto/eth/tw/ctl.py b/mmgen/proto/eth/tw/ctl.py index 4978f53f..59493363 100755 --- a/mmgen/proto/eth/tw/ctl.py +++ b/mmgen/proto/eth/tw/ctl.py @@ -81,9 +81,9 @@ class EthereumTwCtl(TwCtl): self.force_write() msg(f'{self.desc} upgraded successfully!') - async def rpc_get_balance(self, addr): + async def rpc_get_balance(self, addr, block='latest'): return self.proto.coin_amt( - int(await self.rpc.call('eth_getBalance', '0x' + addr, 'latest'), 16), + int(await self.rpc.call('eth_getBalance', '0x' + addr, block), 16), from_unit = 'wei') @write_mode @@ -223,20 +223,20 @@ class EthereumTokenTwCtl(EthereumTwCtl): def data_root_desc(self): return 'token ' + self.get_param('symbol') - async def rpc_get_balance(self, addr): + async def rpc_get_balance(self, addr, block='latest'): return await Token( self.cfg, self.proto, self.token, decimals = self.decimals, - rpc = self.rpc).get_balance(addr) + rpc = self.rpc).get_balance(addr, block=block) - async def get_eth_balance(self, addr, *, force_rpc=False): + async def get_eth_balance(self, addr, *, force_rpc=False, block='latest'): cache = self.cur_eth_balances r = self.data['accounts'] ret = None if force_rpc else self.get_cached_balance(addr, cache, r) if ret is None: - ret = await super().rpc_get_balance(addr) + ret = await super().rpc_get_balance(addr, block=block) self.cache_balance(addr, ret, session_cache=cache, data_root=r) return ret diff --git a/mmgen/tw/ctl.py b/mmgen/tw/ctl.py index 94c313d1..5ea59330 100755 --- a/mmgen/tw/ctl.py +++ b/mmgen/tw/ctl.py @@ -184,10 +184,10 @@ class TwCtl(MMGenObject, metaclass=AsyncInit): if addr in data_root and 'balance' in data_root[addr]: return self.proto.coin_amt(data_root[addr]['balance']) - async def get_balance(self, addr, *, force_rpc=False): + async def get_balance(self, addr, *, force_rpc=False, block='latest'): ret = None if force_rpc else self.get_cached_balance(addr, self.cur_balances, self.data_root) if ret is None: - ret = await self.rpc_get_balance(addr) + ret = await self.rpc_get_balance(addr, block=block) self.cache_balance(addr, ret, session_cache=self.cur_balances, data_root=self.data_root) return ret