github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/schedule_create_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 "errors" 25 "time" 26 27 "github.com/hashgraph/hedera-protobufs-go/services" 28 ) 29 30 // ScheduleCreateTransaction Creates a new schedule entity (or simply, schedule) in the network's action queue. 31 // Upon SUCCESS, the receipt contains the `ScheduleID` of the created schedule. A schedule 32 // entity includes a scheduledTransactionBody to be executed. 33 // When the schedule has collected enough signing Ed25519 keys to satisfy the schedule's signing 34 // requirements, the schedule can be executed. 35 type ScheduleCreateTransaction struct { 36 Transaction 37 payerAccountID *AccountID 38 adminKey Key 39 schedulableBody *services.SchedulableTransactionBody 40 memo string 41 expirationTime *time.Time 42 waitForExpiry bool 43 } 44 45 // NewScheduleCreateTransaction creates ScheduleCreateTransaction which creates a new schedule entity (or simply, schedule) in the network's action queue. 46 // Upon SUCCESS, the receipt contains the `ScheduleID` of the created schedule. A schedule 47 // entity includes a scheduledTransactionBody to be executed. 48 // When the schedule has collected enough signing Ed25519 keys to satisfy the schedule's signing 49 // requirements, the schedule can be executed. 50 func NewScheduleCreateTransaction() *ScheduleCreateTransaction { 51 tx := ScheduleCreateTransaction{ 52 Transaction: _NewTransaction(), 53 } 54 55 tx._SetDefaultMaxTransactionFee(NewHbar(5)) 56 57 return &tx 58 } 59 60 func _ScheduleCreateTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *ScheduleCreateTransaction { 61 key, _ := _KeyFromProtobuf(pb.GetScheduleCreate().GetAdminKey()) 62 var expirationTime time.Time 63 if pb.GetScheduleCreate().GetExpirationTime() != nil { 64 expirationTime = _TimeFromProtobuf(pb.GetScheduleCreate().GetExpirationTime()) 65 } 66 67 return &ScheduleCreateTransaction{ 68 Transaction: tx, 69 payerAccountID: _AccountIDFromProtobuf(pb.GetScheduleCreate().GetPayerAccountID()), 70 adminKey: key, 71 schedulableBody: pb.GetScheduleCreate().GetScheduledTransactionBody(), 72 memo: pb.GetScheduleCreate().GetMemo(), 73 expirationTime: &expirationTime, 74 waitForExpiry: pb.GetScheduleCreate().WaitForExpiry, 75 } 76 } 77 78 // SetPayerAccountID Sets an optional id of the account to be charged the service fee for the scheduled transaction at 79 // the consensus time that it executes (if ever); defaults to the ScheduleCreate payer if not 80 // given 81 func (tx *ScheduleCreateTransaction) SetPayerAccountID(payerAccountID AccountID) *ScheduleCreateTransaction { 82 tx._RequireNotFrozen() 83 tx.payerAccountID = &payerAccountID 84 85 return tx 86 } 87 88 // GetPayerAccountID returns the optional id of the account to be charged the service fee for the scheduled transaction 89 func (tx *ScheduleCreateTransaction) GetPayerAccountID() AccountID { 90 if tx.payerAccountID == nil { 91 return AccountID{} 92 } 93 94 return *tx.payerAccountID 95 } 96 97 // SetAdminKey Sets an optional Hedera key which can be used to sign a ScheduleDelete and remove the schedule 98 func (tx *ScheduleCreateTransaction) SetAdminKey(key Key) *ScheduleCreateTransaction { 99 tx._RequireNotFrozen() 100 tx.adminKey = key 101 102 return tx 103 } 104 105 // SetExpirationTime Sets an optional timestamp for specifying when the transaction should be evaluated for execution and then expire. 106 // Defaults to 30 minutes after the transaction's consensus timestamp. 107 func (tx *ScheduleCreateTransaction) SetExpirationTime(time time.Time) *ScheduleCreateTransaction { 108 tx._RequireNotFrozen() 109 tx.expirationTime = &time 110 111 return tx 112 } 113 114 // GetExpirationTime returns the optional timestamp for specifying when the transaction should be evaluated for execution and then expire. 115 func (tx *ScheduleCreateTransaction) GetExpirationTime() time.Time { 116 if tx.expirationTime != nil { 117 return *tx.expirationTime 118 } 119 120 return time.Time{} 121 } 122 123 // SetWaitForExpiry 124 // When set to true, the transaction will be evaluated for execution at expiration_time instead 125 // of when all required signatures are received. 126 // When set to false, the transaction will execute immediately after sufficient signatures are received 127 // to sign the contained transaction. During the initial ScheduleCreate transaction or via ScheduleSign transactions. 128 // Defaults to false. 129 func (tx *ScheduleCreateTransaction) SetWaitForExpiry(wait bool) *ScheduleCreateTransaction { 130 tx._RequireNotFrozen() 131 tx.waitForExpiry = wait 132 133 return tx 134 } 135 136 // GetWaitForExpiry returns true if the transaction will be evaluated for execution at expiration_time instead 137 // of when all required signatures are received. 138 func (tx *ScheduleCreateTransaction) GetWaitForExpiry() bool { 139 return tx.waitForExpiry 140 } 141 142 func (tx *ScheduleCreateTransaction) _SetSchedulableTransactionBody(txBody *services.SchedulableTransactionBody) *ScheduleCreateTransaction { 143 tx._RequireNotFrozen() 144 tx.schedulableBody = txBody 145 146 return tx 147 } 148 149 // GetAdminKey returns the optional Hedera key which can be used to sign a ScheduleDelete and remove the schedule 150 func (tx *ScheduleCreateTransaction) GetAdminKey() *Key { 151 if tx.adminKey == nil { 152 return nil 153 } 154 return &tx.adminKey 155 } 156 157 // SetScheduleMemo Sets an optional memo with a UTF-8 encoding of no more than 100 bytes which does not contain the zero byte. 158 func (tx *ScheduleCreateTransaction) SetScheduleMemo(memo string) *ScheduleCreateTransaction { 159 tx._RequireNotFrozen() 160 tx.memo = memo 161 162 return tx 163 } 164 165 // GetScheduleMemo returns the optional memo with a UTF-8 encoding of no more than 100 bytes which does not contain the zero byte. 166 func (tx *ScheduleCreateTransaction) GetScheduleMemo() string { 167 return tx.memo 168 } 169 170 // SetScheduledTransaction Sets the scheduled transaction 171 func (tx *ScheduleCreateTransaction) SetScheduledTransaction(scheduledTx ITransaction) (*ScheduleCreateTransaction, error) { 172 tx._RequireNotFrozen() 173 174 scheduled, err := scheduledTx._ConstructScheduleProtobuf() 175 if err != nil { 176 return tx, err 177 } 178 179 tx.schedulableBody = scheduled 180 return tx, nil 181 } 182 183 // ---- Required Interfaces ---- // 184 185 // Sign uses the provided privateKey to sign the transaction. 186 func (tx *ScheduleCreateTransaction) Sign(privateKey PrivateKey) *ScheduleCreateTransaction { 187 tx.Transaction.Sign(privateKey) 188 return tx 189 } 190 191 // SignWithOperator signs the transaction with client's operator privateKey. 192 func (tx *ScheduleCreateTransaction) SignWithOperator(client *Client) (*ScheduleCreateTransaction, error) { 193 _, err := tx.Transaction.signWithOperator(client, tx) 194 if err != nil { 195 return nil, err 196 } 197 return tx, nil 198 } 199 200 // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map 201 // with the publicKey as the map key. 202 func (tx *ScheduleCreateTransaction) SignWith( 203 publicKey PublicKey, 204 signer TransactionSigner, 205 ) *ScheduleCreateTransaction { 206 tx.Transaction.SignWith(publicKey, signer) 207 return tx 208 } 209 210 // AddSignature adds a signature to the transaction. 211 func (tx *ScheduleCreateTransaction) AddSignature(publicKey PublicKey, signature []byte) *ScheduleCreateTransaction { 212 tx.Transaction.AddSignature(publicKey, signature) 213 return tx 214 } 215 216 // SetGrpcDeadline When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.) 217 func (tx *ScheduleCreateTransaction) SetGrpcDeadline(deadline *time.Duration) *ScheduleCreateTransaction { 218 tx.Transaction.SetGrpcDeadline(deadline) 219 return tx 220 } 221 222 func (tx *ScheduleCreateTransaction) Freeze() (*ScheduleCreateTransaction, error) { 223 return tx.FreezeWith(nil) 224 } 225 226 func (tx *ScheduleCreateTransaction) FreezeWith(client *Client) (*ScheduleCreateTransaction, error) { 227 _, err := tx.Transaction.freezeWith(client, tx) 228 return tx, err 229 } 230 231 // SetMaxTransactionFee sets the maximum transaction fee for this ScheduleCreateTransaction. 232 func (tx *ScheduleCreateTransaction) SetMaxTransactionFee(fee Hbar) *ScheduleCreateTransaction { 233 tx.Transaction.SetMaxTransactionFee(fee) 234 return tx 235 } 236 237 // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received 238 func (tx *ScheduleCreateTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *ScheduleCreateTransaction { 239 tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID) 240 return tx 241 } 242 243 // SetTransactionMemo sets the memo for this ScheduleCreateTransaction. 244 func (tx *ScheduleCreateTransaction) SetTransactionMemo(memo string) *ScheduleCreateTransaction { 245 tx.Transaction.SetTransactionMemo(memo) 246 return tx 247 } 248 249 // SetTransactionValidDuration sets the valid duration for this ScheduleCreateTransaction. 250 func (tx *ScheduleCreateTransaction) SetTransactionValidDuration(duration time.Duration) *ScheduleCreateTransaction { 251 tx.Transaction.SetTransactionValidDuration(duration) 252 return tx 253 } 254 255 // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not 256 func (tx *ScheduleCreateTransaction) ToBytes() ([]byte, error) { 257 bytes, err := tx.Transaction.toBytes(tx) 258 if err != nil { 259 return nil, err 260 } 261 return bytes, nil 262 } 263 264 // SetTransactionID sets the TransactionID for this ScheduleCreateTransaction. 265 func (tx *ScheduleCreateTransaction) SetTransactionID(transactionID TransactionID) *ScheduleCreateTransaction { 266 tx.Transaction.SetTransactionID(transactionID) 267 return tx 268 } 269 270 // SetNodeAccountIDs sets the _Node AccountID for this ScheduleCreateTransaction. 271 func (tx *ScheduleCreateTransaction) SetNodeAccountIDs(nodeID []AccountID) *ScheduleCreateTransaction { 272 tx.Transaction.SetNodeAccountIDs(nodeID) 273 return tx 274 } 275 276 // SetMaxRetry sets the max number of errors before execution will fail. 277 func (tx *ScheduleCreateTransaction) SetMaxRetry(count int) *ScheduleCreateTransaction { 278 tx.Transaction.SetMaxRetry(count) 279 return tx 280 } 281 282 // SetMaxBackoff The maximum amount of time to wait between retries. 283 // Every retry attempt will increase the wait time exponentially until it reaches this time. 284 func (tx *ScheduleCreateTransaction) SetMaxBackoff(max time.Duration) *ScheduleCreateTransaction { 285 tx.Transaction.SetMaxBackoff(max) 286 return tx 287 } 288 289 // SetMinBackoff sets the minimum amount of time to wait between retries. 290 func (tx *ScheduleCreateTransaction) SetMinBackoff(min time.Duration) *ScheduleCreateTransaction { 291 tx.Transaction.SetMinBackoff(min) 292 return tx 293 } 294 295 func (tx *ScheduleCreateTransaction) SetLogLevel(level LogLevel) *ScheduleCreateTransaction { 296 tx.Transaction.SetLogLevel(level) 297 return tx 298 } 299 300 func (tx *ScheduleCreateTransaction) Execute(client *Client) (TransactionResponse, error) { 301 return tx.Transaction.execute(client, tx) 302 } 303 304 func (tx *ScheduleCreateTransaction) Schedule() (*ScheduleCreateTransaction, error) { 305 return tx.Transaction.schedule(tx) 306 } 307 308 // ----------- Overridden functions ---------------- 309 310 func (tx *ScheduleCreateTransaction) getName() string { 311 return "ScheduleCreateTransaction" 312 } 313 314 func (tx *ScheduleCreateTransaction) validateNetworkOnIDs(client *Client) error { 315 if client == nil || !client.autoValidateChecksums { 316 return nil 317 } 318 319 if tx.payerAccountID != nil { 320 if err := tx.payerAccountID.ValidateChecksum(client); err != nil { 321 return err 322 } 323 } 324 325 return nil 326 } 327 328 func (tx *ScheduleCreateTransaction) build() *services.TransactionBody { 329 return &services.TransactionBody{ 330 TransactionFee: tx.transactionFee, 331 Memo: tx.Transaction.memo, 332 TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()), 333 TransactionID: tx.transactionID._ToProtobuf(), 334 Data: &services.TransactionBody_ScheduleCreate{ 335 ScheduleCreate: tx.buildProtoBody(), 336 }, 337 } 338 } 339 340 func (tx *ScheduleCreateTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) { 341 return nil, errors.New("cannot schedule `ScheduleCreateTransaction`") 342 } 343 344 func (tx *ScheduleCreateTransaction) buildProtoBody() *services.ScheduleCreateTransactionBody { 345 body := &services.ScheduleCreateTransactionBody{ 346 Memo: tx.memo, 347 WaitForExpiry: tx.waitForExpiry, 348 } 349 350 if tx.payerAccountID != nil { 351 body.PayerAccountID = tx.payerAccountID._ToProtobuf() 352 } 353 354 if tx.adminKey != nil { 355 body.AdminKey = tx.adminKey._ToProtoKey() 356 } 357 358 if tx.schedulableBody != nil { 359 body.ScheduledTransactionBody = tx.schedulableBody 360 } 361 362 if tx.expirationTime != nil { 363 body.ExpirationTime = _TimeToProtobuf(*tx.expirationTime) 364 } 365 366 return body 367 } 368 369 func (tx *ScheduleCreateTransaction) getMethod(channel *_Channel) _Method { 370 return _Method{ 371 transaction: channel._GetSchedule().CreateSchedule, 372 } 373 } 374 375 func (tx *ScheduleCreateTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) { 376 return tx.buildScheduled() 377 }