EstimateEnergy gRPC Method
매개변수
출처:
문자열
필수
로딩 중...
contractAddress
문자열
필수
로딩 중...
메서드
문자열
필수
로딩 중...
jsonString
문자열
필수
로딩 중...
tAmount
정수
필수
로딩 중...
tTokenID
문자열
필수
로딩 중...
tTokenAmount
정수
필수
로딩 중...
반품
결과
객체
로딩 중...
결과
부울
로딩 중...
코드
문자열
로딩 중...
메시지
문자열
로딩 중...
energy_required
정수
로딩 중...
요청
1패키지 main23import (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 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)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패키지 main23import (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 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)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}