EstimateEnergy gRPC Method
參數
來自
字串
必填
載入中...
contractAddress
字串
必填
載入中...
method
字串
必填
載入中...
jsonString
字串
必填
載入中...
tAmount
整數
必填
載入中...
tTokenID
字串
必填
載入中...
tTokenAmount
整數
必填
載入中...
退貨
結果
物件
載入中...
結果
布林值
載入中...
程式碼
字串
載入中...
訊息
字串
載入中...
energy_required
整數
載入中...
請求
1package main23import (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)1213// Quicknode endpoints consist of two crucial components: the endpoint name and the corresponding token14// For eg: QN Endpoint: https://docs-demo.tron-mainnet.quiknode.pro/abcde12345678915// endpoint will be: docs-demo.tron-mainnet.quiknode.pro:50051 {50051 is the port number for Tron gRPC}16// token will be : abcde1234567891718var token = "YOUR_TOKEN"19var endpoint = "YOUR_ENDPOINT:50051"2021type auth struct {22token string23}2425func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {26return map[string]string{27"x-token": a.token,28}, nil29}3031func (a *auth) RequireTransportSecurity() bool {32return false33}3435func main() {3637opts := []grpc.DialOption{38grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),39grpc.WithPerRPCCredentials(&auth{token}),40}41conn := client.NewGrpcClient(endpoint)42if err := conn.Start(opts...); err != nil {43panic(err)44}45defer conn.Conn.Close()4647from := "TTGhREx2pDSxFX555NWz1YwGpiBVPvQA7e"48contractAddress := "TVSvjZdyDSNocHm7dP3jvCmMNsCnMTPa5W"49method := "transfer(address,uint256)"50jsonString := `[{"address": "TE4c73WubeWPhSF1nAovQDmQytjcaLZyY9"},{"uint256": "100"}]`51tAmount := int64(0)52tTokenID := ""53tTokenAmount := int64(0)5455fmt.Println("Estimating energy for contract call...")5657energy, err := conn.EstimateEnergy(58from, // Caller's address59contractAddress, // Contract address60method, // Function selector61jsonString, // Parameters in JSON format62tAmount, // TRX value to send63tTokenID, // Token ID (if applicable)64tTokenAmount, // Token value (if applicable)65)6667if err != nil {68fmt.Printf("Error estimating energy: %v\n", err)69return70}7172fmt.Println("Energy estimation successful! Results:")73jsonData, _ := json.MarshalIndent(energy, "", " ")74fmt.Println(string(jsonData))7576if !energy.Result.Result {77fmt.Println("Energy estimation failed!")78if len(energy.Result.Message) > 0 {79fmt.Printf("Error message: %s\n", energy.Result.Message)80}81return82}8384energyRequired := energy.EnergyRequired85energyBuffer := energyRequired / 5 // Add 20% buffer86recommendedFeeLimit := (energyRequired + energyBuffer) * 4208788fmt.Printf("\nEstimated energy required: %d\n", energyRequired)89fmt.Printf("Recommended fee limit (with 20%% buffer): %d SUN\n", recommendedFeeLimit)90fmt.Println("\nYou can use this fee limit when making the actual transaction.")91}
1package main23import (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)1213// Quicknode endpoints consist of two crucial components: the endpoint name and the corresponding token14// For eg: QN Endpoint: https://docs-demo.tron-mainnet.quiknode.pro/abcde12345678915// endpoint will be: docs-demo.tron-mainnet.quiknode.pro:50051 {50051 is the port number for Tron gRPC}16// token will be : abcde1234567891718var token = "YOUR_TOKEN"19var endpoint = "YOUR_ENDPOINT:50051"2021type auth struct {22token string23}2425func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {26return map[string]string{27"x-token": a.token,28}, nil29}3031func (a *auth) RequireTransportSecurity() bool {32return false33}3435func main() {3637opts := []grpc.DialOption{38grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),39grpc.WithPerRPCCredentials(&auth{token}),40}41conn := client.NewGrpcClient(endpoint)42if err := conn.Start(opts...); err != nil {43panic(err)44}45defer conn.Conn.Close()4647from := "TTGhREx2pDSxFX555NWz1YwGpiBVPvQA7e"48contractAddress := "TVSvjZdyDSNocHm7dP3jvCmMNsCnMTPa5W"49method := "transfer(address,uint256)"50jsonString := `[{"address": "TE4c73WubeWPhSF1nAovQDmQytjcaLZyY9"},{"uint256": "100"}]`51tAmount := int64(0)52tTokenID := ""53tTokenAmount := int64(0)5455fmt.Println("Estimating energy for contract call...")5657energy, err := conn.EstimateEnergy(58from, // Caller's address59contractAddress, // Contract address60method, // Function selector61jsonString, // Parameters in JSON format62tAmount, // TRX value to send63tTokenID, // Token ID (if applicable)64tTokenAmount, // Token value (if applicable)65)6667if err != nil {68fmt.Printf("Error estimating energy: %v\n", err)69return70}7172fmt.Println("Energy estimation successful! Results:")73jsonData, _ := json.MarshalIndent(energy, "", " ")74fmt.Println(string(jsonData))7576if !energy.Result.Result {77fmt.Println("Energy estimation failed!")78if len(energy.Result.Message) > 0 {79fmt.Printf("Error message: %s\n", energy.Result.Message)80}81return82}8384energyRequired := energy.EnergyRequired85energyBuffer := energyRequired / 5 // Add 20% buffer86recommendedFeeLimit := (energyRequired + energyBuffer) * 4208788fmt.Printf("\nEstimated energy required: %d\n", energyRequired)89fmt.Printf("Recommended fee limit (with 20%% buffer): %d SUN\n", recommendedFeeLimit)90fmt.Println("\nYou can use this fee limit when making the actual transaction.")91}