github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/zero_token_operations/main.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 8 "github.com/hashgraph/hedera-sdk-go/v2" 9 "github.com/hashgraph/hedera-sdk-go/v2/examples/contract_helper" 10 ) 11 12 type Contract struct { 13 Bytecode string `json:"bytecode"` 14 } 15 16 // Steps 1-5 are executed through ContractHelper and calling HIP564Example Contract. 17 // Step 6 is executed through the SDK 18 func main() { 19 var client *hedera.Client 20 var err error 21 var contract Contract 22 // Retrieving network type from environment variable HEDERA_NETWORK, i.e. testnet 23 client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 24 if err != nil { 25 panic(fmt.Sprintf("%v : error creating client", err)) 26 } 27 28 //Grab your testnet account ID and private key from the environment variable 29 myAccountId, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) 30 if err != nil { 31 panic(err) 32 } 33 34 myPrivateKey, err := hedera.PrivateKeyFromStringEd25519(os.Getenv("OPERATOR_KEY")) 35 if err != nil { 36 panic(err) 37 } 38 39 //Print your testnet account ID and private key to the console to make sure there was no error 40 fmt.Printf("The account ID is = %v\n", myAccountId) 41 fmt.Printf("The private key is = %v\n", myPrivateKey) 42 43 client.SetOperator(myAccountId, myPrivateKey) 44 //Generate new keys for the account you will create 45 alicePrivateKey, err := hedera.PrivateKeyGenerateEd25519() 46 if err != nil { 47 panic(err) 48 } 49 50 newAccountPublicKey := alicePrivateKey.PublicKey() 51 52 //Create new account and assign the public key 53 aliceAccount, err := hedera.NewAccountCreateTransaction(). 54 SetKey(newAccountPublicKey). 55 SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)). 56 Execute(client) 57 if err != nil { 58 panic(err) 59 } 60 //Request the receipt of the transaction 61 receipt, err := aliceAccount.GetReceipt(client) 62 if err != nil { 63 panic(err) 64 } 65 66 //Get the new account ID from the receipt 67 aliceAccountId := *receipt.AccountID 68 fmt.Println("aliceAcountid is: ", aliceAccountId) 69 //Transfer hbar from your testnet account to the new account 70 transaction := hedera.NewTransferTransaction(). 71 AddHbarTransfer(myAccountId, hedera.HbarFrom(-1000, hedera.HbarUnits.Tinybar)). 72 AddHbarTransfer(aliceAccountId, hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)) 73 74 //Submit the transaction to a Hedera network 75 transaction.Execute(client) 76 77 rawContract, err := os.ReadFile("../precompile_example/ZeroTokenOperations.json") 78 if err != nil { 79 panic(fmt.Sprintf("%v : error reading json", err)) 80 } 81 82 err = json.Unmarshal([]byte(rawContract), &contract) 83 if err != nil { 84 panic(fmt.Sprintf("%v : error unmarshaling the json file", err)) 85 } 86 params, err := hedera.NewContractFunctionParameters().AddAddress(myAccountId.ToSolidityAddress()) 87 if err != nil { 88 panic(fmt.Sprintf("%v : error adding first address to contract function parameters", err)) 89 } 90 params, err = params.AddAddress(aliceAccountId.ToSolidityAddress()) 91 if err != nil { 92 panic(fmt.Sprintf("%v : error adding second address to contract function parameters", err)) 93 } 94 95 helper := contract_helper.NewContractHelper([]byte(contract.Bytecode), *params, client) 96 helper.SetPayableAmountForStep(0, hedera.NewHbar(20)).AddSignerForStep(1, alicePrivateKey) 97 98 keyList := hedera.KeyListWithThreshold(1).Add(myPrivateKey.PublicKey()).Add(helper.ContractID) 99 frozenTxn, err := hedera.NewAccountUpdateTransaction().SetAccountID(myAccountId).SetKey(keyList).FreezeWith(client) 100 if err != nil { 101 panic(err) 102 } 103 tx, err := frozenTxn.Sign(myPrivateKey).Execute(client) 104 if err != nil { 105 panic(err) 106 } 107 _, err = tx.GetReceipt(client) 108 if err != nil { 109 panic(err) 110 } 111 keyList = hedera.KeyListWithThreshold(1).Add(alicePrivateKey.PublicKey()).Add(helper.ContractID) 112 113 frozenTxn, err = hedera.NewAccountUpdateTransaction().SetAccountID(aliceAccountId).SetKey(keyList).FreezeWith(client) 114 if err != nil { 115 panic(fmt.Sprintf("%v : error updating alice's account", err)) 116 } 117 tx, err = frozenTxn.Sign(alicePrivateKey).Execute(client) 118 if err != nil { 119 panic(fmt.Sprintf("%v : error updating alice's account", err)) 120 } 121 _, err = tx.GetReceipt(client) 122 if err != nil { 123 panic(err) 124 } 125 126 // TODO there is currently possible bug in services causing this operation to fail, should be investigated 127 // _, err = helper.ExecuteSteps(0, 5, client) 128 // if err != nil { 129 // panic(fmt.Sprintf("%v : error in helper", err)) 130 // } 131 transactionResponse, err := hedera.NewTokenCreateTransaction(). 132 SetTokenName("Black Sea LimeChain Token"). 133 SetTokenSymbol("BSL"). 134 SetTreasuryAccountID(myAccountId). 135 SetInitialSupply(10000). 136 SetDecimals(2). 137 SetAutoRenewAccount(myAccountId). 138 SetFreezeDefault(false). 139 Execute(client) 140 if err != nil { 141 panic(fmt.Sprintf("%v : error creating token", err)) 142 } 143 144 // Make sure the token create transaction ran 145 transactionReceipt, err := transactionResponse.GetReceipt(client) 146 if err != nil { 147 panic(fmt.Sprintf("%v : error retrieving token creation receipt", err)) 148 } 149 150 // Retrieve the token out of the receipt 151 tokenID := *transactionReceipt.TokenID 152 153 fmt.Printf("token = %v\n", tokenID.String()) 154 155 // Associating the token with the second account, so it can interact with the token 156 associatingTransaction, err := hedera.NewTokenAssociateTransaction(). 157 // The account ID to be associated 158 SetAccountID(aliceAccountId). 159 SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}). 160 // The token ID that the account will be associated to 161 SetTokenIDs(tokenID). 162 FreezeWith(client) 163 if err != nil { 164 panic(fmt.Sprintf("%v : error freezing second token associate transaction", err)) 165 } 166 // Has to be signed by the account2's key 167 transactionResponse, err = associatingTransaction. 168 Sign(alicePrivateKey). 169 Execute(client) 170 if err != nil { 171 panic(fmt.Sprintf("%v : error executing second token associate transaction", err)) 172 } 173 174 // Make sure the transaction succeeded 175 transactionReceipt, err = transactionResponse.GetReceipt(client) 176 if err != nil { 177 panic(fmt.Sprintf("%v : error retrieving second token associate transaction receipt", err)) 178 } 179 180 fmt.Printf("Associated account %v with token %v\n", aliceAccountId.String(), tokenID.String()) 181 182 // Transfer 0 tokens 183 transactionResponse, err = hedera.NewTransferTransaction(). 184 AddTokenTransfer(tokenID, myAccountId, 0).AddTokenTransfer(tokenID, aliceAccountId, 0).Execute(client) 185 if err != nil { 186 panic(fmt.Sprintf("%v : error transfering token", err)) 187 } 188 _, err = transactionResponse.GetRecord(client) 189 if err != nil { 190 panic(fmt.Sprintf("%v : error retrieving transaction", err)) 191 } 192 }