Skip to main content

EstimateEnergy gRPC Method

Loading...

Updated on
May 13, 2025

EstimateEnergy gRPC Method

Parameters

from
string
REQUIRED
Loading...
contractAddress
string
REQUIRED
Loading...
method
string
REQUIRED
Loading...
jsonString
string
REQUIRED
Loading...
tAmount
integer
REQUIRED
Loading...
tTokenID
string
REQUIRED
Loading...
tTokenAmount
integer
REQUIRED
Loading...

Returns

result
object
Loading...
result
boolean
Loading...
code
string
Loading...
message
string
Loading...
energy_required
integer
Loading...
Request
1
package main
2
3
import (
4
"context"
5
"crypto/tls"
6
"encoding/json"
7
"fmt"
8
"github.com/fbsobreira/gotron-sdk/pkg/client"
9
"google.golang.org/grpc"
10
"google.golang.org/grpc/credentials"
11
)
12
13
// Quicknode endpoints consist of two crucial components: the endpoint name and the corresponding token
14
// For eg: QN Endpoint: https://docs-demo.tron-mainnet.quiknode.pro/abcde123456789
15
// endpoint will be: docs-demo.tron-mainnet.quiknode.pro:50051 {50051 is the port number for Tron gRPC}
16
// token will be : abcde123456789
17
18
var token = "YOUR_TOKEN"
19
var endpoint = "YOUR_ENDPOINT:50051"
20
21
type auth struct {
22
token string
23
}
24
25
func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
26
return map[string]string{
27
"x-token": a.token,
28
}, nil
29
}
30
31
func (a *auth) RequireTransportSecurity() bool {
32
return false
33
}
34
35
func main() {
36
37
opts := []grpc.DialOption{
38
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
39
grpc.WithPerRPCCredentials(&auth{token}),
40
}
41
conn := client.NewGrpcClient(endpoint)
42
if err := conn.Start(opts...); err != nil {
43
panic(err)
44
}
45
defer conn.Conn.Close()
46
47
from := "TTGhREx2pDSxFX555NWz1YwGpiBVPvQA7e"
48
contractAddress := "TVSvjZdyDSNocHm7dP3jvCmMNsCnMTPa5W"
49
method := "transfer(address,uint256)"
50
jsonString := `[{"address": "TE4c73WubeWPhSF1nAovQDmQytjcaLZyY9"},{"uint256": "100"}]`
51
tAmount := int64(0)
52
tTokenID := ""
53
tTokenAmount := int64(0)
54
55
fmt.Println("Estimating energy for contract call...")
56
57
energy, err := conn.EstimateEnergy(
58
from, // Caller's address
59
contractAddress, // Contract address
60
method, // Function selector
61
jsonString, // Parameters in JSON format
62
tAmount, // TRX value to send
63
tTokenID, // Token ID (if applicable)
64
tTokenAmount, // Token value (if applicable)
65
)
66
67
if err != nil {
68
fmt.Printf("Error estimating energy: %v\n", err)
69
return
70
}
71
72
fmt.Println("Energy estimation successful! Results:")
73
jsonData, _ := json.MarshalIndent(energy, "", " ")
74
fmt.Println(string(jsonData))
75
76
if !energy.Result.Result {
77
fmt.Println("Energy estimation failed!")
78
if len(energy.Result.Message) > 0 {
79
fmt.Printf("Error message: %s\n", energy.Result.Message)
80
}
81
return
82
}
83
84
energyRequired := energy.EnergyRequired
85
energyBuffer := energyRequired / 5 // Add 20% buffer
86
recommendedFeeLimit := (energyRequired + energyBuffer) * 420
87
88
fmt.Printf("\nEstimated energy required: %d\n", energyRequired)
89
fmt.Printf("Recommended fee limit (with 20%% buffer): %d SUN\n", recommendedFeeLimit)
90
fmt.Println("\nYou can use this fee limit when making the actual transaction.")
91
}
Don't have an account yet?
Create your Quicknode endpoint in seconds and start building
Get started for free