github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_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 // ContractInfoQuery retrieves information about a smart contract instance. This includes the account that it uses, the 30 // file containing its bytecode, and the time when it will expire. 31 type ContractInfoQuery struct { 32 Query 33 contractID *ContractID 34 } 35 36 // NewContractInfoQuery creates a ContractInfoQuery query which can be used to construct and execute a 37 // Contract Get Info Query. 38 func NewContractInfoQuery() *ContractInfoQuery { 39 header := services.QueryHeader{} 40 query := _NewQuery(true, &header) 41 42 return &ContractInfoQuery{ 43 Query: query, 44 } 45 } 46 47 // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.) 48 func (q *ContractInfoQuery) SetGrpcDeadline(deadline *time.Duration) *ContractInfoQuery { 49 q.Query.SetGrpcDeadline(deadline) 50 return q 51 } 52 53 // SetContractID sets the contract for which information is requested 54 func (q *ContractInfoQuery) SetContractID(contractID ContractID) *ContractInfoQuery { 55 q.contractID = &contractID 56 return q 57 } 58 59 func (q *ContractInfoQuery) GetContractID() ContractID { 60 if q.contractID == nil { 61 return ContractID{} 62 } 63 64 return *q.contractID 65 } 66 67 func (q *ContractInfoQuery) GetCost(client *Client) (Hbar, error) { 68 return q.Query.getCost(client, q) 69 } 70 71 // Execute executes the Query with the provided client 72 func (q *ContractInfoQuery) Execute(client *Client) (ContractInfo, error) { 73 resp, err := q.Query.execute(client, q) 74 75 if err != nil { 76 return ContractInfo{}, err 77 } 78 79 info, err := _ContractInfoFromProtobuf(resp.GetContractGetInfo().ContractInfo) 80 if err != nil { 81 return ContractInfo{}, err 82 } 83 84 return info, nil 85 } 86 87 // SetMaxQueryPayment sets the maximum payment allowed for this Query. 88 func (q *ContractInfoQuery) SetMaxQueryPayment(maxPayment Hbar) *ContractInfoQuery { 89 q.Query.SetMaxQueryPayment(maxPayment) 90 return q 91 } 92 93 // SetQueryPayment sets the payment amount for this Query. 94 func (q *ContractInfoQuery) SetQueryPayment(paymentAmount Hbar) *ContractInfoQuery { 95 q.Query.SetQueryPayment(paymentAmount) 96 return q 97 } 98 99 // SetNodeAccountIDs sets the _Node AccountID for this ContractInfoQuery. 100 func (q *ContractInfoQuery) SetNodeAccountIDs(accountID []AccountID) *ContractInfoQuery { 101 q.Query.SetNodeAccountIDs(accountID) 102 return q 103 } 104 105 // SetMaxRetry sets the max number of errors before execution will fail. 106 func (q *ContractInfoQuery) SetMaxRetry(count int) *ContractInfoQuery { 107 q.Query.SetMaxRetry(count) 108 return q 109 } 110 111 // SetMaxBackoff The maximum amount of time to wait between retries. 112 // Every retry attempt will increase the wait time exponentially until it reaches this time. 113 func (q *ContractInfoQuery) SetMaxBackoff(max time.Duration) *ContractInfoQuery { 114 q.Query.SetMaxBackoff(max) 115 return q 116 } 117 118 // SetMinBackoff sets the minimum amount of time to wait between retries. 119 func (q *ContractInfoQuery) SetMinBackoff(min time.Duration) *ContractInfoQuery { 120 q.Query.SetMinBackoff(min) 121 return q 122 } 123 124 func (q *ContractInfoQuery) SetPaymentTransactionID(transactionID TransactionID) *ContractInfoQuery { 125 q.Query.SetPaymentTransactionID(transactionID) 126 return q 127 } 128 129 func (q *ContractInfoQuery) SetLogLevel(level LogLevel) *ContractInfoQuery { 130 q.Query.SetLogLevel(level) 131 return q 132 } 133 134 // ---------- Parent functions specific implementation ---------- 135 136 func (q *ContractInfoQuery) getMethod(channel *_Channel) _Method { 137 return _Method{ 138 query: channel._GetContract().GetContractInfo, 139 } 140 } 141 142 func (q *ContractInfoQuery) getName() string { 143 return "ContractInfoQuery" 144 } 145 146 func (q *ContractInfoQuery) buildQuery() *services.Query { 147 pb := services.Query_ContractGetInfo{ 148 ContractGetInfo: &services.ContractGetInfoQuery{ 149 Header: q.pbHeader, 150 }, 151 } 152 153 if q.contractID != nil { 154 pb.ContractGetInfo.ContractID = q.contractID._ToProtobuf() 155 } 156 157 return &services.Query{ 158 Query: &pb, 159 } 160 } 161 162 func (q *ContractInfoQuery) validateNetworkOnIDs(client *Client) error { 163 if client == nil || !client.autoValidateChecksums { 164 return nil 165 } 166 167 if q.contractID != nil { 168 if err := q.contractID.ValidateChecksum(client); err != nil { 169 return err 170 } 171 } 172 173 return nil 174 } 175 176 func (q *ContractInfoQuery) getQueryResponse(response *services.Response) queryResponse { 177 return response.GetContractGetInfo() 178 }