github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/topic_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 "time" 25 26 "github.com/hashgraph/hedera-protobufs-go/services" 27 ) 28 29 // A TopicCreateTransaction is for creating a new Topic on HCS. 30 type TopicCreateTransaction struct { 31 Transaction 32 autoRenewAccountID *AccountID 33 adminKey Key 34 submitKey Key 35 memo string 36 autoRenewPeriod *time.Duration 37 } 38 39 // NewTopicCreateTransaction creates a TopicCreateTransaction transaction which can be 40 // used to construct and execute a Create Topic Transaction. 41 func NewTopicCreateTransaction() *TopicCreateTransaction { 42 tx := TopicCreateTransaction{ 43 Transaction: _NewTransaction(), 44 } 45 46 tx.SetAutoRenewPeriod(7890000 * time.Second) 47 tx._SetDefaultMaxTransactionFee(NewHbar(2)) 48 49 return &tx 50 } 51 52 func _TopicCreateTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *TopicCreateTransaction { 53 adminKey, _ := _KeyFromProtobuf(pb.GetConsensusCreateTopic().GetAdminKey()) 54 submitKey, _ := _KeyFromProtobuf(pb.GetConsensusCreateTopic().GetSubmitKey()) 55 56 autoRenew := _DurationFromProtobuf(pb.GetConsensusCreateTopic().GetAutoRenewPeriod()) 57 return &TopicCreateTransaction{ 58 Transaction: tx, 59 autoRenewAccountID: _AccountIDFromProtobuf(pb.GetConsensusCreateTopic().GetAutoRenewAccount()), 60 adminKey: adminKey, 61 submitKey: submitKey, 62 memo: pb.GetConsensusCreateTopic().GetMemo(), 63 autoRenewPeriod: &autoRenew, 64 } 65 } 66 67 // SetAdminKey sets the key required to update or delete the topic. If unspecified, anyone can increase the topic's 68 // expirationTime. 69 func (tx *TopicCreateTransaction) SetAdminKey(publicKey Key) *TopicCreateTransaction { 70 tx._RequireNotFrozen() 71 tx.adminKey = publicKey 72 return tx 73 } 74 75 // GetAdminKey returns the key required to update or delete the topic 76 func (tx *TopicCreateTransaction) GetAdminKey() (Key, error) { 77 return tx.adminKey, nil 78 } 79 80 // SetSubmitKey sets the key required for submitting messages to the topic. If unspecified, all submissions are allowed. 81 func (tx *TopicCreateTransaction) SetSubmitKey(publicKey Key) *TopicCreateTransaction { 82 tx._RequireNotFrozen() 83 tx.submitKey = publicKey 84 return tx 85 } 86 87 // GetSubmitKey returns the key required for submitting messages to the topic 88 func (tx *TopicCreateTransaction) GetSubmitKey() (Key, error) { 89 return tx.submitKey, nil 90 } 91 92 // SetTopicMemo sets a short publicly visible memo about the topic. No guarantee of uniqueness. 93 func (tx *TopicCreateTransaction) SetTopicMemo(memo string) *TopicCreateTransaction { 94 tx._RequireNotFrozen() 95 tx.memo = memo 96 return tx 97 } 98 99 // GetTopicMemo returns the memo for this topic 100 func (tx *TopicCreateTransaction) GetTopicMemo() string { 101 return tx.memo 102 } 103 104 // SetAutoRenewPeriod sets the initial lifetime of the topic and the amount of time to extend the topic's lifetime 105 // automatically at expirationTime if the autoRenewAccount is configured and has sufficient funds. 106 // 107 // Required. Limited to a maximum of 90 days (server-sIDe configuration which may change). 108 func (tx *TopicCreateTransaction) SetAutoRenewPeriod(period time.Duration) *TopicCreateTransaction { 109 tx._RequireNotFrozen() 110 tx.autoRenewPeriod = &period 111 return tx 112 } 113 114 // GetAutoRenewPeriod returns the auto renew period for this topic 115 func (tx *TopicCreateTransaction) GetAutoRenewPeriod() time.Duration { 116 if tx.autoRenewPeriod != nil { 117 return *tx.autoRenewPeriod 118 } 119 120 return time.Duration(0) 121 } 122 123 // SetAutoRenewAccountID sets an optional account to be used at the topic's expirationTime to extend the life of the 124 // topic. The topic lifetime will be extended up to a maximum of the autoRenewPeriod or however long the topic can be 125 // extended using all funds on the account (whichever is the smaller duration/amount). 126 // 127 // If specified, there must be an adminKey and the autoRenewAccount must sign this transaction. 128 func (tx *TopicCreateTransaction) SetAutoRenewAccountID(autoRenewAccountID AccountID) *TopicCreateTransaction { 129 tx._RequireNotFrozen() 130 tx.autoRenewAccountID = &autoRenewAccountID 131 return tx 132 } 133 134 // GetAutoRenewAccountID returns the auto renew account ID for this topic 135 func (tx *TopicCreateTransaction) GetAutoRenewAccountID() AccountID { 136 if tx.autoRenewAccountID == nil { 137 return AccountID{} 138 } 139 140 return *tx.autoRenewAccountID 141 } 142 143 // ---- Required Interfaces ---- // 144 145 // Sign uses the provided privateKey to sign the transaction. 146 func (tx *TopicCreateTransaction) Sign(privateKey PrivateKey) *TopicCreateTransaction { 147 tx.Transaction.Sign(privateKey) 148 return tx 149 } 150 151 // SignWithOperator signs the transaction with client's operator privateKey. 152 func (tx *TopicCreateTransaction) SignWithOperator(client *Client) (*TopicCreateTransaction, error) { 153 _, err := tx.Transaction.signWithOperator(client, tx) 154 if err != nil { 155 return nil, err 156 } 157 return tx, nil 158 } 159 160 // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map 161 // with the publicKey as the map key. 162 func (tx *TopicCreateTransaction) SignWith( 163 publicKey PublicKey, 164 signer TransactionSigner, 165 ) *TopicCreateTransaction { 166 tx.Transaction.SignWith(publicKey, signer) 167 return tx 168 } 169 170 // AddSignature adds a signature to the transaction. 171 func (tx *TopicCreateTransaction) AddSignature(publicKey PublicKey, signature []byte) *TopicCreateTransaction { 172 tx.Transaction.AddSignature(publicKey, signature) 173 return tx 174 } 175 176 // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.) 177 func (tx *TopicCreateTransaction) SetGrpcDeadline(deadline *time.Duration) *TopicCreateTransaction { 178 tx.Transaction.SetGrpcDeadline(deadline) 179 return tx 180 } 181 182 func (tx *TopicCreateTransaction) Freeze() (*TopicCreateTransaction, error) { 183 return tx.FreezeWith(nil) 184 } 185 186 func (tx *TopicCreateTransaction) FreezeWith(client *Client) (*TopicCreateTransaction, error) { 187 _, err := tx.Transaction.freezeWith(client, tx) 188 return tx, err 189 } 190 191 // SetMaxTransactionFee sets the max transaction fee for this TopicCreateTransaction. 192 func (tx *TopicCreateTransaction) SetMaxTransactionFee(fee Hbar) *TopicCreateTransaction { 193 tx.Transaction.SetMaxTransactionFee(fee) 194 return tx 195 } 196 197 // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received 198 func (tx *TopicCreateTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *TopicCreateTransaction { 199 tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID) 200 return tx 201 } 202 203 // SetTransactionMemo sets the memo for this TopicCreateTransaction. 204 func (tx *TopicCreateTransaction) SetTransactionMemo(memo string) *TopicCreateTransaction { 205 tx.Transaction.SetTransactionMemo(memo) 206 return tx 207 } 208 209 // SetTransactionValidDuration sets the valid duration for this TopicCreateTransaction. 210 func (tx *TopicCreateTransaction) SetTransactionValidDuration(duration time.Duration) *TopicCreateTransaction { 211 tx.Transaction.SetTransactionValidDuration(duration) 212 return tx 213 } 214 215 // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not 216 func (tx *TopicCreateTransaction) ToBytes() ([]byte, error) { 217 bytes, err := tx.Transaction.toBytes(tx) 218 if err != nil { 219 return nil, err 220 } 221 return bytes, nil 222 } 223 224 // SetTransactionID sets the TransactionID for this TopicCreateTransaction. 225 func (tx *TopicCreateTransaction) SetTransactionID(transactionID TransactionID) *TopicCreateTransaction { 226 tx.Transaction.SetTransactionID(transactionID) 227 return tx 228 } 229 230 // SetNodeAccountIDs sets the _Node AccountID for this TopicCreateTransaction. 231 func (tx *TopicCreateTransaction) SetNodeAccountIDs(nodeID []AccountID) *TopicCreateTransaction { 232 tx.Transaction.SetNodeAccountIDs(nodeID) 233 return tx 234 } 235 236 // SetMaxRetry sets the max number of errors before execution will fail. 237 func (tx *TopicCreateTransaction) SetMaxRetry(count int) *TopicCreateTransaction { 238 tx.Transaction.SetMaxRetry(count) 239 return tx 240 } 241 242 // SetMaxBackoff The maximum amount of time to wait between retries. 243 // Every retry attempt will increase the wait time exponentially until it reaches this time. 244 func (tx *TopicCreateTransaction) SetMaxBackoff(max time.Duration) *TopicCreateTransaction { 245 tx.Transaction.SetMaxBackoff(max) 246 return tx 247 } 248 249 // SetMinBackoff sets the minimum amount of time to wait between retries. 250 func (tx *TopicCreateTransaction) SetMinBackoff(min time.Duration) *TopicCreateTransaction { 251 tx.Transaction.SetMinBackoff(min) 252 return tx 253 } 254 255 func (tx *TopicCreateTransaction) SetLogLevel(level LogLevel) *TopicCreateTransaction { 256 tx.Transaction.SetLogLevel(level) 257 return tx 258 } 259 260 func (tx *TopicCreateTransaction) Execute(client *Client) (TransactionResponse, error) { 261 return tx.Transaction.execute(client, tx) 262 } 263 264 func (tx *TopicCreateTransaction) Schedule() (*ScheduleCreateTransaction, error) { 265 return tx.Transaction.schedule(tx) 266 } 267 268 // ----------- Overridden functions ---------------- 269 270 func (tx *TopicCreateTransaction) getName() string { 271 return "TopicCreateTransaction" 272 } 273 274 func (tx *TopicCreateTransaction) validateNetworkOnIDs(client *Client) error { 275 if client == nil || !client.autoValidateChecksums { 276 return nil 277 } 278 279 if tx.autoRenewAccountID != nil { 280 if err := tx.autoRenewAccountID.ValidateChecksum(client); err != nil { 281 return err 282 } 283 } 284 285 return nil 286 } 287 288 func (tx *TopicCreateTransaction) build() *services.TransactionBody { 289 return &services.TransactionBody{ 290 TransactionFee: tx.transactionFee, 291 Memo: tx.Transaction.memo, 292 TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()), 293 TransactionID: tx.transactionID._ToProtobuf(), 294 Data: &services.TransactionBody_ConsensusCreateTopic{ 295 ConsensusCreateTopic: tx.buildProtoBody(), 296 }, 297 } 298 } 299 300 func (tx *TopicCreateTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) { 301 return &services.SchedulableTransactionBody{ 302 TransactionFee: tx.transactionFee, 303 Memo: tx.Transaction.memo, 304 Data: &services.SchedulableTransactionBody_ConsensusCreateTopic{ 305 ConsensusCreateTopic: tx.buildProtoBody(), 306 }, 307 }, nil 308 } 309 310 func (tx *TopicCreateTransaction) buildProtoBody() *services.ConsensusCreateTopicTransactionBody { 311 body := &services.ConsensusCreateTopicTransactionBody{ 312 Memo: tx.memo, 313 } 314 315 if tx.autoRenewPeriod != nil { 316 body.AutoRenewPeriod = _DurationToProtobuf(*tx.autoRenewPeriod) 317 } 318 319 if tx.autoRenewAccountID != nil { 320 body.AutoRenewAccount = tx.autoRenewAccountID._ToProtobuf() 321 } 322 323 if tx.adminKey != nil { 324 body.AdminKey = tx.adminKey._ToProtoKey() 325 } 326 327 if tx.submitKey != nil { 328 body.SubmitKey = tx.submitKey._ToProtoKey() 329 } 330 331 return body 332 } 333 334 func (tx *TopicCreateTransaction) getMethod(channel *_Channel) _Method { 335 return _Method{ 336 transaction: channel._GetTopic().CreateTopic, 337 } 338 } 339 func (tx *TopicCreateTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) { 340 return tx.buildScheduled() 341 }