github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_allowance_delete_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  // AccountAllowanceDeleteTransaction
    30  // Deletes one or more non-fungible approved allowances from an owner's account. This operation
    31  // will remove the allowances granted to one or more specific non-fungible token serial numbers. Each owner account
    32  // listed as wiping an allowance must sign the transaction. Hbar and fungible token allowances
    33  // can be removed by setting the amount to zero in CryptoApproveAllowance.
    34  type AccountAllowanceDeleteTransaction struct {
    35  	Transaction
    36  	hbarWipe  []*HbarAllowance
    37  	tokenWipe []*TokenAllowance
    38  	nftWipe   []*TokenNftAllowance
    39  }
    40  
    41  // NewAccountAllowanceDeleteTransaction
    42  // Creates AccountAllowanceDeleteTransaction whoch deletes one or more non-fungible approved allowances from an owner's account. This operation
    43  // will remove the allowances granted to one or more specific non-fungible token serial numbers. Each owner account
    44  // listed as wiping an allowance must sign the transaction. Hbar and fungible token allowances
    45  // can be removed by setting the amount to zero in CryptoApproveAllowance.
    46  func NewAccountAllowanceDeleteTransaction() *AccountAllowanceDeleteTransaction {
    47  	tx := AccountAllowanceDeleteTransaction{
    48  		Transaction: _NewTransaction(),
    49  	}
    50  	tx._SetDefaultMaxTransactionFee(NewHbar(2))
    51  
    52  	return &tx
    53  }
    54  
    55  func _AccountAllowanceDeleteTransactionFromProtobuf(transaction Transaction, pb *services.TransactionBody) *AccountAllowanceDeleteTransaction {
    56  	nftWipe := make([]*TokenNftAllowance, 0)
    57  
    58  	for _, ap := range pb.GetCryptoDeleteAllowance().GetNftAllowances() {
    59  		temp := _TokenNftWipeAllowanceProtobuf(ap)
    60  		nftWipe = append(nftWipe, &temp)
    61  	}
    62  
    63  	return &AccountAllowanceDeleteTransaction{
    64  		Transaction: transaction,
    65  		nftWipe:     nftWipe,
    66  	}
    67  }
    68  
    69  // Deprecated
    70  func (tx *AccountAllowanceDeleteTransaction) DeleteAllHbarAllowances(ownerAccountID *AccountID) *AccountAllowanceDeleteTransaction {
    71  	tx._RequireNotFrozen()
    72  	tx.hbarWipe = append(tx.hbarWipe, &HbarAllowance{
    73  		OwnerAccountID: ownerAccountID,
    74  	})
    75  
    76  	return tx
    77  }
    78  
    79  // Deprecated
    80  func (tx *AccountAllowanceDeleteTransaction) GetAllHbarDeleteAllowances() []*HbarAllowance {
    81  	return tx.hbarWipe
    82  }
    83  
    84  // Deprecated
    85  func (tx *AccountAllowanceDeleteTransaction) DeleteAllTokenAllowances(tokenID TokenID, ownerAccountID *AccountID) *AccountAllowanceDeleteTransaction {
    86  	tx._RequireNotFrozen()
    87  	tokenApproval := TokenAllowance{
    88  		TokenID:        &tokenID,
    89  		OwnerAccountID: ownerAccountID,
    90  	}
    91  
    92  	tx.tokenWipe = append(tx.tokenWipe, &tokenApproval)
    93  	return tx
    94  }
    95  
    96  // Deprecated
    97  func (tx *AccountAllowanceDeleteTransaction) GetAllTokenDeleteAllowances() []*TokenAllowance {
    98  	return tx.tokenWipe
    99  }
   100  
   101  // DeleteAllTokenNftAllowances
   102  // The non-fungible token allowance/allowances to remove.
   103  func (tx *AccountAllowanceDeleteTransaction) DeleteAllTokenNftAllowances(nftID NftID, ownerAccountID *AccountID) *AccountAllowanceDeleteTransaction {
   104  	tx._RequireNotFrozen()
   105  
   106  	for _, t := range tx.nftWipe {
   107  		if t.TokenID.String() == nftID.TokenID.String() {
   108  			if t.OwnerAccountID.String() == ownerAccountID.String() {
   109  				b := false
   110  				for _, s := range t.SerialNumbers {
   111  					if s == nftID.SerialNumber {
   112  						b = true
   113  					}
   114  				}
   115  				if !b {
   116  					t.SerialNumbers = append(t.SerialNumbers, nftID.SerialNumber)
   117  				}
   118  				return tx
   119  			}
   120  		}
   121  	}
   122  
   123  	tx.nftWipe = append(tx.nftWipe, &TokenNftAllowance{
   124  		TokenID:        &nftID.TokenID,
   125  		OwnerAccountID: ownerAccountID,
   126  		SerialNumbers:  []int64{nftID.SerialNumber},
   127  		AllSerials:     false,
   128  	})
   129  	return tx
   130  }
   131  
   132  // GetAllTokenNftDeleteAllowances
   133  // Get the non-fungible token allowance/allowances that will be removed.
   134  func (tx *AccountAllowanceDeleteTransaction) GetAllTokenNftDeleteAllowances() []*TokenNftAllowance {
   135  	return tx.nftWipe
   136  }
   137  
   138  // ---- Required Interfaces ---- //
   139  
   140  // Sign uses the provided privateKey to sign the transaction.
   141  func (tx *AccountAllowanceDeleteTransaction) Sign(
   142  	privateKey PrivateKey,
   143  ) *AccountAllowanceDeleteTransaction {
   144  	tx.Transaction.Sign(privateKey)
   145  	return tx
   146  }
   147  
   148  // SignWithOperator signs the transaction with client's operator privateKey.
   149  func (tx *AccountAllowanceDeleteTransaction) SignWithOperator(
   150  	client *Client,
   151  ) (*AccountAllowanceDeleteTransaction, error) {
   152  	// If the transaction is not signed by the _Operator, we need
   153  	// to sign the transaction with the _Operator
   154  	_, err := tx.Transaction.signWithOperator(client, tx)
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  	return tx, nil
   159  }
   160  
   161  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   162  // with the publicKey as the map key.
   163  func (tx *AccountAllowanceDeleteTransaction) SignWith(
   164  	publicKey PublicKey,
   165  	signer TransactionSigner,
   166  ) *AccountAllowanceDeleteTransaction {
   167  	tx.Transaction.SignWith(publicKey, signer)
   168  	return tx
   169  }
   170  
   171  // AddSignature adds a signature to the transaction.
   172  func (tx *AccountAllowanceDeleteTransaction) AddSignature(publicKey PublicKey, signature []byte) *AccountAllowanceDeleteTransaction {
   173  	tx.Transaction.AddSignature(publicKey, signature)
   174  	return tx
   175  }
   176  
   177  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
   178  func (tx *AccountAllowanceDeleteTransaction) SetGrpcDeadline(deadline *time.Duration) *AccountAllowanceDeleteTransaction {
   179  	tx.Transaction.SetGrpcDeadline(deadline)
   180  	return tx
   181  }
   182  
   183  func (tx *AccountAllowanceDeleteTransaction) Freeze() (*AccountAllowanceDeleteTransaction, error) {
   184  	return tx.FreezeWith(nil)
   185  }
   186  
   187  func (tx *AccountAllowanceDeleteTransaction) FreezeWith(client *Client) (*AccountAllowanceDeleteTransaction, error) {
   188  	_, err := tx.Transaction.freezeWith(client, tx)
   189  	return tx, err
   190  }
   191  
   192  // SetMaxTransactionFee sets the max transaction fee for this AccountAllowanceDeleteTransaction.
   193  func (tx *AccountAllowanceDeleteTransaction) SetMaxTransactionFee(fee Hbar) *AccountAllowanceDeleteTransaction {
   194  	tx._RequireNotFrozen()
   195  	tx.Transaction.SetMaxTransactionFee(fee)
   196  	return tx
   197  }
   198  
   199  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   200  func (tx *AccountAllowanceDeleteTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *AccountAllowanceDeleteTransaction {
   201  	tx._RequireNotFrozen()
   202  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   203  	return tx
   204  }
   205  
   206  // SetTransactionMemo sets the memo for this AccountAllowanceDeleteTransaction.
   207  func (tx *AccountAllowanceDeleteTransaction) SetTransactionMemo(memo string) *AccountAllowanceDeleteTransaction {
   208  	tx.Transaction.SetTransactionMemo(memo)
   209  	return tx
   210  }
   211  
   212  // SetTransactionValidDuration sets the valid duration for this AccountAllowanceDeleteTransaction.
   213  func (tx *AccountAllowanceDeleteTransaction) SetTransactionValidDuration(duration time.Duration) *AccountAllowanceDeleteTransaction {
   214  	tx.Transaction.SetTransactionValidDuration(duration)
   215  	return tx
   216  }
   217  
   218  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   219  func (tx *AccountAllowanceDeleteTransaction) ToBytes() ([]byte, error) {
   220  	bytes, err := tx.Transaction.toBytes(tx)
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  	return bytes, nil
   225  }
   226  
   227  // SetTransactionID sets the TransactionID for this AccountAllowanceDeleteTransaction.
   228  func (tx *AccountAllowanceDeleteTransaction) SetTransactionID(transactionID TransactionID) *AccountAllowanceDeleteTransaction {
   229  	tx._RequireNotFrozen()
   230  
   231  	tx.Transaction.SetTransactionID(transactionID)
   232  	return tx
   233  }
   234  
   235  // SetNodeAccountIDs sets the _Node AccountID for this AccountAllowanceDeleteTransaction.
   236  func (tx *AccountAllowanceDeleteTransaction) SetNodeAccountIDs(nodeID []AccountID) *AccountAllowanceDeleteTransaction {
   237  	tx._RequireNotFrozen()
   238  	tx.Transaction.SetNodeAccountIDs(nodeID)
   239  	return tx
   240  }
   241  
   242  // SetMaxRetry sets the max number of errors before execution will fail.
   243  func (tx *AccountAllowanceDeleteTransaction) SetMaxRetry(count int) *AccountAllowanceDeleteTransaction {
   244  	tx.Transaction.SetMaxRetry(count)
   245  	return tx
   246  }
   247  
   248  // SetMaxBackoff The maximum amount of time to wait between retries.
   249  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   250  func (tx *AccountAllowanceDeleteTransaction) SetMaxBackoff(max time.Duration) *AccountAllowanceDeleteTransaction {
   251  	tx.Transaction.SetMaxBackoff(max)
   252  	return tx
   253  }
   254  
   255  // SetMinBackoff sets the min back off for this AccountAllowanceDeleteTransaction.
   256  func (tx *AccountAllowanceDeleteTransaction) SetMinBackoff(min time.Duration) *AccountAllowanceDeleteTransaction {
   257  	tx.Transaction.SetMinBackoff(min)
   258  	return tx
   259  }
   260  
   261  func (tx *AccountAllowanceDeleteTransaction) Execute(client *Client) (TransactionResponse, error) {
   262  	return tx.Transaction.execute(client, tx)
   263  }
   264  
   265  func (tx *AccountAllowanceDeleteTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   266  	return tx.Transaction.schedule(tx)
   267  }
   268  
   269  // ----------- Overridden functions ----------------
   270  
   271  func (tx *AccountAllowanceDeleteTransaction) getName() string {
   272  	return "AccountAllowanceDeleteTransaction"
   273  }
   274  
   275  func (tx *AccountAllowanceDeleteTransaction) validateNetworkOnIDs(client *Client) error {
   276  	if client == nil || !client.autoValidateChecksums {
   277  		return nil
   278  	}
   279  
   280  	for _, ap := range tx.nftWipe {
   281  		if ap.TokenID != nil {
   282  			if err := ap.TokenID.ValidateChecksum(client); err != nil {
   283  				return err
   284  			}
   285  		}
   286  
   287  		if ap.OwnerAccountID != nil {
   288  			if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil {
   289  				return err
   290  			}
   291  		}
   292  	}
   293  
   294  	return nil
   295  }
   296  
   297  func (tx *AccountAllowanceDeleteTransaction) build() *services.TransactionBody {
   298  	return &services.TransactionBody{
   299  		TransactionID:            tx.transactionID._ToProtobuf(),
   300  		TransactionFee:           tx.transactionFee,
   301  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   302  		Memo:                     tx.Transaction.memo,
   303  		Data: &services.TransactionBody_CryptoDeleteAllowance{
   304  			CryptoDeleteAllowance: tx.buildProtoBody(),
   305  		},
   306  	}
   307  }
   308  
   309  func (tx *AccountAllowanceDeleteTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   310  	return &services.SchedulableTransactionBody{
   311  		TransactionFee: tx.transactionFee,
   312  		Memo:           tx.Transaction.memo,
   313  		Data: &services.SchedulableTransactionBody_CryptoDeleteAllowance{
   314  			CryptoDeleteAllowance: tx.buildProtoBody(),
   315  		},
   316  	}, nil
   317  }
   318  
   319  func (tx *AccountAllowanceDeleteTransaction) buildProtoBody() *services.CryptoDeleteAllowanceTransactionBody {
   320  	body := &services.CryptoDeleteAllowanceTransactionBody{}
   321  	nftWipe := make([]*services.NftRemoveAllowance, 0)
   322  
   323  	for _, ap := range tx.nftWipe {
   324  		nftWipe = append(nftWipe, ap._ToWipeProtobuf())
   325  	}
   326  
   327  	body.NftAllowances = nftWipe
   328  	return body
   329  }
   330  
   331  func (tx *AccountAllowanceDeleteTransaction) getMethod(channel *_Channel) _Method {
   332  	return _Method{
   333  		transaction: channel._GetCrypto().DeleteAllowances,
   334  	}
   335  }
   336  
   337  func (this *AccountAllowanceDeleteTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   338  	return this.buildScheduled()
   339  }