github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/prng_transaction.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 // PrngTransaction is used to generate a random number in a given range 30 type PrngTransaction struct { 31 Transaction 32 rang uint32 33 } 34 35 // NewPrngTransaction creates a PrngTransaction transaction which can be used to construct and execute 36 // a Prng Transaction. 37 func NewPrngTransaction() *PrngTransaction { 38 tx := PrngTransaction{ 39 Transaction: _NewTransaction(), 40 } 41 42 tx._SetDefaultMaxTransactionFee(NewHbar(5)) 43 44 return &tx 45 } 46 47 func _PrngTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *PrngTransaction { 48 return &PrngTransaction{ 49 Transaction: tx, 50 rang: uint32(pb.GetUtilPrng().GetRange()), 51 } 52 } 53 54 // SetPayerAccountID Sets an optional id of the account to be charged the service fee for the scheduled transaction at 55 // the consensus time that it executes (if ever); defaults to the ScheduleCreate payer if not 56 // given 57 func (tx *PrngTransaction) SetRange(r uint32) *PrngTransaction { 58 tx._RequireNotFrozen() 59 tx.rang = r 60 61 return tx 62 } 63 64 // GetRange returns the range of the prng 65 func (tx *PrngTransaction) GetRange() uint32 { 66 return tx.rang 67 } 68 69 // ---- Required Interfaces ---- // 70 71 // Sign uses the provided privateKey to sign the transaction. 72 func (tx *PrngTransaction) Sign(privateKey PrivateKey) *PrngTransaction { 73 tx.Transaction.Sign(privateKey) 74 return tx 75 } 76 77 // SignWithOperator signs the transaction with client's operator privateKey. 78 func (tx *PrngTransaction) SignWithOperator(client *Client) (*PrngTransaction, error) { 79 _, err := tx.Transaction.signWithOperator(client, tx) 80 if err != nil { 81 return nil, err 82 } 83 return tx, nil 84 } 85 86 // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map 87 // with the publicKey as the map key. 88 func (tx *PrngTransaction) SignWith( 89 publicKey PublicKey, 90 signer TransactionSigner, 91 ) *PrngTransaction { 92 tx.Transaction.SignWith(publicKey, signer) 93 return tx 94 } 95 96 // AddSignature adds a signature to the transaction. 97 func (tx *PrngTransaction) AddSignature(publicKey PublicKey, signature []byte) *PrngTransaction { 98 tx.Transaction.AddSignature(publicKey, signature) 99 return tx 100 } 101 102 // SetGrpcDeadline When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.) 103 func (tx *PrngTransaction) SetGrpcDeadline(deadline *time.Duration) *PrngTransaction { 104 tx.Transaction.SetGrpcDeadline(deadline) 105 return tx 106 } 107 108 func (tx *PrngTransaction) Freeze() (*PrngTransaction, error) { 109 return tx.FreezeWith(nil) 110 } 111 112 func (tx *PrngTransaction) FreezeWith(client *Client) (*PrngTransaction, error) { 113 _, err := tx.Transaction.freezeWith(client, tx) 114 return tx, err 115 } 116 117 // SetMaxTransactionFee sets the maximum transaction fee for this PrngTransaction. 118 func (tx *PrngTransaction) SetMaxTransactionFee(fee Hbar) *PrngTransaction { 119 tx.Transaction.SetMaxTransactionFee(fee) 120 return tx 121 } 122 123 // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received 124 func (tx *PrngTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *PrngTransaction { 125 tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID) 126 return tx 127 } 128 129 // SetTransactionMemo sets the memo for this PrngTransaction. 130 func (tx *PrngTransaction) SetTransactionMemo(memo string) *PrngTransaction { 131 tx.Transaction.SetTransactionMemo(memo) 132 return tx 133 } 134 135 // SetTransactionValidDuration sets the valid duration for this PrngTransaction. 136 func (tx *PrngTransaction) SetTransactionValidDuration(duration time.Duration) *PrngTransaction { 137 tx.Transaction.SetTransactionValidDuration(duration) 138 return tx 139 } 140 141 // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not 142 func (tx *PrngTransaction) ToBytes() ([]byte, error) { 143 bytes, err := tx.Transaction.toBytes(tx) 144 if err != nil { 145 return nil, err 146 } 147 return bytes, nil 148 } 149 150 // SetTransactionID sets the TransactionID for this PrngTransaction. 151 func (tx *PrngTransaction) SetTransactionID(transactionID TransactionID) *PrngTransaction { 152 tx.Transaction.SetTransactionID(transactionID) 153 return tx 154 } 155 156 // SetNodeAccountIDs sets the _Node AccountID for this PrngTransaction. 157 func (tx *PrngTransaction) SetNodeAccountIDs(nodeID []AccountID) *PrngTransaction { 158 tx.Transaction.SetNodeAccountIDs(nodeID) 159 return tx 160 } 161 162 // SetMaxRetry sets the max number of errors before execution will fail. 163 func (tx *PrngTransaction) SetMaxRetry(count int) *PrngTransaction { 164 tx.Transaction.SetMaxRetry(count) 165 return tx 166 } 167 168 // SetMaxBackoff The maximum amount of time to wait between retries. 169 // Every retry attempt will increase the wait time exponentially until it reaches this time. 170 func (tx *PrngTransaction) SetMaxBackoff(max time.Duration) *PrngTransaction { 171 tx.Transaction.SetMaxBackoff(max) 172 return tx 173 } 174 175 // SetMinBackoff sets the minimum amount of time to wait between retries. 176 func (tx *PrngTransaction) SetMinBackoff(min time.Duration) *PrngTransaction { 177 tx.Transaction.SetMinBackoff(min) 178 return tx 179 } 180 181 func (tx *PrngTransaction) SetLogLevel(level LogLevel) *PrngTransaction { 182 tx.Transaction.SetLogLevel(level) 183 return tx 184 } 185 186 func (tx *PrngTransaction) Execute(client *Client) (TransactionResponse, error) { 187 return tx.Transaction.execute(client, tx) 188 } 189 190 func (tx *PrngTransaction) Schedule() (*ScheduleCreateTransaction, error) { 191 return tx.Transaction.schedule(tx) 192 } 193 194 // ----------- Overridden functions ---------------- 195 196 func (tx *PrngTransaction) getName() string { 197 return "PrngTransaction" 198 } 199 200 func (tx *PrngTransaction) build() *services.TransactionBody { 201 return &services.TransactionBody{ 202 TransactionFee: tx.transactionFee, 203 Memo: tx.Transaction.memo, 204 TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()), 205 TransactionID: tx.transactionID._ToProtobuf(), 206 Data: &services.TransactionBody_UtilPrng{ 207 UtilPrng: tx.buildProtoBody(), 208 }, 209 } 210 } 211 212 func (tx *PrngTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) { 213 return &services.SchedulableTransactionBody{ 214 TransactionFee: tx.transactionFee, 215 Memo: tx.Transaction.memo, 216 Data: &services.SchedulableTransactionBody_UtilPrng{ 217 UtilPrng: tx.buildProtoBody(), 218 }, 219 }, nil 220 } 221 222 func (tx *PrngTransaction) buildProtoBody() *services.UtilPrngTransactionBody { 223 body := &services.UtilPrngTransactionBody{ 224 Range: int32(tx.rang), 225 } 226 227 return body 228 } 229 230 func (tx *PrngTransaction) getMethod(channel *_Channel) _Method { 231 return _Method{ 232 transaction: channel._GetUtil().Prng, 233 } 234 } 235 func (tx *PrngTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) { 236 return tx.buildScheduled() 237 } 238 239 func (tx *PrngTransaction) validateNetworkOnIDs(client *Client) error { 240 return nil 241 }