github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/create_simple_contract/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 ) 10 11 // a simple contract struct 12 type contract struct { 13 // ignore the link references since it is empty 14 Object string `json:"object"` 15 OpCodes string `json:"opcodes"` 16 SourceMap string `json:"sourceMap"` 17 } 18 19 func main() { 20 var client *hedera.Client 21 var err error 22 23 net := os.Getenv("HEDERA_NETWORK") 24 client, err = hedera.ClientForName(net) 25 if err != nil { 26 panic(fmt.Sprintf("%v : error creating client", err)) 27 } 28 29 configOperatorID := os.Getenv("OPERATOR_ID") 30 configOperatorKey := os.Getenv("OPERATOR_KEY") 31 32 if configOperatorID != "" && configOperatorKey != "" { 33 operatorAccountID, err := hedera.AccountIDFromString(configOperatorID) 34 if err != nil { 35 panic(fmt.Sprintf("%v : error converting string to AccountID", err)) 36 } 37 38 operatorKey, err := hedera.PrivateKeyFromString(configOperatorKey) 39 if err != nil { 40 panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) 41 } 42 43 client.SetOperator(operatorAccountID, operatorKey) 44 } 45 46 defer func() { 47 err = client.Close() 48 if err != nil { 49 panic(fmt.Sprintf("%v : error closing client", err)) 50 } 51 }() 52 53 // R contents from hello_world.json file 54 rawContract, err := os.ReadFile("./hello_world.json") 55 if err != nil { 56 panic(fmt.Sprintf("%v : error reading hello_world.json", err)) 57 } 58 59 // Initialize simple contract 60 contract := contract{} 61 62 // Unmarshal the json read from the file into the simple contract 63 err = json.Unmarshal([]byte(rawContract), &contract) 64 if err != nil { 65 panic(fmt.Sprintf("%v : error unmarshaling the json file", err)) 66 } 67 68 // Convert contract to bytes 69 contractByteCode := []byte(contract.Object) 70 71 fmt.Println("Simple contract example") 72 fmt.Printf("Contract bytecode size: %v bytes\n", len(contractByteCode)) 73 74 // Upload a file containing the byte code 75 byteCodeTransactionID, err := hedera.NewFileCreateTransaction(). 76 SetMaxTransactionFee(hedera.NewHbar(2)). 77 // All keys at the top level of a key list must sign to create or modify the file 78 SetKeys(client.GetOperatorPublicKey()). 79 // Initial contents, in our case it's the contract object converted to bytes 80 SetContents(contractByteCode). 81 Execute(client) 82 83 if err != nil { 84 panic(fmt.Sprintf("%v : error creating file", err)) 85 } 86 87 // Get the record 88 byteCodeTransactionRecord, err := byteCodeTransactionID.GetRecord(client) 89 if err != nil { 90 panic(fmt.Sprintf("%v : error getting file creation record", err)) 91 } 92 93 fmt.Printf("contract bytecode file upload fee: %v\n", byteCodeTransactionRecord.TransactionFee) 94 95 // Get the file ID from the record we got 96 byteCodeFileID := *byteCodeTransactionRecord.Receipt.FileID 97 98 fmt.Printf("contract bytecode file: %v\n", byteCodeFileID) 99 100 // Instantiate the contract instance 101 contractTransactionResponse, err := hedera.NewContractCreateTransaction(). 102 // Failing to set this to a sufficient amount will result in "INSUFFICIENT_GAS" status 103 SetGas(100000). 104 // The file ID we got from the record of the file created previously 105 SetBytecodeFileID(byteCodeFileID). 106 // Setting an admin key allows you to delete the contract in the future 107 SetAdminKey(client.GetOperatorPublicKey()). 108 Execute(client) 109 110 if err != nil { 111 panic(fmt.Sprintf("%v : error creating contract", err)) 112 } 113 114 // get the record for the contract we created 115 contractRecord, err := contractTransactionResponse.GetRecord(client) 116 if err != nil { 117 panic(fmt.Sprintf("%v : error retrieving contract creation record", err)) 118 } 119 120 contractCreateResult, err := contractRecord.GetContractCreateResult() 121 if err != nil { 122 panic(fmt.Sprintf("%v : error retrieving contract creation result", err)) 123 } 124 125 // get the contract ID from the record 126 newContractID := *contractRecord.Receipt.ContractID 127 128 fmt.Printf("Contract create gas used: %v\n", contractCreateResult.GasUsed) 129 fmt.Printf("Contract create transaction fee: %v\n", contractRecord.TransactionFee) 130 fmt.Printf("Contract: %v\n", newContractID) 131 132 // Call the contract to receive the greeting 133 callResult, err := hedera.NewContractCallQuery(). 134 SetContractID(newContractID). 135 // The amount of gas to use for the call 136 // All of the gas offered will be used and charged a corresponding fee 137 SetGas(100000). 138 // This query requires payment, depends on gas used 139 SetQueryPayment(hedera.NewHbar(1)). 140 // Specified which function to call, and the parameters to pass to the function 141 SetFunction("greet", nil). 142 // This requires payment 143 SetMaxQueryPayment(hedera.NewHbar(5)). 144 Execute(client) 145 146 if err != nil { 147 panic(fmt.Sprintf("%v : error executing contract call query", err)) 148 } 149 150 fmt.Printf("Call gas used: %v\n", callResult.GasUsed) 151 fmt.Printf("Message: %v\n", callResult.GetString(0)) 152 153 // Clean up, delete the transaction 154 deleteTransactionResponse, err := hedera.NewContractDeleteTransaction(). 155 // Only thing required here is the contract ID 156 SetContractID(newContractID). 157 SetTransferAccountID(client.GetOperatorAccountID()). 158 Execute(client) 159 160 if err != nil { 161 panic(fmt.Sprintf("%v : error deleting contract", err)) 162 } 163 164 deleteTransactionReceipt, err := deleteTransactionResponse.GetReceipt(client) 165 if err != nil { 166 panic(fmt.Sprintf("%v : error retrieving contract delete receipt", err)) 167 } 168 169 fmt.Printf("Status of transaction deletion: %v\n", deleteTransactionReceipt.Status) 170 }