Skip to main content

GetEnergyPrices gRPC Method

Loading...

Updated on
May 13, 2025

GetEnergyPrices gRPC Method

Parameters

This method does not accept any parameters

Returns

prices
string
Loading...
Request
1
package main
2
3
import (
4
"context"
5
"crypto/tls"
6
"fmt"
7
"github.com/fbsobreira/gotron-sdk/pkg/client"
8
"google.golang.org/grpc"
9
"google.golang.org/grpc/credentials"
10
"log"
11
"strings"
12
)
13
14
// Quicknode endpoints consist of two crucial components: the endpoint name and the corresponding token
15
// For eg: QN Endpoint: https://docs-demo.tron-mainnet.quiknode.pro/abcde123456789
16
// endpoint will be: docs-demo.tron-mainnet.quiknode.pro:50051 {50051 is the port number for Tron gRPC}
17
// token will be : abcde123456789
18
19
var token = "YOUR_TOKEN"
20
var endpoint = "YOUR_ENDPOINT:50051"
21
22
type auth struct {
23
token string
24
}
25
26
func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
27
return map[string]string{
28
"x-token": a.token,
29
}, nil
30
}
31
32
func (a *auth) RequireTransportSecurity() bool {
33
return false
34
}
35
36
func main() {
37
38
opts := []grpc.DialOption{
39
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
40
grpc.WithPerRPCCredentials(&auth{token}),
41
}
42
conn := client.NewGrpcClient(endpoint)
43
if err := conn.Start(opts...); err != nil {
44
panic(err)
45
}
46
defer conn.Conn.Close()
47
48
fmt.Println("Fetching historical energy prices from the TRON network...")
49
energyPrices, err := conn.GetEnergyPrices()
50
if err != nil {
51
log.Fatalf("Error getting energy prices: %v\n", err)
52
}
53
54
fmt.Println("\nHistorical Energy Prices:")
55
pricesString := energyPrices.GetPrices()
56
if pricesString == "" {
57
fmt.Println("No energy price data available.")
58
return
59
}
60
61
prices := strings.Split(pricesString, ",")
62
fmt.Printf("Found %d historical price records\n", len(prices))
63
fmt.Println("\nTimestamp : Price (in SUN per Energy unit)")
64
for _, priceEntry := range prices {
65
fmt.Println(priceEntry)
66
}
67
}
Don't have an account yet?
Create your Quicknode endpoint in seconds and start building
Get started for free