forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
464 lines (401 loc) · 15.3 KB
/
main.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package main
import (
"fmt"
"os"
"github.com/hashgraph/hedera-sdk-go/v2"
)
func main() {
var client *hedera.Client
var err error
// Retrieving network type from environment variable HEDERA_NETWORK
client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
if err != nil {
println(err.Error(), ": error creating client")
return
}
// Retrieving operator ID from environment variable OPERATOR_ID
operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
println(err.Error(), ": error converting string to AccountID")
return
}
// Retrieving operator key from environment variable OPERATOR_KEY
operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
println(err.Error(), ": error converting string to PrivateKey")
return
}
// Setting the client operator ID and key
client.SetOperator(operatorAccountID, operatorKey)
key1, err := hedera.GeneratePrivateKey()
if err != nil {
println(err.Error(), ": error generating PrivateKey")
return
}
key2, err := hedera.GeneratePrivateKey()
if err != nil {
println(err.Error(), ": error generating PrivateKey")
return
}
fmt.Printf("privateKey = %v\n", key1.String())
fmt.Printf("publicKey = %v\n", key1.PublicKey().String())
fmt.Printf("privateKey = %v\n", key2.String())
fmt.Printf("publicKey = %v\n", key2.PublicKey().String())
// Creating 2 accounts for transferring tokens
transactionResponse, err := hedera.NewAccountCreateTransaction().
// The key that must sign each transfer out of the account. If receiverSigRequired is true, then
// it must also sign any transfer into the account.
SetKey(key1.PublicKey()).
Execute(client)
if err != nil {
println(err.Error(), ": error creating account")
return
}
// First receipt with account ID 1, will error if transaction failed
transactionReceipt, err := transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving account creation receipt")
return
}
// Retrieving account ID out of the first receipt
accountID1 := *transactionReceipt.AccountID
fmt.Printf("account = %v\n", accountID1.String())
// Creating a new account for the token
transactionResponse, err = hedera.NewAccountCreateTransaction().
// The key that must sign each transfer out of the account. If receiverSigRequired is true, then
// it must also sign any transfer into the account.
SetKey(key2.PublicKey()).
Execute(client)
if err != nil {
println(err.Error(), ": error creating second account")
return
}
// Second receipt with account ID 2, will error if transaction failed
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving second account creation receipt")
return
}
// Retrieving account ID out of the second receipt
accountID2 := *transactionReceipt.AccountID
fmt.Printf("account = %v\n", accountID2.String())
// Creating a new token
transactionResponse, err = hedera.NewTokenCreateTransaction().
// The publicly visible name of the token
SetTokenName("ffff").
// The publicly visible token symbol
SetTokenSymbol("F").
SetMaxTransactionFee(hedera.NewHbar(1000)).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// For tokens of type FUNGIBLE_COMMON - the number of decimal places a
// token is divisible by. For tokens of type NON_FUNGIBLE_UNIQUE - value
// must be 0
SetDecimals(3).
// Specifies the initial supply of tokens to be put in circulation. The
// initial supply is sent to the Treasury Account. The supply is in the
// lowest denomination possible. In the case for NON_FUNGIBLE_UNIQUE Type
// the value must be 0
SetInitialSupply(1000000).
// The account which will act as a treasury for the token. This account
// will receive the specified initial supply or the newly minted NFTs in
// the case for NON_FUNGIBLE_UNIQUE Type
SetTreasuryAccountID(client.GetOperatorAccountID()).
// The key which can perform update/delete operations on the token. If empty, the token can be
// perceived as immutable (not being able to be updated/deleted)
SetAdminKey(client.GetOperatorPublicKey()).
// The key which can sign to freeze or unfreeze an account for token transactions. If empty,
// freezing is not possible
SetFreezeKey(client.GetOperatorPublicKey()).
// The key which can wipe the token balance of an account. If empty, wipe is not possible
SetWipeKey(client.GetOperatorPublicKey()).
// The key which can grant or revoke KYC of an account for the token's transactions. If empty,
// KYC is not required, and KYC grant or revoke operations are not possible.
SetKycKey(client.GetOperatorPublicKey()).
// The key which can change the supply of a token. The key is used to sign Token Mint/Burn
// operations
SetSupplyKey(client.GetOperatorPublicKey()).
// The default Freeze status (frozen or unfrozen) of Hedera accounts relative to this token. If
// true, an account must be unfrozen before it can receive the token
SetFreezeDefault(false).
Execute(client)
if err != nil {
println(err.Error(), ": error creating token")
return
}
// Make sure the token create transaction ran
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving token creation receipt")
return
}
// Retrieve the token out of the receipt
tokenID := *transactionReceipt.TokenID
fmt.Printf("token = %v\n", tokenID.String())
// Associating the token with the first account, so it can interact with the token
transaction, err := hedera.NewTokenAssociateTransaction().
// The account ID to be associated
SetAccountID(accountID1).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// The token ID that the account will be associated to
SetTokenIDs(tokenID).
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing token associate transaction")
return
}
// Has to be signed by the account1's key
transactionResponse, err = transaction.
Sign(key1).
Execute(client)
if err != nil {
println(err.Error(), ": error associating token")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving token associate transaction receipt")
return
}
fmt.Printf("Associated account %v with token %v\n", accountID1.String(), tokenID.String())
// Associating the token with the second account, so it can interact with the token
transaction, err = hedera.NewTokenAssociateTransaction().
// The account ID to be associated
SetAccountID(accountID2).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// The token ID that the account will be associated to
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing second token associate transaction")
return
}
// Has to be signed by the account2's key
transactionResponse, err = transaction.
Sign(key2).
Execute(client)
if err != nil {
println(err.Error(), ": error executing second token associate transaction")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving second token associate transaction receipt")
return
}
fmt.Printf("Associated account %v with token %v\n", accountID2.String(), tokenID.String())
// This transaction grants Kyc to the first account
// Must be signed by the Token's kycKey.
transactionResponse, err = hedera.NewTokenGrantKycTransaction().
SetTokenID(tokenID).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// The account that KYC is being granted to
SetAccountID(accountID1).
// As the token kyc key is client.GetOperatorPublicKey(), we don't have to explicitly sign with anything
// as it's done automatically by execute for the operator
Execute(client)
if err != nil {
println(err.Error(), ": error granting kyc")
return
}
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving grant kyc transaction receipt")
return
}
fmt.Printf("Granted KYC for account %v on token %v\n", accountID1.String(), tokenID.String())
// This transaction grants Kyc to the second account
transactionResponse, err = hedera.NewTokenGrantKycTransaction().
SetTokenID(tokenID).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// The account that KYC is being granted to
SetAccountID(accountID2).
// As the token kyc key is client.GetOperatorPublicKey(), we don't have to explicitly sign with anything
// as it's done automatically by execute for the operator
Execute(client)
if err != nil {
println(err.Error(), ": error granting kyc to second account")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving grant kyc transaction receipt")
return
}
fmt.Printf("Granted KYC for account %v on token %v\n", accountID2.String(), tokenID.String())
transactionResponse, err = hedera.NewTransferTransaction().
// Same as for Hbar transfer, token value has to be negated to denote they are being taken out
AddTokenTransfer(tokenID, client.GetOperatorAccountID(), -10).
// Same as for Hbar transfer, the 2 transfers here have to be equal, otherwise it will lead to an error
AddTokenTransfer(tokenID, accountID1, 10).
// We don't have to sign this one as we are transferring tokens from the operator
Execute(client)
if err != nil {
println(err.Error(), ": error transferring from operator to account1")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving transfer from operator to account1 receipt")
return
}
fmt.Printf(
"Sent 10 tokens from account %v to account %v on token %v\n",
client.GetOperatorAccountID().String(),
accountID1.String(),
tokenID.String(),
)
transferTransaction, err := hedera.NewTransferTransaction().
// 10 tokens from account 1
AddTokenTransfer(tokenID, accountID1, -10).
// 10 token to account 2
AddTokenTransfer(tokenID, accountID2, 10).
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing transfer from account1 to account2")
return
}
// As we are now transferring tokens from accountID1 to accountID2, this has to be signed by accountID1's key
transferTransaction = transferTransaction.Sign(key1)
// Execute the transfer transaction
transactionResponse, err = transferTransaction.Execute(client)
if err != nil {
println(err.Error(), ": error transferring from account1 to account2")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving transfer from account1 to account2 receipt")
return
}
fmt.Printf(
"Sent 10 tokens from account %v to account %v on token %v\n",
accountID1.String(),
accountID2.String(),
tokenID.String(),
)
transferTransaction, err = hedera.NewTransferTransaction().
// 10 tokens from account 2
AddTokenTransfer(tokenID, accountID2, -10).
// 10 token to account 1
AddTokenTransfer(tokenID, accountID1, 10).
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing transfer from account2 to account1")
return
}
// As we are now transferring tokens from accountID2 back to accountID1, this has to be signed by accountID2's key
transferTransaction = transferTransaction.Sign(key2)
// Executing the transfer transaction
transactionResponse, err = transferTransaction.Execute(client)
if err != nil {
println(err.Error(), ": error transferring from account2 to account1")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving transfer from account2 to account1 receipt")
return
}
fmt.Printf(
"Sent 10 tokens from account %v to account %v on token %v\n",
accountID2.String(),
accountID1.String(),
tokenID.String(),
)
// Clean up
// Now we can wipe the 10 tokens that are in possession of accountID1
// Has to be signed by wipe key of the token, in this case it was the operator key
transactionResponse, err = hedera.NewTokenWipeTransaction().
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// From which account
SetAccountID(accountID1).
// For which token
SetTokenID(tokenID).
// How many
SetAmount(10).
Execute(client)
if err != nil {
println(err.Error(), ": error wiping from token")
return
}
// Make sure the transaction succeeded
_, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving token wipe transaction receipt")
return
}
fmt.Printf("Wiped account %v on token %v\n", accountID1.String(), tokenID.String())
// Now to delete the token
// Has to be signed by admin key of the token, in this case it was the operator key
transactionResponse, err = hedera.NewTokenDeleteTransaction().
SetTokenID(tokenID).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
Execute(client)
if err != nil {
println(err.Error(), ": error deleting token")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving token delete transaction receipt")
return
}
fmt.Printf("DeletedAt token %v\n", tokenID.String())
// Now that the tokens have been wiped from accountID1, we can safely delete it
accountDeleteTx, err := hedera.NewAccountDeleteTransaction().
SetAccountID(accountID1).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// Tp which account to transfer the account 1 balance
SetTransferAccountID(client.GetOperatorAccountID()).
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing account delete transaction")
return
}
// Account deletion has to always be signed by the key for the account
transactionResponse, err = accountDeleteTx.
Sign(key1).
Execute(client)
if err != nil {
println(err.Error(), ": error deleting account 1")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving transfer transaction receipt")
return
}
fmt.Printf("DeletedAt account %v\n", accountID1.String())
accountDeleteTx, err = hedera.NewAccountDeleteTransaction().
SetAccountID(accountID2).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// Tp which account to transfer the account 2 balance
SetTransferAccountID(client.GetOperatorAccountID()).
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing account delete transaction")
return
}
// Account deletion has to always be signed by the key for the account
transactionResponse, err = accountDeleteTx.
Sign(key2).
Execute(client)
if err != nil {
println(err.Error(), ": error deleting account2")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving account delete transaction receipt")
return
}
fmt.Printf("DeletedAt account %v\n", accountID2.String())
}