github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/topic_update_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  	"google.golang.org/protobuf/types/known/wrapperspb"
    27  
    28  	"github.com/hashgraph/hedera-protobufs-go/services"
    29  )
    30  
    31  // TopicUpdateTransaction
    32  // Updates all fields on a Topic that are set in the transaction.
    33  type TopicUpdateTransaction struct {
    34  	Transaction
    35  	topicID            *TopicID
    36  	autoRenewAccountID *AccountID
    37  	adminKey           Key
    38  	submitKey          Key
    39  	memo               string
    40  	autoRenewPeriod    *time.Duration
    41  	expirationTime     *time.Time
    42  }
    43  
    44  // NewTopicUpdateTransaction creates a TopicUpdateTransaction transaction which
    45  // updates all fields on a Topic that are set in the transaction.
    46  func NewTopicUpdateTransaction() *TopicUpdateTransaction {
    47  	tx := TopicUpdateTransaction{
    48  		Transaction: _NewTransaction(),
    49  	}
    50  
    51  	tx.SetAutoRenewPeriod(7890000 * time.Second)
    52  	tx._SetDefaultMaxTransactionFee(NewHbar(2))
    53  
    54  	return &tx
    55  }
    56  
    57  func _TopicUpdateTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *TopicUpdateTransaction {
    58  	adminKey, _ := _KeyFromProtobuf(pb.GetConsensusUpdateTopic().GetAdminKey())
    59  	submitKey, _ := _KeyFromProtobuf(pb.GetConsensusUpdateTopic().GetSubmitKey())
    60  
    61  	expirationTime := _TimeFromProtobuf(pb.GetConsensusUpdateTopic().GetExpirationTime())
    62  	autoRenew := _DurationFromProtobuf(pb.GetConsensusUpdateTopic().GetAutoRenewPeriod())
    63  	return &TopicUpdateTransaction{
    64  		Transaction:        tx,
    65  		topicID:            _TopicIDFromProtobuf(pb.GetConsensusUpdateTopic().GetTopicID()),
    66  		autoRenewAccountID: _AccountIDFromProtobuf(pb.GetConsensusUpdateTopic().GetAutoRenewAccount()),
    67  		adminKey:           adminKey,
    68  		submitKey:          submitKey,
    69  		memo:               pb.GetConsensusUpdateTopic().GetMemo().Value,
    70  		autoRenewPeriod:    &autoRenew,
    71  		expirationTime:     &expirationTime,
    72  	}
    73  }
    74  
    75  // SetTopicID sets the topic to be updated.
    76  func (tx *TopicUpdateTransaction) SetTopicID(topicID TopicID) *TopicUpdateTransaction {
    77  	tx._RequireNotFrozen()
    78  	tx.topicID = &topicID
    79  	return tx
    80  }
    81  
    82  // GetTopicID returns the topic to be updated.
    83  func (tx *TopicUpdateTransaction) GetTopicID() TopicID {
    84  	if tx.topicID == nil {
    85  		return TopicID{}
    86  	}
    87  
    88  	return *tx.topicID
    89  }
    90  
    91  // SetAdminKey sets the key required to update/delete the topic. If unset, the key will not be changed.
    92  //
    93  // Setting the AdminKey to an empty KeyList will clear the adminKey.
    94  func (tx *TopicUpdateTransaction) SetAdminKey(publicKey Key) *TopicUpdateTransaction {
    95  	tx._RequireNotFrozen()
    96  	tx.adminKey = publicKey
    97  	return tx
    98  }
    99  
   100  // GetAdminKey returns the key required to update/delete the topic.
   101  func (tx *TopicUpdateTransaction) GetAdminKey() (Key, error) {
   102  	return tx.adminKey, nil
   103  }
   104  
   105  // SetSubmitKey will set the key allowed to submit messages to the topic.  If unset, the key will not be changed.
   106  //
   107  // Setting the submitKey to an empty KeyList will clear the submitKey.
   108  func (tx *TopicUpdateTransaction) SetSubmitKey(publicKey Key) *TopicUpdateTransaction {
   109  	tx._RequireNotFrozen()
   110  	tx.submitKey = publicKey
   111  	return tx
   112  }
   113  
   114  // GetSubmitKey returns the key allowed to submit messages to the topic.
   115  func (tx *TopicUpdateTransaction) GetSubmitKey() (Key, error) {
   116  	return tx.submitKey, nil
   117  }
   118  
   119  // SetTopicMemo sets a short publicly visible memo about the topic. No guarantee of uniqueness.
   120  func (tx *TopicUpdateTransaction) SetTopicMemo(memo string) *TopicUpdateTransaction {
   121  	tx._RequireNotFrozen()
   122  	tx.memo = memo
   123  	return tx
   124  }
   125  
   126  // GetTopicMemo returns the short publicly visible memo about the topic.
   127  func (tx *TopicUpdateTransaction) GetTopicMemo() string {
   128  	return tx.memo
   129  }
   130  
   131  // SetExpirationTime sets the effective  timestamp at (and after) which all  transactions and queries
   132  // will fail. The expirationTime may be no longer than 90 days from the  timestamp of this transaction.
   133  func (tx *TopicUpdateTransaction) SetExpirationTime(expiration time.Time) *TopicUpdateTransaction {
   134  	tx._RequireNotFrozen()
   135  	tx.expirationTime = &expiration
   136  	return tx
   137  }
   138  
   139  // GetExpirationTime returns the effective  timestamp at (and after) which all transactions and queries will fail.
   140  func (tx *TopicUpdateTransaction) GetExpirationTime() time.Time {
   141  	if tx.expirationTime != nil {
   142  		return *tx.expirationTime
   143  	}
   144  
   145  	return time.Time{}
   146  }
   147  
   148  // SetAutoRenewPeriod sets the amount of time to extend the topic's lifetime automatically at expirationTime if the
   149  // autoRenewAccount is configured and has funds. This is limited to a maximum of 90 days (server-sIDe configuration
   150  // which may change).
   151  func (tx *TopicUpdateTransaction) SetAutoRenewPeriod(period time.Duration) *TopicUpdateTransaction {
   152  	tx._RequireNotFrozen()
   153  	tx.autoRenewPeriod = &period
   154  	return tx
   155  }
   156  
   157  // GetAutoRenewPeriod returns the amount of time to extend the topic's lifetime automatically at expirationTime if the
   158  // autoRenewAccount is configured and has funds.
   159  func (tx *TopicUpdateTransaction) GetAutoRenewPeriod() time.Duration {
   160  	if tx.autoRenewPeriod != nil {
   161  		return *tx.autoRenewPeriod
   162  	}
   163  
   164  	return time.Duration(0)
   165  }
   166  
   167  // SetAutoRenewAccountID sets the optional account to be used at the topic's expirationTime to extend the life of the
   168  // topic. The topic lifetime will be extended up to a maximum of the autoRenewPeriod or however long the topic can be
   169  // extended using all funds on the account (whichever is the smaller duration/amount). If specified as the default value
   170  // (0.0.0), the autoRenewAccount will be removed.
   171  func (tx *TopicUpdateTransaction) SetAutoRenewAccountID(autoRenewAccountID AccountID) *TopicUpdateTransaction {
   172  	tx._RequireNotFrozen()
   173  	tx.autoRenewAccountID = &autoRenewAccountID
   174  	return tx
   175  }
   176  
   177  // GetAutoRenewAccountID returns the optional account to be used at the topic's expirationTime to extend the life of the
   178  // topic.
   179  func (tx *TopicUpdateTransaction) GetAutoRenewAccountID() AccountID {
   180  	if tx.autoRenewAccountID == nil {
   181  		return AccountID{}
   182  	}
   183  
   184  	return *tx.autoRenewAccountID
   185  }
   186  
   187  // ClearTopicMemo explicitly clears any memo on the topic by sending an empty string as the memo
   188  func (tx *TopicUpdateTransaction) ClearTopicMemo() *TopicUpdateTransaction {
   189  	return tx.SetTopicMemo("")
   190  }
   191  
   192  // ClearAdminKey explicitly clears any admin key on the topic by sending an empty key list as the key
   193  func (tx *TopicUpdateTransaction) ClearAdminKey() *TopicUpdateTransaction {
   194  	return tx.SetAdminKey(PublicKey{nil, nil})
   195  }
   196  
   197  // ClearSubmitKey explicitly clears any submit key on the topic by sending an empty key list as the key
   198  func (tx *TopicUpdateTransaction) ClearSubmitKey() *TopicUpdateTransaction {
   199  	return tx.SetSubmitKey(PublicKey{nil, nil})
   200  }
   201  
   202  // ClearAutoRenewAccountID explicitly clears any auto renew account ID on the topic by sending an empty accountID
   203  func (tx *TopicUpdateTransaction) ClearAutoRenewAccountID() *TopicUpdateTransaction {
   204  	tx.autoRenewAccountID = &AccountID{}
   205  	return tx
   206  }
   207  
   208  // ---- Required Interfaces ---- //
   209  
   210  // Sign uses the provided privateKey to sign the transaction.
   211  func (tx *TopicUpdateTransaction) Sign(privateKey PrivateKey) *TopicUpdateTransaction {
   212  	tx.Transaction.Sign(privateKey)
   213  	return tx
   214  }
   215  
   216  // SignWithOperator signs the transaction with client's operator privateKey.
   217  func (tx *TopicUpdateTransaction) SignWithOperator(client *Client) (*TopicUpdateTransaction, error) {
   218  	_, err := tx.Transaction.signWithOperator(client, tx)
   219  	if err != nil {
   220  		return nil, err
   221  	}
   222  	return tx, nil
   223  }
   224  
   225  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   226  // with the publicKey as the map key.
   227  func (tx *TopicUpdateTransaction) SignWith(
   228  	publicKey PublicKey,
   229  	signer TransactionSigner,
   230  ) *TopicUpdateTransaction {
   231  	tx.Transaction.SignWith(publicKey, signer)
   232  	return tx
   233  }
   234  
   235  // AddSignature adds a signature to the transaction.
   236  func (tx *TopicUpdateTransaction) AddSignature(publicKey PublicKey, signature []byte) *TopicUpdateTransaction {
   237  	tx.Transaction.AddSignature(publicKey, signature)
   238  	return tx
   239  }
   240  
   241  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
   242  func (tx *TopicUpdateTransaction) SetGrpcDeadline(deadline *time.Duration) *TopicUpdateTransaction {
   243  	tx.Transaction.SetGrpcDeadline(deadline)
   244  	return tx
   245  }
   246  
   247  func (tx *TopicUpdateTransaction) Freeze() (*TopicUpdateTransaction, error) {
   248  	return tx.FreezeWith(nil)
   249  }
   250  
   251  func (tx *TopicUpdateTransaction) FreezeWith(client *Client) (*TopicUpdateTransaction, error) {
   252  	_, err := tx.Transaction.freezeWith(client, tx)
   253  	return tx, err
   254  }
   255  
   256  // SetMaxTransactionFee sets the max transaction fee for this TopicUpdateTransaction.
   257  func (tx *TopicUpdateTransaction) SetMaxTransactionFee(fee Hbar) *TopicUpdateTransaction {
   258  	tx.Transaction.SetMaxTransactionFee(fee)
   259  	return tx
   260  }
   261  
   262  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   263  func (tx *TopicUpdateTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *TopicUpdateTransaction {
   264  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   265  	return tx
   266  }
   267  
   268  // SetTransactionMemo sets the memo for this TopicUpdateTransaction.
   269  func (tx *TopicUpdateTransaction) SetTransactionMemo(memo string) *TopicUpdateTransaction {
   270  	tx.Transaction.SetTransactionMemo(memo)
   271  	return tx
   272  }
   273  
   274  // SetTransactionValidDuration sets the valid duration for this TopicUpdateTransaction.
   275  func (tx *TopicUpdateTransaction) SetTransactionValidDuration(duration time.Duration) *TopicUpdateTransaction {
   276  	tx.Transaction.SetTransactionValidDuration(duration)
   277  	return tx
   278  }
   279  
   280  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   281  func (tx *TopicUpdateTransaction) ToBytes() ([]byte, error) {
   282  	bytes, err := tx.Transaction.toBytes(tx)
   283  	if err != nil {
   284  		return nil, err
   285  	}
   286  	return bytes, nil
   287  }
   288  
   289  // SetTransactionID sets the TransactionID for this TopicUpdateTransaction.
   290  func (tx *TopicUpdateTransaction) SetTransactionID(transactionID TransactionID) *TopicUpdateTransaction {
   291  	tx.Transaction.SetTransactionID(transactionID)
   292  	return tx
   293  }
   294  
   295  // SetNodeAccountIDs sets the _Node AccountID for this TopicUpdateTransaction.
   296  func (tx *TopicUpdateTransaction) SetNodeAccountIDs(nodeID []AccountID) *TopicUpdateTransaction {
   297  	tx.Transaction.SetNodeAccountIDs(nodeID)
   298  	return tx
   299  }
   300  
   301  // SetMaxRetry sets the max number of errors before execution will fail.
   302  func (tx *TopicUpdateTransaction) SetMaxRetry(count int) *TopicUpdateTransaction {
   303  	tx.Transaction.SetMaxRetry(count)
   304  	return tx
   305  }
   306  
   307  // SetMaxBackoff The maximum amount of time to wait between retries.
   308  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   309  func (tx *TopicUpdateTransaction) SetMaxBackoff(max time.Duration) *TopicUpdateTransaction {
   310  	tx.Transaction.SetMaxBackoff(max)
   311  	return tx
   312  }
   313  
   314  // SetMinBackoff sets the minimum amount of time to wait between retries.
   315  func (tx *TopicUpdateTransaction) SetMinBackoff(min time.Duration) *TopicUpdateTransaction {
   316  	tx.Transaction.SetMinBackoff(min)
   317  	return tx
   318  }
   319  
   320  func (tx *TopicUpdateTransaction) SetLogLevel(level LogLevel) *TopicUpdateTransaction {
   321  	tx.Transaction.SetLogLevel(level)
   322  	return tx
   323  }
   324  
   325  func (tx *TopicUpdateTransaction) Execute(client *Client) (TransactionResponse, error) {
   326  	return tx.Transaction.execute(client, tx)
   327  }
   328  
   329  func (tx *TopicUpdateTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   330  	return tx.Transaction.schedule(tx)
   331  }
   332  
   333  // ----------- Overridden functions ----------------
   334  
   335  func (tx *TopicUpdateTransaction) getName() string {
   336  	return "TopicUpdateTransaction"
   337  }
   338  
   339  func (tx *TopicUpdateTransaction) validateNetworkOnIDs(client *Client) error {
   340  	if client == nil || !client.autoValidateChecksums {
   341  		return nil
   342  	}
   343  
   344  	if tx.topicID != nil {
   345  		if err := tx.topicID.ValidateChecksum(client); err != nil {
   346  			return err
   347  		}
   348  	}
   349  
   350  	if tx.autoRenewAccountID != nil {
   351  		if err := tx.autoRenewAccountID.ValidateChecksum(client); err != nil {
   352  			return err
   353  		}
   354  	}
   355  
   356  	return nil
   357  }
   358  
   359  func (tx *TopicUpdateTransaction) build() *services.TransactionBody {
   360  	return &services.TransactionBody{
   361  		TransactionFee:           tx.transactionFee,
   362  		Memo:                     tx.Transaction.memo,
   363  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   364  		TransactionID:            tx.transactionID._ToProtobuf(),
   365  		Data: &services.TransactionBody_ConsensusUpdateTopic{
   366  			ConsensusUpdateTopic: tx.buildProtoBody(),
   367  		},
   368  	}
   369  }
   370  
   371  func (tx *TopicUpdateTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   372  	return &services.SchedulableTransactionBody{
   373  		TransactionFee: tx.transactionFee,
   374  		Memo:           tx.Transaction.memo,
   375  		Data: &services.SchedulableTransactionBody_ConsensusUpdateTopic{
   376  			ConsensusUpdateTopic: tx.buildProtoBody(),
   377  		},
   378  	}, nil
   379  }
   380  
   381  func (tx *TopicUpdateTransaction) buildProtoBody() *services.ConsensusUpdateTopicTransactionBody {
   382  	body := &services.ConsensusUpdateTopicTransactionBody{
   383  		Memo: &wrapperspb.StringValue{Value: tx.memo},
   384  	}
   385  
   386  	if tx.topicID != nil {
   387  		body.TopicID = tx.topicID._ToProtobuf()
   388  	}
   389  
   390  	if tx.autoRenewAccountID != nil {
   391  		body.AutoRenewAccount = tx.autoRenewAccountID._ToProtobuf()
   392  	}
   393  
   394  	if tx.autoRenewPeriod != nil {
   395  		body.AutoRenewPeriod = _DurationToProtobuf(*tx.autoRenewPeriod)
   396  	}
   397  
   398  	if tx.expirationTime != nil {
   399  		body.ExpirationTime = _TimeToProtobuf(*tx.expirationTime)
   400  	}
   401  
   402  	if tx.adminKey != nil {
   403  		body.AdminKey = tx.adminKey._ToProtoKey()
   404  	}
   405  
   406  	if tx.submitKey != nil {
   407  		body.SubmitKey = tx.submitKey._ToProtoKey()
   408  	}
   409  
   410  	return body
   411  }
   412  
   413  func (tx *TopicUpdateTransaction) getMethod(channel *_Channel) _Method {
   414  	return _Method{
   415  		transaction: channel._GetTopic().UpdateTopic,
   416  	}
   417  }
   418  func (tx *TopicUpdateTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   419  	return tx.buildScheduled()
   420  }