Are there any docs which states how to get a block from cosmos-SDK blockchains?

81 views Asked by At

I want to fetch blocks using golang from a blockchain which is built using cosmos-SDK.

Are there any docs which helps us to use golang client of cosmos-SDK ?

Its very annoying as i see no docs to acheive the same.

Any help will be appreciated.

1

There are 1 answers

0
Daniel Harapko On

Depending on whether you are using CometBFT or Tendermint as the consensus mechanism for your blockchain, you may need to use different libraries for interacting with RPC. Here is the code example that you can use:


    package main
    
    import (
        "context"
        "github.com/cometbft/cometbft/rpc/client/http"
        "log"
    )
    
    func main() {
        rpcClient, err := http.New("https://rpc_address:443", "/websocket")
        if err != nil {
            log.Println("Error creating RPC client: ", err)
            return
        }
        height := int64(12345)
        resultBlock, err := rpcClient.Block(context.Background(), &height)
        if err != nil {
            log.Println("Error receiving block: ", err)
            return
        }
        log.Println("block result: ", resultBlock)
    }

Set your RPC address with 443 port or 26657 & your block height number. Also need to note that you can use Tendermint RPC & Cosmos SDK REST/LCD for it:

Tendermint RPC(link depends on version & can will be changed): https://docs.tendermint.com/v0.34/rpc/ Cosmos SDK REST/LCD: https://docs.cosmos.network/api