github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_allowance_approve_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 // AccountAllowanceApproveTransaction 30 // Creates one or more hbar/token approved allowances <b>relative to the owner account specified in the allowances of 31 // tx transaction</b>. Each allowance grants a spender the right to transfer a pre-determined amount of the owner's 32 // hbar/token to any other account of the spender's choice. If the owner is not specified in any allowance, the payer 33 // of transaction is considered to be the owner for that particular allowance. 34 // Setting the amount to zero in CryptoAllowance or TokenAllowance will remove the respective allowance for the spender. 35 // 36 // (So if account <tt>0.0.X</tt> pays for this transaction and owner is not specified in the allowance, 37 // then at consensus each spender account will have new allowances to spend hbar or tokens from <tt>0.0.X</tt>). 38 type AccountAllowanceApproveTransaction struct { 39 Transaction 40 hbarAllowances []*HbarAllowance 41 tokenAllowances []*TokenAllowance 42 nftAllowances []*TokenNftAllowance 43 } 44 45 // NewAccountAllowanceApproveTransaction 46 // Creates an AccountAloowanceApproveTransaction which creates 47 // one or more hbar/token approved allowances relative to the owner account specified in the allowances of 48 // tx transaction. Each allowance grants a spender the right to transfer a pre-determined amount of the owner's 49 // hbar/token to any other account of the spender's choice. If the owner is not specified in any allowance, the payer 50 // of transaction is considered to be the owner for that particular allowance. 51 // Setting the amount to zero in CryptoAllowance or TokenAllowance will remove the respective allowance for the spender. 52 // 53 // (So if account 0.0.X pays for this transaction and owner is not specified in the allowance, 54 // then at consensus each spender account will have new allowances to spend hbar or tokens from 0.0.X). 55 func NewAccountAllowanceApproveTransaction() *AccountAllowanceApproveTransaction { 56 tx := AccountAllowanceApproveTransaction{ 57 Transaction: _NewTransaction(), 58 } 59 tx._SetDefaultMaxTransactionFee(NewHbar(2)) 60 61 return &tx 62 } 63 64 func _AccountAllowanceApproveTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *AccountAllowanceApproveTransaction { 65 accountApproval := make([]*HbarAllowance, 0) 66 tokenApproval := make([]*TokenAllowance, 0) 67 nftApproval := make([]*TokenNftAllowance, 0) 68 69 for _, ap := range pb.GetCryptoApproveAllowance().GetCryptoAllowances() { 70 temp := _HbarAllowanceFromProtobuf(ap) 71 accountApproval = append(accountApproval, &temp) 72 } 73 74 for _, ap := range pb.GetCryptoApproveAllowance().GetTokenAllowances() { 75 temp := _TokenAllowanceFromProtobuf(ap) 76 tokenApproval = append(tokenApproval, &temp) 77 } 78 79 for _, ap := range pb.GetCryptoApproveAllowance().GetNftAllowances() { 80 temp := _TokenNftAllowanceFromProtobuf(ap) 81 nftApproval = append(nftApproval, &temp) 82 } 83 84 return &AccountAllowanceApproveTransaction{ 85 Transaction: tx, 86 hbarAllowances: accountApproval, 87 tokenAllowances: tokenApproval, 88 nftAllowances: nftApproval, 89 } 90 } 91 92 func (tx *AccountAllowanceApproveTransaction) _ApproveHbarApproval(ownerAccountID *AccountID, id AccountID, amount Hbar) *AccountAllowanceApproveTransaction { 93 tx._RequireNotFrozen() 94 tx.hbarAllowances = append(tx.hbarAllowances, &HbarAllowance{ 95 SpenderAccountID: &id, 96 Amount: amount.AsTinybar(), 97 OwnerAccountID: ownerAccountID, 98 }) 99 100 return tx 101 } 102 103 // AddHbarApproval 104 // Deprecated - Use ApproveHbarAllowance instead 105 func (tx *AccountAllowanceApproveTransaction) AddHbarApproval(id AccountID, amount Hbar) *AccountAllowanceApproveTransaction { 106 return tx._ApproveHbarApproval(nil, id, amount) 107 } 108 109 // ApproveHbarApproval 110 // Deprecated - Use ApproveHbarAllowance instead 111 func (tx *AccountAllowanceApproveTransaction) ApproveHbarApproval(ownerAccountID AccountID, id AccountID, amount Hbar) *AccountAllowanceApproveTransaction { 112 return tx._ApproveHbarApproval(&ownerAccountID, id, amount) 113 } 114 115 // ApproveHbarAllowance 116 // Approves allowance of hbar transfers for a spender. 117 func (tx *AccountAllowanceApproveTransaction) ApproveHbarAllowance(ownerAccountID AccountID, id AccountID, amount Hbar) *AccountAllowanceApproveTransaction { 118 return tx._ApproveHbarApproval(&ownerAccountID, id, amount) 119 } 120 121 // List of hbar allowance records 122 func (tx *AccountAllowanceApproveTransaction) GetHbarAllowances() []*HbarAllowance { 123 return tx.hbarAllowances 124 } 125 126 func (tx *AccountAllowanceApproveTransaction) _ApproveTokenApproval(tokenID TokenID, ownerAccountID *AccountID, accountID AccountID, amount int64) *AccountAllowanceApproveTransaction { 127 tx._RequireNotFrozen() 128 tokenApproval := TokenAllowance{ 129 TokenID: &tokenID, 130 SpenderAccountID: &accountID, 131 Amount: amount, 132 OwnerAccountID: ownerAccountID, 133 } 134 135 tx.tokenAllowances = append(tx.tokenAllowances, &tokenApproval) 136 return tx 137 } 138 139 // Deprecated - Use ApproveTokenAllowance instead 140 func (tx *AccountAllowanceApproveTransaction) AddTokenApproval(tokenID TokenID, accountID AccountID, amount int64) *AccountAllowanceApproveTransaction { 141 return tx._ApproveTokenApproval(tokenID, nil, accountID, amount) 142 } 143 144 // ApproveTokenApproval 145 // Deprecated - Use ApproveTokenAllowance instead 146 func (tx *AccountAllowanceApproveTransaction) ApproveTokenApproval(tokenID TokenID, ownerAccountID AccountID, accountID AccountID, amount int64) *AccountAllowanceApproveTransaction { 147 return tx._ApproveTokenApproval(tokenID, &ownerAccountID, accountID, amount) 148 } 149 150 // ApproveTokenAllowance 151 // Approve allowance of fungible token transfers for a spender. 152 func (tx *AccountAllowanceApproveTransaction) ApproveTokenAllowance(tokenID TokenID, ownerAccountID AccountID, accountID AccountID, amount int64) *AccountAllowanceApproveTransaction { 153 return tx._ApproveTokenApproval(tokenID, &ownerAccountID, accountID, amount) 154 } 155 156 // List of token allowance records 157 func (tx *AccountAllowanceApproveTransaction) GetTokenAllowances() []*TokenAllowance { 158 return tx.tokenAllowances 159 } 160 161 func (tx *AccountAllowanceApproveTransaction) _ApproveTokenNftApproval(nftID NftID, ownerAccountID *AccountID, spenderAccountID *AccountID, delegatingSpenderAccountId *AccountID) *AccountAllowanceApproveTransaction { 162 tx._RequireNotFrozen() 163 164 for _, t := range tx.nftAllowances { 165 if t.TokenID.String() == nftID.TokenID.String() { 166 if t.SpenderAccountID.String() == spenderAccountID.String() { 167 b := false 168 for _, s := range t.SerialNumbers { 169 if s == nftID.SerialNumber { 170 b = true 171 } 172 } 173 if !b { 174 t.SerialNumbers = append(t.SerialNumbers, nftID.SerialNumber) 175 } 176 return tx 177 } 178 } 179 } 180 181 tx.nftAllowances = append(tx.nftAllowances, &TokenNftAllowance{ 182 TokenID: &nftID.TokenID, 183 SpenderAccountID: spenderAccountID, 184 SerialNumbers: []int64{nftID.SerialNumber}, 185 AllSerials: false, 186 OwnerAccountID: ownerAccountID, 187 DelegatingSpender: delegatingSpenderAccountId, 188 }) 189 return tx 190 } 191 192 // AddTokenNftApproval 193 // Deprecated - Use ApproveTokenNftAllowance instead 194 func (tx *AccountAllowanceApproveTransaction) AddTokenNftApproval(nftID NftID, accountID AccountID) *AccountAllowanceApproveTransaction { 195 return tx._ApproveTokenNftApproval(nftID, nil, &accountID, nil) 196 } 197 198 // ApproveTokenNftApproval 199 // Deprecated - Use ApproveTokenNftAllowance instead 200 func (tx *AccountAllowanceApproveTransaction) ApproveTokenNftApproval(nftID NftID, ownerAccountID AccountID, accountID AccountID) *AccountAllowanceApproveTransaction { 201 return tx._ApproveTokenNftApproval(nftID, &ownerAccountID, &accountID, nil) 202 } 203 204 func (tx *AccountAllowanceApproveTransaction) ApproveTokenNftAllowanceWithDelegatingSpender(nftID NftID, ownerAccountID AccountID, spenderAccountId AccountID, delegatingSpenderAccountID AccountID) *AccountAllowanceApproveTransaction { 205 tx._RequireNotFrozen() 206 return tx._ApproveTokenNftApproval(nftID, &ownerAccountID, &spenderAccountId, &delegatingSpenderAccountID) 207 } 208 209 // ApproveTokenNftAllowance 210 // Approve allowance of non-fungible token transfers for a spender. 211 func (tx *AccountAllowanceApproveTransaction) ApproveTokenNftAllowance(nftID NftID, ownerAccountID AccountID, accountID AccountID) *AccountAllowanceApproveTransaction { 212 return tx._ApproveTokenNftApproval(nftID, &ownerAccountID, &accountID, nil) 213 } 214 215 func (tx *AccountAllowanceApproveTransaction) _ApproveTokenNftAllowanceAllSerials(tokenID TokenID, ownerAccountID *AccountID, spenderAccount AccountID) *AccountAllowanceApproveTransaction { 216 for _, t := range tx.nftAllowances { 217 if t.TokenID.String() == tokenID.String() { 218 if t.SpenderAccountID.String() == spenderAccount.String() { 219 t.SerialNumbers = []int64{} 220 t.AllSerials = true 221 return tx 222 } 223 } 224 } 225 226 tx.nftAllowances = append(tx.nftAllowances, &TokenNftAllowance{ 227 TokenID: &tokenID, 228 SpenderAccountID: &spenderAccount, 229 SerialNumbers: []int64{}, 230 AllSerials: true, 231 OwnerAccountID: ownerAccountID, 232 }) 233 return tx 234 } 235 236 // AddAllTokenNftApproval 237 // Approve allowance of non-fungible token transfers for a spender. 238 // Spender has access to all of the owner's NFT units of type tokenId (currently 239 // owned and any in the future). 240 func (tx *AccountAllowanceApproveTransaction) AddAllTokenNftApproval(tokenID TokenID, spenderAccount AccountID) *AccountAllowanceApproveTransaction { 241 return tx._ApproveTokenNftAllowanceAllSerials(tokenID, nil, spenderAccount) 242 } 243 244 // ApproveTokenNftAllowanceAllSerials 245 // Approve allowance of non-fungible token transfers for a spender. 246 // Spender has access to all of the owner's NFT units of type tokenId (currently 247 // owned and any in the future). 248 func (tx *AccountAllowanceApproveTransaction) ApproveTokenNftAllowanceAllSerials(tokenID TokenID, ownerAccountID AccountID, spenderAccount AccountID) *AccountAllowanceApproveTransaction { 249 return tx._ApproveTokenNftAllowanceAllSerials(tokenID, &ownerAccountID, spenderAccount) 250 } 251 252 // List of NFT allowance records 253 func (tx *AccountAllowanceApproveTransaction) GetTokenNftAllowances() []*TokenNftAllowance { 254 return tx.nftAllowances 255 } 256 257 // ---- Required Interfaces ---- // 258 259 // Sign uses the provided privateKey to sign the transaction. 260 func (tx *AccountAllowanceApproveTransaction) Sign( 261 privateKey PrivateKey, 262 ) *AccountAllowanceApproveTransaction { 263 tx.Transaction.Sign(privateKey) 264 return tx 265 } 266 267 // SignWithOperator signs the transaction with client's operator privateKey. 268 func (tx *AccountAllowanceApproveTransaction) SignWithOperator( 269 client *Client, 270 ) (*AccountAllowanceApproveTransaction, error) { 271 _, err := tx.Transaction.signWithOperator(client, tx) 272 if err != nil { 273 return nil, err 274 } 275 return tx, nil 276 } 277 278 // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map 279 // with the publicKey as the map key. 280 func (tx *AccountAllowanceApproveTransaction) SignWith( 281 publicKey PublicKey, 282 signer TransactionSigner, 283 ) *AccountAllowanceApproveTransaction { 284 tx.Transaction.SignWith(publicKey, signer) 285 return tx 286 } 287 288 // AddSignature adds a signature to the transaction. 289 func (tx *AccountAllowanceApproveTransaction) AddSignature(publicKey PublicKey, signature []byte) *AccountAllowanceApproveTransaction { 290 tx.Transaction.AddSignature(publicKey, signature) 291 return tx 292 } 293 294 // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.) 295 func (tx *AccountAllowanceApproveTransaction) SetGrpcDeadline(deadline *time.Duration) *AccountAllowanceApproveTransaction { 296 tx.Transaction.SetGrpcDeadline(deadline) 297 return tx 298 } 299 300 func (tx *AccountAllowanceApproveTransaction) Freeze() (*AccountAllowanceApproveTransaction, error) { 301 return tx.FreezeWith(nil) 302 } 303 304 func (tx *AccountAllowanceApproveTransaction) FreezeWith(client *Client) (*AccountAllowanceApproveTransaction, error) { 305 _, err := tx.Transaction.freezeWith(client, tx) 306 return tx, err 307 } 308 309 // GetMaxTransactionFee returns the maximum transaction fee the operator (paying account) is willing to pay. 310 func (tx *AccountAllowanceApproveTransaction) GetMaxTransactionFee() Hbar { 311 return tx.Transaction.GetMaxTransactionFee() 312 } 313 314 // SetMaxTransactionFee sets the maximum transaction fee the operator (paying account) is willing to pay. 315 func (tx *AccountAllowanceApproveTransaction) SetMaxTransactionFee(fee Hbar) *AccountAllowanceApproveTransaction { 316 tx._RequireNotFrozen() 317 tx.Transaction.SetMaxTransactionFee(fee) 318 return tx 319 } 320 321 // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received 322 func (tx *AccountAllowanceApproveTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *AccountAllowanceApproveTransaction { 323 tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID) 324 return tx 325 } 326 327 // SetTransactionMemo sets the memo for this AccountAllowanceApproveTransaction. 328 func (tx *AccountAllowanceApproveTransaction) SetTransactionMemo(memo string) *AccountAllowanceApproveTransaction { 329 tx._RequireNotFrozen() 330 tx.Transaction.SetTransactionMemo(memo) 331 return tx 332 } 333 334 // SetTransactionValidDuration sets the valid duration for this AccountAllowanceApproveTransaction. 335 func (tx *AccountAllowanceApproveTransaction) SetTransactionValidDuration(duration time.Duration) *AccountAllowanceApproveTransaction { 336 tx.Transaction.SetTransactionValidDuration(duration) 337 return tx 338 } 339 340 // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not 341 func (tx *AccountAllowanceApproveTransaction) ToBytes() ([]byte, error) { 342 bytes, err := tx.Transaction.toBytes(tx) 343 if err != nil { 344 return nil, err 345 } 346 return bytes, nil 347 } 348 349 // SetTransactionID sets the TransactionID for this AccountAllowanceApproveTransaction. 350 func (tx *AccountAllowanceApproveTransaction) SetTransactionID(transactionID TransactionID) *AccountAllowanceApproveTransaction { 351 tx.Transaction.SetTransactionID(transactionID) 352 return tx 353 } 354 355 // SetNodeAccountIDs sets the _Node AccountID for this AccountAllowanceApproveTransaction. 356 func (tx *AccountAllowanceApproveTransaction) SetNodeAccountIDs(nodeID []AccountID) *AccountAllowanceApproveTransaction { 357 tx._RequireNotFrozen() 358 tx.Transaction.SetNodeAccountIDs(nodeID) 359 return tx 360 } 361 362 // SetMaxRetry sets the max number of errors before execution will fail. 363 func (tx *AccountAllowanceApproveTransaction) SetMaxRetry(count int) *AccountAllowanceApproveTransaction { 364 tx.Transaction.SetMaxRetry(count) 365 return tx 366 } 367 368 // SetMaxBackoff The maximum amount of time to wait between retries. 369 // Every retry attempt will increase the wait time exponentially until it reaches this time. 370 func (tx *AccountAllowanceApproveTransaction) SetMaxBackoff(max time.Duration) *AccountAllowanceApproveTransaction { 371 tx.Transaction.SetMaxBackoff(max) 372 return tx 373 } 374 375 // SetMinBackoff sets the min back off for this AccountAllowanceApproveTransaction. 376 func (tx *AccountAllowanceApproveTransaction) SetMinBackoff(min time.Duration) *AccountAllowanceApproveTransaction { 377 tx.Transaction.SetMinBackoff(min) 378 return tx 379 } 380 381 func (tx *AccountAllowanceApproveTransaction) Execute(client *Client) (TransactionResponse, error) { 382 return tx.Transaction.execute(client, tx) 383 } 384 385 func (tx *AccountAllowanceApproveTransaction) Schedule() (*ScheduleCreateTransaction, error) { 386 return tx.Transaction.schedule(tx) 387 } 388 389 // ----------- Overridden functions ---------------- 390 391 func (tx *AccountAllowanceApproveTransaction) getName() string { 392 return "AccountAllowanceApproveTransaction" 393 } 394 func (tx *AccountAllowanceApproveTransaction) validateNetworkOnIDs(client *Client) error { 395 if client == nil || !client.autoValidateChecksums { 396 return nil 397 } 398 399 for _, ap := range tx.hbarAllowances { 400 if ap.SpenderAccountID != nil { 401 if err := ap.SpenderAccountID.ValidateChecksum(client); err != nil { 402 return err 403 } 404 } 405 406 if ap.OwnerAccountID != nil { 407 if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil { 408 return err 409 } 410 } 411 } 412 413 for _, ap := range tx.tokenAllowances { 414 if ap.SpenderAccountID != nil { 415 if err := ap.SpenderAccountID.ValidateChecksum(client); err != nil { 416 return err 417 } 418 } 419 420 if ap.TokenID != nil { 421 if err := ap.TokenID.ValidateChecksum(client); err != nil { 422 return err 423 } 424 } 425 426 if ap.OwnerAccountID != nil { 427 if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil { 428 return err 429 } 430 } 431 } 432 433 for _, ap := range tx.nftAllowances { 434 if ap.SpenderAccountID != nil { 435 if err := ap.SpenderAccountID.ValidateChecksum(client); err != nil { 436 return err 437 } 438 } 439 440 if ap.TokenID != nil { 441 if err := ap.TokenID.ValidateChecksum(client); err != nil { 442 return err 443 } 444 } 445 446 if ap.OwnerAccountID != nil { 447 if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil { 448 return err 449 } 450 } 451 } 452 453 return nil 454 } 455 456 func (tx *AccountAllowanceApproveTransaction) build() *services.TransactionBody { 457 return &services.TransactionBody{ 458 TransactionID: tx.transactionID._ToProtobuf(), 459 TransactionFee: tx.transactionFee, 460 TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()), 461 Memo: tx.Transaction.memo, 462 Data: &services.TransactionBody_CryptoApproveAllowance{ 463 CryptoApproveAllowance: tx.buildProtoBody(), 464 }, 465 } 466 } 467 468 func (tx *AccountAllowanceApproveTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) { 469 return &services.SchedulableTransactionBody{ 470 TransactionFee: tx.transactionFee, 471 Memo: tx.Transaction.memo, 472 Data: &services.SchedulableTransactionBody_CryptoApproveAllowance{ 473 CryptoApproveAllowance: tx.buildProtoBody(), 474 }, 475 }, nil 476 } 477 478 func (tx *AccountAllowanceApproveTransaction) buildProtoBody() *services.CryptoApproveAllowanceTransactionBody { 479 body := &services.CryptoApproveAllowanceTransactionBody{ 480 CryptoAllowances: make([]*services.CryptoAllowance, 0), 481 TokenAllowances: make([]*services.TokenAllowance, 0), 482 NftAllowances: make([]*services.NftAllowance, 0), 483 } 484 485 for _, ap := range tx.hbarAllowances { 486 body.CryptoAllowances = append(body.CryptoAllowances, ap._ToProtobuf()) 487 } 488 489 for _, ap := range tx.tokenAllowances { 490 body.TokenAllowances = append(body.TokenAllowances, ap._ToProtobuf()) 491 } 492 493 for _, ap := range tx.nftAllowances { 494 body.NftAllowances = append(body.NftAllowances, ap._ToProtobuf()) 495 } 496 497 return body 498 } 499 500 func (tx *AccountAllowanceApproveTransaction) getMethod(channel *_Channel) _Method { 501 return _Method{ 502 transaction: channel._GetCrypto().ApproveAllowances, 503 } 504 } 505 506 func (tx *AccountAllowanceApproveTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) { 507 return tx.buildScheduled() 508 }