Skip to content

Commit

Permalink
support query result for block height
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code committed Oct 8, 2023
1 parent 380029c commit d849c2d
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions client/grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from google.protobuf.any_pb2 import Any
from urllib.parse import urlparse
from typing import Optional

from wallet.builder import TxBuilder
from x.cosmos.auth.v1beta1.auth_pb2 import BaseAccount
Expand All @@ -28,42 +29,49 @@
from x.tendermint.p2p.types_pb2 import DefaultNodeInfo
from x.tendermint.types.block_pb2 import Block

GRPCBlockHeightHeader = 'x-cosmos-block-height'


class BaseClient:
def __init__(self, url: str = 'localhost:9090'):
self.__height = 0
if urlparse(url).scheme == "https":
self.channel = grpc.secure_channel(
urlparse(url).netloc, grpc.ssl_channel_credentials())
else:
self.channel = grpc.insecure_channel(url)

def query_account_info(self, address: str) -> BaseAccount:
def query_account_info(self, address: str, height: Optional[int] = 0) -> BaseAccount:

account_any = AuthQuery(self.channel).Account(
QueryAccountRequest(address=address)).account
metadata = [(GRPCBlockHeightHeader, str(height))]
response = AuthQuery(self.channel).Account(QueryAccountRequest(address=address), metadata=metadata)
account_any = response.account
account = BaseAccount()
if account_any.Is(account.DESCRIPTOR):
account_any.Unpack(account)
return account

def query_all_balances(self, address: str) -> [Coin]:
response = BankQuery(self.channel).AllBalances(QueryAllBalancesRequest(address=address))
def query_all_balances(self, address: str, height: Optional[int] = 0) -> [Coin]:

metadata = [(GRPCBlockHeightHeader, str(height))]
response = BankQuery(self.channel).AllBalances(QueryAllBalancesRequest(address=address), metadata=metadata)
return response.balances

def query_balance(self, address: str, denom: str) -> Coin:
def query_balance(self, address: str, denom: str, height: Optional[int] = 0) -> Coin:

response = BankQuery(self.channel).Balance(QueryBalanceRequest(address=address, denom=denom))
metadata = [(GRPCBlockHeightHeader, str(height))]
response = BankQuery(self.channel).Balance(QueryBalanceRequest(address=address, denom=denom), metadata=metadata)
return response.balance

def query_total_supply(self) -> [Supply]:
def query_total_supply(self, height: Optional[int] = 0) -> [Supply]:

response = BankQuery(self.channel).TotalSupply(QueryTotalSupplyRequest())
metadata = [(GRPCBlockHeightHeader, str(height))]
response = BankQuery(self.channel).TotalSupply(QueryTotalSupplyRequest(), metadata=metadata)
return response.supply

def query_gas_price(self) -> [Coin]:
def query_gas_price(self, height: Optional[int] = 0) -> [Coin]:

response = CosmosNodeClient(self.channel).Config(ConfigRequest())
metadata = [(GRPCBlockHeightHeader, str(height))]
response = CosmosNodeClient(self.channel).Config(ConfigRequest(), metadata=metadata)
return response.minimum_gas_price

def query_block_by_height(self, height: int) -> Block:
Expand Down Expand Up @@ -91,17 +99,18 @@ def query_node_info(self) -> DefaultNodeInfo:
response = TendermintClient(self.channel).GetNodeInfo(GetNodeInfoRequest())
return response.default_node_info

def query_validator_list(self) -> [Validator]:
def query_validator_list(self, height: Optional[int] = 0) -> [Validator]:

response = StakingClient(self.channel).Validators(QueryValidatorsRequest())
metadata = [(GRPCBlockHeightHeader, str(height))]
response = StakingClient(self.channel).Validators(QueryValidatorsRequest(), metadata=metadata)
return response.validators

def query_tx(self, tx_hash: str) -> GetTxResponse:

return TxClient(self.channel).GetTx(GetTxRequest(hash=tx_hash))

def build_tx(self, tx_builder: TxBuilder, msg: [Any], account_number: int = -1, sequence: int = -1,
gas_limit: int = 0) -> Tx:
def build_tx(self, tx_builder: TxBuilder, msg: [Any], account_number: int = -1,
sequence: int = -1, gas_limit: int = 0) -> Tx:

if tx_builder.chain_id == '':
tx_builder.chain_id = self.query_chain_id()
Expand Down Expand Up @@ -150,6 +159,7 @@ def broadcast_tx(self, tx: Tx, mode: BroadcastMode = BROADCAST_MODE_BLOCK) -> Tx
return response.tx_response

def wait_mint_tx(self, tx_hash: str) -> TxResponse:

for i in range(5):
time.sleep(5)
try:
Expand Down

0 comments on commit d849c2d

Please sign in to comment.