github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/transaction_response.go (about)

     1  package hedera
     2  
     3  import (
     4  	"encoding/hex"
     5  
     6  	jsoniter "github.com/json-iterator/go"
     7  )
     8  
     9  /*-
    10   *
    11   * Hedera Go SDK
    12   *
    13   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    14   *
    15   * Licensed under the Apache License, Version 2.0 (the "License");
    16   * you may not use this file except in compliance with the License.
    17   * You may obtain a copy of the License at
    18   *
    19   *      http://www.apache.org/licenses/LICENSE-2.0
    20   *
    21   * Unless required by applicable law or agreed to in writing, software
    22   * distributed under the License is distributed on an "AS IS" BASIS,
    23   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    24   * See the License for the specific language governing permissions and
    25   * limitations under the License.
    26   *
    27   */
    28  
    29  // When the client sends the node a transaction of any kind, the node replies with this, which
    30  // simply says that the transaction passed the precheck (so the node will submit it to the network)
    31  // or it failed (so it won't). If the fee offered was insufficient, this will also contain the
    32  // amount of the required fee. To learn the consensus result, the client should later obtain a
    33  // receipt (free), or can buy a more detailed record (not free).
    34  type TransactionResponse struct {
    35  	TransactionID          TransactionID
    36  	ScheduledTransactionId TransactionID // nolint
    37  	NodeID                 AccountID
    38  	Hash                   []byte
    39  	ValidateStatus         bool
    40  	IncludeChildReceipts   bool
    41  	Transaction            TransactionInterface
    42  }
    43  
    44  // MarshalJSON returns the JSON representation of the TransactionResponse.
    45  // This should yield the same result in all SDK's.
    46  func (response TransactionResponse) MarshalJSON() ([]byte, error) {
    47  	var json = jsoniter.ConfigCompatibleWithStandardLibrary
    48  	obj := make(map[string]interface{})
    49  	obj["nodeID"] = response.NodeID.String()
    50  	obj["hash"] = hex.EncodeToString(response.Hash)
    51  	obj["transactionID"] = response.TransactionID.String()
    52  	return json.Marshal(obj)
    53  }
    54  
    55  // retryTransaction is a helper function to retry a transaction that was throttled
    56  func retryTransaction(client *Client, transaction TransactionInterface) (TransactionReceipt, error) {
    57  	resp, err := TransactionExecute(transaction, client)
    58  	if err != nil {
    59  		return TransactionReceipt{}, err
    60  	}
    61  	receipt, err := NewTransactionReceiptQuery().
    62  		SetTransactionID(resp.TransactionID).
    63  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    64  		Execute(client)
    65  	return receipt, err
    66  }
    67  
    68  // GetReceipt retrieves the receipt for the transaction
    69  func (response TransactionResponse) GetReceipt(client *Client) (TransactionReceipt, error) {
    70  	receipt, err := NewTransactionReceiptQuery().
    71  		SetTransactionID(response.TransactionID).
    72  		SetNodeAccountIDs([]AccountID{response.NodeID}).
    73  		SetIncludeChildren(response.IncludeChildReceipts).
    74  		Execute(client)
    75  
    76  	for receipt.Status == StatusThrottledAtConsensus {
    77  		receipt, err = retryTransaction(client, response.Transaction)
    78  	}
    79  
    80  	if err != nil {
    81  		return receipt, err
    82  	}
    83  
    84  	return receipt, receipt.ValidateStatus(response.ValidateStatus)
    85  }
    86  
    87  // GetRecord retrieves the record for the transaction
    88  func (response TransactionResponse) GetRecord(client *Client) (TransactionRecord, error) {
    89  	receipt, err := NewTransactionReceiptQuery().
    90  		SetTransactionID(response.TransactionID).
    91  		SetNodeAccountIDs([]AccountID{response.NodeID}).
    92  		Execute(client)
    93  
    94  	if err != nil {
    95  		// Manually add the receipt, because an empty TransactionRecord will have an empty receipt and empty receipt has no status and no status defaults to 0, which means success
    96  		return TransactionRecord{Receipt: receipt}, err
    97  	}
    98  
    99  	return NewTransactionRecordQuery().
   100  		SetTransactionID(response.TransactionID).
   101  		SetNodeAccountIDs([]AccountID{response.NodeID}).
   102  		Execute(client)
   103  }
   104  
   105  // GetReceiptQuery retrieves the receipt query for the transaction
   106  func (response TransactionResponse) GetReceiptQuery() *TransactionReceiptQuery {
   107  	return NewTransactionReceiptQuery().
   108  		SetTransactionID(response.TransactionID).
   109  		SetNodeAccountIDs([]AccountID{response.NodeID})
   110  }
   111  
   112  // GetRecordQuery retrieves the record query for the transaction
   113  func (response TransactionResponse) GetRecordQuery() *TransactionRecordQuery {
   114  	return NewTransactionRecordQuery().
   115  		SetTransactionID(response.TransactionID).
   116  		SetNodeAccountIDs([]AccountID{response.NodeID})
   117  }
   118  
   119  // SetValidateStatus sets the validate status for the transaction
   120  func (response TransactionResponse) SetValidateStatus(validate bool) *TransactionResponse {
   121  	response.ValidateStatus = validate
   122  	return &response
   123  }
   124  
   125  // GetValidateStatus returns the validate status for the transaction
   126  func (response TransactionResponse) GetValidateStatus() bool {
   127  	return response.ValidateStatus
   128  }
   129  
   130  // SetIncludeChildren Sets whether the response should include the receipts of any child transactions spawned by the
   131  // top-level transaction with the given transactionID.
   132  func (response TransactionResponse) SetIncludeChildren(include bool) *TransactionResponse {
   133  	response.IncludeChildReceipts = include
   134  	return &response
   135  }
   136  
   137  // GetIncludeChildren returns whether the response should include the receipts of any child transactions spawned by the
   138  // top-level transaction with the given transactionID.
   139  func (response TransactionResponse) GetIncludeChildren() bool {
   140  	return response.IncludeChildReceipts
   141  }