github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_claim_airdrop_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  type TokenClaimAirdropTransaction struct {
    30  	Transaction
    31  	pendingAirdropIds []*PendingAirdropId
    32  }
    33  
    34  func NewTokenClaimAirdropTransaction() *TokenClaimAirdropTransaction {
    35  	tx := TokenClaimAirdropTransaction{
    36  		Transaction:       _NewTransaction(),
    37  		pendingAirdropIds: make([]*PendingAirdropId, 0),
    38  	}
    39  
    40  	tx._SetDefaultMaxTransactionFee(NewHbar(1))
    41  
    42  	return &tx
    43  }
    44  
    45  func _TokenClaimAirdropTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *TokenClaimAirdropTransaction {
    46  	tokenClaim := &TokenClaimAirdropTransaction{
    47  		Transaction: tx,
    48  	}
    49  
    50  	for _, pendingAirdrops := range pb.GetTokenClaimAirdrop().PendingAirdrops {
    51  		tokenClaim.pendingAirdropIds = append(tokenClaim.pendingAirdropIds, _PendingAirdropIdFromProtobuf(pendingAirdrops))
    52  	}
    53  
    54  	return tokenClaim
    55  }
    56  
    57  // SetPendingAirdropIds sets the pending airdrop IDs for this TokenClaimAirdropTransaction.
    58  func (tx *TokenClaimAirdropTransaction) SetPendingAirdropIds(ids []*PendingAirdropId) *TokenClaimAirdropTransaction {
    59  	tx._RequireNotFrozen()
    60  	tx.pendingAirdropIds = ids
    61  	return tx
    62  }
    63  
    64  // AddPendingAirdropId adds a pending airdrop ID to this TokenClaimAirdropTransaction.
    65  func (tx *TokenClaimAirdropTransaction) AddPendingAirdropId(id PendingAirdropId) *TokenClaimAirdropTransaction {
    66  	tx._RequireNotFrozen()
    67  	tx.pendingAirdropIds = append(tx.pendingAirdropIds, &id)
    68  	return tx
    69  }
    70  
    71  // GetPendingAirdropIds returns the pending airdrop IDs for this TokenClaimAirdropTransaction.
    72  func (tx *TokenClaimAirdropTransaction) GetPendingAirdropIds() []*PendingAirdropId {
    73  	return tx.pendingAirdropIds
    74  }
    75  
    76  // ---- Required Interfaces ---- //
    77  
    78  // Sign uses the provided privateKey to sign the transaction.
    79  func (tx *TokenClaimAirdropTransaction) Sign(privateKey PrivateKey) *TokenClaimAirdropTransaction {
    80  	tx.Transaction.Sign(privateKey)
    81  	return tx
    82  }
    83  
    84  // SignWithOperator signs the transaction with client's operator privateKey.
    85  func (tx *TokenClaimAirdropTransaction) SignWithOperator(client *Client) (*TokenClaimAirdropTransaction, error) {
    86  	_, err := tx.Transaction.signWithOperator(client, tx)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	return tx, nil
    91  }
    92  
    93  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
    94  // with the publicKey as the map key.
    95  func (tx *TokenClaimAirdropTransaction) SignWith(
    96  	publicKey PublicKey,
    97  	signer TransactionSigner,
    98  ) *TokenClaimAirdropTransaction {
    99  	tx.Transaction.SignWith(publicKey, signer)
   100  	return tx
   101  }
   102  
   103  // AddSignature adds a signature to the transaction.
   104  func (tx *TokenClaimAirdropTransaction) AddSignature(publicKey PublicKey, signature []byte) *TokenClaimAirdropTransaction {
   105  	tx.Transaction.AddSignature(publicKey, signature)
   106  	return tx
   107  }
   108  
   109  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
   110  func (tx *TokenClaimAirdropTransaction) SetGrpcDeadline(deadline *time.Duration) *TokenClaimAirdropTransaction {
   111  	tx.Transaction.SetGrpcDeadline(deadline)
   112  	return tx
   113  }
   114  
   115  func (tx *TokenClaimAirdropTransaction) Freeze() (*TokenClaimAirdropTransaction, error) {
   116  	return tx.FreezeWith(nil)
   117  }
   118  
   119  func (tx *TokenClaimAirdropTransaction) FreezeWith(client *Client) (*TokenClaimAirdropTransaction, error) {
   120  	_, err := tx.Transaction.freezeWith(client, tx)
   121  	return tx, err
   122  }
   123  
   124  // SetMaxTransactionFee sets the max transaction fee for this TokenClaimAirdropTransaction.
   125  func (tx *TokenClaimAirdropTransaction) SetMaxTransactionFee(fee Hbar) *TokenClaimAirdropTransaction {
   126  	tx.Transaction.SetMaxTransactionFee(fee)
   127  	return tx
   128  }
   129  
   130  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   131  func (tx *TokenClaimAirdropTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *TokenClaimAirdropTransaction {
   132  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   133  	return tx
   134  }
   135  
   136  // SetTransactionMemo sets the memo for this TokenClaimAirdropTransaction.
   137  func (tx *TokenClaimAirdropTransaction) SetTransactionMemo(memo string) *TokenClaimAirdropTransaction {
   138  	tx.Transaction.SetTransactionMemo(memo)
   139  	return tx
   140  }
   141  
   142  // SetTransactionValidDuration sets the valid duration for this TokenClaimAirdropTransaction.
   143  func (tx *TokenClaimAirdropTransaction) SetTransactionValidDuration(duration time.Duration) *TokenClaimAirdropTransaction {
   144  	tx.Transaction.SetTransactionValidDuration(duration)
   145  	return tx
   146  }
   147  
   148  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   149  func (tx *TokenClaimAirdropTransaction) ToBytes() ([]byte, error) {
   150  	bytes, err := tx.Transaction.toBytes(tx)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  	return bytes, nil
   155  }
   156  
   157  // SetTransactionID sets the TransactionID for this TokenClaimAirdropTransaction.
   158  func (tx *TokenClaimAirdropTransaction) SetTransactionID(transactionID TransactionID) *TokenClaimAirdropTransaction {
   159  	tx.Transaction.SetTransactionID(transactionID)
   160  	return tx
   161  }
   162  
   163  // SetNodeAccountIDs sets the _Node AccountID for this TokenClaimAirdropTransaction.
   164  func (tx *TokenClaimAirdropTransaction) SetNodeAccountIDs(nodeID []AccountID) *TokenClaimAirdropTransaction {
   165  	tx.Transaction.SetNodeAccountIDs(nodeID)
   166  	return tx
   167  }
   168  
   169  // SetMaxRetry sets the max number of errors before execution will fail.
   170  func (tx *TokenClaimAirdropTransaction) SetMaxRetry(count int) *TokenClaimAirdropTransaction {
   171  	tx.Transaction.SetMaxRetry(count)
   172  	return tx
   173  }
   174  
   175  // SetMaxBackoff The maximum amount of time to wait between retries.
   176  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   177  func (tx *TokenClaimAirdropTransaction) SetMaxBackoff(max time.Duration) *TokenClaimAirdropTransaction {
   178  	tx.Transaction.SetMaxBackoff(max)
   179  	return tx
   180  }
   181  
   182  // SetMinBackoff sets the minimum amount of time to wait between retries.
   183  func (tx *TokenClaimAirdropTransaction) SetMinBackoff(min time.Duration) *TokenClaimAirdropTransaction {
   184  	tx.Transaction.SetMinBackoff(min)
   185  	return tx
   186  }
   187  
   188  func (tx *TokenClaimAirdropTransaction) SetLogLevel(level LogLevel) *TokenClaimAirdropTransaction {
   189  	tx.Transaction.SetLogLevel(level)
   190  	return tx
   191  }
   192  
   193  func (tx *TokenClaimAirdropTransaction) Execute(client *Client) (TransactionResponse, error) {
   194  	return tx.Transaction.execute(client, tx)
   195  }
   196  
   197  func (tx *TokenClaimAirdropTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   198  	return tx.Transaction.schedule(tx)
   199  }
   200  
   201  // ----------- Overridden functions ----------------
   202  
   203  func (tx *TokenClaimAirdropTransaction) getName() string {
   204  	return "TokenClaimAirdropTransaction"
   205  }
   206  
   207  func (tx *TokenClaimAirdropTransaction) validateNetworkOnIDs(client *Client) error {
   208  	if client == nil || !client.autoValidateChecksums {
   209  		return nil
   210  	}
   211  
   212  	for _, pendingAirdropId := range tx.pendingAirdropIds {
   213  		if pendingAirdropId.sender != nil {
   214  			if err := pendingAirdropId.sender.ValidateChecksum(client); err != nil {
   215  				return err
   216  			}
   217  		}
   218  
   219  		if pendingAirdropId.receiver != nil {
   220  			if err := pendingAirdropId.receiver.ValidateChecksum(client); err != nil {
   221  				return err
   222  			}
   223  		}
   224  
   225  		if pendingAirdropId.nftID != nil {
   226  			if err := pendingAirdropId.nftID.Validate(client); err != nil {
   227  				return err
   228  			}
   229  		}
   230  
   231  		if pendingAirdropId.tokenID != nil {
   232  			if err := pendingAirdropId.tokenID.ValidateChecksum(client); err != nil {
   233  				return err
   234  			}
   235  		}
   236  	}
   237  	return nil
   238  }
   239  
   240  func (tx *TokenClaimAirdropTransaction) build() *services.TransactionBody {
   241  	return &services.TransactionBody{
   242  		TransactionFee:           tx.transactionFee,
   243  		Memo:                     tx.Transaction.memo,
   244  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   245  		TransactionID:            tx.transactionID._ToProtobuf(),
   246  		Data: &services.TransactionBody_TokenClaimAirdrop{
   247  			TokenClaimAirdrop: tx.buildProtoBody(),
   248  		},
   249  	}
   250  }
   251  
   252  func (tx *TokenClaimAirdropTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   253  	return &services.SchedulableTransactionBody{
   254  		TransactionFee: tx.transactionFee,
   255  		Data: &services.SchedulableTransactionBody_TokenClaimAirdrop{
   256  			TokenClaimAirdrop: tx.buildProtoBody(),
   257  		},
   258  	}, nil
   259  }
   260  
   261  func (tx *TokenClaimAirdropTransaction) buildProtoBody() *services.TokenClaimAirdropTransactionBody {
   262  	pendingAirdrops := make([]*services.PendingAirdropId, len(tx.pendingAirdropIds))
   263  	for i, pendingAirdropId := range tx.pendingAirdropIds {
   264  		pendingAirdrops[i] = pendingAirdropId._ToProtobuf()
   265  	}
   266  
   267  	return &services.TokenClaimAirdropTransactionBody{
   268  		PendingAirdrops: pendingAirdrops,
   269  	}
   270  }
   271  
   272  func (tx *TokenClaimAirdropTransaction) getMethod(channel *_Channel) _Method {
   273  	return _Method{
   274  		transaction: channel._GetToken().ClaimAirdrop,
   275  	}
   276  }
   277  
   278  func (tx *TokenClaimAirdropTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   279  	return tx.buildScheduled()
   280  }