-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer_nft.go
170 lines (126 loc) · 5.35 KB
/
transfer_nft.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
package main
import (
"fmt"
"os"
"github.com/hashgraph/hedera-sdk-go/v2"
"github.com/joho/godotenv"
)
func main() {
//Loads the .env file and throws an error if it cannot load the variables from that file corectly
err := godotenv.Load(".env")
if err != nil {
panic(fmt.Errorf("Unable to load enviroment variables from .env file. Error:\n%v\n", err))
}
//Grab your testnet account ID and private key from the .env file
myAccountId, err := hedera.AccountIDFromString(os.Getenv("MY_ACCOUNT_ID"))
if err != nil {
panic(err)
}
myPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("MY_PRIVATE_KEY"))
if err != nil {
panic(err)
}
//Print your testnet account ID and private key to the console to make sure there was no error
fmt.Printf("The account ID is = %v\n", myAccountId)
fmt.Printf("The private key is = %v\n", myPrivateKey)
//Create your testnet client
client := hedera.ClientForTestnet()
client.SetOperator(myAccountId, myPrivateKey)
//Create a treasury Key
treasuryKey, err := hedera.GeneratePrivateKey()
treasuryPublicKey := treasuryKey.PublicKey()
//Create treasury account
treasuryAccount, err := hedera.NewAccountCreateTransaction().
SetKey(treasuryPublicKey).
SetInitialBalance(hedera.NewHbar(10)).
Execute(client)
//Get the receipt of the transaction
receipt, err := treasuryAccount.GetReceipt(client)
//Get the account ID
treasuryAccountId := *receipt.AccountID
//Alice Key
aliceKey, err := hedera.GeneratePrivateKey()
alicePublicKey := aliceKey.PublicKey()
//Create Alice's account
aliceAccount, err := hedera.NewAccountCreateTransaction().
SetKey(alicePublicKey).
SetInitialBalance(hedera.NewHbar(10)).
Execute(client)
//Get the receipt of the transaction
receipt2, err := aliceAccount.GetReceipt(client)
//Get the account ID
aliceAccountId := *receipt2.AccountID
//Create a supply key
supplyKey, err := hedera.GeneratePrivateKey()
//Create the NFT
nftCreate, err := hedera.NewTokenCreateTransaction().
SetTokenName("diploma").
SetTokenSymbol("GRAD").
SetTokenType(hedera.TokenTypeNonFungibleUnique).
SetDecimals(0).
SetInitialSupply(0).
SetTreasuryAccountID(treasuryAccountId).
SetSupplyType(hedera.TokenSupplyTypeFinite).
SetMaxSupply(250).
SetSupplyKey(supplyKey).
FreezeWith(client)
//Sign the transaction with the treasury key
nftCreateTxSign := nftCreate.Sign(treasuryKey)
//Submit the transaction to a Hedera network
nftCreateSubmit, err := nftCreateTxSign.Execute(client)
//Get the transaction receipt
nftCreateRx, err := nftCreateSubmit.GetReceipt(client)
//Get the token ID
tokenId := *nftCreateRx.TokenID
//Log the token ID
fmt.Println("Created NFT with token ID", tokenId)
// IPFS content identifiers for which we will create a NFT
CID := "QmTzWcVfk88JRqjTpVwHzBeULRTNzHY7mnBSG42CpwHmPa"
// Minet new NFT
mintTx, err := hedera.NewTokenMintTransaction().
SetTokenID(tokenId).
SetMetadata([]byte(CID)).
FreezeWith(client)
//Sign the transaction with the supply key
mintTxSign := mintTx.Sign(supplyKey)
//Submit the transaction to a Hedera network
mintTxSubmit, err := mintTxSign.Execute(client)
//Get the transaction receipt
mintRx, err := mintTxSubmit.GetReceipt(client)
//Log the serial number
fmt.Println("Created NFT", tokenId, "with serial:", mintRx.SerialNumbers)
//Create the associate transaction
associateAliceTx, err := hedera.NewTokenAssociateTransaction().
SetAccountID(aliceAccountId).
SetTokenIDs(tokenId).
FreezeWith(client)
//Sign with Alice's key
signTx := associateAliceTx.Sign(aliceKey)
//Submit the transaction to a Hedera network
associateAliceTxSubmit, err := signTx.Execute(client)
//Get the transaction receipt
associateAliceRx, err := associateAliceTxSubmit.GetReceipt(client)
//Confirm the transaction was successful
fmt.Println("NFT association with Alice's account:", associateAliceRx.Status)
// Check the balance before the transfer for the treasury account
balanceCheckTreasury, err := hedera.NewAccountBalanceQuery().SetAccountID(treasuryAccountId).Execute(client)
fmt.Println("Treasury balance:", balanceCheckTreasury.Tokens, "NFTs of ID", tokenId)
// Check the balance before the transfer for Alice's account
balanceCheckAlice, err := hedera.NewAccountBalanceQuery().SetAccountID(aliceAccountId).Execute(client)
fmt.Println("Alice's balance:", balanceCheckAlice.Tokens, "NFTs of ID", tokenId)
// Transfer the NFT from treasury to Alice
tokenTransferTx, err := hedera.NewTransferTransaction().
AddNftTransfer(hedera.NftID{TokenID: tokenId, SerialNumber: 1}, treasuryAccountId, aliceAccountId).
FreezeWith(client)
// Sign with the treasury key to authorize the transfer
signTransferTx := tokenTransferTx.Sign(treasuryKey)
tokenTransferSubmit, err := signTransferTx.Execute(client)
tokenTransferRx, err := tokenTransferSubmit.GetReceipt(client)
fmt.Println("NFT transfer from Treasury to Alice:", tokenTransferRx.Status)
// Check the balance of the treasury account after the transfer
balanceCheckTreasury2, err := hedera.NewAccountBalanceQuery().SetAccountID(treasuryAccountId).Execute(client)
fmt.Println("Treasury balance:", balanceCheckTreasury2.Tokens, "NFTs of ID", tokenId)
// Check the balance of Alice's account after the transfer
balanceCheckAlice2, err := hedera.NewAccountBalanceQuery().SetAccountID(aliceAccountId).Execute(client)
fmt.Println("Alice's balance:", balanceCheckAlice2.Tokens, "NFTs of ID", tokenId)
}