github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_allowance_adjust_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 // Deprecated 30 type AccountAllowanceAdjustTransaction struct { 31 Transaction 32 hbarAllowances []*HbarAllowance 33 tokenAllowances []*TokenAllowance 34 nftAllowances []*TokenNftAllowance 35 } 36 37 func NewAccountAllowanceAdjustTransaction() *AccountAllowanceAdjustTransaction { 38 tx := AccountAllowanceAdjustTransaction{ 39 Transaction: _NewTransaction(), 40 } 41 tx._SetDefaultMaxTransactionFee(NewHbar(2)) 42 43 return &tx 44 } 45 46 func (tx *AccountAllowanceAdjustTransaction) _AdjustHbarAllowance(ownerAccountID *AccountID, id AccountID, amount Hbar) *AccountAllowanceAdjustTransaction { 47 tx._RequireNotFrozen() 48 tx.hbarAllowances = append(tx.hbarAllowances, &HbarAllowance{ 49 SpenderAccountID: &id, 50 OwnerAccountID: ownerAccountID, 51 Amount: amount.AsTinybar(), 52 }) 53 54 return tx 55 } 56 57 // Deprecated 58 func (tx *AccountAllowanceAdjustTransaction) AddHbarAllowance(id AccountID, amount Hbar) *AccountAllowanceAdjustTransaction { 59 return tx._AdjustHbarAllowance(nil, id, amount) 60 } 61 62 // Deprecated 63 func (tx *AccountAllowanceAdjustTransaction) GrantHbarAllowance(ownerAccountID AccountID, id AccountID, amount Hbar) *AccountAllowanceAdjustTransaction { 64 return tx._AdjustHbarAllowance(&ownerAccountID, id, amount) 65 } 66 67 // Deprecated 68 func (tx *AccountAllowanceAdjustTransaction) RevokeHbarAllowance(ownerAccountID AccountID, id AccountID, amount Hbar) *AccountAllowanceAdjustTransaction { 69 return tx._AdjustHbarAllowance(&ownerAccountID, id, amount.Negated()) 70 } 71 72 // Deprecated 73 func (tx *AccountAllowanceAdjustTransaction) GetHbarAllowances() []*HbarAllowance { 74 return tx.hbarAllowances 75 } 76 77 func (tx *AccountAllowanceAdjustTransaction) _AdjustTokenAllowance(tokenID TokenID, ownerAccountID *AccountID, accountID AccountID, amount int64) *AccountAllowanceAdjustTransaction { 78 tx._RequireNotFrozen() 79 tokenApproval := TokenAllowance{ 80 TokenID: &tokenID, 81 SpenderAccountID: &accountID, 82 OwnerAccountID: ownerAccountID, 83 Amount: amount, 84 } 85 86 tx.tokenAllowances = append(tx.tokenAllowances, &tokenApproval) 87 return tx 88 } 89 90 // Deprecated 91 func (tx *AccountAllowanceAdjustTransaction) AddTokenAllowance(tokenID TokenID, accountID AccountID, amount int64) *AccountAllowanceAdjustTransaction { 92 return tx._AdjustTokenAllowance(tokenID, nil, accountID, amount) 93 } 94 95 // Deprecated 96 func (tx *AccountAllowanceAdjustTransaction) GrantTokenAllowance(tokenID TokenID, ownerAccountID AccountID, accountID AccountID, amount int64) *AccountAllowanceAdjustTransaction { 97 return tx._AdjustTokenAllowance(tokenID, &ownerAccountID, accountID, amount) 98 } 99 100 // Deprecated 101 func (tx *AccountAllowanceAdjustTransaction) RevokeTokenAllowance(tokenID TokenID, ownerAccountID AccountID, accountID AccountID, amount uint64) *AccountAllowanceAdjustTransaction { 102 return tx._AdjustTokenAllowance(tokenID, &ownerAccountID, accountID, -int64(amount)) 103 } 104 105 // Deprecated 106 func (tx *AccountAllowanceAdjustTransaction) GetTokenAllowances() []*TokenAllowance { 107 return tx.tokenAllowances 108 } 109 110 func (tx *AccountAllowanceAdjustTransaction) _AdjustTokenNftAllowance(nftID NftID, ownerAccountID *AccountID, accountID AccountID) *AccountAllowanceAdjustTransaction { 111 tx._RequireNotFrozen() 112 113 for _, t := range tx.nftAllowances { 114 if t.TokenID.String() == nftID.TokenID.String() { 115 if t.SpenderAccountID.String() == accountID.String() { 116 b := false 117 for _, s := range t.SerialNumbers { 118 if s == nftID.SerialNumber { 119 b = true 120 } 121 } 122 if !b { 123 t.SerialNumbers = append(t.SerialNumbers, nftID.SerialNumber) 124 } 125 return tx 126 } 127 } 128 } 129 130 tx.nftAllowances = append(tx.nftAllowances, &TokenNftAllowance{ 131 TokenID: &nftID.TokenID, 132 SpenderAccountID: &accountID, 133 OwnerAccountID: ownerAccountID, 134 SerialNumbers: []int64{nftID.SerialNumber}, 135 AllSerials: false, 136 }) 137 return tx 138 } 139 140 // Deprecated 141 func (tx *AccountAllowanceAdjustTransaction) AddTokenNftAllowance(nftID NftID, accountID AccountID) *AccountAllowanceAdjustTransaction { 142 return tx._AdjustTokenNftAllowance(nftID, nil, accountID) 143 } 144 145 // Deprecated 146 func (tx *AccountAllowanceAdjustTransaction) GrantTokenNftAllowance(nftID NftID, ownerAccountID AccountID, accountID AccountID) *AccountAllowanceAdjustTransaction { 147 return tx._AdjustTokenNftAllowance(nftID, &ownerAccountID, accountID) 148 } 149 150 // Deprecated 151 func (tx *AccountAllowanceAdjustTransaction) RevokeTokenNftAllowance(nftID NftID, ownerAccountID AccountID, accountID AccountID) *AccountAllowanceAdjustTransaction { 152 return tx._AdjustTokenNftAllowance(nftID, &ownerAccountID, accountID) 153 } 154 155 func (tx *AccountAllowanceAdjustTransaction) _AdjustTokenNftAllowanceAllSerials(tokenID TokenID, ownerAccountID *AccountID, spenderAccount AccountID, allSerials bool) *AccountAllowanceAdjustTransaction { 156 for _, t := range tx.nftAllowances { 157 if t.TokenID.String() == tokenID.String() { 158 if t.SpenderAccountID.String() == spenderAccount.String() { 159 t.SerialNumbers = []int64{} 160 t.AllSerials = true 161 return tx 162 } 163 } 164 } 165 166 tx.nftAllowances = append(tx.nftAllowances, &TokenNftAllowance{ 167 TokenID: &tokenID, 168 SpenderAccountID: &spenderAccount, 169 OwnerAccountID: ownerAccountID, 170 SerialNumbers: []int64{}, 171 AllSerials: allSerials, 172 }) 173 return tx 174 } 175 176 // Deprecated 177 func (tx *AccountAllowanceAdjustTransaction) AddAllTokenNftAllowance(tokenID TokenID, spenderAccount AccountID) *AccountAllowanceAdjustTransaction { 178 return tx._AdjustTokenNftAllowanceAllSerials(tokenID, nil, spenderAccount, true) 179 } 180 181 // Deprecated 182 func (tx *AccountAllowanceAdjustTransaction) GrantTokenNftAllowanceAllSerials(ownerAccountID AccountID, tokenID TokenID, spenderAccount AccountID) *AccountAllowanceAdjustTransaction { 183 return tx._AdjustTokenNftAllowanceAllSerials(tokenID, &ownerAccountID, spenderAccount, true) 184 } 185 186 // Deprecated 187 func (tx *AccountAllowanceAdjustTransaction) RevokeTokenNftAllowanceAllSerials(ownerAccountID AccountID, tokenID TokenID, spenderAccount AccountID) *AccountAllowanceAdjustTransaction { 188 return tx._AdjustTokenNftAllowanceAllSerials(tokenID, &ownerAccountID, spenderAccount, false) 189 } 190 191 // Deprecated 192 func (tx *AccountAllowanceAdjustTransaction) GetTokenNftAllowances() []*TokenNftAllowance { 193 return tx.nftAllowances 194 } 195 196 func (tx *AccountAllowanceAdjustTransaction) Sign( 197 privateKey PrivateKey, 198 ) *AccountAllowanceAdjustTransaction { 199 tx.Transaction.Sign(privateKey) 200 return tx 201 } 202 203 func (tx *AccountAllowanceAdjustTransaction) SignWithOperator( 204 client *Client, 205 ) (*AccountAllowanceAdjustTransaction, error) { 206 // If the transaction is not signed by the _Operator, we need 207 // to sign the transaction with the _Operator 208 _, err := tx.Transaction.signWithOperator(client, tx) 209 if err != nil { 210 return nil, err 211 } 212 return tx, nil 213 } 214 215 func (tx *AccountAllowanceAdjustTransaction) SignWith( 216 publicKey PublicKey, 217 signer TransactionSigner, 218 ) *AccountAllowanceAdjustTransaction { 219 tx.Transaction.SignWith(publicKey, signer) 220 return tx 221 } 222 223 func (tx *AccountAllowanceAdjustTransaction) Freeze() (*AccountAllowanceAdjustTransaction, error) { 224 return tx.FreezeWith(nil) 225 } 226 227 func (tx *AccountAllowanceAdjustTransaction) FreezeWith(client *Client) (*AccountAllowanceAdjustTransaction, error) { 228 _, err := tx.Transaction.freezeWith(client, tx) 229 return tx, err 230 } 231 232 // SetMaxTransactionFee sets the max transaction fee for this AccountAllowanceAdjustTransaction. 233 func (tx *AccountAllowanceAdjustTransaction) SetMaxTransactionFee(fee Hbar) *AccountAllowanceAdjustTransaction { 234 tx._RequireNotFrozen() 235 tx.Transaction.SetMaxTransactionFee(fee) 236 return tx 237 } 238 239 // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received 240 func (tx *AccountAllowanceAdjustTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *AccountAllowanceAdjustTransaction { 241 tx._RequireNotFrozen() 242 tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID) 243 return tx 244 } 245 246 // SetTransactionMemo sets the memo for this AccountAllowanceAdjustTransaction. 247 func (tx *AccountAllowanceAdjustTransaction) SetTransactionMemo(memo string) *AccountAllowanceAdjustTransaction { 248 tx._RequireNotFrozen() 249 tx.Transaction.SetTransactionMemo(memo) 250 return tx 251 } 252 253 // SetTransactionValidDuration sets the valid duration for this AccountAllowanceAdjustTransaction. 254 func (tx *AccountAllowanceAdjustTransaction) SetTransactionValidDuration(duration time.Duration) *AccountAllowanceAdjustTransaction { 255 tx._RequireNotFrozen() 256 tx.Transaction.SetTransactionValidDuration(duration) 257 return tx 258 } 259 260 // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not 261 func (tx *AccountAllowanceAdjustTransaction) ToBytes() ([]byte, error) { 262 bytes, err := tx.Transaction.toBytes(tx) 263 if err != nil { 264 return nil, err 265 } 266 return bytes, nil 267 } 268 269 // SetTransactionID sets the TransactionID for this AccountAllowanceAdjustTransaction. 270 func (tx *AccountAllowanceAdjustTransaction) SetTransactionID(transactionID TransactionID) *AccountAllowanceAdjustTransaction { 271 tx._RequireNotFrozen() 272 273 tx.Transaction.SetTransactionID(transactionID) 274 return tx 275 } 276 277 // SetNodeAccountIDs sets the _Node AccountID for this AccountAllowanceAdjustTransaction. 278 func (tx *AccountAllowanceAdjustTransaction) SetNodeAccountIDs(nodeID []AccountID) *AccountAllowanceAdjustTransaction { 279 tx.Transaction.SetNodeAccountIDs(nodeID) 280 return tx 281 } 282 283 func (tx *AccountAllowanceAdjustTransaction) SetMaxRetry(count int) *AccountAllowanceAdjustTransaction { 284 tx.Transaction.SetMaxRetry(count) 285 return tx 286 } 287 288 func (tx *AccountAllowanceAdjustTransaction) AddSignature(publicKey PublicKey, signature []byte) *AccountAllowanceAdjustTransaction { 289 tx.Transaction.AddSignature(publicKey, signature) 290 return tx 291 } 292 293 func (tx *AccountAllowanceAdjustTransaction) SetMaxBackoff(max time.Duration) *AccountAllowanceAdjustTransaction { 294 tx.Transaction.SetMaxBackoff(max) 295 return tx 296 } 297 298 func (tx *AccountAllowanceAdjustTransaction) SetMinBackoff(min time.Duration) *AccountAllowanceAdjustTransaction { 299 tx.Transaction.SetMinBackoff(min) 300 return tx 301 } 302 303 // ----------- Overridden functions ---------------- 304 305 func (tx *AccountAllowanceAdjustTransaction) getName() string { 306 return "AccountAllowanceAdjustTransaction" 307 } 308 309 func (tx *AccountAllowanceAdjustTransaction) validateNetworkOnIDs(client *Client) error { 310 if client == nil || !client.autoValidateChecksums { 311 return nil 312 } 313 314 for _, ap := range tx.hbarAllowances { 315 if ap.SpenderAccountID != nil { 316 if err := ap.SpenderAccountID.ValidateChecksum(client); err != nil { 317 return err 318 } 319 } 320 321 if ap.OwnerAccountID != nil { 322 if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil { 323 return err 324 } 325 } 326 } 327 328 for _, ap := range tx.tokenAllowances { 329 if ap.SpenderAccountID != nil { 330 if err := ap.SpenderAccountID.ValidateChecksum(client); err != nil { 331 return err 332 } 333 } 334 335 if ap.TokenID != nil { 336 if err := ap.TokenID.ValidateChecksum(client); err != nil { 337 return err 338 } 339 } 340 341 if ap.OwnerAccountID != nil { 342 if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil { 343 return err 344 } 345 } 346 } 347 348 for _, ap := range tx.nftAllowances { 349 if ap.SpenderAccountID != nil { 350 if err := ap.SpenderAccountID.ValidateChecksum(client); err != nil { 351 return err 352 } 353 } 354 355 if ap.TokenID != nil { 356 if err := ap.TokenID.ValidateChecksum(client); err != nil { 357 return err 358 } 359 } 360 361 if ap.OwnerAccountID != nil { 362 if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil { 363 return err 364 } 365 } 366 } 367 368 return nil 369 } 370 371 func (tx *AccountAllowanceAdjustTransaction) build() *services.TransactionBody { 372 return &services.TransactionBody{} 373 } 374 375 func (tx *AccountAllowanceAdjustTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) { 376 return &services.SchedulableTransactionBody{}, nil 377 } 378 379 func (tx *AccountAllowanceAdjustTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) { 380 return tx.buildScheduled() 381 }