github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_balance_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 // AccountBalanceQuery gets the balance of a CryptoCurrency account. This returns only the balance, so it is a smaller 30 // and faster reply than AccountInfoQuery, which returns the balance plus additional information. 31 type AccountBalanceQuery struct { 32 Query 33 accountID *AccountID 34 contractID *ContractID 35 } 36 37 // NewAccountBalanceQuery creates an AccountBalanceQuery query which can be used to construct and execute 38 // an AccountBalanceQuery. 39 // It is recommended that you use this for creating new instances of an AccountBalanceQuery 40 // instead of manually creating an instance of the struct. 41 func NewAccountBalanceQuery() *AccountBalanceQuery { 42 header := services.QueryHeader{} 43 return &AccountBalanceQuery{ 44 Query: _NewQuery(false, &header), 45 } 46 } 47 48 // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.) 49 func (q *AccountBalanceQuery) SetGrpcDeadline(deadline *time.Duration) *AccountBalanceQuery { 50 q.Query.SetGrpcDeadline(deadline) 51 return q 52 } 53 54 // SetAccountID sets the AccountID for which you wish to query the balance. 55 // 56 // Note: you can only query an Account or Contract but not both -- if a Contract ID or Account ID has already been set, 57 // it will be overwritten by this _Method. 58 func (q *AccountBalanceQuery) SetAccountID(accountID AccountID) *AccountBalanceQuery { 59 q.accountID = &accountID 60 return q 61 } 62 63 // GetAccountID returns the AccountID for which you wish to query the balance. 64 func (q *AccountBalanceQuery) GetAccountID() AccountID { 65 if q.accountID == nil { 66 return AccountID{} 67 } 68 69 return *q.accountID 70 } 71 72 // SetContractID sets the ContractID for which you wish to query the balance. 73 // 74 // Note: you can only query an Account or Contract but not both -- if a Contract ID or Account ID has already been set, 75 // it will be overwritten by this _Method. 76 func (q *AccountBalanceQuery) SetContractID(contractID ContractID) *AccountBalanceQuery { 77 q.contractID = &contractID 78 return q 79 } 80 81 // GetContractID returns the ContractID for which you wish to query the balance. 82 func (q *AccountBalanceQuery) GetContractID() ContractID { 83 if q.contractID == nil { 84 return ContractID{} 85 } 86 87 return *q.contractID 88 } 89 90 func (q *AccountBalanceQuery) GetCost(client *Client) (Hbar, error) { 91 return q.Query.getCost(client, q) 92 } 93 94 // Execute executes the query with the provided client 95 func (q *AccountBalanceQuery) Execute(client *Client) (AccountBalance, error) { 96 if client == nil { 97 return AccountBalance{}, errNoClientProvided 98 } 99 100 err := q.validateNetworkOnIDs(client) 101 if err != nil { 102 return AccountBalance{}, err 103 } 104 105 resp, err := q.Query.execute(client, q) 106 if err != nil { 107 return AccountBalance{}, err 108 } 109 return _AccountBalanceFromProtobuf(resp.GetCryptogetAccountBalance()), nil 110 } 111 112 // SetMaxQueryPayment sets the maximum payment allowed for this query. 113 func (q *AccountBalanceQuery) SetMaxQueryPayment(maxPayment Hbar) *AccountBalanceQuery { 114 q.Query.SetMaxQueryPayment(maxPayment) 115 return q 116 } 117 118 // SetQueryPayment sets the payment amount for this query. 119 func (q *AccountBalanceQuery) SetQueryPayment(paymentAmount Hbar) *AccountBalanceQuery { 120 q.Query.SetQueryPayment(paymentAmount) 121 return q 122 } 123 124 // SetNodeAccountIDs sets the _Node AccountID for this AccountBalanceQuery. 125 func (q *AccountBalanceQuery) SetNodeAccountIDs(accountID []AccountID) *AccountBalanceQuery { 126 q.Query.SetNodeAccountIDs(accountID) 127 return q 128 } 129 130 // SetMaxRetry sets the max number of errors before execution will fail. 131 func (q *AccountBalanceQuery) SetMaxRetry(count int) *AccountBalanceQuery { 132 q.Query.SetMaxRetry(count) 133 return q 134 } 135 136 // SetMaxBackoff The maximum amount of time to wait between retries. Every retry attempt will increase the wait time exponentially until it reaches this time. 137 func (q *AccountBalanceQuery) SetMaxBackoff(max time.Duration) *AccountBalanceQuery { 138 q.Query.SetMaxBackoff(max) 139 return q 140 } 141 142 // SetMinBackoff sets the minimum amount of time to wait between retries. 143 func (q *AccountBalanceQuery) SetMinBackoff(min time.Duration) *AccountBalanceQuery { 144 q.Query.SetMinBackoff(min) 145 return q 146 } 147 148 // SetPaymentTransactionID assigns the payment transaction id. 149 func (q *AccountBalanceQuery) SetPaymentTransactionID(transactionID TransactionID) *AccountBalanceQuery { 150 q.Query.SetPaymentTransactionID(transactionID) 151 return q 152 } 153 154 func (q *AccountBalanceQuery) SetLogLevel(level LogLevel) *AccountBalanceQuery { 155 q.Query.SetLogLevel(level) 156 return q 157 } 158 159 // ---------- Parent functions specific implementation ---------- 160 161 func (q *AccountBalanceQuery) getMethod(channel *_Channel) _Method { 162 return _Method{ 163 query: channel._GetCrypto().CryptoGetBalance, 164 } 165 } 166 167 func (q *AccountBalanceQuery) getName() string { 168 return "AccountBalanceQuery" 169 } 170 171 func (q *AccountBalanceQuery) buildProtoBody() *services.CryptoGetAccountBalanceQuery { 172 pb := services.CryptoGetAccountBalanceQuery{Header: &services.QueryHeader{}} 173 174 if q.accountID != nil { 175 pb.BalanceSource = &services.CryptoGetAccountBalanceQuery_AccountID{ 176 AccountID: q.accountID._ToProtobuf(), 177 } 178 } 179 180 if q.contractID != nil { 181 pb.BalanceSource = &services.CryptoGetAccountBalanceQuery_ContractID{ 182 ContractID: q.contractID._ToProtobuf(), 183 } 184 } 185 return &pb 186 } 187 func (q *AccountBalanceQuery) buildQuery() *services.Query { 188 pb := q.buildProtoBody() 189 190 return &services.Query{ 191 Query: &services.Query_CryptogetAccountBalance{ 192 CryptogetAccountBalance: pb, 193 }, 194 } 195 } 196 197 func (q *AccountBalanceQuery) validateNetworkOnIDs(client *Client) error { 198 if client == nil || !client.autoValidateChecksums { 199 return nil 200 } 201 202 if q.accountID != nil { 203 if err := q.accountID.ValidateChecksum(client); err != nil { 204 return err 205 } 206 } 207 208 if q.contractID != nil { 209 if err := q.contractID.ValidateChecksum(client); err != nil { 210 return err 211 } 212 } 213 214 return nil 215 } 216 217 func (q *AccountBalanceQuery) getQueryResponse(response *services.Response) queryResponse { 218 return response.GetCryptogetAccountBalance() 219 }