英文:
Unable to get Block by Height using Cosmpy module
问题
我正在与Cosmos Blockchain进行交互,尝试通过输入其高度来获取区块详细信息。为此,我尝试使用CosmPy模块。
我在库中找到了一个名为GetBlockByHeight()的函数,它可以做我想要的事情,但我无法使用它,因为它需要区块高度(我有了)和一个self参数(?)当我尝试这样调用它时:
from cosmpy.tendermint.interface import CosmosBaseTendermint
print(CosmosBaseTendermint.GetBlockByHeight(block_height))
我应该使用什么代码来正确调用这个函数?
英文:
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?
答案1
得分: 1
以下是已翻译的代码部分:
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))
英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论