github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_records_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  // AccountRecordsQuery gets all of the records for an account for any transfers into it and out of
    30  // it, that were above the threshold, during the last 25 hours.
    31  type AccountRecordsQuery struct {
    32  	Query
    33  	accountID *AccountID
    34  }
    35  
    36  // NewAccountRecordsQuery creates an AccountRecordsQuery Query which can be used to construct and execute
    37  // an AccountRecordsQuery.
    38  //
    39  // It is recommended that you use this for creating new instances of an AccountRecordQuery
    40  // instead of manually creating an instance of the struct.
    41  func NewAccountRecordsQuery() *AccountRecordsQuery {
    42  	header := services.QueryHeader{}
    43  	return &AccountRecordsQuery{
    44  		Query: _NewQuery(true, &header),
    45  	}
    46  }
    47  
    48  // SetGrpcDeadline When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
    49  func (q *AccountRecordsQuery) SetGrpcDeadline(deadline *time.Duration) *AccountRecordsQuery {
    50  	q.Query.SetGrpcDeadline(deadline)
    51  	return q
    52  }
    53  
    54  // SetAccountID sets the account ID for which the records should be retrieved.
    55  func (q *AccountRecordsQuery) SetAccountID(accountID AccountID) *AccountRecordsQuery {
    56  	q.accountID = &accountID
    57  	return q
    58  }
    59  
    60  // GetAccountID returns the account ID for which the records will be retrieved.
    61  func (q *AccountRecordsQuery) GetAccountID() AccountID {
    62  	if q.accountID == nil {
    63  		return AccountID{}
    64  	}
    65  
    66  	return *q.accountID
    67  }
    68  
    69  func (q *AccountRecordsQuery) GetCost(client *Client) (Hbar, error) {
    70  	return q.Query.getCost(client, q)
    71  }
    72  
    73  // Execute executes the Query with the provided client
    74  func (q *AccountRecordsQuery) Execute(client *Client) ([]TransactionRecord, error) {
    75  	resp, err := q.Query.execute(client, q)
    76  	records := make([]TransactionRecord, 0)
    77  
    78  	if err != nil {
    79  		return records, err
    80  	}
    81  
    82  	for _, element := range resp.GetCryptoGetAccountRecords().Records {
    83  		record := _TransactionRecordFromProtobuf(&services.TransactionGetRecordResponse{TransactionRecord: element}, nil)
    84  		records = append(records, record)
    85  	}
    86  
    87  	return records, err
    88  }
    89  
    90  // SetMaxQueryPayment sets the maximum payment allowed for this Query.
    91  func (q *AccountRecordsQuery) SetMaxQueryPayment(maxPayment Hbar) *AccountRecordsQuery {
    92  	q.Query.SetMaxQueryPayment(maxPayment)
    93  	return q
    94  }
    95  
    96  // SetQueryPayment sets the payment amount for this Query.
    97  func (q *AccountRecordsQuery) SetQueryPayment(paymentAmount Hbar) *AccountRecordsQuery {
    98  	q.Query.SetQueryPayment(paymentAmount)
    99  	return q
   100  }
   101  
   102  // SetNodeAccountIDs sets the _Node AccountID for this AccountRecordsQuery.
   103  func (q *AccountRecordsQuery) SetNodeAccountIDs(accountID []AccountID) *AccountRecordsQuery {
   104  	q.Query.SetNodeAccountIDs(accountID)
   105  	return q
   106  }
   107  
   108  // SetMaxRetry sets the max number of errors before execution will fail.
   109  func (q *AccountRecordsQuery) SetMaxRetry(count int) *AccountRecordsQuery {
   110  	q.Query.SetMaxRetry(count)
   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 *AccountRecordsQuery) SetMaxBackoff(max time.Duration) *AccountRecordsQuery {
   117  	q.Query.SetMaxBackoff(max)
   118  	return q
   119  }
   120  
   121  func (q *AccountRecordsQuery) SetMinBackoff(min time.Duration) *AccountRecordsQuery {
   122  	q.Query.SetMinBackoff(min)
   123  	return q
   124  }
   125  
   126  // SetPaymentTransactionID assigns the payment transaction id.
   127  func (q *AccountRecordsQuery) SetPaymentTransactionID(transactionID TransactionID) *AccountRecordsQuery {
   128  	q.Query.SetPaymentTransactionID(transactionID)
   129  	return q
   130  }
   131  
   132  func (q *AccountRecordsQuery) SetLogLevel(level LogLevel) *AccountRecordsQuery {
   133  	q.Query.SetLogLevel(level)
   134  	return q
   135  }
   136  
   137  // ---------- Parent functions specific implementation ----------
   138  
   139  func (q *AccountRecordsQuery) getMethod(channel *_Channel) _Method {
   140  	return _Method{
   141  		query: channel._GetCrypto().GetAccountRecords,
   142  	}
   143  }
   144  
   145  func (q *AccountRecordsQuery) getName() string {
   146  	return "AccountRecordsQuery"
   147  }
   148  
   149  func (q *AccountRecordsQuery) buildQuery() *services.Query {
   150  	pb := services.Query_CryptoGetAccountRecords{
   151  		CryptoGetAccountRecords: &services.CryptoGetAccountRecordsQuery{
   152  			Header: q.pbHeader,
   153  		},
   154  	}
   155  
   156  	if q.accountID != nil {
   157  		pb.CryptoGetAccountRecords.AccountID = q.accountID._ToProtobuf()
   158  	}
   159  
   160  	return &services.Query{
   161  		Query: &pb,
   162  	}
   163  }
   164  
   165  func (q *AccountRecordsQuery) validateNetworkOnIDs(client *Client) error {
   166  	if client == nil || !client.autoValidateChecksums {
   167  		return nil
   168  	}
   169  
   170  	if q.accountID != nil {
   171  		if err := q.accountID.ValidateChecksum(client); err != nil {
   172  			return err
   173  		}
   174  	}
   175  
   176  	return nil
   177  }
   178  
   179  func (q *AccountRecordsQuery) getQueryResponse(response *services.Response) queryResponse {
   180  	return response.GetCryptoGetAccountRecords()
   181  }