github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_stakers_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  // AccountStakersQuery gets all of the accounts that are proxy staking to this account. For each of  them, the amount
    30  // currently staked will be given. This is not yet implemented, but will be in a future version of the API.
    31  type AccountStakersQuery struct {
    32  	Query
    33  	accountID *AccountID
    34  }
    35  
    36  // NewAccountStakersQuery creates an AccountStakersQuery query which can be used to construct and execute
    37  // an AccountStakersQuery.
    38  //
    39  // It is recommended that you use this for creating new instances of an AccountStakersQuery
    40  // instead of manually creating an instance of the struct.
    41  func NewAccountStakersQuery() *AccountStakersQuery {
    42  	header := services.QueryHeader{}
    43  	return &AccountStakersQuery{
    44  		Query: _NewQuery(true, &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 *AccountStakersQuery) SetGrpcDeadline(deadline *time.Duration) *AccountStakersQuery {
    50  	q.Query.SetGrpcDeadline(deadline)
    51  	return q
    52  }
    53  
    54  // SetAccountID sets the Account ID for which the stakers should be retrieved
    55  func (q *AccountStakersQuery) SetAccountID(accountID AccountID) *AccountStakersQuery {
    56  	q.accountID = &accountID
    57  	return q
    58  }
    59  
    60  // GetAccountID returns the AccountID for this AccountStakersQuery.
    61  func (q *AccountStakersQuery) GetAccountID() AccountID {
    62  	if q.accountID == nil {
    63  		return AccountID{}
    64  	}
    65  
    66  	return *q.accountID
    67  }
    68  
    69  func (q *AccountStakersQuery) 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 *AccountStakersQuery) Execute(client *Client) ([]Transfer, error) {
    75  	resp, err := q.Query.execute(client, q)
    76  
    77  	if err != nil {
    78  		return []Transfer{}, err
    79  	}
    80  
    81  	var stakers = make([]Transfer, len(resp.GetCryptoGetProxyStakers().Stakers.ProxyStaker))
    82  
    83  	// TODO: This is wrong, q _Method shold return `[]ProxyStaker` not `[]Transfer`
    84  	for i, element := range resp.GetCryptoGetProxyStakers().Stakers.ProxyStaker {
    85  		id := _AccountIDFromProtobuf(element.AccountID)
    86  		accountID := AccountID{}
    87  
    88  		if id == nil {
    89  			accountID = *id
    90  		}
    91  
    92  		stakers[i] = Transfer{
    93  			AccountID: accountID,
    94  			Amount:    HbarFromTinybar(element.Amount),
    95  		}
    96  	}
    97  
    98  	return stakers, err
    99  }
   100  
   101  // SetMaxQueryPayment sets the maximum payment allowed for this Query.
   102  func (q *AccountStakersQuery) SetMaxQueryPayment(maxPayment Hbar) *AccountStakersQuery {
   103  	q.Query.SetMaxQueryPayment(maxPayment)
   104  	return q
   105  }
   106  
   107  // SetQueryPayment sets the payment amount for this Query.
   108  func (q *AccountStakersQuery) SetQueryPayment(paymentAmount Hbar) *AccountStakersQuery {
   109  	q.Query.SetQueryPayment(paymentAmount)
   110  	return q
   111  }
   112  
   113  // SetNodeAccountIDs sets the _Node AccountID for this AccountStakersQuery.
   114  func (q *AccountStakersQuery) SetNodeAccountIDs(accountID []AccountID) *AccountStakersQuery {
   115  	q.Query.SetNodeAccountIDs(accountID)
   116  	return q
   117  }
   118  
   119  // SetMaxRetry sets the max number of errors before execution will fail.
   120  func (q *AccountStakersQuery) SetMaxRetry(count int) *AccountStakersQuery {
   121  	q.Query.SetMaxRetry(count)
   122  	return q
   123  }
   124  
   125  // SetMaxBackoff The maximum amount of time to wait between retries.
   126  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   127  func (q *AccountStakersQuery) SetMaxBackoff(max time.Duration) *AccountStakersQuery {
   128  	q.Query.SetMaxBackoff(max)
   129  	return q
   130  }
   131  
   132  // SetMinBackoff sets the minimum amount of time to wait between retries.
   133  func (q *AccountStakersQuery) SetMinBackoff(min time.Duration) *AccountStakersQuery {
   134  	q.Query.SetMinBackoff(min)
   135  	return q
   136  }
   137  
   138  // SetPaymentTransactionID assigns the payment transaction id.
   139  func (q *AccountStakersQuery) SetPaymentTransactionID(transactionID TransactionID) *AccountStakersQuery {
   140  	q.Query.SetPaymentTransactionID(transactionID)
   141  	return q
   142  }
   143  
   144  func (q *AccountStakersQuery) SetLogLevel(level LogLevel) *AccountStakersQuery {
   145  	q.Query.SetLogLevel(level)
   146  	return q
   147  }
   148  
   149  // ---------- Parent functions specific implementation ----------
   150  
   151  func (q *AccountStakersQuery) getMethod(channel *_Channel) _Method {
   152  	return _Method{
   153  		query: channel._GetCrypto().GetStakersByAccountID,
   154  	}
   155  }
   156  
   157  func (q *AccountStakersQuery) getName() string {
   158  	return "AccountStakersQuery"
   159  }
   160  
   161  func (q *AccountStakersQuery) buildQuery() *services.Query {
   162  	pb := services.Query_CryptoGetProxyStakers{
   163  		CryptoGetProxyStakers: &services.CryptoGetStakersQuery{
   164  			Header: q.pbHeader,
   165  		},
   166  	}
   167  
   168  	if q.accountID != nil {
   169  		pb.CryptoGetProxyStakers.AccountID = q.accountID._ToProtobuf()
   170  	}
   171  
   172  	return &services.Query{
   173  		Query: &pb,
   174  	}
   175  }
   176  
   177  func (q *AccountStakersQuery) validateNetworkOnIDs(client *Client) error {
   178  	if client == nil || !client.autoValidateChecksums {
   179  		return nil
   180  	}
   181  
   182  	if q.accountID != nil {
   183  		if err := q.accountID.ValidateChecksum(client); err != nil {
   184  			return err
   185  		}
   186  	}
   187  
   188  	return nil
   189  }
   190  
   191  func (q *AccountStakersQuery) getQueryResponse(response *services.Response) queryResponse {
   192  	return response.GetCryptoGetProxyStakers()
   193  }