forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (149 loc) · 5.25 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/hashgraph/hedera-sdk-go/v2"
)
// a simple contract struct
type contract struct {
// ignore the link references since it is empty
Object string `json:"object"`
OpCodes string `json:"opcodes"`
SourceMap string `json:"sourceMap"`
}
func main() {
var client *hedera.Client
var err error
net := os.Getenv("HEDERA_NETWORK")
client, err = hedera.ClientForName(net)
if err != nil {
println(err.Error(), ": error creating client")
return
}
configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")
if configOperatorID != "" && configOperatorKey != "" && client.GetOperatorPublicKey().Bytes() == nil {
operatorAccountID, err := hedera.AccountIDFromString(configOperatorID)
if err != nil {
println(err.Error(), ": error converting string to AccountID")
return
}
operatorKey, err := hedera.PrivateKeyFromString(configOperatorKey)
if err != nil {
println(err.Error(), ": error converting string to PrivateKey")
return
}
client.SetOperator(operatorAccountID, operatorKey)
}
defer func() {
err = client.Close()
if err != nil {
println(err.Error(), ": error closing client")
return
}
}()
// R contents from hello_world.json file
rawContract, err := os.ReadFile("./hello_world.json")
if err != nil {
println(err.Error(), ": error reading hello_world.json")
return
}
// Initialize simple contract
contract := contract{}
// Unmarshal the json read from the file into the simple contract
err = json.Unmarshal([]byte(rawContract), &contract)
if err != nil {
println(err.Error(), ": error unmarshaling the json file")
return
}
// Convert contract to bytes
contractByteCode := []byte(contract.Object)
fmt.Println("Simple contract example")
fmt.Printf("Contract bytecode size: %v bytes\n", len(contractByteCode))
// Upload a file containing the byte code
byteCodeTransactionID, err := hedera.NewFileCreateTransaction().
SetMaxTransactionFee(hedera.NewHbar(2)).
// All keys at the top level of a key list must sign to create or modify the file
SetKeys(client.GetOperatorPublicKey()).
// Initial contents, in our case it's the contract object converted to bytes
SetContents(contractByteCode).
Execute(client)
if err != nil {
println(err.Error(), ": error creating file")
return
}
// Get the record
byteCodeTransactionRecord, err := byteCodeTransactionID.GetRecord(client)
if err != nil {
println(err.Error(), ": error getting file creation record")
return
}
fmt.Printf("contract bytecode file upload fee: %v\n", byteCodeTransactionRecord.TransactionFee)
// Get the file ID from the record we got
byteCodeFileID := *byteCodeTransactionRecord.Receipt.FileID
fmt.Printf("contract bytecode file: %v\n", byteCodeFileID)
// Instantiate the contract instance
contractTransactionResponse, err := hedera.NewContractCreateTransaction().
// Failing to set this to a sufficient amount will result in "INSUFFICIENT_GAS" status
SetGas(100000).
// The file ID we got from the record of the file created previously
SetBytecodeFileID(byteCodeFileID).
// Setting an admin key allows you to delete the contract in the future
SetAdminKey(client.GetOperatorPublicKey()).
Execute(client)
if err != nil {
println(err.Error(), ": error creating contract")
return
}
// get the record for the contract we created
contractRecord, err := contractTransactionResponse.GetRecord(client)
if err != nil {
println(err.Error(), ": error retrieving contract creation record")
return
}
contractCreateResult, err := contractRecord.GetContractCreateResult()
if err != nil {
println(err.Error(), ": error retrieving contract creation result")
return
}
// get the contract ID from the record
newContractID := *contractRecord.Receipt.ContractID
fmt.Printf("Contract create gas used: %v\n", contractCreateResult.GasUsed)
fmt.Printf("Contract create transaction fee: %v\n", contractRecord.TransactionFee)
fmt.Printf("Contract: %v\n", newContractID)
// Call the contract to receive the greeting
callResult, err := hedera.NewContractCallQuery().
SetContractID(newContractID).
// The amount of gas to use for the call
// All of the gas offered will be used and charged a corresponding fee
SetGas(100000).
// This query requires payment, depends on gas used
SetQueryPayment(hedera.NewHbar(1)).
// Specified which function to call, and the parameters to pass to the function
SetFunction("greet", nil).
// This requires payment
SetMaxQueryPayment(hedera.NewHbar(5)).
Execute(client)
if err != nil {
println(err.Error(), ": error executing contract call query")
return
}
fmt.Printf("Call gas used: %v\n", callResult.GasUsed)
fmt.Printf("Message: %v\n", callResult.GetString(0))
// Clean up, delete the transaction
deleteTransactionResponse, err := hedera.NewContractDeleteTransaction().
// Only thing required here is the contract ID
SetContractID(newContractID).
Execute(client)
if err != nil {
println(err.Error(), ": error deleting contract")
return
}
deleteTransactionReceipt, err := deleteTransactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving contract delete receipt")
return
}
fmt.Printf("Status of transaction deletion: %v\n", deleteTransactionReceipt.Status)
}