TwCtl.get_balance(): add block parameter
This commit is contained in:
parent
82294e6a88
commit
43d9b7bb60
4 changed files with 12 additions and 12 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue