forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (153 loc) · 5.35 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
package main
import (
"encoding/json"
"fmt"
"github.com/hashgraph/hedera-sdk-go/v2"
"github.com/hashgraph/hedera-sdk-go/v2/examples/contract_helper"
"os"
)
type Contract struct {
Bytecode string `json:"bytecode"`
}
// Steps 1-5 are executed through ContractHelper and calling HIP564Example Contract.
// Step 6 is executed through the SDK
func main() {
var client *hedera.Client
var err error
var contract Contract
// Retrieving network type from environment variable HEDERA_NETWORK, i.e. testnet
client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
if err != nil {
println(err.Error(), ": error creating client")
return
}
//Grab your testnet account ID and private key from the environment variable
myAccountId, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
panic(err)
}
myPrivateKey, err := hedera.PrivateKeyFromStringEd25519(os.Getenv("OPERATOR_KEY"))
if err != nil {
panic(err)
}
//Print your testnet account ID and private key to the console to make sure there was no error
fmt.Printf("The account ID is = %v\n", myAccountId)
fmt.Printf("The private key is = %v\n", myPrivateKey)
client.SetOperator(myAccountId, myPrivateKey)
//Generate new keys for the account you will create
alicePrivateKey, err := hedera.PrivateKeyGenerateEd25519()
if err != nil {
panic(err)
}
newAccountPublicKey := alicePrivateKey.PublicKey()
//Create new account and assign the public key
aliceAccount, err := hedera.NewAccountCreateTransaction().
SetKey(newAccountPublicKey).
SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
Execute(client)
if err != nil {
panic(err)
}
//Request the receipt of the transaction
receipt, err := aliceAccount.GetReceipt(client)
if err != nil {
panic(err)
}
//Get the new account ID from the receipt
aliceAccountId := *receipt.AccountID
fmt.Println("aliceAcountid is: ", aliceAccountId)
//Transfer hbar from your testnet account to the new account
transaction := hedera.NewTransferTransaction().
AddHbarTransfer(myAccountId, hedera.HbarFrom(-1000, hedera.HbarUnits.Tinybar)).
AddHbarTransfer(aliceAccountId, hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar))
//Submit the transaction to a Hedera network
transaction.Execute(client)
rawContract, err := os.ReadFile("../precompile_example/ZeroTokenOperations.json")
if err != nil {
println(err.Error(), ": error reading json")
return
}
err = json.Unmarshal([]byte(rawContract), &contract)
if err != nil {
println(err.Error(), ": error unmarshaling the json file")
return
}
params, err := hedera.NewContractFunctionParameters().AddAddress(myAccountId.ToSolidityAddress())
if err != nil {
println(err.Error(), ": error adding first address to contract function parameters")
return
}
params, err = params.AddAddress(aliceAccountId.ToSolidityAddress())
if err != nil {
println(err.Error(), ": error adding second address to contract function parameters")
return
}
helper := contract_helper.NewContractHelper([]byte(contract.Bytecode), *params, client)
helper.SetPayableAmountForStep(0, hedera.NewHbar(20)).AddSignerForStep(1, alicePrivateKey)
_, err = helper.ExecuteSteps(0, 5, client)
if err != nil {
println(err.Error(), ": error in helper")
return
}
transactionResponse, err := hedera.NewTokenCreateTransaction().
SetTokenName("Black Sea LimeChain Token").
SetTokenSymbol("BSL").
SetTreasuryAccountID(myAccountId).
SetInitialSupply(10000).
SetDecimals(2).
SetAutoRenewAccount(myAccountId).
SetFreezeDefault(false).
Execute(client)
if err != nil {
println(err.Error(), ": error creating token")
return
}
// Make sure the token create transaction ran
transactionReceipt, err := transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving token creation receipt")
return
}
// Retrieve the token out of the receipt
tokenID := *transactionReceipt.TokenID
fmt.Printf("token = %v\n", tokenID.String())
// Associating the token with the second account, so it can interact with the token
associatingTransaction, err := hedera.NewTokenAssociateTransaction().
// The account ID to be associated
SetAccountID(aliceAccountId).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// The token ID that the account will be associated to
SetTokenIDs(tokenID).
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing second token associate transaction")
return
}
// Has to be signed by the account2's key
transactionResponse, err = associatingTransaction.
Sign(alicePrivateKey).
Execute(client)
if err != nil {
println(err.Error(), ": error executing second token associate transaction")
return
}
// Make sure the transaction succeeded
transactionReceipt, err = transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving second token associate transaction receipt")
return
}
fmt.Printf("Associated account %v with token %v\n", aliceAccountId.String(), tokenID.String())
// Transfer 0 tokens
transactionResponse, err = hedera.NewTransferTransaction().
AddTokenTransfer(tokenID, myAccountId, 0).AddTokenTransfer(tokenID, aliceAccountId, 0).Execute(client)
if err != nil {
println(err.Error(), ": error transfering token")
return
}
_, err = transactionResponse.GetRecord(client)
if err != nil {
println(err.Error(), ": error retrieving transaction")
return
}
}