Skip to main content

ExchangeWithdraw gRPC Method

Loading...

Updated on
May 21, 2025

ExchangeWithdraw gRPC Method

Parameters

from
string
REQUIRED
Loading...
exchangeID
integer
REQUIRED
Loading...
tokenID
string
REQUIRED
Loading...
amountToken
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
"google.golang.org/grpc"
10
"google.golang.org/grpc/credentials"
11
)
12
13
// Quicknode endpoints consist of two crucial components: the endpoint name and the corresponding token
14
// For eg: QN Endpoint: https://docs-demo.tron-mainnet.quiknode.pro/abcde123456789
15
// endpoint will be: docs-demo.tron-mainnet.quiknode.pro:50051 {50051 is the port number for Tron gRPC}
16
// token will be : abcde123456789
17
18
var token = "YOUR_TOKEN"
19
var endpoint = "YOUR_ENDPOINT:50051"
20
21
type auth struct {
22
token string
23
}
24
25
func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
26
return map[string]string{
27
"x-token": a.token,
28
}, nil
29
}
30
31
func (a *auth) RequireTransportSecurity() bool {
32
return false
33
}
34
35
func main() {
36
37
opts := []grpc.DialOption{
38
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
39
grpc.WithPerRPCCredentials(&auth{token}),
40
}
41
conn := client.NewGrpcClient(endpoint)
42
if err := conn.Start(opts...); err != nil {
43
panic(err)
44
}
45
defer conn.Conn.Close()
46
47
from := "TZ4UXDV5ZhNW7fb2AMSbgfAEZ7hWsnYS2g"
48
exchangeID := int64(123)
49
50
fmt.Printf("Verifying exchange ID %d...\n", exchangeID)
51
exchange, err := conn.ExchangeByID(exchangeID)
52
if err != nil {
53
fmt.Printf("Error: The specified exchange does not exist: %v\n", err)
54
return
55
}
56
57
creatorAddress := string(exchange.CreatorAddress)
58
fmt.Printf("Exchange creator: %s\n", creatorAddress)
59
if creatorAddress != from {
60
fmt.Printf("Error: Only the exchange creator (%s) can withdraw liquidity\n", creatorAddress)
61
return
62
}
63
64
firstTokenID := string(exchange.FirstTokenId)
65
secondTokenID := string(exchange.SecondTokenId)
66
fmt.Printf("Exchange %d contains tokens: %s and %s\n",
67
exchangeID, firstTokenID, secondTokenID)
68
fmt.Printf("Current liquidity: %d %s and %d %s\n",
69
exchange.FirstTokenBalance, firstTokenID,
70
exchange.SecondTokenBalance, secondTokenID)
71
72
tokenID := firstTokenID
73
amountToken := int64(1000000)
74
75
if tokenID != firstTokenID && tokenID != secondTokenID {
76
fmt.Printf("Error: Token %s is not in exchange %d\n", tokenID, exchangeID)
77
return
78
}
79
80
var availableAmount int64
81
if tokenID == firstTokenID {
82
availableAmount = exchange.FirstTokenBalance
83
} else {
84
availableAmount = exchange.SecondTokenBalance
85
}
86
87
if amountToken > availableAmount {
88
fmt.Printf("Error: Insufficient liquidity. Requested: %d, Available: %d\n",
89
amountToken, availableAmount)
90
return
91
}
92
93
var otherTokenAmount int64
94
if tokenID == firstTokenID {
95
otherTokenAmount = amountToken * exchange.SecondTokenBalance / exchange.FirstTokenBalance
96
fmt.Printf("Withdrawing %d %s will also withdraw approximately %d %s\n",
97
amountToken, firstTokenID, otherTokenAmount, secondTokenID)
98
} else {
99
otherTokenAmount = amountToken * exchange.FirstTokenBalance / exchange.SecondTokenBalance
100
fmt.Printf("Withdrawing %d %s will also withdraw approximately %d %s\n",
101
amountToken, secondTokenID, otherTokenAmount, firstTokenID)
102
}
103
104
fmt.Println("Creating exchange withdrawal transaction...")
105
106
tx, err := conn.ExchangeWithdraw(
107
from, // The address withdrawing liquidity (must be exchange creator)
108
exchangeID, // Exchange ID
109
tokenID, // Token ID to withdraw
110
amountToken, // Amount to withdraw
111
)
112
113
if err != nil {
114
fmt.Printf("Error creating withdrawal transaction: %v\n", err)
115
return
116
}
117
118
fmt.Println("Withdrawal transaction created successfully. Transaction details:")
119
jsonData, _ := json.MarshalIndent(tx, "", " ")
120
fmt.Println(string(jsonData))
121
122
// NOTE: In a production environment, the transaction should be signed here
123
fmt.Println("\nBroadcasting transaction to the TRON network...")
124
result, err := conn.Broadcast(tx.Transaction)
125
if err != nil {
126
fmt.Printf("Error broadcasting transaction: %v\n", err)
127
return
128
}
129
130
if !result.GetResult() {
131
fmt.Printf("Broadcast failed: %s\n", result.GetMessage())
132
return
133
}
134
135
fmt.Println("Liquidity withdrawal executed successfully! Result:")
136
resultJSON, _ := json.MarshalIndent(result, "", " ")
137
fmt.Println(string(resultJSON))
138
}
Don't have an account yet?
Create your Quicknode endpoint in seconds and start building
Get started for free