package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
gasestimation "celestia-grpc/celestia/core/v1/gas_estimation"
)
// QuickNode endpoints consist of two crucial components: the endpoint name and the corresponding token
// For eg: QN Endpoint: https://docs-demo.celestia-mainnet.quiknode.pro/abcde123456789
// endpoint will be: docs-demo.celestia-mainnet.quiknode.pro:9090 {9090 is the port number for Celestia gRPC}
// token will be : abcde123456789
var token = "YOUR_TOKEN"
var endpoint = "YOUR_ENDPOINT:9090"
type auth struct {
token string
}
func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"x-token": a.token,
}, nil
}
func (a *auth) RequireTransportSecurity() bool {
return false
}
func main() {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(&auth{token}),
}
// Create gRPC connection
conn, err := grpc.Dial(endpoint, opts...)
if err != nil {
log.Fatalf("Failed to connect to Celestia gRPC server: %v", err)
}
defer conn.Close()
client := gasestimation.NewGasEstimatorClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.EstimateGasPrice(ctx, &gasestimation.EstimateGasPriceRequest{
TxPriority: gasestimation.TxPriority_TX_PRIORITY_MEDIUM,
})
if err != nil {
log.Fatalf("Failed to estimate gas price: %v", err)
}
jsonData, err := json.MarshalIndent(resp, "", " ")
if err != nil {
log.Printf("Error converting to JSON: %v", err)
} else {
fmt.Println("\nOutput:")
fmt.Println(string(jsonData))
}
}