github.com/InjectiveLabs/sdk-go@v1.53.0/examples/chain/distribution/query/4_ValidatorSlashes/example.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/cosmos/cosmos-sdk/types/query"
    10  
    11  	"github.com/InjectiveLabs/sdk-go/client"
    12  	chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
    13  	"github.com/InjectiveLabs/sdk-go/client/common"
    14  	rpchttp "github.com/cometbft/cometbft/rpc/client/http"
    15  )
    16  
    17  func main() {
    18  	network := common.LoadNetwork("testnet", "lb")
    19  	tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  
    24  	senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
    25  		os.Getenv("HOME")+"/.injectived",
    26  		"injectived",
    27  		"file",
    28  		"inj-user",
    29  		"12345678",
    30  		"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
    31  		false,
    32  	)
    33  
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  
    38  	clientCtx, err := chainclient.NewClientContext(
    39  		network.ChainId,
    40  		senderAddress.String(),
    41  		cosmosKeyring,
    42  	)
    43  
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  
    48  	clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
    49  
    50  	chainClient, err := chainclient.NewChainClient(
    51  		clientCtx,
    52  		network,
    53  		common.OptionGasPrices(client.DefaultGasPriceWithDenom),
    54  	)
    55  
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  
    60  	validatorAddress := "injvaloper1jue5dpr9lerjn6wlwtrywxrsenrf28ru89z99z"
    61  	startingHeight := uint64(0)
    62  	endingHeight := uint64(0)
    63  	pagination := query.PageRequest{Limit: 10}
    64  	ctx := context.Background()
    65  
    66  	res, err := chainClient.FetchValidatorSlashes(ctx, validatorAddress, startingHeight, endingHeight, &pagination)
    67  	if err != nil {
    68  		fmt.Println(err)
    69  	}
    70  
    71  	str, _ := json.MarshalIndent(res, "", " ")
    72  	fmt.Print(string(str))
    73  
    74  }