ExchangeTrade gRPC Method
パラメータ
出典:
文字列
必須
読み込み中...
exchangeID
整数
必須
読み込み中...
tokenID
文字列
必須
読み込み中...
amountToken
整数
必須
読み込み中...
amountExpected
整数
必須
読み込み中...
返品
取引
オブジェクト
読み込み中...
raw_data
オブジェクト
読み込み中...
ref_block_bytes
文字列
読み込み中...
ref_block_num
整数
読み込み中...
ref_block_hash
文字列
読み込み中...
expiration
整数
読み込み中...
auths
配列
読み込み中...
データ
文字列
読み込み中...
契約
配列
読み込み中...
タイプ
文字列
読み込み中...
parameter
オブジェクト
読み込み中...
値
文字列
読み込み中...
type_url
文字列
読み込み中...
provider
文字列
読み込み中...
ContractName
文字列
読み込み中...
Permission_id
整数
読み込み中...
scripts
文字列
読み込み中...
タイムスタンプ
整数
読み込み中...
fee_limit
整数
読み込み中...
署名
配列
読み込み中...
ret
配列
読み込み中...
手数料
整数
読み込み中...
ret
文字列
読み込み中...
contractRet
文字列
読み込み中...
assetIssueID
文字列
読み込み中...
withdraw_amount
整数
読み込み中...
unfreeze_amount
整数
読み込み中...
exchange_received_amount
整数
読み込み中...
exchange_inject_another_amount
整数
読み込み中...
exchange_withdraw_another_amount
整数
読み込み中...
exchange_id
整数
読み込み中...
shielded_transaction_fee
整数
読み込み中...
orderId
文字列
読み込み中...
orderDetails
配列
読み込み中...
makerOrderId
文字列
読み込み中...
takerOrderId
文字列
読み込み中...
fillSellQuantity
整数
読み込み中...
fillBuyQuantity
整数
読み込み中...
withdraw_expire_amount
整数
読み込み中...
cancelUnfreezeV2Amount
オブジェクト
読み込み中...
txid
文字列
読み込み中...
constant_result
配列
読み込み中...
結果
オブジェクト
読み込み中...
結果
ブール値
読み込み中...
コード
文字列
読み込み中...
メッセージ
文字列
読み込み中...
energy_used
整数
読み込み中...
ログ
配列
読み込み中...
住所
文字列
読み込み中...
トピック
配列
読み込み中...
データ
文字列
読み込み中...
internal_transactions
配列
読み込み中...
ハッシュ
文字列
読み込み中...
caller_address
文字列
読み込み中...
transferTo_address
文字列
読み込み中...
callValueInfo
配列
読み込み中...
callValue
整数
読み込み中...
tokenId
文字列
読み込み中...
注
文字列
読み込み中...
rejected
ブール値
読み込み中...
extra
文字列
読み込み中...
energy_penalty
整数
読み込み中...
リクエスト
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 := "TZ4UXDV5ZhNW7fb2AMSbgfAEZ7hWsnYS2g" // Address performing the trade48exchangeID := int64(123) // ID of the exchange pair4950fmt.Printf("Verifying exchange ID %d...\n", exchangeID)51exchange, err := conn.ExchangeByID(exchangeID)52if err != nil {53fmt.Printf("Error: The specified exchange does not exist: %v\n", err)54return55}5657firstTokenID := string(exchange.FirstTokenId)58secondTokenID := string(exchange.SecondTokenId)59fmt.Printf("Exchange %d contains tokens: %s and %s\n",60exchangeID, firstTokenID, secondTokenID)6162tokenID := firstTokenID63amountToken := int64(1000000)64amountExpected := int64(1800000)6566if tokenID != firstTokenID && tokenID != secondTokenID {67fmt.Printf("Error: Token %s is not in exchange %d\n", tokenID, exchangeID)68return69}7071fmt.Printf("Checking token balance for %s...\n", tokenID)72var tokenBalance int6473// The method to get balance depends on whether we're trading TRX or a TRC10 token74if tokenID == "" {75// For TRX76account, err := conn.GetAccount(from)77if err != nil {78fmt.Printf("Error getting account: %v\n", err)79return80}81tokenBalance = account.Balance82} else {83// For TRC10 tokens84account, err := conn.GetAccount(from)85if err != nil {86fmt.Printf("Error getting account: %v\n", err)87return88}8990found := false91for tokenName, balance := range account.AssetV2 {92if string(tokenName) == tokenID {93tokenBalance = balance94found = true95break96}97}98if !found {99tokenBalance = 0100}101}102103fmt.Printf("Token balance: %d\n", tokenBalance)104105if tokenBalance < amountToken {106fmt.Printf("Error: Insufficient token balance. Have: %d, Need: %d\n", tokenBalance, amountToken)107fmt.Println("Please make sure you have enough tokens before trading.")108return109}110111// Create the trade transaction112fmt.Println("Creating exchange trade transaction...")113114tx, err := conn.ExchangeTrade(115from, // The trader's address116exchangeID, // Exchange ID117tokenID, // Token ID to trade118amountToken, // Amount to trade119amountExpected, // Minimum expected return120)121122if err != nil {123fmt.Printf("Error creating trade transaction: %v\n", err)124return125}126127fmt.Println("Trade transaction created successfully. Transaction details:")128jsonData, _ := json.MarshalIndent(tx, "", " ")129fmt.Println(string(jsonData))130131// NOTE: In a production environment, the transaction should be signed here132fmt.Println("\nBroadcasting transaction to the TRON network...")133result, err := conn.Broadcast(tx.Transaction)134if err != nil {135fmt.Printf("Error broadcasting transaction: %v\n", err)136return137}138139if !result.GetResult() {140fmt.Printf("Broadcast failed: %s\n", result.GetMessage())141return142}143144fmt.Println("Trade executed successfully! Result:")145resultJSON, _ := json.MarshalIndent(result, "", " ")146fmt.Println(string(resultJSON))147}
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 := "TZ4UXDV5ZhNW7fb2AMSbgfAEZ7hWsnYS2g" // Address performing the trade48exchangeID := int64(123) // ID of the exchange pair4950fmt.Printf("Verifying exchange ID %d...\n", exchangeID)51exchange, err := conn.ExchangeByID(exchangeID)52if err != nil {53fmt.Printf("Error: The specified exchange does not exist: %v\n", err)54return55}5657firstTokenID := string(exchange.FirstTokenId)58secondTokenID := string(exchange.SecondTokenId)59fmt.Printf("Exchange %d contains tokens: %s and %s\n",60exchangeID, firstTokenID, secondTokenID)6162tokenID := firstTokenID63amountToken := int64(1000000)64amountExpected := int64(1800000)6566if tokenID != firstTokenID && tokenID != secondTokenID {67fmt.Printf("Error: Token %s is not in exchange %d\n", tokenID, exchangeID)68return69}7071fmt.Printf("Checking token balance for %s...\n", tokenID)72var tokenBalance int6473// The method to get balance depends on whether we're trading TRX or a TRC10 token74if tokenID == "" {75// For TRX76account, err := conn.GetAccount(from)77if err != nil {78fmt.Printf("Error getting account: %v\n", err)79return80}81tokenBalance = account.Balance82} else {83// For TRC10 tokens84account, err := conn.GetAccount(from)85if err != nil {86fmt.Printf("Error getting account: %v\n", err)87return88}8990found := false91for tokenName, balance := range account.AssetV2 {92if string(tokenName) == tokenID {93tokenBalance = balance94found = true95break96}97}98if !found {99tokenBalance = 0100}101}102103fmt.Printf("Token balance: %d\n", tokenBalance)104105if tokenBalance < amountToken {106fmt.Printf("Error: Insufficient token balance. Have: %d, Need: %d\n", tokenBalance, amountToken)107fmt.Println("Please make sure you have enough tokens before trading.")108return109}110111// Create the trade transaction112fmt.Println("Creating exchange trade transaction...")113114tx, err := conn.ExchangeTrade(115from, // The trader's address116exchangeID, // Exchange ID117tokenID, // Token ID to trade118amountToken, // Amount to trade119amountExpected, // Minimum expected return120)121122if err != nil {123fmt.Printf("Error creating trade transaction: %v\n", err)124return125}126127fmt.Println("Trade transaction created successfully. Transaction details:")128jsonData, _ := json.MarshalIndent(tx, "", " ")129fmt.Println(string(jsonData))130131// NOTE: In a production environment, the transaction should be signed here132fmt.Println("\nBroadcasting transaction to the TRON network...")133result, err := conn.Broadcast(tx.Transaction)134if err != nil {135fmt.Printf("Error broadcasting transaction: %v\n", err)136return137}138139if !result.GetResult() {140fmt.Printf("Broadcast failed: %s\n", result.GetMessage())141return142}143144fmt.Println("Trade executed successfully! Result:")145resultJSON, _ := json.MarshalIndent(result, "", " ")146fmt.Println(string(resultJSON))147}