github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_info_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  // AccountInfoQuery
    30  // Get all the information about an account, including the balance. This does not get the list of
    31  // account records.
    32  type AccountInfoQuery struct {
    33  	Query
    34  	accountID *AccountID
    35  }
    36  
    37  // NewAccountInfoQuery
    38  // Creates an AccountInfoQuery which retrieves all the information about an account, including the balance. This does not get the list of
    39  // account records.
    40  func NewAccountInfoQuery() *AccountInfoQuery {
    41  	header := services.QueryHeader{}
    42  	return &AccountInfoQuery{
    43  		Query: _NewQuery(true, &header),
    44  	}
    45  }
    46  
    47  func (q *AccountInfoQuery) GetCost(client *Client) (Hbar, error) {
    48  	return q.Query.getCost(client, q)
    49  }
    50  
    51  // Execute executes the Query with the provided client
    52  func (q *AccountInfoQuery) Execute(client *Client) (AccountInfo, error) {
    53  	resp, err := q.execute(client, q)
    54  
    55  	if err != nil {
    56  		return AccountInfo{}, err
    57  	}
    58  
    59  	return _AccountInfoFromProtobuf(resp.GetCryptoGetInfo().AccountInfo)
    60  }
    61  
    62  // SetGrpcDeadline When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
    63  func (q *AccountInfoQuery) SetGrpcDeadline(deadline *time.Duration) *AccountInfoQuery {
    64  	q.Query.SetGrpcDeadline(deadline)
    65  	return q
    66  }
    67  
    68  // SetAccountID sets the AccountID for this AccountInfoQuery.
    69  func (q *AccountInfoQuery) SetAccountID(accountID AccountID) *AccountInfoQuery {
    70  	q.accountID = &accountID
    71  	return q
    72  }
    73  
    74  // GetAccountID returns the AccountID for this AccountInfoQuery.
    75  func (q *AccountInfoQuery) GetAccountID() AccountID {
    76  	if q.accountID == nil {
    77  		return AccountID{}
    78  	}
    79  
    80  	return *q.accountID
    81  }
    82  
    83  // SetNodeAccountIDs sets the _Node AccountID for this AccountInfoQuery.
    84  func (q *AccountInfoQuery) SetNodeAccountIDs(accountID []AccountID) *AccountInfoQuery {
    85  	q.Query.SetNodeAccountIDs(accountID)
    86  	return q
    87  }
    88  
    89  // SetQueryPayment sets the Hbar payment to pay the _Node a fee for handling this query
    90  func (q *AccountInfoQuery) SetQueryPayment(queryPayment Hbar) *AccountInfoQuery {
    91  	q.queryPayment = queryPayment
    92  	return q
    93  }
    94  
    95  // SetMaxQueryPayment sets the maximum payment allowable for this query.
    96  func (q *AccountInfoQuery) SetMaxQueryPayment(queryMaxPayment Hbar) *AccountInfoQuery {
    97  	q.maxQueryPayment = queryMaxPayment
    98  	return q
    99  }
   100  
   101  // SetMaxRetry sets the max number of errors before execution will fail.
   102  func (q *AccountInfoQuery) SetMaxRetry(count int) *AccountInfoQuery {
   103  	q.Query.SetMaxRetry(count)
   104  	return q
   105  }
   106  
   107  // SetMaxBackoff The maximum amount of time to wait between retries. Every retry attempt will increase the wait time exponentially until it reaches this time.
   108  func (q *AccountInfoQuery) SetMaxBackoff(max time.Duration) *AccountInfoQuery {
   109  	q.Query.SetMaxBackoff(max)
   110  	return q
   111  }
   112  
   113  // SetMinBackoff sets the minimum amount of time to wait between retries.
   114  func (q *AccountInfoQuery) SetMinBackoff(min time.Duration) *AccountInfoQuery {
   115  	q.Query.SetMinBackoff(min)
   116  	return q
   117  }
   118  
   119  // SetPaymentTransactionID assigns the payment transaction id.
   120  func (q *AccountInfoQuery) SetPaymentTransactionID(transactionID TransactionID) *AccountInfoQuery {
   121  	q.Query.SetPaymentTransactionID(transactionID)
   122  	return q
   123  }
   124  
   125  func (q *AccountInfoQuery) SetLogLevel(level LogLevel) *AccountInfoQuery {
   126  	q.Query.SetLogLevel(level)
   127  	return q
   128  }
   129  
   130  // ---------- Parent functions specific implementation ----------
   131  
   132  func (q *AccountInfoQuery) getMethod(channel *_Channel) _Method {
   133  	return _Method{
   134  		query: channel._GetCrypto().GetAccountInfo,
   135  	}
   136  }
   137  
   138  func (q *AccountInfoQuery) getName() string {
   139  	return "AccountInfoQuery"
   140  }
   141  
   142  func (q *AccountInfoQuery) buildQuery() *services.Query {
   143  	pbQuery := services.Query_CryptoGetInfo{
   144  		CryptoGetInfo: &services.CryptoGetInfoQuery{
   145  			Header: q.pbHeader,
   146  		},
   147  	}
   148  
   149  	if q.accountID != nil {
   150  		pbQuery.CryptoGetInfo.AccountID = q.accountID._ToProtobuf()
   151  	}
   152  
   153  	return &services.Query{
   154  		Query: &pbQuery,
   155  	}
   156  }
   157  
   158  func (q *AccountInfoQuery) validateNetworkOnIDs(client *Client) error {
   159  	if client == nil || !client.autoValidateChecksums {
   160  		return nil
   161  	}
   162  
   163  	if q.accountID != nil {
   164  		if err := q.accountID.ValidateChecksum(client); err != nil {
   165  			return err
   166  		}
   167  	}
   168  
   169  	return nil
   170  }
   171  
   172  func (q *AccountInfoQuery) getQueryResponse(response *services.Response) queryResponse {
   173  	return response.GetCryptoGetInfo()
   174  }