package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/fbsobreira/gotron-sdk/pkg/client"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"log"
)
// QuickNode endpoints consist of two crucial components: the endpoint name and the corresponding token
// For eg: QN Endpoint: https://docs-demo.tron-mainnet.quiknode.pro/abcde123456789
// endpoint will be: docs-demo.tron-mainnet.quiknode.pro:50051 {50051 is the port number for Tron gRPC}
// token will be : abcde123456789
var token = "YOUR_TOKEN"
var endpoint = "YOUR_ENDPOINT:50051"
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}),
}
conn := client.NewGrpcClient(endpoint)
if err := conn.Start(opts...); err != nil {
panic(err)
}
defer conn.Conn.Close()
contractAddress := "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"
fmt.Printf("Fetching ABI for contract: %s\n", contractAddress)
contractABI, err := conn.GetContractABI(contractAddress)
if err != nil {
log.Fatalf("Error getting contract ABI: %v\n", err)
}
fmt.Println("\nContract ABI Information:")
abiJSON, err := json.MarshalIndent(contractABI, "", " ")
if err != nil {
log.Printf("Error marshaling ABI to JSON: %v\n", err)
} else {
fmt.Println(string(abiJSON))
}
if contractABI.Entrys != nil && len(contractABI.Entrys) > 0 {
fmt.Printf("\nContract has %d function entries\n", len(contractABI.Entrys))
maxFunctions := 5
if len(contractABI.Entrys) < maxFunctions {
maxFunctions = len(contractABI.Entrys)
}
fmt.Println("\nFunction Overview:")
for i := 0; i < maxFunctions; i++ {
entry := contractABI.Entrys[i]
fmt.Printf("%d. Name: %s, Type: %s\n", i+1, entry.Name, entry.Type)
if len(entry.Inputs) > 0 {
fmt.Print(" Parameters: ")
for j, input := range entry.Inputs {
fmt.Printf("%s (%s)", input.Name, input.Type)
if j < len(entry.Inputs)-1 {
fmt.Print(", ")
}
}
fmt.Println()
}
if len(entry.Outputs) > 0 {
fmt.Print(" Returns: ")
for j, output := range entry.Outputs {
fmt.Printf("%s (%s)", output.Name, output.Type)
if j < len(entry.Outputs)-1 {
fmt.Print(", ")
}
}
fmt.Println()
}
}
if len(contractABI.Entrys) > maxFunctions {
fmt.Printf("\n... and %d more functions\n", len(contractABI.Entrys)-maxFunctions)
}
} else {
fmt.Println("No function entries found in this contract's ABI.")
}
}