github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/schedule_transfer_example/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/hashgraph/hedera-sdk-go/v2" 8 ) 9 10 func main() { 11 var client *hedera.Client 12 var err error 13 14 // Retrieving network type from environment variable HEDERA_NETWORK 15 client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 16 if err != nil { 17 panic(fmt.Sprintf("%v : error creating client", err)) 18 } 19 20 // Retrieving operator ID from environment variable OPERATOR_ID 21 operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) 22 if err != nil { 23 panic(fmt.Sprintf("%v : error converting string to AccountID", err)) 24 } 25 26 // Retrieving operator key from environment variable OPERATOR_KEY 27 operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) 28 if err != nil { 29 panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) 30 } 31 32 // Setting the client operator ID and key 33 client.SetOperator(operatorAccountID, operatorKey) 34 35 bobsKey, err := hedera.PrivateKeyGenerateEd25519() 36 if err != nil { 37 panic(fmt.Sprintf("%v : error generating Bob's key", err)) 38 } 39 40 bobsAccountCreate, err := hedera.NewAccountCreateTransaction(). 41 SetReceiverSignatureRequired(true). 42 SetKey(bobsKey). 43 SetInitialBalance(hedera.NewHbar(10)). 44 FreezeWith(client) 45 if err != nil { 46 panic(fmt.Sprintf("%v : error freezing account creation", err)) 47 } 48 49 bobsAccountCreate.Sign(bobsKey) 50 51 response, err := bobsAccountCreate.Execute(client) 52 if err != nil { 53 panic(fmt.Sprintf("%v : error creating Bob's account", err)) 54 } 55 56 transactionReceipt, err := response.GetReceipt(client) 57 if err != nil { 58 panic(fmt.Sprintf("%v : error getting receipt", err)) 59 } 60 61 if transactionReceipt.AccountID == nil { 62 panic(fmt.Sprintf("%v : missing Bob's AccountID", err)) 63 } 64 65 bobsID := *transactionReceipt.AccountID 66 67 println("Alice's ID:", client.GetOperatorAccountID().String()) 68 println("Bob's ID:", bobsID.String()) 69 70 bobsInitialBalance, err := hedera.NewAccountBalanceQuery(). 71 SetAccountID(bobsID). 72 Execute(client) 73 if err != nil { 74 panic(fmt.Sprintf("%v : error getting Bob's balance", err)) 75 } 76 77 println("Bob's initial balance:", bobsInitialBalance.Hbars.String()) 78 79 transactionID := hedera.TransactionIDGenerate(bobsID) 80 81 transferToSchedule := hedera.NewTransferTransaction(). 82 SetTransactionID(transactionID). 83 AddHbarTransfer(client.GetOperatorAccountID(), hedera.HbarFrom(-2, hedera.HbarUnits.Hbar)). 84 AddHbarTransfer(bobsID, hedera.HbarFrom(2, hedera.HbarUnits.Hbar)) 85 86 scheduleTransaction, err := transferToSchedule.Schedule() 87 if err != nil { 88 panic(fmt.Sprintf("%v : error setting schedule transaction", err)) 89 } 90 91 frozenScheduleTransaction, err := scheduleTransaction.FreezeWith(client) 92 if err != nil { 93 panic(fmt.Sprintf("%v : error freezing scheduled transaction", err)) 94 } 95 96 frozenScheduleTransaction.Sign(bobsKey) 97 98 response, err = frozenScheduleTransaction.Execute(client) 99 if err != nil { 100 panic(fmt.Sprintf("%v : error executing create scheduled transaction", err)) 101 } 102 103 transactionReceipt, err = response.GetReceipt(client) 104 if err != nil { 105 panic(fmt.Sprintf("%v : error getting schedule create receipt", err)) 106 } 107 108 if transactionReceipt.ScheduleID == nil { 109 panic(fmt.Sprintf("%v : missing Bob's ScheduleID", err)) 110 } 111 112 bobsBalanceAfterSchedule, err := hedera.NewAccountBalanceQuery(). 113 SetAccountID(bobsID). 114 Execute(client) 115 if err != nil { 116 panic(fmt.Sprintf("%v : error getting Bob's balance", err)) 117 } 118 119 println("Bob's balance after schedule:", bobsBalanceAfterSchedule.Hbars.String()) 120 121 //clean up 122 123 deleteAccount, err := hedera.NewAccountDeleteTransaction(). 124 SetAccountID(bobsID). 125 SetTransferAccountID(client.GetOperatorAccountID()). 126 FreezeWith(client) 127 if err != nil { 128 panic(fmt.Sprintf("%v : error cleaning up", err)) 129 } 130 131 deleteAccount.Sign(bobsKey) 132 133 response, err = deleteAccount.Execute(client) 134 if err != nil { 135 panic(fmt.Sprintf("%v : error cleaning up", err)) 136 } 137 138 _, err = response.GetReceipt(client) 139 if err != nil { 140 panic(fmt.Sprintf("%v : error cleaning up", err)) 141 } 142 }