github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/schedule_with_expiration/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/hashgraph/hedera-sdk-go/v2"
     9  )
    10  
    11  func main() {
    12  	var client *hedera.Client
    13  	var err error
    14  
    15  	// Retrieving network type from environment variable HEDERA_NETWORK
    16  	client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
    17  	if err != nil {
    18  		panic(fmt.Sprintf("%v : error creating client", err))
    19  	}
    20  
    21  	// Retrieving operator ID from environment variable OPERATOR_ID
    22  	operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
    23  	if err != nil {
    24  		panic(fmt.Sprintf("%v : error converting string to AccountID", err))
    25  	}
    26  
    27  	// Retrieving operator key from environment variable OPERATOR_KEY
    28  	operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    29  	if err != nil {
    30  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    31  	}
    32  
    33  	// Setting the client operator ID and key
    34  	client.SetOperator(operatorAccountID, operatorKey)
    35  
    36  	keys := make([]hedera.PrivateKey, 2)
    37  	pubKeys := make([]hedera.PublicKey, 2)
    38  
    39  	fmt.Println("Scheduled transaction example with expiration")
    40  	fmt.Println("Keys: ")
    41  
    42  	// Loop to generate keys for the KeyList
    43  	for i := range keys {
    44  		newKey, err := hedera.PrivateKeyGenerateEd25519()
    45  		if err != nil {
    46  			panic(fmt.Sprintf("%v : error generating PrivateKey}", err))
    47  		}
    48  
    49  		fmt.Printf("Key %v:\n", i)
    50  		fmt.Printf("private = %v\n", newKey)
    51  		fmt.Printf("public = %v\n", newKey.PublicKey())
    52  
    53  		keys[i] = newKey
    54  		pubKeys[i] = newKey.PublicKey()
    55  	}
    56  
    57  	// A threshold key with a threshold of 2 and length of 3 requires
    58  	// at least 2 of the 3 keys to sign anything modifying the account
    59  	keyList := hedera.NewKeyList().
    60  		AddAllPublicKeys(pubKeys)
    61  
    62  	// We are using all of these keys, so the scheduled transaction doesn't automatically go through
    63  	// It works perfectly fine with just one key
    64  	createResponse, err := hedera.NewAccountCreateTransaction().
    65  		// The key that must sign each transfer out of the account. If receiverSigRequired is true, then
    66  		// it must also sign any transfer into the account.
    67  		SetKey(keyList).
    68  		SetNodeAccountIDs([]hedera.AccountID{{Account: 3}}).
    69  		SetInitialBalance(hedera.NewHbar(10)).
    70  		Execute(client)
    71  	if err != nil {
    72  		panic(fmt.Sprintf("%v : error executing create account transaction", err))
    73  	}
    74  
    75  	// Make sure the transaction succeeded
    76  	transactionReceipt, err := createResponse.GetReceipt(client)
    77  	if err != nil {
    78  		panic(fmt.Sprintf("%v : error getting receipt", err))
    79  	}
    80  
    81  	// Pre-generating transaction id for the scheduled transaction so we can track it
    82  	transactionID := hedera.TransactionIDGenerate(client.GetOperatorAccountID())
    83  
    84  	println("TransactionID for transaction to be scheduled = ", transactionID.String())
    85  
    86  	// Not really necessary as its client.GetOperatorAccountID()
    87  	newAccountID := *transactionReceipt.AccountID
    88  
    89  	fmt.Printf("account = %v\n", newAccountID)
    90  
    91  	// Creating a non frozen transaction for the scheduled transaction
    92  	// In this case its TransferTransaction
    93  	transferTx := hedera.NewTransferTransaction().
    94  		SetTransactionID(transactionID).
    95  		AddHbarTransfer(newAccountID, hedera.HbarFrom(-1, hedera.HbarUnits.Hbar)).
    96  		AddHbarTransfer(client.GetOperatorAccountID(), hedera.HbarFrom(1, hedera.HbarUnits.Hbar))
    97  
    98  	// Scheduling it, this gives us hedera.ScheduleCreateTransaction
    99  	scheduled, err := transferTx.Schedule()
   100  	if err != nil {
   101  		panic(fmt.Sprintf("%v : error scheduling Transfer Transaction", err))
   102  	}
   103  
   104  	// Executing the scheduled transaction
   105  	scheduleResponse, err := scheduled.
   106  		SetExpirationTime(time.Now().Add(30 * time.Minute)).
   107  		Execute(client)
   108  	if err != nil {
   109  		panic(fmt.Sprintf("%v : error executing schedule create", err))
   110  	}
   111  
   112  	// Make sure it executed successfully
   113  	scheduleRecord, err := scheduleResponse.GetRecord(client)
   114  	if err != nil {
   115  		panic(fmt.Sprintf("%v : error getting schedule create record", err))
   116  	}
   117  
   118  	// Taking out the schedule ID
   119  	scheduleID := *scheduleRecord.Receipt.ScheduleID
   120  	scheduledTransactionID := scheduleRecord.TransactionID
   121  	println("Scheduled TransactionID:", scheduledTransactionID.String())
   122  
   123  	println("Signing with first key")
   124  
   125  	// Creating a scheduled sign transaction, we have to sign with all of the keys in the KeyList
   126  	signTransaction, err := hedera.NewScheduleSignTransaction().
   127  		SetNodeAccountIDs([]hedera.AccountID{createResponse.NodeID}).
   128  		SetScheduleID(scheduleID).
   129  		FreezeWith(client)
   130  	if err != nil {
   131  		panic(fmt.Sprintf("%v : error freezing sign transaction", err))
   132  	}
   133  
   134  	// Signing the scheduled transaction
   135  	signTransaction.Sign(keys[0])
   136  
   137  	resp, err := signTransaction.Execute(client)
   138  	if err != nil {
   139  		panic(fmt.Sprintf("%v : error executing schedule sign transaction", err))
   140  	}
   141  
   142  	// Getting the receipt to make sure the signing executed properly
   143  	_, err = resp.GetReceipt(client)
   144  	if err != nil {
   145  		panic(fmt.Sprintf("%v : error executing schedule sign receipt", err))
   146  	}
   147  
   148  	// Making sure the scheduled transaction executed properly with schedule info query
   149  	info, err := hedera.NewScheduleInfoQuery().
   150  		SetScheduleID(scheduleID).
   151  		SetNodeAccountIDs([]hedera.AccountID{createResponse.NodeID}).
   152  		Execute(client)
   153  	if err != nil {
   154  		panic(fmt.Sprintf("%v : error retrieving schedule info after signing", err))
   155  	}
   156  
   157  	println("Signers: ", info.Signatories.String())
   158  
   159  	println("Signing with second key")
   160  
   161  	signTransaction, err = hedera.NewScheduleSignTransaction().
   162  		SetNodeAccountIDs([]hedera.AccountID{createResponse.NodeID}).
   163  		SetScheduleID(scheduleID).
   164  		FreezeWith(client)
   165  	if err != nil {
   166  		panic(fmt.Sprintf("%v : error freezing sign transaction", err))
   167  	}
   168  
   169  	// Signing the scheduled transaction
   170  	signTransaction.Sign(keys[1])
   171  
   172  	resp, err = signTransaction.Execute(client)
   173  	if err != nil {
   174  		panic(fmt.Sprintf("%v : error executing schedule sign transaction", err))
   175  	}
   176  
   177  	// Getting the receipt to make sure the signing executed properly
   178  	_, err = resp.GetReceipt(client)
   179  	if err != nil {
   180  		panic(fmt.Sprintf("%v : error executing schedule sign receipt", err))
   181  	}
   182  
   183  	info, err = hedera.NewScheduleInfoQuery().
   184  		SetScheduleID(scheduleID).
   185  		SetNodeAccountIDs([]hedera.AccountID{createResponse.NodeID}).
   186  		Execute(client)
   187  	if err != nil {
   188  		panic(fmt.Sprintf("%v : error retrieving schedule info after signing", err))
   189  	}
   190  
   191  	println("Signers: ", info.Signatories.String())
   192  
   193  	if info.ExecutedAt != nil {
   194  		println("Singing success, executed at: ", info.ExecutedAt.String())
   195  	}
   196  }