Skip to main content

FreezeBalanceV2 gRPC Method

Loading...

Updated on
May 13, 2025

FreezeBalanceV2 gRPC Method

Parameters

from
string
REQUIRED
Loading...
resource
string
REQUIRED
Loading...
frozenBalance
integer
REQUIRED
Loading...

Returns

transaction
object
Loading...
raw_data
object
Loading...
ref_block_bytes
string
Loading...
ref_block_num
integer
Loading...
ref_block_hash
string
Loading...
expiration
integer
Loading...
auths
array
Loading...
data
string
Loading...
contract
array
Loading...
type
string
Loading...
parameter
object
Loading...
value
string
Loading...
type_url
string
Loading...
provider
string
Loading...
ContractName
string
Loading...
Permission_id
integer
Loading...
scripts
string
Loading...
timestamp
integer
Loading...
fee_limit
integer
Loading...
signature
array
Loading...
ret
array
Loading...
fee
integer
Loading...
ret
string
Loading...
contractRet
string
Loading...
assetIssueID
string
Loading...
withdraw_amount
integer
Loading...
unfreeze_amount
integer
Loading...
exchange_received_amount
integer
Loading...
exchange_inject_another_amount
integer
Loading...
exchange_withdraw_another_amount
integer
Loading...
exchange_id
integer
Loading...
shielded_transaction_fee
integer
Loading...
orderId
string
Loading...
orderDetails
array
Loading...
makerOrderId
string
Loading...
takerOrderId
string
Loading...
fillSellQuantity
integer
Loading...
fillBuyQuantity
integer
Loading...
withdraw_expire_amount
integer
Loading...
cancelUnfreezeV2Amount
object
Loading...
txid
string
Loading...
constant_result
array
Loading...
result
object
Loading...
result
boolean
Loading...
code
string
Loading...
message
string
Loading...
energy_used
integer
Loading...
logs
array
Loading...
address
string
Loading...
topics
array
Loading...
data
string
Loading...
internal_transactions
array
Loading...
hash
string
Loading...
caller_address
string
Loading...
transferTo_address
string
Loading...
callValueInfo
array
Loading...
callValue
integer
Loading...
tokenId
string
Loading...
note
string
Loading...
rejected
boolean
Loading...
extra
string
Loading...
energy_penalty
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
"github.com/fbsobreira/gotron-sdk/pkg/proto/core"
10
"google.golang.org/grpc"
11
"google.golang.org/grpc/credentials"
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
// Convert SUN to TRX for display (1 TRX = 1,000,000 SUN)
37
func sunToTrx(sun int64) float64 {
38
return float64(sun) / 1000000.0
39
}
40
41
func main() {
42
/*
43
* 1. The FreezeBalanceV2 method is the upgraded version of FreezeBalance for obtaining resources.
44
* 2. Unlike V1, frozen TRX using V2 can be unfrozen at any time without a waiting period.
45
* 3. V2 does not support delegating resources to other accounts directly (use DelegateResource instead).
46
* 4. The account must have sufficient TRX balance for the freeze operation.
47
* 5. The transaction must be signed before broadcasting to the network.
48
* 6. FreezeBalanceV2 is the recommended method for obtaining resources in newer Tron versions.
49
*/
50
51
opts := []grpc.DialOption{
52
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
53
grpc.WithPerRPCCredentials(&auth{token}),
54
}
55
conn := client.NewGrpcClient(endpoint)
56
if err := conn.Start(opts...); err != nil {
57
panic(err)
58
}
59
defer conn.Conn.Close()
60
61
from := "TZ4UXDV5ZhNW7fb2AMSbgfAEZ7hWsnYS2g" // Address that is freezing TRX
62
resource := core.ResourceCode_BANDWIDTH // Resource type (BANDWIDTH or ENERGY)
63
frozenBalance := int64(10000000) // Amount of TRX to freeze in SUN (10 TRX)
64
65
resourceName := "BANDWIDTH"
66
if resource == core.ResourceCode_ENERGY {
67
resourceName = "ENERGY"
68
}
69
70
fmt.Printf("Checking balance for account %s...\n", from)
71
account, err := conn.GetAccount(from)
72
if err != nil {
73
fmt.Printf("Error getting account: %v\n", err)
74
return
75
}
76
77
currentBalance := account.GetBalance()
78
fmt.Printf("Current balance: %d SUN (%.6f TRX)\n", currentBalance, sunToTrx(currentBalance))
79
80
if currentBalance < frozenBalance {
81
fmt.Printf("Error: Insufficient balance. Have: %d SUN (%.6f TRX), Need: %d SUN (%.6f TRX)\n",
82
currentBalance, sunToTrx(currentBalance), frozenBalance, sunToTrx(frozenBalance))
83
if currentBalance > 1000000 { // At least 1 TRX available
84
suggestedAmount := (currentBalance / 1000000) * 1000000 // Round down to nearest TRX
85
fmt.Printf("\nAdjusting freeze amount to %d SUN (%.0f TRX) based on available balance\n",
86
suggestedAmount, sunToTrx(suggestedAmount))
87
frozenBalance = suggestedAmount
88
} else {
89
fmt.Println("\nYour balance is too low to freeze any meaningful amount of TRX.")
90
fmt.Println("Please add more TRX to your account and try again.")
91
return
92
}
93
}
94
95
fmt.Printf("Freezing %d SUN (%.6f TRX) to obtain %s resources\n",
96
frozenBalance, sunToTrx(frozenBalance), resourceName)
97
98
fmt.Println("Creating freeze balance V2 transaction...")
99
100
tx, err := conn.FreezeBalanceV2(
101
from, // From address
102
resource, // Resource type (BANDWIDTH or ENERGY)
103
frozenBalance, // Amount to freeze in SUN
104
)
105
106
if err != nil {
107
fmt.Printf("Error creating freeze transaction: %v\n", err)
108
return
109
}
110
111
fmt.Println("Freeze transaction created successfully. Transaction details:")
112
jsonData, _ := json.MarshalIndent(tx, "", " ")
113
fmt.Println(string(jsonData))
114
115
// Broadcast the transaction
116
fmt.Println("\nBroadcasting transaction to the TRON network...")
117
result, err := conn.Broadcast(tx.Transaction)
118
if err != nil {
119
fmt.Printf("Error broadcasting transaction: %v\n", err)
120
return
121
}
122
123
if !result.GetResult() {
124
fmt.Printf("Broadcast failed: %s\n", result.GetMessage())
125
return
126
}
127
128
fmt.Println("TRX frozen successfully using V2! Result:")
129
resultJSON, _ := json.MarshalIndent(result, "", " ")
130
fmt.Println(string(resultJSON))
131
}
Don't have an account yet?
Create your Quicknode endpoint in seconds and start building
Get started for free