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

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  )
    28  
    29  // LiveHashQuery Requests a livehash associated to an account.
    30  type LiveHashQuery struct {
    31  	Query
    32  	accountID *AccountID
    33  	hash      []byte
    34  }
    35  
    36  // NewLiveHashQuery creates a LiveHashQuery that requests a livehash associated to an account.
    37  func NewLiveHashQuery() *LiveHashQuery {
    38  	header := services.QueryHeader{}
    39  	return &LiveHashQuery{
    40  		Query: _NewQuery(true, &header),
    41  	}
    42  }
    43  
    44  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
    45  func (q *LiveHashQuery) SetGrpcDeadline(deadline *time.Duration) *LiveHashQuery {
    46  	q.Query.SetGrpcDeadline(deadline)
    47  	return q
    48  }
    49  
    50  // SetAccountID Sets the AccountID to which the livehash is associated
    51  func (q *LiveHashQuery) SetAccountID(accountID AccountID) *LiveHashQuery {
    52  	q.accountID = &accountID
    53  	return q
    54  }
    55  
    56  // GetAccountID returns the AccountID to which the livehash is associated
    57  func (q *LiveHashQuery) GetAccountID() AccountID {
    58  	if q.accountID == nil {
    59  		return AccountID{}
    60  	}
    61  
    62  	return *q.accountID
    63  }
    64  
    65  // SetHash Sets the SHA-384 data in the livehash
    66  func (q *LiveHashQuery) SetHash(hash []byte) *LiveHashQuery {
    67  	q.hash = hash
    68  	return q
    69  }
    70  
    71  // GetHash returns the SHA-384 data in the livehash
    72  func (q *LiveHashQuery) GetGetHash() []byte {
    73  	return q.hash
    74  }
    75  
    76  func (q *LiveHashQuery) GetCost(client *Client) (Hbar, error) {
    77  	return q.Query.getCost(client, q)
    78  }
    79  
    80  // Execute executes the Query with the provided client
    81  func (q *LiveHashQuery) Execute(client *Client) (LiveHash, error) {
    82  	resp, err := q.Query.execute(client, q)
    83  
    84  	if err != nil {
    85  		return LiveHash{}, err
    86  	}
    87  
    88  	liveHash, err := _LiveHashFromProtobuf(resp.GetCryptoGetLiveHash().LiveHash)
    89  	if err != nil {
    90  		return LiveHash{}, err
    91  	}
    92  
    93  	return liveHash, nil
    94  }
    95  
    96  // SetMaxQueryPayment sets the maximum payment allowed for this Query.
    97  func (q *LiveHashQuery) SetMaxQueryPayment(maxPayment Hbar) *LiveHashQuery {
    98  	q.Query.SetMaxQueryPayment(maxPayment)
    99  	return q
   100  }
   101  
   102  // SetQueryPayment sets the payment amount for this Query.
   103  func (q *LiveHashQuery) SetQueryPayment(paymentAmount Hbar) *LiveHashQuery {
   104  	q.Query.SetQueryPayment(paymentAmount)
   105  	return q
   106  }
   107  
   108  // SetNodeAccountIDs sets the _Node AccountID for this LiveHashQuery.
   109  func (q *LiveHashQuery) SetNodeAccountIDs(accountID []AccountID) *LiveHashQuery {
   110  	q.Query.SetNodeAccountIDs(accountID)
   111  	return q
   112  }
   113  
   114  // SetMaxBackoff The maximum amount of time to wait between retries.
   115  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   116  func (q *LiveHashQuery) SetMaxBackoff(max time.Duration) *LiveHashQuery {
   117  	q.Query.SetMaxBackoff(max)
   118  	return q
   119  }
   120  
   121  // SetMinBackoff sets the minimum amount of time to wait between retries.
   122  func (q *LiveHashQuery) SetMinBackoff(min time.Duration) *LiveHashQuery {
   123  	q.Query.SetMinBackoff(min)
   124  	return q
   125  }
   126  
   127  // SetPaymentTransactionID assigns the payment transaction id.
   128  func (q *LiveHashQuery) SetPaymentTransactionID(transactionID TransactionID) *LiveHashQuery {
   129  	q.Query.SetPaymentTransactionID(transactionID)
   130  	return q
   131  }
   132  
   133  // SetMaxRetry sets the max number of errors before execution will fail.
   134  func (q *LiveHashQuery) SetMaxRetry(count int) *LiveHashQuery {
   135  	q.Query.SetMaxRetry(count)
   136  	return q
   137  }
   138  
   139  func (q *LiveHashQuery) SetLogLevel(level LogLevel) *LiveHashQuery {
   140  	q.Query.SetLogLevel(level)
   141  	return q
   142  }
   143  
   144  // ---------- Parent functions specific implementation ----------
   145  
   146  func (q *LiveHashQuery) getMethod(channel *_Channel) _Method {
   147  	return _Method{
   148  		query: channel._GetCrypto().GetLiveHash,
   149  	}
   150  }
   151  
   152  func (q *LiveHashQuery) getName() string {
   153  	return "LiveHashQuery"
   154  }
   155  
   156  func (q *LiveHashQuery) buildQuery() *services.Query {
   157  	body := &services.CryptoGetLiveHashQuery{
   158  		Header: q.pbHeader,
   159  	}
   160  	if q.accountID != nil {
   161  		body.AccountID = q.accountID._ToProtobuf()
   162  	}
   163  
   164  	if len(q.hash) > 0 {
   165  		body.Hash = q.hash
   166  	}
   167  
   168  	return &services.Query{
   169  		Query: &services.Query_CryptoGetLiveHash{
   170  			CryptoGetLiveHash: body,
   171  		},
   172  	}
   173  }
   174  
   175  func (q *LiveHashQuery) validateNetworkOnIDs(client *Client) error {
   176  	if client == nil || !client.autoValidateChecksums {
   177  		return nil
   178  	}
   179  
   180  	if q.accountID != nil {
   181  		if err := q.accountID.ValidateChecksum(client); err != nil {
   182  			return err
   183  		}
   184  	}
   185  
   186  	return nil
   187  }
   188  
   189  func (q *LiveHashQuery) getQueryResponse(response *services.Response) queryResponse {
   190  	return response.GetCryptoGetLiveHash()
   191  }