github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_fee_schedule_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  	"github.com/pkg/errors"
    27  
    28  	"github.com/hashgraph/hedera-protobufs-go/services"
    29  )
    30  
    31  // TokenFeeScheduleUpdateTransaction
    32  // At consensus, updates a token type's fee schedule to the given list of custom fees.
    33  //
    34  // If the target token type has no fee_schedule_key, resolves to TOKEN_HAS_NO_FEE_SCHEDULE_KEY.
    35  // Otherwise this transaction must be signed to the fee_schedule_key, or the transaction will
    36  // resolve to INVALID_SIGNATURE.
    37  //
    38  // If the custom_fees list is empty, clears the fee schedule or resolves to
    39  // CUSTOM_SCHEDULE_ALREADY_HAS_NO_FEES if the fee schedule was already empty.
    40  type TokenFeeScheduleUpdateTransaction struct {
    41  	Transaction
    42  	tokenID    *TokenID
    43  	customFees []Fee
    44  }
    45  
    46  // NewTokenFeeScheduleUpdateTransaction creates TokenFeeScheduleUpdateTransaction which
    47  // at consensus, updates a token type's fee schedule to the given list of custom fees.
    48  //
    49  // If the target token type has no fee_schedule_key, resolves to TOKEN_HAS_NO_FEE_SCHEDULE_KEY.
    50  // Otherwise this transaction must be signed to the fee_schedule_key, or the transaction will
    51  // resolve to INVALID_SIGNATURE.
    52  //
    53  // If the custom_fees list is empty, clears the fee schedule or resolves to
    54  // CUSTOM_SCHEDULE_ALREADY_HAS_NO_FEES if the fee schedule was already empty.
    55  func NewTokenFeeScheduleUpdateTransaction() *TokenFeeScheduleUpdateTransaction {
    56  	tx := TokenFeeScheduleUpdateTransaction{
    57  		Transaction: _NewTransaction(),
    58  	}
    59  
    60  	tx._SetDefaultMaxTransactionFee(NewHbar(5))
    61  
    62  	return &tx
    63  }
    64  
    65  func _TokenFeeScheduleUpdateTransactionFromProtobuf(transaction Transaction, pb *services.TransactionBody) *TokenFeeScheduleUpdateTransaction {
    66  	customFees := make([]Fee, 0)
    67  
    68  	for _, fee := range pb.GetTokenFeeScheduleUpdate().GetCustomFees() {
    69  		customFees = append(customFees, _CustomFeeFromProtobuf(fee))
    70  	}
    71  
    72  	return &TokenFeeScheduleUpdateTransaction{
    73  		Transaction: transaction,
    74  		tokenID:     _TokenIDFromProtobuf(pb.GetTokenFeeScheduleUpdate().TokenId),
    75  		customFees:  customFees,
    76  	}
    77  }
    78  
    79  // SetTokenID Sets the token whose fee schedule is to be updated
    80  func (tx *TokenFeeScheduleUpdateTransaction) SetTokenID(tokenID TokenID) *TokenFeeScheduleUpdateTransaction {
    81  	tx._RequireNotFrozen()
    82  	tx.tokenID = &tokenID
    83  	return tx
    84  }
    85  
    86  // GetTokenID returns the token whose fee schedule is to be updated
    87  func (tx *TokenFeeScheduleUpdateTransaction) GetTokenID() TokenID {
    88  	if tx.tokenID == nil {
    89  		return TokenID{}
    90  	}
    91  
    92  	return *tx.tokenID
    93  }
    94  
    95  // SetCustomFees Sets the new custom fees to be assessed during a CryptoTransfer that transfers units of this token
    96  func (tx *TokenFeeScheduleUpdateTransaction) SetCustomFees(fees []Fee) *TokenFeeScheduleUpdateTransaction {
    97  	tx._RequireNotFrozen()
    98  	tx.customFees = fees
    99  	return tx
   100  }
   101  
   102  // GetCustomFees returns the new custom fees to be assessed during a CryptoTransfer that transfers units of this token
   103  func (tx *TokenFeeScheduleUpdateTransaction) GetCustomFees() []Fee {
   104  	return tx.customFees
   105  }
   106  
   107  // ---- Required Interfaces ---- //
   108  
   109  // Sign uses the provided privateKey to sign the transaction.
   110  func (tx *TokenFeeScheduleUpdateTransaction) Sign(privateKey PrivateKey) *TokenFeeScheduleUpdateTransaction {
   111  	tx.Transaction.Sign(privateKey)
   112  	return tx
   113  }
   114  
   115  // SignWithOperator signs the transaction with client's operator privateKey.
   116  func (tx *TokenFeeScheduleUpdateTransaction) SignWithOperator(client *Client) (*TokenFeeScheduleUpdateTransaction, error) {
   117  	_, err := tx.Transaction.signWithOperator(client, tx)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	return tx, nil
   122  }
   123  
   124  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   125  // with the publicKey as the map key.
   126  func (tx *TokenFeeScheduleUpdateTransaction) SignWith(
   127  	publicKey PublicKey,
   128  	signer TransactionSigner,
   129  ) *TokenFeeScheduleUpdateTransaction {
   130  	tx.Transaction.SignWith(publicKey, signer)
   131  	return tx
   132  }
   133  
   134  // AddSignature adds a signature to the transaction.
   135  func (tx *TokenFeeScheduleUpdateTransaction) AddSignature(publicKey PublicKey, signature []byte) *TokenFeeScheduleUpdateTransaction {
   136  	tx.Transaction.AddSignature(publicKey, signature)
   137  	return tx
   138  }
   139  
   140  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
   141  func (tx *TokenFeeScheduleUpdateTransaction) SetGrpcDeadline(deadline *time.Duration) *TokenFeeScheduleUpdateTransaction {
   142  	tx.Transaction.SetGrpcDeadline(deadline)
   143  	return tx
   144  }
   145  
   146  func (tx *TokenFeeScheduleUpdateTransaction) Freeze() (*TokenFeeScheduleUpdateTransaction, error) {
   147  	return tx.FreezeWith(nil)
   148  }
   149  
   150  func (tx *TokenFeeScheduleUpdateTransaction) FreezeWith(client *Client) (*TokenFeeScheduleUpdateTransaction, error) {
   151  	_, err := tx.Transaction.freezeWith(client, tx)
   152  	return tx, err
   153  }
   154  
   155  // SetMaxTransactionFee sets the max transaction fee for this TokenFeeScheduleUpdateTransaction.
   156  func (tx *TokenFeeScheduleUpdateTransaction) SetMaxTransactionFee(fee Hbar) *TokenFeeScheduleUpdateTransaction {
   157  	tx.Transaction.SetMaxTransactionFee(fee)
   158  	return tx
   159  }
   160  
   161  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   162  func (tx *TokenFeeScheduleUpdateTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *TokenFeeScheduleUpdateTransaction {
   163  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   164  	return tx
   165  }
   166  
   167  // SetTransactionMemo sets the memo for this TokenFeeScheduleUpdateTransaction.
   168  func (tx *TokenFeeScheduleUpdateTransaction) SetTransactionMemo(memo string) *TokenFeeScheduleUpdateTransaction {
   169  	tx.Transaction.SetTransactionMemo(memo)
   170  	return tx
   171  }
   172  
   173  // SetTransactionValidDuration sets the valid duration for this TokenFeeScheduleUpdateTransaction.
   174  func (tx *TokenFeeScheduleUpdateTransaction) SetTransactionValidDuration(duration time.Duration) *TokenFeeScheduleUpdateTransaction {
   175  	tx.Transaction.SetTransactionValidDuration(duration)
   176  	return tx
   177  }
   178  
   179  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   180  func (tx *TokenFeeScheduleUpdateTransaction) ToBytes() ([]byte, error) {
   181  	bytes, err := tx.Transaction.toBytes(tx)
   182  	if err != nil {
   183  		return nil, err
   184  	}
   185  	return bytes, nil
   186  }
   187  
   188  // SetTransactionID sets the TransactionID for this TokenFeeScheduleUpdateTransaction.
   189  func (tx *TokenFeeScheduleUpdateTransaction) SetTransactionID(transactionID TransactionID) *TokenFeeScheduleUpdateTransaction {
   190  	tx.Transaction.SetTransactionID(transactionID)
   191  	return tx
   192  }
   193  
   194  // SetNodeAccountIDs sets the _Node AccountID for this TokenFeeScheduleUpdateTransaction.
   195  func (tx *TokenFeeScheduleUpdateTransaction) SetNodeAccountIDs(nodeID []AccountID) *TokenFeeScheduleUpdateTransaction {
   196  	tx.Transaction.SetNodeAccountIDs(nodeID)
   197  	return tx
   198  }
   199  
   200  // SetMaxRetry sets the max number of errors before execution will fail.
   201  func (tx *TokenFeeScheduleUpdateTransaction) SetMaxRetry(count int) *TokenFeeScheduleUpdateTransaction {
   202  	tx.Transaction.SetMaxRetry(count)
   203  	return tx
   204  }
   205  
   206  // SetMaxBackoff The maximum amount of time to wait between retries.
   207  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   208  func (tx *TokenFeeScheduleUpdateTransaction) SetMaxBackoff(max time.Duration) *TokenFeeScheduleUpdateTransaction {
   209  	tx.Transaction.SetMaxBackoff(max)
   210  	return tx
   211  }
   212  
   213  // SetMinBackoff sets the minimum amount of time to wait between retries.
   214  func (tx *TokenFeeScheduleUpdateTransaction) SetMinBackoff(min time.Duration) *TokenFeeScheduleUpdateTransaction {
   215  	tx.Transaction.SetMinBackoff(min)
   216  	return tx
   217  }
   218  
   219  func (tx *TokenFeeScheduleUpdateTransaction) SetLogLevel(level LogLevel) *TokenFeeScheduleUpdateTransaction {
   220  	tx.Transaction.SetLogLevel(level)
   221  	return tx
   222  }
   223  
   224  func (tx *TokenFeeScheduleUpdateTransaction) Execute(client *Client) (TransactionResponse, error) {
   225  	return tx.Transaction.execute(client, tx)
   226  }
   227  
   228  func (tx *TokenFeeScheduleUpdateTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   229  	return tx.Transaction.schedule(tx)
   230  }
   231  
   232  // ----------- Overridden functions ----------------
   233  
   234  func (tx *TokenFeeScheduleUpdateTransaction) getName() string {
   235  	return "TokenFeeScheduleUpdateTransaction"
   236  }
   237  
   238  func (tx *TokenFeeScheduleUpdateTransaction) validateNetworkOnIDs(client *Client) error {
   239  	if client == nil || !client.autoValidateChecksums {
   240  		return nil
   241  	}
   242  
   243  	if tx.tokenID != nil {
   244  		if err := tx.tokenID.ValidateChecksum(client); err != nil {
   245  			return err
   246  		}
   247  	}
   248  
   249  	for _, customFee := range tx.customFees {
   250  		if err := customFee.validateNetworkOnIDs(client); err != nil {
   251  			return err
   252  		}
   253  	}
   254  
   255  	return nil
   256  }
   257  
   258  func (tx *TokenFeeScheduleUpdateTransaction) build() *services.TransactionBody {
   259  	return &services.TransactionBody{
   260  		TransactionFee:           tx.transactionFee,
   261  		Memo:                     tx.Transaction.memo,
   262  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   263  		TransactionID:            tx.transactionID._ToProtobuf(),
   264  		Data: &services.TransactionBody_TokenFeeScheduleUpdate{
   265  			TokenFeeScheduleUpdate: tx.buildProtoBody(),
   266  		},
   267  	}
   268  }
   269  
   270  func (tx *TokenFeeScheduleUpdateTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   271  	return nil, errors.New("cannot schedule `TokenFeeScheduleUpdateTransaction")
   272  }
   273  
   274  func (tx *TokenFeeScheduleUpdateTransaction) buildProtoBody() *services.TokenFeeScheduleUpdateTransactionBody {
   275  	body := &services.TokenFeeScheduleUpdateTransactionBody{}
   276  	if tx.tokenID != nil {
   277  		body.TokenId = tx.tokenID._ToProtobuf()
   278  	}
   279  
   280  	if len(tx.customFees) > 0 {
   281  		for _, customFee := range tx.customFees {
   282  			if body.CustomFees == nil {
   283  				body.CustomFees = make([]*services.CustomFee, 0)
   284  			}
   285  			body.CustomFees = append(body.CustomFees, customFee._ToProtobuf())
   286  		}
   287  	}
   288  
   289  	return body
   290  }
   291  
   292  func (tx *TokenFeeScheduleUpdateTransaction) getMethod(channel *_Channel) _Method {
   293  	return _Method{
   294  		transaction: channel._GetToken().UpdateTokenFeeSchedule,
   295  	}
   296  }
   297  func (tx *TokenFeeScheduleUpdateTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   298  	return tx.buildScheduled()
   299  }