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

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