-1

I am playing with Cosmos Blockchain and trying to get Block details inputting its height. For this purpose I am trying to use CosmPy module.

I found a function inside the library that allows you to do exaclty what I want that is called

GetBlockByHeight()

But I am unable to use it since it require the block height (got it) and a self parameter (?) when I try to call it using:

from cosmpy.tendermint.interface import CosmosBaseTendermint
print(CosmosBaseTendermint.GetBlockByHeight(block_height))

What code should I use to properly call the function?

giacomomaraglino
  • 177
  • 2
  • 14

1 Answers1

1

Here's the working code:

from google.protobuf.json_format import MessageToJson
from cosmpy.common.rest_client import RestClient
from cosmpy.protos.cosmos.base.tendermint.v1beta1.query_pb2 import (
    GetBlockByHeightRequest,
    GetBlockByHeightResponse,
)
from cosmpy.tendermint.rest_client import CosmosBaseTendermintRestClient

rest_client = RestClient("https://rest-fetchhub.fetch.ai:443")
client = CosmosBaseTendermintRestClient(rest_client)
resp = client.GetBlockByHeight(GetBlockByHeightRequest(height=6000000))
print(MessageToJson(resp))
giacomomaraglino
  • 177
  • 2
  • 14
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 13 '23 at 08:45