EstimateEnergy gRPC Method
参数
来自
字符串
必填
正在加载...
contractAddress
字符串
必填
正在加载...
method
字符串
必填
正在加载...
jsonString
字符串
必填
正在加载...
tAmount
整数
必填
正在加载...
tTokenID
字符串
必填
正在加载...
tTokenAmount
整数
必填
正在加载...
退货
结果
对象
正在加载...
结果
布尔型
正在加载...
代码
字符串
正在加载...
消息
字符串
正在加载...
energy_required
整数
正在加载...
请求
1包 main23导入 (4"上下文"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 端点 = "YOUR_ENDPOINT:50051"2021类型 auth 结构体 {22token string23}2425func (a *auth) GetRequestMetadata(ctx 上下文.上下文, uri ...字符串) (map[字符串]字符串, 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)69返回70}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}81返回82}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}
1包 main23导入 (4"上下文"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 端点 = "YOUR_ENDPOINT:50051"2021类型 auth 结构体 {22token string23}2425func (a *auth) GetRequestMetadata(ctx 上下文.上下文, uri ...字符串) (map[字符串]字符串, 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)69返回70}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}81返回82}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}