Skip to main content

DelegateResource gRPC Method

Loading...

Updated on
May 13, 2025

DelegateResource gRPC Method

Parameters

from
string
REQUIRED
Loading...
to
string
REQUIRED
Loading...
resource
string
REQUIRED
Loading...
delegateBalance
integer
REQUIRED
Loading...
lock
boolean
REQUIRED
Loading...
lockPeriod
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
func main() {
37
/*
38
* IMPORTANT NOTES FOR DOCUMENTATION:
39
* 1. The DelegateResource method allows an account to delegate bandwidth or energy to another account.
40
* 2. The delegator retains ownership of the resources but allows another account to use them.
41
* 3. The delegation can be for either bandwidth (NET) or energy (CPU) resources.
42
* 4. The delegation can be locked for a specified period (minimum 3 days).
43
* 5. The owner account must have sufficient resources to delegate.
44
*
45
* PREREQUISITE:
46
* Before using DelegateResource, you must first freeze TRX for resources using FreezeBalanceV2.
47
* You can only delegate resources that you've previously frozen, and the delegation amount
48
* must be less than or equal to your available frozen resources of the specified type.
49
* Example:
50
* // First freeze TRX for bandwidth
51
* conn.FreezeBalanceV2(from, delegateBalance, core.ResourceCode_BANDWIDTH)
52
* // Then delegate a portion of those frozen resources
53
* conn.DelegateResource(from, to, core.ResourceCode_BANDWIDTH, delegateBalance, lock, lockPeriod)
54
*/
55
opts := []grpc.DialOption{
56
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
57
grpc.WithPerRPCCredentials(&auth{token}),
58
}
59
conn := client.NewGrpcClient(endpoint)
60
if err := conn.Start(opts...); err != nil {
61
panic(err)
62
}
63
defer conn.Conn.Close()
64
65
from := "TZ4UXDV5ZhNW7fb2AMSbgfAEZ7hWsnYS2g"
66
to := "TFgY1uN8buRxAtV2r6Zy5sG3ACko6pJT1y"
67
delegateBalance := int64(1000000)
68
lockPeriod := int64(3)
69
lock := false
70
resource := core.ResourceCode_BANDWIDTH // For bandwidth delegation
71
// resource := core.ResourceCode_ENERGY // For energy delegation
72
73
fmt.Println("Creating resource delegation transaction...")
74
75
// NOTE: This will fail if the owner address doesn't have enough frozen bandwidth resources.
76
// You must first freeze TRX for bandwidth using FreezeBalanceV2 before delegating.
77
78
tx, err := conn.DelegateResource(
79
from,
80
to,
81
resource,
82
delegateBalance,
83
lock,
84
lockPeriod,
85
)
86
87
if err != nil {
88
fmt.Printf("Error creating resource delegation transaction: %v\n", err)
89
return
90
}
91
92
fmt.Println("Resource delegation transaction created successfully. Transaction details:")
93
jsonData, _ := json.MarshalIndent(tx, "", " ")
94
fmt.Println(string(jsonData))
95
96
fmt.Println("\nBroadcasting transaction to the TRON network...")
97
result, err := conn.Broadcast(tx.Transaction)
98
if err != nil {
99
fmt.Printf("Error broadcasting transaction: %v\n", err)
100
return
101
}
102
103
if !result.GetResult() {
104
fmt.Printf("Broadcast failed: %s\n", result.GetMessage())
105
return
106
}
107
108
fmt.Println("Resource delegation successful! Result:")
109
resultJSON, _ := json.MarshalIndent(result, "", " ")
110
fmt.Println(string(resultJSON))
111
}
Don't have an account yet?
Create your Quicknode endpoint in seconds and start building
Get started for free