Skip to main content

ExchangeCreate gRPC Method

Loading...

Updated on
May 13, 2025

ExchangeCreate gRPC Method

Parameters

from
string
REQUIRED
Loading...
tokenID1
string
REQUIRED
Loading...
amountToken1
integer
REQUIRED
Loading...
tokenID2
string
REQUIRED
Loading...
amountToken2
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
// Using variable names that match the function signature
48
from := "TZ4UXDV5ZhNW7fb2AMSbgfAEZ7hWsnYS2g" // Address creating the exchange
49
tokenID1 := "1000001"
50
amountToken1 := int64(100000000)
51
tokenID2 := "1000002"
52
amountToken2 := int64(200000000)
53
54
fmt.Println("Creating exchange pair...")
55
56
tx, err := conn.ExchangeCreate(
57
from, // The creator's address
58
tokenID1, // First token ID
59
amountToken1, // First token amount
60
tokenID2, // Second token ID
61
amountToken2, // Second token amount
62
)
63
64
if err != nil {
65
fmt.Printf("Error creating exchange pair: %v\n", err)
66
fmt.Println("Note: Creating an exchange pair requires 1024 TRX fee. Make sure the account has sufficient TRX balance.")
67
return
68
}
69
70
fmt.Println("Exchange pair creation transaction created successfully. Transaction details:")
71
jsonData, _ := json.MarshalIndent(tx, "", " ")
72
fmt.Println(string(jsonData))
73
74
// NOTE: In a production environment, the transaction should be signed here
75
fmt.Println("\nBroadcasting transaction to the TRON network...")
76
result, err := conn.Broadcast(tx.Transaction)
77
if err != nil {
78
fmt.Printf("Error broadcasting transaction: %v\n", err)
79
return
80
}
81
82
if !result.GetResult() {
83
fmt.Printf("Broadcast failed: %s\n", result.GetMessage())
84
return
85
}
86
87
fmt.Println("Exchange pair created successfully! Result:")
88
resultJSON, _ := json.MarshalIndent(result, "", " ")
89
fmt.Println(string(resultJSON))
90
91
rate1 := float64(amountToken2) / float64(amountToken1)
92
rate2 := float64(amountToken1) / float64(amountToken2)
93
fmt.Printf("\nInitial Exchange Rates:\n")
94
fmt.Printf("1 %s = %.6f %s\n", tokenID1, rate1, tokenID2)
95
fmt.Printf("1 %s = %.6f %s\n", tokenID2, rate2, tokenID1)
96
fmt.Println("\nAfter transaction confirmation, users can swap tokens using this exchange pair.")
97
}
Don't have an account yet?
Create your Quicknode endpoint in seconds and start building
Get started for free