forked from geniesinc/flow-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.go
293 lines (232 loc) · 8.21 KB
/
examples.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Flow Go SDK
*
* Copyright 2019 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package examples
import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/access"
"github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go-sdk/crypto"
"github.com/onflow/flow-go-sdk/templates"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/sema"
)
const configPath = "./flow.json"
var (
conf config
)
type config struct {
Accounts struct {
Service struct {
Address string `json:"address"`
Key string `json:"key"`
} `json:"emulator-account"`
}
Contracts map[string]string `json:"contracts"`
}
// ReadFile reads a file from the file system.
func ReadFile(path string) string {
contents, err := ioutil.ReadFile(path)
Handle(err)
return string(contents)
}
func readConfig() config {
f, err := os.Open(configPath)
if err != nil {
if os.IsNotExist(err) {
fmt.Println("Emulator examples must be run from the flow-go-sdk/examples directory. Please see flow-go-sdk/examples/README.md for more details.")
} else {
fmt.Printf("Failed to load config from %s: %s\n", configPath, err.Error())
}
os.Exit(1)
}
var conf config
err = json.NewDecoder(f).Decode(&conf)
Handle(err)
return conf
}
func init() {
conf = readConfig()
}
func ServiceAccount(flowClient access.Client) (flow.Address, *flow.AccountKey, crypto.Signer) {
privateKey, err := crypto.DecodePrivateKeyHex(crypto.ECDSA_P256, conf.Accounts.Service.Key)
Handle(err)
addr := flow.HexToAddress(conf.Accounts.Service.Address)
acc, err := flowClient.GetAccount(context.Background(), addr)
Handle(err)
accountKey := acc.Keys[0]
signer, err := crypto.NewInMemorySigner(privateKey, accountKey.HashAlgo)
Handle(err)
return addr, accountKey, signer
}
// RandomPrivateKey returns a randomly generated ECDSA P-256 private key.
func RandomPrivateKey() crypto.PrivateKey {
seed := make([]byte, crypto.MinSeedLength)
_, err := rand.Read(seed)
Handle(err)
privateKey, err := crypto.GeneratePrivateKey(crypto.ECDSA_P256, seed)
Handle(err)
return privateKey
}
func RandomTransaction(flowClient access.Client) *flow.Transaction {
serviceAcctAddr, serviceAcctKey, serviceSigner := ServiceAccount(flowClient)
tx := flow.NewTransaction().
SetPayer(serviceAcctAddr).
SetProposalKey(serviceAcctAddr, serviceAcctKey.Index, serviceAcctKey.SequenceNumber).
SetScript([]byte("transaction { prepare(auth: AuthAccount) {} }")).
AddAuthorizer(serviceAcctAddr).
SetReferenceBlockID(GetReferenceBlockId(flowClient))
err := tx.SignEnvelope(serviceAcctAddr, serviceAcctKey.Index, serviceSigner)
Handle(err)
err = flowClient.SendTransaction(context.Background(), *tx)
Handle(err)
return tx
}
func RandomAccount(flowClient access.Client) (flow.Address, *flow.AccountKey, crypto.Signer) {
privateKey := RandomPrivateKey()
accountKey := flow.NewAccountKey().
FromPrivateKey(privateKey).
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold)
account := CreateAccount(flowClient, []*flow.AccountKey{accountKey})
FundAccountInEmulator(flowClient, account.Address, 10.0)
signer, err := crypto.NewInMemorySigner(privateKey, accountKey.HashAlgo)
Handle(err)
return account.Address, account.Keys[0], signer
}
func GetReferenceBlockId(flowClient access.Client) flow.Identifier {
block, err := flowClient.GetLatestBlock(context.Background(), true)
Handle(err)
return block.ID
}
func CreateAccountWithContracts(flowClient access.Client, publicKeys []*flow.AccountKey, contracts []templates.Contract) *flow.Account {
serviceAcctAddr, serviceAcctKey, serviceSigner := ServiceAccount(flowClient)
referenceBlockID := GetReferenceBlockId(flowClient)
createAccountTx, err := templates.CreateAccount(publicKeys, contracts, serviceAcctAddr)
Handle(err)
createAccountTx.
SetProposalKey(serviceAcctAddr, serviceAcctKey.Index, serviceAcctKey.SequenceNumber).
SetReferenceBlockID(referenceBlockID).
SetPayer(serviceAcctAddr)
err = createAccountTx.SignEnvelope(serviceAcctAddr, serviceAcctKey.Index, serviceSigner)
Handle(err)
ctx := context.Background()
err = flowClient.SendTransaction(ctx, *createAccountTx)
Handle(err)
result := WaitForSeal(ctx, flowClient, createAccountTx.ID())
Handle(result.Error)
for _, event := range result.Events {
if event.Type != flow.EventAccountCreated {
continue
}
accountCreatedEvent := flow.AccountCreatedEvent(event)
addr := accountCreatedEvent.Address()
account, err := flowClient.GetAccount(ctx, addr)
Handle(err)
return account
}
panic("could not find an AccountCreatedEvent")
}
/**
* mintTokensToAccountTemplate transaction mints tokens by using the service account (in the emulator)
* and deposits them to the recipient.
*/
var mintTokensToAccountTemplate = `
import FungibleToken from 0x%s
import FlowToken from 0x%s
transaction(recipient: Address, amount: UFix64) {
let tokenAdmin: &FlowToken.Administrator
let tokenReceiver: &{FungibleToken.Receiver}
prepare(signer: AuthAccount) {
self.tokenAdmin = signer
.borrow<&FlowToken.Administrator>(from: /storage/flowTokenAdmin)
?? panic("Signer is not the token admin")
self.tokenReceiver = getAccount(recipient)
.getCapability(/public/flowTokenReceiver)
.borrow<&{FungibleToken.Receiver}>()
?? panic("Unable to borrow receiver reference")
}
execute {
let minter <- self.tokenAdmin.createNewMinter(allowedAmount: amount)
let mintedVault <- minter.mintTokens(amount: amount)
self.tokenReceiver.deposit(from: <-mintedVault)
destroy minter
}
}
`
// FundAccountInEmulator Mints FLOW to an account. Minting only works in an emulator environment.
func FundAccountInEmulator(flowClient access.Client, address flow.Address, amount float64) {
serviceAcctAddr, serviceAcctKey, serviceSigner := ServiceAccount(flowClient)
referenceBlockID := GetReferenceBlockId(flowClient)
fungibleTokenAddress := flow.HexToAddress(conf.Contracts["FungibleToken"])
flowTokenAddress := flow.HexToAddress(conf.Contracts["FlowToken"])
recipient := cadence.NewAddress(address)
uintAmount := uint64(amount * sema.Fix64Factor)
cadenceAmount := cadence.UFix64(uintAmount)
fundAccountTx :=
flow.NewTransaction().
SetScript([]byte(fmt.Sprintf(mintTokensToAccountTemplate, fungibleTokenAddress, flowTokenAddress))).
AddAuthorizer(serviceAcctAddr).
AddRawArgument(jsoncdc.MustEncode(recipient)).
AddRawArgument(jsoncdc.MustEncode(cadenceAmount)).
SetProposalKey(serviceAcctAddr, serviceAcctKey.Index, serviceAcctKey.SequenceNumber).
SetReferenceBlockID(referenceBlockID).
SetPayer(serviceAcctAddr)
err := fundAccountTx.SignEnvelope(serviceAcctAddr, serviceAcctKey.Index, serviceSigner)
Handle(err)
ctx := context.Background()
err = flowClient.SendTransaction(ctx, *fundAccountTx)
Handle(err)
result := WaitForSeal(ctx, flowClient, fundAccountTx.ID())
Handle(result.Error)
}
func CreateAccount(flowClient access.Client, publicKeys []*flow.AccountKey) *flow.Account {
return CreateAccountWithContracts(flowClient, publicKeys, nil)
}
func Handle(err error) {
if err != nil {
fmt.Println("err:", err.Error())
panic(err)
}
}
func NewFlowGRPCClient() *grpc.Client {
c, err := grpc.NewClient(grpc.EmulatorHost)
Handle(err)
return c
}
func WaitForSeal(ctx context.Context, c access.Client, id flow.Identifier) *flow.TransactionResult {
result, err := c.GetTransactionResult(ctx, id)
Handle(err)
fmt.Printf("Waiting for transaction %s to be sealed...\n", id)
for result.Status != flow.TransactionStatusSealed {
time.Sleep(time.Second)
fmt.Print(".")
result, err = c.GetTransactionResult(ctx, id)
Handle(err)
}
fmt.Println()
fmt.Printf("Transaction %s sealed\n", id)
return result
}