forked from onflow/flowkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowkit.go
1101 lines (930 loc) · 29.2 KB
/
flowkit.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Flow CLI
*
* 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 flowkit
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"strconv"
"strings"
"sync"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lmars/go-slip10"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go-sdk/crypto"
goeth "github.com/onflow/go-ethereum/accounts"
"github.com/pkg/errors"
"github.com/tyler-smith/go-bip39"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"github.com/onflow/flowkit/v2/accounts"
"github.com/onflow/flowkit/v2/config"
"github.com/onflow/flowkit/v2/gateway"
"github.com/onflow/flowkit/v2/output"
"github.com/onflow/flowkit/v2/project"
"github.com/onflow/flowkit/v2/transactions"
)
// BlockQuery defines possible queries for block.
type BlockQuery struct {
ID *flow.Identifier
Height uint64
Latest bool
}
// LatestBlockQuery specifies the latest block.
var LatestBlockQuery = BlockQuery{Latest: true}
// NewBlockQuery creates block query based on the passed query value.
//
// Query string options:
// - "latest" : return the latest block
// - height (e.g. 123456789) : return block at this height
// - ID : return block with this ID
// if none of the valid values are passed an error is returned.
func NewBlockQuery(query string) (BlockQuery, error) {
if query == "latest" {
return LatestBlockQuery, nil
}
if height, ce := strconv.ParseUint(query, 10, 64); ce == nil {
return BlockQuery{Height: height}, nil
}
if id := flow.HexToID(query); id != flow.EmptyID {
return BlockQuery{ID: &id}, nil
}
return BlockQuery{}, fmt.Errorf("invalid query: %s, valid are: \"latest\", block height or block ID", query)
}
// ScriptQuery defines block ID or height at which we should execute the script.
type ScriptQuery struct {
Latest bool
ID flow.Identifier
Height uint64
}
// LatestScriptQuery specifies the latest block at which query is executed.
var LatestScriptQuery = ScriptQuery{Latest: true}
// EventWorker defines how many workers do we want to start, each in its own subroutine, and how many blocks
// each worker fetches from the network. This is used to process the event requests concurrently.
type EventWorker struct {
Count int
BlocksPerWorker uint64
}
var _ Services = &Flowkit{}
func NewFlowkit(
state *State,
network config.Network,
gateway gateway.Gateway,
logger output.Logger,
) *Flowkit {
return &Flowkit{state, network, gateway, logger}
}
type Flowkit struct {
state *State
network config.Network
gateway gateway.Gateway
logger output.Logger
}
func (f *Flowkit) Network() config.Network {
return f.network
}
func (f *Flowkit) Gateway() gateway.Gateway {
return f.gateway
}
func (f *Flowkit) SetLogger(logger output.Logger) {
f.logger = logger
}
func (f *Flowkit) State() (*State, error) {
if f.state == nil {
return nil, config.ErrDoesNotExist
}
return f.state, nil
}
func (f *Flowkit) Ping() error {
return f.gateway.Ping()
}
func (f *Flowkit) WaitServer(ctx context.Context) error {
return f.gateway.WaitServer(ctx)
}
// GetAccount fetches account on the Flow network.
func (f *Flowkit) GetAccount(ctx context.Context, address flow.Address) (*flow.Account, error) {
return f.gateway.GetAccount(ctx, address)
}
// CreateAccount on the Flow network with the provided keys and using the signer for creation transaction.
// Returns the newly created account as well as the ID of the transaction that created the account.
//
// Keys is a slice but only one can be passed as well. If the transaction fails or there are other issues an error is returned.
func (f *Flowkit) CreateAccount(
ctx context.Context,
signer *accounts.Account,
keys []accounts.PublicKey,
) (*flow.Account, flow.Identifier, error) {
var accKeys []*flow.AccountKey
for _, k := range keys {
if k.Weight == 0 { // if key weight is not specified
k.Weight = flow.AccountKeyWeightThreshold
}
accKey := &flow.AccountKey{
PublicKey: k.Public,
SigAlgo: k.SigAlgo,
HashAlgo: k.HashAlgo,
Weight: k.Weight,
}
err := accKey.Validate()
if err != nil {
return nil, flow.EmptyID, fmt.Errorf("invalid account key: %w", err)
}
accKeys = append(accKeys, accKey)
}
tx, err := transactions.NewCreateAccount(signer, accKeys, nil)
if err != nil {
return nil, flow.EmptyID, err
}
tx, err = f.prepareTransaction(ctx, tx, signer)
if err != nil {
return nil, flow.EmptyID, err
}
f.logger.Info(fmt.Sprintf("Transaction ID: %s", tx.FlowTransaction().ID()))
f.logger.StartProgress("Creating account...")
defer f.logger.StopProgress()
sentTx, err := f.gateway.SendSignedTransaction(ctx, tx.FlowTransaction())
if err != nil {
return nil, flow.EmptyID, errors.Wrap(err, "account creation transaction failed")
}
f.logger.StartProgress("Waiting for transaction to be sealed...")
defer f.logger.StopProgress()
result, err := f.gateway.GetTransactionResult(ctx, sentTx.ID(), true)
if err != nil {
return nil, flow.EmptyID, err
}
if result.Error != nil {
return nil, flow.EmptyID, result.Error
}
events := EventsFromTransaction(result)
newAccountAddress := events.GetCreatedAddresses()
if len(newAccountAddress) == 0 {
return nil, flow.EmptyID, fmt.Errorf("new account address couldn't be fetched")
}
account, err := f.gateway.GetAccount(ctx, *newAccountAddress[0]) // we know it's the only and first event
if err != nil {
return nil, flow.EmptyID, err
}
return account, sentTx.ID(), nil
}
// prepareTransaction prepares transaction for sending with data from network
func (f *Flowkit) prepareTransaction(
ctx context.Context,
tx *transactions.Transaction,
account *accounts.Account,
) (*transactions.Transaction, error) {
block, err := f.gateway.GetLatestBlock(ctx)
if err != nil {
return nil, err
}
proposer, err := f.gateway.GetAccount(ctx, account.Address)
if err != nil {
return nil, err
}
tx.SetBlockReference(block)
if err = tx.SetProposer(proposer, account.Key.Index()); err != nil {
return nil, err
}
tx, err = tx.Sign()
if err != nil {
return nil, err
}
return tx, nil
}
var errUpdateNoDiff = errors.New("contract already exists and is the same as the contract provided for update")
type UpdateContract func(existing []byte, new []byte) bool
func UpdateExistingContract(updateExisting bool) UpdateContract {
return func(existing []byte, new []byte) bool {
return updateExisting
}
}
// AddContract to the Flow account provided and return the transaction ID.
//
// If the contract already exists on the account the operation will fail and error will be returned.
// Use UpdateExistingContract(bool) to define whether a contract should be updated or not, or you can also
// define a custom UpdateContract function which returns bool indicating whether a contract should be updated or not.
func (f *Flowkit) AddContract(
ctx context.Context,
account *accounts.Account,
contract Script,
update UpdateContract,
) (flow.Identifier, bool, error) {
state, err := f.State()
if err != nil {
return flow.EmptyID, false, err
}
program, err := project.NewProgram(contract.Code, contract.Args, contract.Location)
if err != nil {
return flow.EmptyID, false, err
}
if program.HasImports() {
contracts, err := state.DeploymentContractsByNetwork(f.network)
if err != nil {
return flow.EmptyID, false, err
}
importReplacer := project.NewImportReplacer(
contracts,
state.AliasesForNetwork(f.network),
)
program, err = importReplacer.Replace(program)
if err != nil {
return flow.EmptyID, false, err
}
}
name, err := program.Name()
if err != nil {
return flow.EmptyID, false, err
}
tx, err := transactions.NewAddAccountContract(
account,
name,
program.Code(),
contract.Args,
)
if err != nil {
return flow.EmptyID, false, err
}
f.logger.StartProgress(fmt.Sprintf("Checking contract '%s' on account '%s'...", name, account.Address))
defer f.logger.StopProgress()
// check if contract exists on account
flowAccount, err := f.gateway.GetAccount(ctx, account.Address)
if err != nil {
return flow.EmptyID, false, err
}
existingContract, exists := flowAccount.Contracts[name]
noDiffInContract := bytes.Equal(program.Code(), existingContract)
if exists && noDiffInContract {
return flow.EmptyID, false, errUpdateNoDiff
}
updateExisting := update(existingContract, program.Code())
if exists && !updateExisting {
return flow.EmptyID, false, fmt.Errorf(fmt.Sprintf("contract %s exists in account %s", name, account.Name))
}
if exists && updateExisting {
tx, err = transactions.NewUpdateAccountContract(account, name, program.Code())
if err != nil {
return flow.EmptyID, false, err
}
}
tx, err = f.prepareTransaction(ctx, tx, account)
if err != nil {
return flow.EmptyID, false, err
}
// send transaction with contract
sentTx, err := f.gateway.SendSignedTransaction(ctx, tx.FlowTransaction())
if err != nil {
return tx.FlowTransaction().ID(), false, fmt.Errorf("failed to send transaction to deploy a contract: %w", err)
}
if exists {
f.logger.StartProgress(fmt.Sprintf("Contract '%s' updating on the account '%s'.", name, account.Address))
} else {
f.logger.StartProgress(fmt.Sprintf("Contract '%s' deploying on the account '%s'.", name, account.Address))
}
// we wait for transaction to be sealed
trx, err := f.gateway.GetTransactionResult(ctx, sentTx.ID(), true)
if err != nil {
return tx.FlowTransaction().ID(), false, err
}
if trx.Error != nil {
return tx.FlowTransaction().ID(), false, trx.Error
}
d := state.Deployments().ByAccountAndNetwork(account.Name, f.network.Name)
cd := config.ContractDeployment{
Name: name,
Args: contract.Args,
}
if d != nil {
d.AddContract(cd)
} else {
deployment := config.Deployment{
Network: f.network.Name,
Account: account.Name,
Contracts: []config.ContractDeployment{cd},
}
state.Deployments().AddOrUpdate(deployment)
}
// don't add contract if it already exists because it might overwrite existing data
if c, _ := state.Contracts().ByName(name); c == nil {
state.Contracts().AddOrUpdate(config.Contract{
Name: name,
Location: contract.Location,
})
}
return sentTx.ID(), updateExisting, err
}
// RemoveContract from the provided account by its name.
//
// If removal is successful transaction ID is returned.
func (f *Flowkit) RemoveContract(
ctx context.Context,
account *accounts.Account,
contractName string,
) (flow.Identifier, error) {
// check if contracts exists on the account
flowAcc, err := f.gateway.GetAccount(ctx, account.Address)
if err != nil {
return flow.EmptyID, err
}
existingContracts := maps.Keys(flowAcc.Contracts)
if !slices.Contains(existingContracts, contractName) {
return flow.EmptyID, fmt.Errorf(
"can not remove a non-existing contract named '%s'. Account only contains the contracts: %v",
contractName,
strings.Join(existingContracts, ", "),
)
}
tx, err := transactions.NewRemoveAccountContract(account, contractName)
if err != nil {
return flow.EmptyID, err
}
tx, err = f.prepareTransaction(ctx, tx, account)
if err != nil {
return flow.EmptyID, err
}
f.logger.StartProgress(
fmt.Sprintf("Removing Contract %s from %s...", contractName, account.Address),
)
defer f.logger.StopProgress()
sentTx, err := f.gateway.SendSignedTransaction(ctx, tx.FlowTransaction())
if err != nil {
return flow.EmptyID, err
}
txr, err := f.gateway.GetTransactionResult(ctx, sentTx.ID(), true)
if err != nil {
return flow.EmptyID, err
}
if txr != nil && txr.Error != nil {
return flow.EmptyID, txr.Error
}
f.logger.StopProgress()
return sentTx.ID(), nil
}
// GetBlock by the query from Flow blockchain. Query can define a block by ID, block by height or require the latest block.
func (f *Flowkit) GetBlock(ctx context.Context, query BlockQuery) (*flow.Block, error) {
var err error
var block *flow.Block
if query.Latest {
block, err = f.gateway.GetLatestBlock(ctx)
} else if query.ID != nil {
block, err = f.gateway.GetBlockByID(ctx, *query.ID)
} else {
block, err = f.gateway.GetBlockByHeight(ctx, query.Height)
}
if err != nil {
return nil, fmt.Errorf("error fetching block: %s", err.Error())
}
if block == nil {
return nil, fmt.Errorf("block not found")
}
return block, err
}
// GetCollection by the ID from Flow network.
func (f *Flowkit) GetCollection(ctx context.Context, ID flow.Identifier) (*flow.Collection, error) {
return f.gateway.GetCollection(ctx, ID)
}
// GetEvents from Flow network by their event name in the specified height interval defined by start and end inclusive.
// Optional worker defines parameters for how many concurrent workers do we want to fetch our events,
// and how many blocks between the provided interval each worker fetches.
//
// Providing worker value will produce faster response as the interval will be scanned concurrently. This parameter is optional,
// if not provided only a single worker will be used.
func (f *Flowkit) GetEvents(
ctx context.Context,
names []string,
startHeight uint64,
endHeight uint64,
worker *EventWorker,
) ([]flow.BlockEvents, error) {
if endHeight < startHeight {
return nil, fmt.Errorf("cannot have end height (%d) of block range less that start height (%d)", endHeight, startHeight)
}
if worker == nil { // if no worker is passed, create a default one
worker = &EventWorker{
Count: 1,
BlocksPerWorker: 250,
}
}
queries := makeEventQueries(names, startHeight, endHeight, worker.BlocksPerWorker)
jobChan := make(chan grpc.EventRangeQuery, worker.Count)
results := make(chan eventWorkerResult)
var wg sync.WaitGroup
for i := 0; i < worker.Count; i++ {
wg.Add(1)
go func() {
defer wg.Done()
f.eventWorker(ctx, jobChan, results)
}()
}
// wait on the workers to finish and close the result channel
// to signal downstream that all work is done
go func() {
defer close(results)
wg.Wait()
}()
go func() {
defer close(jobChan)
for _, query := range queries {
jobChan <- query
}
}()
var resultEvents []flow.BlockEvents
for eventResult := range results {
if eventResult.err != nil {
return nil, eventResult.err
}
resultEvents = append(resultEvents, eventResult.events...)
}
return resultEvents, nil
}
func (f *Flowkit) eventWorker(ctx context.Context, jobChan <-chan grpc.EventRangeQuery, results chan<- eventWorkerResult) {
for q := range jobChan {
blockEvents, err := f.gateway.GetEvents(ctx, q.Type, q.StartHeight, q.EndHeight)
if err != nil {
results <- eventWorkerResult{nil, err}
}
results <- eventWorkerResult{blockEvents, nil}
}
}
type eventWorkerResult struct {
events []flow.BlockEvents
err error
}
func makeEventQueries(
events []string,
startHeight uint64,
endHeight uint64,
blockCount uint64,
) []grpc.EventRangeQuery {
var queries []grpc.EventRangeQuery
for startHeight <= endHeight {
suggestedEndHeight := startHeight + blockCount - 1 // since we are inclusive
end := endHeight
if suggestedEndHeight < endHeight {
end = suggestedEndHeight
}
for _, event := range events {
queries = append(queries, grpc.EventRangeQuery{
Type: event,
StartHeight: startHeight,
EndHeight: end,
})
}
startHeight = suggestedEndHeight + 1
}
return queries
}
// GenerateKey using the signature algorithm and optional seed. If seed is not provided a random safe seed will be generated.
func (f *Flowkit) GenerateKey(
_ context.Context,
sigAlgo crypto.SignatureAlgorithm,
inputSeed string,
) (crypto.PrivateKey, error) {
seed := []byte(inputSeed)
if inputSeed == "" {
seed = make([]byte, crypto.MinSeedLength)
_, err := rand.Read(seed)
if err != nil {
return nil, fmt.Errorf("failed to generate random seed: %v", err)
}
}
privateKey, err := crypto.GeneratePrivateKey(sigAlgo, seed)
if err != nil {
return nil, fmt.Errorf("failed to generate private key: %w", err)
}
return privateKey, nil
}
// GenerateMnemonicKey will generate a new key with the signature algorithm and optional derivation path.
//
// If the derivation path is not provided a default "m/44'/539'/0'/0/0" will be used.
func (f *Flowkit) GenerateMnemonicKey(
_ context.Context,
sigAlgo crypto.SignatureAlgorithm,
derivationPath string,
) (crypto.PrivateKey, string, error) {
entropy, err := bip39.NewEntropy(128)
if err != nil {
return nil, "", err
}
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return nil, "", err
}
if !bip39.IsMnemonicValid(mnemonic) {
return nil, "", fmt.Errorf("invalid mnemonic")
}
seed := bip39.NewSeed(mnemonic, "")
key, err := f.derivePrivateKeyFromSeed(seed, sigAlgo, derivationPath)
if err != nil {
return nil, "", err
}
return key, mnemonic, nil
}
func (f *Flowkit) DerivePrivateKeyFromMnemonic(
_ context.Context,
mnemonic string,
sigAlgo crypto.SignatureAlgorithm,
derivationPath string,
) (crypto.PrivateKey, error) {
if !bip39.IsMnemonicValid(mnemonic) {
return nil, fmt.Errorf("invalid mnemonic")
}
seed := bip39.NewSeed(mnemonic, "")
return f.derivePrivateKeyFromSeed(seed, sigAlgo, derivationPath)
}
func (f *Flowkit) derivePrivateKeyFromSeed(
seed []byte,
sigAlgo crypto.SignatureAlgorithm,
derivationPath string,
) (crypto.PrivateKey, error) {
// sanity check of seed length
if len(seed) < 16 {
return nil, fmt.Errorf("seed length should be at least 16 bytes, got %d", len(seed))
}
if derivationPath == "" {
derivationPath = "m/44'/539'/0'/0/0"
}
path, err := goeth.ParseDerivationPath(derivationPath)
if err != nil {
return nil, fmt.Errorf("invalid derivation path")
}
curve := slip10.CurveBitcoin // case ECDSA_secp256k1
if sigAlgo == crypto.ECDSA_P256 {
curve = slip10.CurveP256
} else if sigAlgo != crypto.ECDSA_secp256k1 {
return nil, fmt.Errorf("invalid signature algorithm")
}
accountKey, err := slip10.NewMasterKeyWithCurve(seed, curve)
if err != nil {
return nil, err
}
for _, n := range path {
accountKey, err = accountKey.NewChildKey(n)
if err != nil {
return nil, err
}
}
privateKey, err := crypto.DecodePrivateKey(sigAlgo, accountKey.Key)
if err != nil {
return nil, err
}
return privateKey, nil
}
// DeployProject contracts to the Flow network or update if already exists and UpdateContracts returns true.
//
// Retrieve all the contracts for specified network, sort them for deployment deploy one by one and replace
// the imports in the contract source, so it corresponds to the account name the contract was deployed to.
// If contracts already exist use UpdateExistingContract(bool) to define whether a contract should be updated or not.
func (f *Flowkit) DeployProject(ctx context.Context, update UpdateContract) ([]*project.Contract, error) {
state, err := f.State()
if err != nil {
return nil, err
}
contracts, err := state.DeploymentContractsByNetwork(f.network)
if err != nil {
return nil, err
}
deployment, err := project.NewDeployment(contracts, state.AliasesForNetwork(f.network))
if err != nil {
return nil, err
}
sorted, err := deployment.Sort()
if err != nil {
return nil, err
}
f.logger.Info(fmt.Sprintf(
"\nDeploying %d contracts for accounts: %s\n",
len(sorted),
state.AccountsForNetwork(f.network).String(),
))
defer f.logger.StopProgress()
deployErr := &ProjectDeploymentError{}
for _, contract := range sorted {
targetAccount, err := state.Accounts().ByName(contract.AccountName)
if err != nil {
return nil, fmt.Errorf("target account for deploying contract not found in configuration")
}
txID, updated, err := f.AddContract(
ctx,
targetAccount,
Script{Code: contract.Code(), Args: contract.Args, Location: contract.Location()},
update,
)
if err != nil && errors.Is(err, errUpdateNoDiff) {
f.logger.Info(fmt.Sprintf(
"%s -> 0x%s [skipping, no changes found]",
output.Italic(contract.Name),
contract.AccountAddress.String(),
))
continue
} else if err != nil {
deployErr.add(contract, err, fmt.Sprintf("failed to deploy contract %s", contract.Name))
continue
}
f.logger.Info(fmt.Sprintf(
"%s -> 0x%s (%s) %s",
output.Green(contract.Name),
contract.AccountAddress,
txID.String(),
map[bool]string{true: "[updated]", false: ""}[updated],
))
}
if len(deployErr.contracts) > 0 {
return nil, deployErr
}
f.logger.Info(fmt.Sprintf("\n%s All contracts deployed successfully", output.SuccessEmoji()))
return sorted, nil
}
type ProjectDeploymentError struct {
contracts map[string]error
}
func (d *ProjectDeploymentError) add(contract *project.Contract, err error, msg string) {
if d.contracts == nil {
d.contracts = make(map[string]error)
}
d.contracts[contract.Name] = fmt.Errorf("%s: %w", msg, err)
}
func (d *ProjectDeploymentError) Contracts() map[string]error {
return d.contracts
}
func (d *ProjectDeploymentError) Error() string {
err := ""
for c, e := range d.contracts {
err = fmt.Sprintf("%s %s: %s,", err, c, e.Error())
}
return err
}
// Script includes Cadence code and optional arguments and filename.
//
// Filename is only required to be passed if the code has imports you want to resolve.
type Script struct {
Code []byte
Args []cadence.Value
Location string
}
// ExecuteScript on the Flow network and return the Cadence value as a result. The script is executed at the
// block provided as part of the ScriptQuery value.
func (f *Flowkit) ExecuteScript(ctx context.Context, script Script, query ScriptQuery) (cadence.Value, error) {
state, err := f.State()
if err != nil {
return nil, err
}
program, err := project.NewProgram(script.Code, script.Args, script.Location)
if err != nil {
return nil, err
}
if program.HasImports() {
contracts, err := state.DeploymentContractsByNetwork(f.network)
if err != nil {
return nil, err
}
importReplacer := project.NewImportReplacer(
contracts,
state.AliasesForNetwork(f.network),
)
if state == nil {
return nil, config.ErrDoesNotExist
}
if f.network == config.EmptyNetwork {
return nil, fmt.Errorf("missing network, specify which network to use to resolve imports in script code")
}
if script.Location == "" {
return nil, fmt.Errorf("resolving imports in scripts not supported")
}
program, err = importReplacer.Replace(program)
if err != nil {
return nil, err
}
}
if query.Latest {
return f.gateway.ExecuteScript(ctx, program.Code(), script.Args)
} else if query.ID != flow.EmptyID {
return f.gateway.ExecuteScriptAtID(ctx, program.Code(), script.Args, query.ID)
} else {
return f.gateway.ExecuteScriptAtHeight(ctx, program.Code(), script.Args, query.Height)
}
}
// GetTransactionByID from the Flow network including the transaction result. Using the waitSeal we can wait for the transaction to be sealed.
func (f *Flowkit) GetTransactionByID(
ctx context.Context,
ID flow.Identifier,
waitSeal bool,
) (*flow.Transaction, *flow.TransactionResult, error) {
f.logger.StartProgress("Fetching Transaction...")
defer f.logger.StopProgress()
tx, err := f.gateway.GetTransaction(ctx, ID)
if err != nil {
return nil, nil, err
}
if waitSeal {
f.logger.StartProgress("Waiting for transaction to be sealed...")
defer f.logger.StopProgress()
}
result, err := f.gateway.GetTransactionResult(ctx, ID, waitSeal)
return tx, result, err
}
func (f *Flowkit) GetTransactionsByBlockID(
ctx context.Context,
blockID flow.Identifier,
) ([]*flow.Transaction, []*flow.TransactionResult, error) {
tx, err := f.gateway.GetTransactionsByBlockID(ctx, blockID)
if err != nil {
return nil, nil, err
}
txRes, err := f.gateway.GetTransactionResultsByBlockID(ctx, blockID)
if err != nil {
return nil, nil, err
}
return tx, txRes, nil
}
// BuildTransaction builds a new transaction type for later signing and submitting to the network.
//
// AddressesRoles type defines the address for each role (payer, proposer, authorizers) and the script defines the transaction content.
func (f *Flowkit) BuildTransaction(
ctx context.Context,
addresses transactions.AddressesRoles,
proposerKeyIndex int,
script Script,
gasLimit uint64,
) (*transactions.Transaction, error) {
state, err := f.State()
if err != nil {
return nil, err
}
latestBlock, err := f.gateway.GetLatestBlock(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get latest sealed block: %w", err)
}
proposerAccount, err := f.gateway.GetAccount(ctx, addresses.Proposer)
if err != nil {
return nil, err
}
tx := transactions.New().
SetPayer(addresses.Payer).
SetComputeLimit(gasLimit).
SetBlockReference(latestBlock)
program, err := project.NewProgram(script.Code, script.Args, script.Location)
if err != nil {
return nil, err
}
if program.HasImports() {
if f.network == config.EmptyNetwork {
return nil, fmt.Errorf("missing network, specify which network to use to resolve imports in transaction code")
}
if script.Location == "" { // when used as lib with code we don't support imports
return nil, fmt.Errorf("resolving imports in transactions not supported")
}
contracts, err := state.DeploymentContractsByNetwork(f.network)
if err != nil {
return nil, err
}
importReplacer := project.NewImportReplacer(
contracts,
state.AliasesForNetwork(f.network),
)
program, err = importReplacer.Replace(program)
if err != nil {
return nil, fmt.Errorf("error resolving imports: %w", err)
}
}
if err := tx.SetProposer(proposerAccount, proposerKeyIndex); err != nil {
return nil, err
}
if err := tx.SetScriptWithArgs(program.Code(), script.Args); err != nil {
return nil, err
}
tx, err = tx.AddAuthorizers(addresses.Authorizers)
if err != nil {
return nil, err
}
return tx, nil
}
// SignTransactionPayload will use the signer account provided and the payload raw byte content to sign it.
//
// The payload should be RLP encoded transaction payload and is suggested to be used in pair with BuildTransaction function.
func (f *Flowkit) SignTransactionPayload(
_ context.Context,
signer *accounts.Account,
payload []byte,
) (*transactions.Transaction, error) {
tx, err := transactions.NewFromPayload(payload)
if err != nil {
return nil, err
}
err = tx.SetSigner(signer)
if err != nil {
return nil, err
}
return tx.Sign()
}
// SendSignedTransaction will send a prebuilt and signed transaction to the Flow network.
//
// You can build the transaction using the BuildTransaction method and then sign it using the SignTranscation method.
func (f *Flowkit) SendSignedTransaction(
ctx context.Context,
tx *transactions.Transaction,
) (*flow.Transaction, *flow.TransactionResult, error) {
sentTx, err := f.gateway.SendSignedTransaction(ctx, tx.FlowTransaction())
if err != nil {
return nil, nil, err
}