github.com/jimpick/sp-kyc-checks@v0.0.0-20230201194251-fa84fca72da8/checks/geoip/current_epoch.go (about)

     1  package geoip
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	"net/http"
     7  
     8  	jsonrpc "github.com/filecoin-project/go-jsonrpc"
     9  	lotusapi "github.com/filecoin-project/lotus/api"
    10  )
    11  
    12  // GetCurrentEpoch gets the current chain height from the Lotus API
    13  func GetCurrentEpoch(ctx context.Context) (int64, error) {
    14  	headers := http.Header{}
    15  	api_addr := "api.chain.love"
    16  
    17  	var api lotusapi.FullNodeStruct
    18  	closer, err := jsonrpc.NewMergeClient(ctx,
    19  		"wss://"+api_addr+"/rpc/v0", "Filecoin",
    20  		[]interface{}{&api.Internal, &api.CommonStruct.Internal}, headers)
    21  	if err != nil {
    22  		log.Fatalf("connecting with lotus failed: %s", err)
    23  	}
    24  	defer closer()
    25  
    26  	ts, err := api.ChainHead(ctx)
    27  	if err != nil {
    28  		return 0, err
    29  	}
    30  	height := int64(ts.Height())
    31  	log.Printf("Chain height: %v\n", height)
    32  	return height, nil
    33  }