-
Notifications
You must be signed in to change notification settings - Fork 65
/
account_allowance_approve_transaction.go
379 lines (324 loc) · 14.1 KB
/
account_allowance_approve_transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package hedera
/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import (
"github.com/hashgraph/hedera-sdk-go/v2/proto/services"
)
// AccountAllowanceApproveTransaction
// Creates one or more hbar/token approved allowances <b>relative to the owner account specified in the allowances of
// tx transaction</b>. Each allowance grants a spender the right to transfer a pre-determined amount of the owner's
// hbar/token to any other account of the spender's choice. If the owner is not specified in any allowance, the payer
// of transaction is considered to be the owner for that particular allowance.
// Setting the amount to zero in CryptoAllowance or TokenAllowance will remove the respective allowance for the spender.
//
// (So if account <tt>0.0.X</tt> pays for this transaction and owner is not specified in the allowance,
// then at consensus each spender account will have new allowances to spend hbar or tokens from <tt>0.0.X</tt>).
type AccountAllowanceApproveTransaction struct {
*Transaction[*AccountAllowanceApproveTransaction]
hbarAllowances []*HbarAllowance
tokenAllowances []*TokenAllowance
nftAllowances []*TokenNftAllowance
}
// NewAccountAllowanceApproveTransaction
// Creates an AccountAloowanceApproveTransaction which creates
// one or more hbar/token approved allowances relative to the owner account specified in the allowances of
// tx transaction. Each allowance grants a spender the right to transfer a pre-determined amount of the owner's
// hbar/token to any other account of the spender's choice. If the owner is not specified in any allowance, the payer
// of transaction is considered to be the owner for that particular allowance.
// Setting the amount to zero in CryptoAllowance or TokenAllowance will remove the respective allowance for the spender.
//
// (So if account 0.0.X pays for this transaction and owner is not specified in the allowance,
// then at consensus each spender account will have new allowances to spend hbar or tokens from 0.0.X).
func NewAccountAllowanceApproveTransaction() *AccountAllowanceApproveTransaction {
tx := &AccountAllowanceApproveTransaction{}
tx.Transaction = _NewTransaction(tx)
tx._SetDefaultMaxTransactionFee(NewHbar(2))
return tx
}
func _AccountAllowanceApproveTransactionFromProtobuf(tx Transaction[*AccountAllowanceApproveTransaction], pb *services.TransactionBody) AccountAllowanceApproveTransaction {
accountApproval := make([]*HbarAllowance, 0)
tokenApproval := make([]*TokenAllowance, 0)
nftApproval := make([]*TokenNftAllowance, 0)
for _, ap := range pb.GetCryptoApproveAllowance().GetCryptoAllowances() {
temp := _HbarAllowanceFromProtobuf(ap)
accountApproval = append(accountApproval, &temp)
}
for _, ap := range pb.GetCryptoApproveAllowance().GetTokenAllowances() {
temp := _TokenAllowanceFromProtobuf(ap)
tokenApproval = append(tokenApproval, &temp)
}
for _, ap := range pb.GetCryptoApproveAllowance().GetNftAllowances() {
temp := _TokenNftAllowanceFromProtobuf(ap)
nftApproval = append(nftApproval, &temp)
}
accountAllowanceAppoveTransaction := AccountAllowanceApproveTransaction{
hbarAllowances: accountApproval,
tokenAllowances: tokenApproval,
nftAllowances: nftApproval,
}
tx.childTransaction = &accountAllowanceAppoveTransaction
accountAllowanceAppoveTransaction.Transaction = &tx
return accountAllowanceAppoveTransaction
}
func (tx *AccountAllowanceApproveTransaction) _ApproveHbarApproval(ownerAccountID *AccountID, id AccountID, amount Hbar) *AccountAllowanceApproveTransaction {
tx._RequireNotFrozen()
tx.hbarAllowances = append(tx.hbarAllowances, &HbarAllowance{
SpenderAccountID: &id,
Amount: amount.AsTinybar(),
OwnerAccountID: ownerAccountID,
})
return tx
}
// AddHbarApproval
// Deprecated - Use ApproveHbarAllowance instead
func (tx *AccountAllowanceApproveTransaction) AddHbarApproval(id AccountID, amount Hbar) *AccountAllowanceApproveTransaction {
return tx._ApproveHbarApproval(nil, id, amount)
}
// ApproveHbarApproval
// Deprecated - Use ApproveHbarAllowance instead
func (tx *AccountAllowanceApproveTransaction) ApproveHbarApproval(ownerAccountID AccountID, id AccountID, amount Hbar) *AccountAllowanceApproveTransaction {
return tx._ApproveHbarApproval(&ownerAccountID, id, amount)
}
// ApproveHbarAllowance
// Approves allowance of hbar transfers for a spender.
func (tx *AccountAllowanceApproveTransaction) ApproveHbarAllowance(ownerAccountID AccountID, id AccountID, amount Hbar) *AccountAllowanceApproveTransaction {
return tx._ApproveHbarApproval(&ownerAccountID, id, amount)
}
// List of hbar allowance records
func (tx *AccountAllowanceApproveTransaction) GetHbarAllowances() []*HbarAllowance {
return tx.hbarAllowances
}
func (tx *AccountAllowanceApproveTransaction) _ApproveTokenApproval(tokenID TokenID, ownerAccountID *AccountID, accountID AccountID, amount int64) *AccountAllowanceApproveTransaction {
tx._RequireNotFrozen()
tokenApproval := TokenAllowance{
TokenID: &tokenID,
SpenderAccountID: &accountID,
Amount: amount,
OwnerAccountID: ownerAccountID,
}
tx.tokenAllowances = append(tx.tokenAllowances, &tokenApproval)
return tx
}
// Deprecated - Use ApproveTokenAllowance instead
func (tx *AccountAllowanceApproveTransaction) AddTokenApproval(tokenID TokenID, accountID AccountID, amount int64) *AccountAllowanceApproveTransaction {
return tx._ApproveTokenApproval(tokenID, nil, accountID, amount)
}
// ApproveTokenApproval
// Deprecated - Use ApproveTokenAllowance instead
func (tx *AccountAllowanceApproveTransaction) ApproveTokenApproval(tokenID TokenID, ownerAccountID AccountID, accountID AccountID, amount int64) *AccountAllowanceApproveTransaction {
return tx._ApproveTokenApproval(tokenID, &ownerAccountID, accountID, amount)
}
// ApproveTokenAllowance
// Approve allowance of fungible token transfers for a spender.
func (tx *AccountAllowanceApproveTransaction) ApproveTokenAllowance(tokenID TokenID, ownerAccountID AccountID, accountID AccountID, amount int64) *AccountAllowanceApproveTransaction {
return tx._ApproveTokenApproval(tokenID, &ownerAccountID, accountID, amount)
}
// List of token allowance records
func (tx *AccountAllowanceApproveTransaction) GetTokenAllowances() []*TokenAllowance {
return tx.tokenAllowances
}
func (tx *AccountAllowanceApproveTransaction) _ApproveTokenNftApproval(nftID NftID, ownerAccountID *AccountID, spenderAccountID *AccountID, delegatingSpenderAccountId *AccountID) *AccountAllowanceApproveTransaction {
tx._RequireNotFrozen()
for _, t := range tx.nftAllowances {
if t.TokenID.String() == nftID.TokenID.String() {
if t.SpenderAccountID.String() == spenderAccountID.String() {
b := false
for _, s := range t.SerialNumbers {
if s == nftID.SerialNumber {
b = true
}
}
if !b {
t.SerialNumbers = append(t.SerialNumbers, nftID.SerialNumber)
}
return tx
}
}
}
tx.nftAllowances = append(tx.nftAllowances, &TokenNftAllowance{
TokenID: &nftID.TokenID,
SpenderAccountID: spenderAccountID,
SerialNumbers: []int64{nftID.SerialNumber},
AllSerials: false,
OwnerAccountID: ownerAccountID,
DelegatingSpender: delegatingSpenderAccountId,
})
return tx
}
// AddTokenNftApproval
// Deprecated - Use ApproveTokenNftAllowance instead
func (tx *AccountAllowanceApproveTransaction) AddTokenNftApproval(nftID NftID, accountID AccountID) *AccountAllowanceApproveTransaction {
return tx._ApproveTokenNftApproval(nftID, nil, &accountID, nil)
}
// ApproveTokenNftApproval
// Deprecated - Use ApproveTokenNftAllowance instead
func (tx *AccountAllowanceApproveTransaction) ApproveTokenNftApproval(nftID NftID, ownerAccountID AccountID, accountID AccountID) *AccountAllowanceApproveTransaction {
return tx._ApproveTokenNftApproval(nftID, &ownerAccountID, &accountID, nil)
}
func (tx *AccountAllowanceApproveTransaction) ApproveTokenNftAllowanceWithDelegatingSpender(nftID NftID, ownerAccountID AccountID, spenderAccountId AccountID, delegatingSpenderAccountID AccountID) *AccountAllowanceApproveTransaction {
tx._RequireNotFrozen()
return tx._ApproveTokenNftApproval(nftID, &ownerAccountID, &spenderAccountId, &delegatingSpenderAccountID)
}
// ApproveTokenNftAllowance
// Approve allowance of non-fungible token transfers for a spender.
func (tx *AccountAllowanceApproveTransaction) ApproveTokenNftAllowance(nftID NftID, ownerAccountID AccountID, accountID AccountID) *AccountAllowanceApproveTransaction {
return tx._ApproveTokenNftApproval(nftID, &ownerAccountID, &accountID, nil)
}
func (tx *AccountAllowanceApproveTransaction) _ApproveTokenNftAllowanceAllSerials(tokenID TokenID, ownerAccountID *AccountID, spenderAccount AccountID) *AccountAllowanceApproveTransaction {
for _, t := range tx.nftAllowances {
if t.TokenID.String() == tokenID.String() {
if t.SpenderAccountID.String() == spenderAccount.String() {
t.SerialNumbers = []int64{}
t.AllSerials = true
return tx
}
}
}
tx.nftAllowances = append(tx.nftAllowances, &TokenNftAllowance{
TokenID: &tokenID,
SpenderAccountID: &spenderAccount,
SerialNumbers: []int64{},
AllSerials: true,
OwnerAccountID: ownerAccountID,
})
return tx
}
// AddAllTokenNftApproval
// Approve allowance of non-fungible token transfers for a spender.
// Spender has access to all of the owner's NFT units of type tokenId (currently
// owned and any in the future).
func (tx *AccountAllowanceApproveTransaction) AddAllTokenNftApproval(tokenID TokenID, spenderAccount AccountID) *AccountAllowanceApproveTransaction {
return tx._ApproveTokenNftAllowanceAllSerials(tokenID, nil, spenderAccount)
}
// ApproveTokenNftAllowanceAllSerials
// Approve allowance of non-fungible token transfers for a spender.
// Spender has access to all of the owner's NFT units of type tokenId (currently
// owned and any in the future).
func (tx *AccountAllowanceApproveTransaction) ApproveTokenNftAllowanceAllSerials(tokenID TokenID, ownerAccountID AccountID, spenderAccount AccountID) *AccountAllowanceApproveTransaction {
return tx._ApproveTokenNftAllowanceAllSerials(tokenID, &ownerAccountID, spenderAccount)
}
// List of NFT allowance records
func (tx *AccountAllowanceApproveTransaction) GetTokenNftAllowances() []*TokenNftAllowance {
return tx.nftAllowances
}
// ----------- Overridden functions ----------------
func (tx AccountAllowanceApproveTransaction) getName() string {
return "AccountAllowanceApproveTransaction"
}
func (tx AccountAllowanceApproveTransaction) validateNetworkOnIDs(client *Client) error {
if client == nil || !client.autoValidateChecksums {
return nil
}
for _, ap := range tx.hbarAllowances {
if ap.SpenderAccountID != nil {
if err := ap.SpenderAccountID.ValidateChecksum(client); err != nil {
return err
}
}
if ap.OwnerAccountID != nil {
if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil {
return err
}
}
}
for _, ap := range tx.tokenAllowances {
if ap.SpenderAccountID != nil {
if err := ap.SpenderAccountID.ValidateChecksum(client); err != nil {
return err
}
}
if ap.TokenID != nil {
if err := ap.TokenID.ValidateChecksum(client); err != nil {
return err
}
}
if ap.OwnerAccountID != nil {
if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil {
return err
}
}
}
for _, ap := range tx.nftAllowances {
if ap.SpenderAccountID != nil {
if err := ap.SpenderAccountID.ValidateChecksum(client); err != nil {
return err
}
}
if ap.TokenID != nil {
if err := ap.TokenID.ValidateChecksum(client); err != nil {
return err
}
}
if ap.OwnerAccountID != nil {
if err := ap.OwnerAccountID.ValidateChecksum(client); err != nil {
return err
}
}
}
return nil
}
func (tx AccountAllowanceApproveTransaction) build() *services.TransactionBody {
return &services.TransactionBody{
TransactionID: tx.transactionID._ToProtobuf(),
TransactionFee: tx.transactionFee,
TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
Memo: tx.Transaction.memo,
Data: &services.TransactionBody_CryptoApproveAllowance{
CryptoApproveAllowance: tx.buildProtoBody(),
},
}
}
func (tx AccountAllowanceApproveTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
return &services.SchedulableTransactionBody{
TransactionFee: tx.transactionFee,
Memo: tx.Transaction.memo,
Data: &services.SchedulableTransactionBody_CryptoApproveAllowance{
CryptoApproveAllowance: tx.buildProtoBody(),
},
}, nil
}
func (tx AccountAllowanceApproveTransaction) buildProtoBody() *services.CryptoApproveAllowanceTransactionBody {
body := &services.CryptoApproveAllowanceTransactionBody{
CryptoAllowances: make([]*services.CryptoAllowance, 0),
TokenAllowances: make([]*services.TokenAllowance, 0),
NftAllowances: make([]*services.NftAllowance, 0),
}
for _, ap := range tx.hbarAllowances {
body.CryptoAllowances = append(body.CryptoAllowances, ap._ToProtobuf())
}
for _, ap := range tx.tokenAllowances {
body.TokenAllowances = append(body.TokenAllowances, ap._ToProtobuf())
}
for _, ap := range tx.nftAllowances {
body.NftAllowances = append(body.NftAllowances, ap._ToProtobuf())
}
return body
}
func (tx AccountAllowanceApproveTransaction) getMethod(channel *_Channel) _Method {
return _Method{
transaction: channel._GetCrypto().ApproveAllowances,
}
}
func (tx AccountAllowanceApproveTransaction) constructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
return tx.buildScheduled()
}
func (tx AccountAllowanceApproveTransaction) getBaseTransaction() *Transaction[TransactionInterface] {
return castFromConcreteToBaseTransaction(tx.Transaction, &tx)
}