forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
233 lines (204 loc) · 6.69 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"fmt"
"github.com/hashgraph/hedera-sdk-go/v2"
"os"
)
func main() {
client, err := hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
if err != nil {
fmt.Println(err.Error(), ": error creating client")
return
}
id, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
fmt.Println(err.Error(), ": error converting string to AccountID")
return
}
// Retrieving operator key from environment variable OPERATOR_KEY
key, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
fmt.Println(err.Error(), ": error converting string to PrivateKey")
return
}
// Setting the client operator ID and key
client.SetOperator(id, key)
/** Example 1
*
* Step 1
*
* Create accounts A, B, and C
*/
firstAccountPrivateKey, err := hedera.PrivateKeyGenerateEd25519()
if err != nil {
panic(err)
}
firstPublicKey := firstAccountPrivateKey.PublicKey()
firstAccount, err := hedera.NewAccountCreateTransaction().
SetKey(firstPublicKey).
SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
Execute(client)
if err != nil {
panic(err)
}
receiptFirstAccount, err := firstAccount.GetReceipt(client)
if err != nil {
panic(err)
}
//Get the new account ID from the receipt
firstAccountId := *receiptFirstAccount.AccountID
fmt.Println("firstAccountId: ", firstAccountId)
secondAccountPrivateKey, err := hedera.PrivateKeyGenerateEd25519()
if err != nil {
panic(err)
}
secondAccountPublicKey := secondAccountPrivateKey.PublicKey()
secondAccount, err := hedera.NewAccountCreateTransaction().
SetKey(secondAccountPublicKey).
SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
Execute(client)
if err != nil {
panic(err)
}
receiptSecondAccount, err := secondAccount.GetReceipt(client)
if err != nil {
panic(err)
}
//Get the new account ID from the receipt
secondAccountId := *receiptSecondAccount.AccountID
fmt.Println("secondAccountId: ", secondAccountId)
thirdAccountPrivateKey, err := hedera.PrivateKeyGenerateEd25519()
if err != nil {
panic(err)
}
thirdAccountPublicKey := thirdAccountPrivateKey.PublicKey()
thirdAccount, err := hedera.NewAccountCreateTransaction().
SetKey(thirdAccountPublicKey).
SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
Execute(client)
if err != nil {
panic(err)
}
receiptThirdAccount, err := thirdAccount.GetReceipt(client)
if err != nil {
panic(err)
}
//Get the new account ID from the receipt
thirdAccountId := *receiptThirdAccount.AccountID
fmt.Println("thirdAccountId: ", thirdAccountId)
/**
* Step 2
*
* 2. Create a fungible token that has three fractional fees
* Fee #1 sends 1/100 of the transferred value to collector 0.0.A.
* Fee #2 sends 2/100 of the transferred value to collector 0.0.B.
* Fee #3 sends 3/100 of the transferred value to collector 0.0.C.
*/
fee1 := hedera.NewCustomFractionalFee().SetFeeCollectorAccountID(firstAccountId).SetNumerator(1).SetDenominator(100).SetAllCollectorsAreExempt(true)
fee2 := hedera.NewCustomFractionalFee().SetFeeCollectorAccountID(secondAccountId).SetNumerator(2).SetDenominator(100).SetAllCollectorsAreExempt(true)
fee3 := hedera.NewCustomFractionalFee().SetFeeCollectorAccountID(thirdAccountId).SetNumerator(3).SetDenominator(100).SetAllCollectorsAreExempt(true)
tokenCreateTransaction, err := hedera.NewTokenCreateTransaction().
SetTokenName("HIP-573 Token").SetTokenSymbol("H573").
SetTokenType(hedera.TokenTypeFungibleCommon).
SetTreasuryAccountID(id).SetAutoRenewAccount(id).
SetAdminKey(key.PublicKey()).SetFreezeKey(key.PublicKey()).
SetWipeKey(key.PublicKey()).SetInitialSupply(100000000). // Total supply = 100000000 / 10 ^ 2
SetDecimals(2).SetCustomFees([]hedera.Fee{fee1, fee2, fee3}).FreezeWith(client)
if err != nil {
fmt.Println(err)
return
}
transactionResponse, err := tokenCreateTransaction.Sign(key).
Sign(firstAccountPrivateKey).
Sign(secondAccountPrivateKey).
Sign(thirdAccountPrivateKey).Execute(client)
if err != nil {
fmt.Println(err)
return
}
receipt, err := transactionResponse.GetReceipt(client)
if err != nil {
fmt.Println(err)
return
}
tokenId := *receipt.TokenID
fmt.Println("Created token with token id: ", tokenId)
/**
* Step 3
*
* Collector 0.0.B sends 10_000 units of the token to 0.0.A.
*/
const amount = 10_000
// First we transfer the amount from treasury account to second account
treasuryTokenTransferTransaction, err := hedera.NewTransferTransaction().
AddTokenTransfer(tokenId, id, -amount).AddTokenTransfer(tokenId, secondAccountId, amount).
FreezeWith(client)
if err != nil {
fmt.Println(err)
return
}
treasuryTokenTransferSubmit, err := treasuryTokenTransferTransaction.Sign(key).Execute(client)
if err != nil {
fmt.Println(err)
return
}
treasuryTransferReceipt, err := treasuryTokenTransferSubmit.GetReceipt(client)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Sending from treasury account to the second account - 'TransferTransaction' status: ", treasuryTransferReceipt.Status)
tokenTransferTx, err := hedera.NewTransferTransaction().
AddTokenTransfer(tokenId, secondAccountId, -amount).
AddTokenTransfer(tokenId, firstAccountId, amount).
FreezeWith(client)
if err != nil {
fmt.Println(err)
return
}
submitTransaction, err := tokenTransferTx.Sign(key).Sign(secondAccountPrivateKey).Execute(client)
if err != nil {
fmt.Println(err)
return
}
record, err := submitTransaction.GetRecord(client)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Transaction fee: ", record.TransactionFee)
/**
* Step 5
*
* Show that the fee collector accounts in the custom fee list
* of the token that was created was not charged a custom fee in the transfer
*/
firstAccountBalanceAfter, err := hedera.NewAccountBalanceQuery().
SetAccountID(firstAccountId).
Execute(client)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("first's balance:", firstAccountBalanceAfter.Tokens.Get(tokenId))
secondAccountBalanceAfter, err := hedera.NewAccountBalanceQuery().
SetAccountID(secondAccountId).
Execute(client)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("second's balance:", secondAccountBalanceAfter.Tokens.Get(tokenId))
thirdAccountBalanceAfter, err := hedera.NewAccountBalanceQuery().
SetAccountID(thirdAccountId).
Execute(client)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("third's balance:", secondAccountBalanceAfter.Tokens.Get(tokenId))
if firstAccountBalanceAfter.Tokens.Get(tokenId) == amount && secondAccountBalanceAfter.Tokens.Get(tokenId) == 0 && thirdAccountBalanceAfter.Tokens.Get(tokenId) == 0 {
fmt.Println("Fee collector accounts were not charged after transfer transaction")
}
client.Close()
}