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