github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_reject_transaction.go (about)

     1  package hedera
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/hashgraph/hedera-protobufs-go/services"
     7  )
     8  
     9  /*-
    10   *
    11   * Hedera Go SDK
    12   *
    13   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    14   *
    15   * Licensed under the Apache License, Version 2.0 (the "License");
    16   * you may not use this file except in compliance with the License.
    17   * You may obtain a copy of the License at
    18   *
    19   *      http://www.apache.org/licenses/LICENSE-2.0
    20   *
    21   * Unless required by applicable law or agreed to in writing, software
    22   * distributed under the License is distributed on an "AS IS" BASIS,
    23   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    24   * See the License for the specific language governing permissions and
    25   * limitations under the License.
    26   *
    27   */
    28  
    29  /**
    30   * A transaction body to "reject" undesired tokens.<br/>
    31   * This transaction will transfer one or more tokens or token
    32   * balances held by the requesting account to the treasury
    33   * for each token type.
    34   * <p>
    35   * Each transfer MUST be one of the following:
    36   * <ul>
    37   *   <li>A single non-fungible/unique token.</li>
    38   *   <li>The full balance held for a fungible/common
    39   *       token type.</li>
    40   * </ul>
    41   * When complete, the requesting account SHALL NOT hold the
    42   * rejected tokens.<br/>
    43   * Custom fees and royalties defined for the tokens rejected
    44   * SHALL NOT be charged for this transaction.
    45   */
    46  type TokenRejectTransaction struct {
    47  	Transaction
    48  	ownerID  *AccountID
    49  	tokenIDs []TokenID
    50  	nftIDs   []NftID
    51  }
    52  
    53  func NewTokenRejectTransaction() *TokenRejectTransaction {
    54  	tx := TokenRejectTransaction{
    55  		Transaction: _NewTransaction(),
    56  	}
    57  	return &tx
    58  }
    59  
    60  func _TokenRejectTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *TokenRejectTransaction {
    61  	rejectTransaction := &TokenRejectTransaction{
    62  		Transaction: tx,
    63  		ownerID:     _AccountIDFromProtobuf(pb.GetTokenReject().Owner),
    64  	}
    65  
    66  	for _, rejection := range pb.GetTokenReject().Rejections {
    67  		if rejection.GetFungibleToken() != nil {
    68  			rejectTransaction.AddTokenID(*_TokenIDFromProtobuf(rejection.GetFungibleToken()))
    69  		} else if rejection.GetNft() != nil {
    70  			rejectTransaction.AddNftID(_NftIDFromProtobuf(rejection.GetNft()))
    71  		}
    72  	}
    73  
    74  	return rejectTransaction
    75  }
    76  
    77  // SetOwnerID Sets the account which owns the tokens to be rejected
    78  func (tx *TokenRejectTransaction) SetOwnerID(ownerID AccountID) *TokenRejectTransaction {
    79  	tx._RequireNotFrozen()
    80  	tx.ownerID = &ownerID
    81  	return tx
    82  }
    83  
    84  // GetOwnerID Gets the account which owns the tokens to be rejected
    85  func (tx *TokenRejectTransaction) GetOwnerID() AccountID {
    86  	if tx.ownerID == nil {
    87  		return AccountID{}
    88  	}
    89  	return *tx.ownerID
    90  }
    91  
    92  // SetTokenIDs Sets the tokens to be rejected
    93  func (tx *TokenRejectTransaction) SetTokenIDs(ids ...TokenID) *TokenRejectTransaction {
    94  	tx._RequireNotFrozen()
    95  	tx.tokenIDs = make([]TokenID, len(ids))
    96  	copy(tx.tokenIDs, ids)
    97  
    98  	return tx
    99  }
   100  
   101  // AddTokenID Adds a token to be rejected
   102  func (tx *TokenRejectTransaction) AddTokenID(id TokenID) *TokenRejectTransaction {
   103  	tx._RequireNotFrozen()
   104  	tx.tokenIDs = append(tx.tokenIDs, id)
   105  	return tx
   106  }
   107  
   108  // GetTokenIDs Gets the tokens to be rejected
   109  func (tx *TokenRejectTransaction) GetTokenIDs() []TokenID {
   110  	return tx.tokenIDs
   111  }
   112  
   113  // SetNftIDs Sets the NFTs to be rejected
   114  func (tx *TokenRejectTransaction) SetNftIDs(ids ...NftID) *TokenRejectTransaction {
   115  	tx._RequireNotFrozen()
   116  	tx.nftIDs = make([]NftID, len(ids))
   117  	copy(tx.nftIDs, ids)
   118  
   119  	return tx
   120  }
   121  
   122  // AddNftID Adds an NFT to be rejected
   123  func (tx *TokenRejectTransaction) AddNftID(id NftID) *TokenRejectTransaction {
   124  	tx._RequireNotFrozen()
   125  	tx.nftIDs = append(tx.nftIDs, id)
   126  	return tx
   127  }
   128  
   129  // GetNftIDs Gets the NFTs to be rejected
   130  func (tx *TokenRejectTransaction) GetNftIDs() []NftID {
   131  	return tx.nftIDs
   132  }
   133  
   134  // ---- Required Interfaces ---- //
   135  
   136  // Sign uses the provided privateKey to sign the transaction.
   137  func (tx *TokenRejectTransaction) Sign(privateKey PrivateKey) *TokenRejectTransaction {
   138  	tx.Transaction.Sign(privateKey)
   139  	return tx
   140  }
   141  
   142  // SignWithOperator signs the transaction with client's operator privateKey.
   143  func (tx *TokenRejectTransaction) SignWithOperator(client *Client) (*TokenRejectTransaction, error) {
   144  	_, err := tx.Transaction.signWithOperator(client, tx)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	return tx, nil
   149  }
   150  
   151  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   152  // with the publicKey as the map key.
   153  func (tx *TokenRejectTransaction) SignWith(
   154  	publicKey PublicKey,
   155  	signer TransactionSigner,
   156  ) *TokenRejectTransaction {
   157  	tx.Transaction.SignWith(publicKey, signer)
   158  	return tx
   159  }
   160  
   161  // AddSignature adds a signature to the transaction.
   162  func (tx *TokenRejectTransaction) AddSignature(publicKey PublicKey, signature []byte) *TokenRejectTransaction {
   163  	tx.Transaction.AddSignature(publicKey, signature)
   164  	return tx
   165  }
   166  
   167  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
   168  func (tx *TokenRejectTransaction) SetGrpcDeadline(deadline *time.Duration) *TokenRejectTransaction {
   169  	tx.Transaction.SetGrpcDeadline(deadline)
   170  	return tx
   171  }
   172  
   173  func (tx *TokenRejectTransaction) Freeze() (*TokenRejectTransaction, error) {
   174  	return tx.FreezeWith(nil)
   175  }
   176  
   177  func (tx *TokenRejectTransaction) FreezeWith(client *Client) (*TokenRejectTransaction, error) {
   178  	_, err := tx.Transaction.freezeWith(client, tx)
   179  	return tx, err
   180  }
   181  
   182  // SetMaxTransactionFee sets the max transaction fee for this TokenRejectTransaction.
   183  func (tx *TokenRejectTransaction) SetMaxTransactionFee(fee Hbar) *TokenRejectTransaction {
   184  	tx.Transaction.SetMaxTransactionFee(fee)
   185  	return tx
   186  }
   187  
   188  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   189  func (tx *TokenRejectTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *TokenRejectTransaction {
   190  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   191  	return tx
   192  }
   193  
   194  // SetTransactionMemo sets the memo for this TokenRejectTransaction.
   195  func (tx *TokenRejectTransaction) SetTransactionMemo(memo string) *TokenRejectTransaction {
   196  	tx.Transaction.SetTransactionMemo(memo)
   197  	return tx
   198  }
   199  
   200  // SetTransactionValidDuration sets the valid duration for this TokenRejectTransaction.
   201  func (tx *TokenRejectTransaction) SetTransactionValidDuration(duration time.Duration) *TokenRejectTransaction {
   202  	tx.Transaction.SetTransactionValidDuration(duration)
   203  	return tx
   204  }
   205  
   206  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   207  func (tx *TokenRejectTransaction) ToBytes() ([]byte, error) {
   208  	bytes, err := tx.Transaction.toBytes(tx)
   209  	if err != nil {
   210  		return nil, err
   211  	}
   212  	return bytes, nil
   213  }
   214  
   215  // SetTransactionID sets the TransactionID for this TokenRejectTransaction.
   216  func (tx *TokenRejectTransaction) SetTransactionID(transactionID TransactionID) *TokenRejectTransaction {
   217  	tx.Transaction.SetTransactionID(transactionID)
   218  	return tx
   219  }
   220  
   221  // SetNodeAccountIDs sets the _Node AccountID for this TokenRejectTransaction.
   222  func (tx *TokenRejectTransaction) SetNodeAccountIDs(nodeID []AccountID) *TokenRejectTransaction {
   223  	tx.Transaction.SetNodeAccountIDs(nodeID)
   224  	return tx
   225  }
   226  
   227  // SetMaxRetry sets the max number of errors before execution will fail.
   228  func (tx *TokenRejectTransaction) SetMaxRetry(count int) *TokenRejectTransaction {
   229  	tx.Transaction.SetMaxRetry(count)
   230  	return tx
   231  }
   232  
   233  // SetMaxBackoff The maximum amount of time to wait between retries.
   234  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   235  func (tx *TokenRejectTransaction) SetMaxBackoff(max time.Duration) *TokenRejectTransaction {
   236  	tx.Transaction.SetMaxBackoff(max)
   237  	return tx
   238  }
   239  
   240  // SetMinBackoff sets the minimum amount of time to wait between retries.
   241  func (tx *TokenRejectTransaction) SetMinBackoff(min time.Duration) *TokenRejectTransaction {
   242  	tx.Transaction.SetMinBackoff(min)
   243  	return tx
   244  }
   245  
   246  func (tx *TokenRejectTransaction) SetLogLevel(level LogLevel) *TokenRejectTransaction {
   247  	tx.Transaction.SetLogLevel(level)
   248  	return tx
   249  }
   250  
   251  func (tx *TokenRejectTransaction) Execute(client *Client) (TransactionResponse, error) {
   252  	return tx.Transaction.execute(client, tx)
   253  }
   254  
   255  func (tx *TokenRejectTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   256  	return tx.Transaction.schedule(tx)
   257  }
   258  
   259  // ----------- Overridden functions ----------------
   260  
   261  func (tx *TokenRejectTransaction) getName() string {
   262  	return "TokenRejectTransaction"
   263  }
   264  
   265  func (tx *TokenRejectTransaction) validateNetworkOnIDs(client *Client) error {
   266  	if client == nil || !client.autoValidateChecksums {
   267  		return nil
   268  	}
   269  
   270  	if tx.ownerID != nil {
   271  		if err := tx.ownerID.ValidateChecksum(client); err != nil {
   272  			return err
   273  		}
   274  	}
   275  
   276  	for _, tokenID := range tx.tokenIDs {
   277  		if err := tokenID.ValidateChecksum(client); err != nil {
   278  			return err
   279  		}
   280  	}
   281  
   282  	for _, nftID := range tx.nftIDs {
   283  		if err := nftID.TokenID.ValidateChecksum(client); err != nil {
   284  			return err
   285  		}
   286  	}
   287  
   288  	return nil
   289  }
   290  
   291  func (tx *TokenRejectTransaction) build() *services.TransactionBody {
   292  	body := tx.buildProtoBody()
   293  
   294  	return &services.TransactionBody{
   295  		TransactionFee:           tx.transactionFee,
   296  		Memo:                     tx.Transaction.memo,
   297  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   298  		TransactionID:            tx.transactionID._ToProtobuf(),
   299  		Data: &services.TransactionBody_TokenReject{
   300  			TokenReject: body,
   301  		},
   302  	}
   303  }
   304  
   305  func (tx *TokenRejectTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   306  	return &services.SchedulableTransactionBody{
   307  		TransactionFee: tx.transactionFee,
   308  		Memo:           tx.Transaction.memo,
   309  		Data: &services.SchedulableTransactionBody_TokenReject{
   310  			TokenReject: tx.buildProtoBody(),
   311  		},
   312  	}, nil
   313  }
   314  
   315  func (tx *TokenRejectTransaction) buildProtoBody() *services.TokenRejectTransactionBody {
   316  	body := &services.TokenRejectTransactionBody{}
   317  
   318  	if tx.ownerID != nil {
   319  		body.Owner = tx.ownerID._ToProtobuf()
   320  	}
   321  
   322  	for _, tokenID := range tx.tokenIDs {
   323  		tokenReference := &services.TokenReference_FungibleToken{
   324  			FungibleToken: tokenID._ToProtobuf(),
   325  		}
   326  
   327  		body.Rejections = append(body.Rejections, &services.TokenReference{
   328  			TokenIdentifier: tokenReference,
   329  		})
   330  	}
   331  
   332  	for _, nftID := range tx.nftIDs {
   333  		tokenReference := &services.TokenReference_Nft{
   334  			Nft: nftID._ToProtobuf(),
   335  		}
   336  
   337  		body.Rejections = append(body.Rejections, &services.TokenReference{
   338  			TokenIdentifier: tokenReference,
   339  		})
   340  	}
   341  
   342  	return body
   343  }
   344  
   345  func (tx *TokenRejectTransaction) getMethod(channel *_Channel) _Method {
   346  	return _Method{
   347  		transaction: channel._GetToken().RejectToken,
   348  	}
   349  }
   350  
   351  func (tx *TokenRejectTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   352  	return tx.buildScheduled()
   353  }