Skip to content

Commit f0376ce

Browse files
authored
Merge pull request #9 from bjartek/context-master
feat: added ctx support to all methods that require it and fixed tests
2 parents 2933899 + 51b35cf commit f0376ce

File tree

8 files changed

+354
-323
lines changed

8 files changed

+354
-323
lines changed

flowkit.go

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,16 @@ import (
3434
"github.com/onflow/flow-go-sdk"
3535
"github.com/onflow/flow-go-sdk/access/grpc"
3636
"github.com/onflow/flow-go-sdk/crypto"
37-
"github.com/pkg/errors"
38-
"github.com/tyler-smith/go-bip39"
39-
"golang.org/x/exp/maps"
40-
"golang.org/x/exp/slices"
41-
4237
"github.com/onflow/flowkit/accounts"
4338
"github.com/onflow/flowkit/config"
4439
"github.com/onflow/flowkit/gateway"
4540
"github.com/onflow/flowkit/output"
4641
"github.com/onflow/flowkit/project"
4742
"github.com/onflow/flowkit/transactions"
43+
"github.com/pkg/errors"
44+
"github.com/tyler-smith/go-bip39"
45+
"golang.org/x/exp/maps"
46+
"golang.org/x/exp/slices"
4847
)
4948

5049
// BlockQuery defines possible queries for block.
@@ -137,16 +136,16 @@ func (f *Flowkit) Ping() error {
137136
}
138137

139138
// GetAccount fetches account on the Flow network.
140-
func (f *Flowkit) GetAccount(_ context.Context, address flow.Address) (*flow.Account, error) {
141-
return f.gateway.GetAccount(address)
139+
func (f *Flowkit) GetAccount(ctx context.Context, address flow.Address) (*flow.Account, error) {
140+
return f.gateway.GetAccount(ctx, address)
142141
}
143142

144143
// CreateAccount on the Flow network with the provided keys and using the signer for creation transaction.
145144
// Returns the newly created account as well as the ID of the transaction that created the account.
146145
//
147146
// 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.
148147
func (f *Flowkit) CreateAccount(
149-
_ context.Context,
148+
ctx context.Context,
150149
signer *accounts.Account,
151150
keys []accounts.PublicKey,
152151
) (*flow.Account, flow.Identifier, error) {
@@ -176,7 +175,7 @@ func (f *Flowkit) CreateAccount(
176175
return nil, flow.EmptyID, err
177176
}
178177

179-
tx, err = f.prepareTransaction(tx, signer)
178+
tx, err = f.prepareTransaction(ctx, tx, signer)
180179
if err != nil {
181180
return nil, flow.EmptyID, err
182181
}
@@ -185,15 +184,15 @@ func (f *Flowkit) CreateAccount(
185184
f.logger.StartProgress("Creating account...")
186185
defer f.logger.StopProgress()
187186

188-
sentTx, err := f.gateway.SendSignedTransaction(tx.FlowTransaction())
187+
sentTx, err := f.gateway.SendSignedTransaction(ctx, tx.FlowTransaction())
189188
if err != nil {
190189
return nil, flow.EmptyID, errors.Wrap(err, "account creation transaction failed")
191190
}
192191

193192
f.logger.StartProgress("Waiting for transaction to be sealed...")
194193
defer f.logger.StopProgress()
195194

196-
result, err := f.gateway.GetTransactionResult(sentTx.ID(), true)
195+
result, err := f.gateway.GetTransactionResult(ctx, sentTx.ID(), true)
197196
if err != nil {
198197
return nil, flow.EmptyID, err
199198
}
@@ -208,7 +207,7 @@ func (f *Flowkit) CreateAccount(
208207
return nil, flow.EmptyID, fmt.Errorf("new account address couldn't be fetched")
209208
}
210209

211-
account, err := f.gateway.GetAccount(*newAccountAddress[0]) // we know it's the only and first event
210+
account, err := f.gateway.GetAccount(ctx, *newAccountAddress[0]) // we know it's the only and first event
212211
if err != nil {
213212
return nil, flow.EmptyID, err
214213
}
@@ -218,15 +217,16 @@ func (f *Flowkit) CreateAccount(
218217

219218
// prepareTransaction prepares transaction for sending with data from network
220219
func (f *Flowkit) prepareTransaction(
220+
ctx context.Context,
221221
tx *transactions.Transaction,
222222
account *accounts.Account,
223223
) (*transactions.Transaction, error) {
224-
block, err := f.gateway.GetLatestBlock()
224+
block, err := f.gateway.GetLatestBlock(ctx)
225225
if err != nil {
226226
return nil, err
227227
}
228228

229-
proposer, err := f.gateway.GetAccount(account.Address)
229+
proposer, err := f.gateway.GetAccount(ctx, account.Address)
230230
if err != nil {
231231
return nil, err
232232
}
@@ -311,7 +311,7 @@ func (f *Flowkit) AddContract(
311311
defer f.logger.StopProgress()
312312

313313
// check if contract exists on account
314-
flowAccount, err := f.gateway.GetAccount(account.Address)
314+
flowAccount, err := f.gateway.GetAccount(ctx, account.Address)
315315
if err != nil {
316316
return flow.EmptyID, false, err
317317
}
@@ -334,13 +334,13 @@ func (f *Flowkit) AddContract(
334334
}
335335
}
336336

337-
tx, err = f.prepareTransaction(tx, account)
337+
tx, err = f.prepareTransaction(ctx, tx, account)
338338
if err != nil {
339339
return flow.EmptyID, false, err
340340
}
341341

342342
// send transaction with contract
343-
sentTx, err := f.gateway.SendSignedTransaction(tx.FlowTransaction())
343+
sentTx, err := f.gateway.SendSignedTransaction(ctx, tx.FlowTransaction())
344344
if err != nil {
345345
return tx.FlowTransaction().ID(), false, fmt.Errorf("failed to send transaction to deploy a contract: %w", err)
346346
}
@@ -352,7 +352,7 @@ func (f *Flowkit) AddContract(
352352
}
353353

354354
// we wait for transaction to be sealed
355-
trx, err := f.gateway.GetTransactionResult(sentTx.ID(), true)
355+
trx, err := f.gateway.GetTransactionResult(ctx, sentTx.ID(), true)
356356
if err != nil {
357357
return tx.FlowTransaction().ID(), false, err
358358
}
@@ -391,12 +391,12 @@ func (f *Flowkit) AddContract(
391391
//
392392
// If removal is successful transaction ID is returned.
393393
func (f *Flowkit) RemoveContract(
394-
_ context.Context,
394+
ctx context.Context,
395395
account *accounts.Account,
396396
contractName string,
397397
) (flow.Identifier, error) {
398398
// check if contracts exists on the account
399-
flowAcc, err := f.gateway.GetAccount(account.Address)
399+
flowAcc, err := f.gateway.GetAccount(ctx, account.Address)
400400
if err != nil {
401401
return flow.EmptyID, err
402402
}
@@ -415,7 +415,7 @@ func (f *Flowkit) RemoveContract(
415415
return flow.EmptyID, err
416416
}
417417

418-
tx, err = f.prepareTransaction(tx, account)
418+
tx, err = f.prepareTransaction(ctx, tx, account)
419419
if err != nil {
420420
return flow.EmptyID, err
421421
}
@@ -425,12 +425,12 @@ func (f *Flowkit) RemoveContract(
425425
)
426426
defer f.logger.StopProgress()
427427

428-
sentTx, err := f.gateway.SendSignedTransaction(tx.FlowTransaction())
428+
sentTx, err := f.gateway.SendSignedTransaction(ctx, tx.FlowTransaction())
429429
if err != nil {
430430
return flow.EmptyID, err
431431
}
432432

433-
txr, err := f.gateway.GetTransactionResult(sentTx.ID(), true)
433+
txr, err := f.gateway.GetTransactionResult(ctx, sentTx.ID(), true)
434434
if err != nil {
435435
return flow.EmptyID, err
436436
}
@@ -444,15 +444,15 @@ func (f *Flowkit) RemoveContract(
444444
}
445445

446446
// GetBlock by the query from Flow blockchain. Query can define a block by ID, block by height or require the latest block.
447-
func (f *Flowkit) GetBlock(_ context.Context, query BlockQuery) (*flow.Block, error) {
447+
func (f *Flowkit) GetBlock(ctx context.Context, query BlockQuery) (*flow.Block, error) {
448448
var err error
449449
var block *flow.Block
450450
if query.Latest {
451-
block, err = f.gateway.GetLatestBlock()
451+
block, err = f.gateway.GetLatestBlock(ctx)
452452
} else if query.ID != nil {
453-
block, err = f.gateway.GetBlockByID(*query.ID)
453+
block, err = f.gateway.GetBlockByID(ctx, *query.ID)
454454
} else {
455-
block, err = f.gateway.GetBlockByHeight(query.Height)
455+
block, err = f.gateway.GetBlockByHeight(ctx, query.Height)
456456
}
457457

458458
if err != nil {
@@ -467,8 +467,8 @@ func (f *Flowkit) GetBlock(_ context.Context, query BlockQuery) (*flow.Block, er
467467
}
468468

469469
// GetCollection by the ID from Flow network.
470-
func (f *Flowkit) GetCollection(_ context.Context, ID flow.Identifier) (*flow.Collection, error) {
471-
return f.gateway.GetCollection(ID)
470+
func (f *Flowkit) GetCollection(ctx context.Context, ID flow.Identifier) (*flow.Collection, error) {
471+
return f.gateway.GetCollection(ctx, ID)
472472
}
473473

474474
// GetEvents from Flow network by their event name in the specified height interval defined by start and end inclusive.
@@ -478,7 +478,7 @@ func (f *Flowkit) GetCollection(_ context.Context, ID flow.Identifier) (*flow.Co
478478
// Providing worker value will produce faster response as the interval will be scanned concurrently. This parameter is optional,
479479
// if not provided only a single worker will be used.
480480
func (f *Flowkit) GetEvents(
481-
_ context.Context,
481+
ctx context.Context,
482482
names []string,
483483
startHeight uint64,
484484
endHeight uint64,
@@ -506,7 +506,7 @@ func (f *Flowkit) GetEvents(
506506
wg.Add(1)
507507
go func() {
508508
defer wg.Done()
509-
f.eventWorker(jobChan, results)
509+
f.eventWorker(ctx, jobChan, results)
510510
}()
511511
}
512512

@@ -536,9 +536,9 @@ func (f *Flowkit) GetEvents(
536536
return resultEvents, nil
537537
}
538538

539-
func (f *Flowkit) eventWorker(jobChan <-chan grpc.EventRangeQuery, results chan<- eventWorkerResult) {
539+
func (f *Flowkit) eventWorker(ctx context.Context, jobChan <-chan grpc.EventRangeQuery, results chan<- eventWorkerResult) {
540540
for q := range jobChan {
541-
blockEvents, err := f.gateway.GetEvents(q.Type, q.StartHeight, q.EndHeight)
541+
blockEvents, err := f.gateway.GetEvents(ctx, q.Type, q.StartHeight, q.EndHeight)
542542
if err != nil {
543543
results <- eventWorkerResult{nil, err}
544544
}
@@ -800,7 +800,7 @@ type Script struct {
800800

801801
// ExecuteScript on the Flow network and return the Cadence value as a result. The script is executed at the
802802
// block provided as part of the ScriptQuery value.
803-
func (f *Flowkit) ExecuteScript(_ context.Context, script Script, query ScriptQuery) (cadence.Value, error) {
803+
func (f *Flowkit) ExecuteScript(ctx context.Context, script Script, query ScriptQuery) (cadence.Value, error) {
804804
state, err := f.State()
805805
if err != nil {
806806
return nil, err
@@ -839,24 +839,24 @@ func (f *Flowkit) ExecuteScript(_ context.Context, script Script, query ScriptQu
839839
}
840840

841841
if query.Latest {
842-
return f.gateway.ExecuteScript(program.Code(), script.Args)
842+
return f.gateway.ExecuteScript(ctx, program.Code(), script.Args)
843843
} else if query.ID != flow.EmptyID {
844-
return f.gateway.ExecuteScriptAtID(program.Code(), script.Args, query.ID)
844+
return f.gateway.ExecuteScriptAtID(ctx, program.Code(), script.Args, query.ID)
845845
} else {
846-
return f.gateway.ExecuteScriptAtHeight(program.Code(), script.Args, query.Height)
846+
return f.gateway.ExecuteScriptAtHeight(ctx, program.Code(), script.Args, query.Height)
847847
}
848848
}
849849

850850
// GetTransactionByID from the Flow network including the transaction result. Using the waitSeal we can wait for the transaction to be sealed.
851851
func (f *Flowkit) GetTransactionByID(
852-
_ context.Context,
852+
ctx context.Context,
853853
ID flow.Identifier,
854854
waitSeal bool,
855855
) (*flow.Transaction, *flow.TransactionResult, error) {
856856
f.logger.StartProgress("Fetching Transaction...")
857857
defer f.logger.StopProgress()
858858

859-
tx, err := f.gateway.GetTransaction(ID)
859+
tx, err := f.gateway.GetTransaction(ctx, ID)
860860
if err != nil {
861861
return nil, nil, err
862862
}
@@ -866,20 +866,20 @@ func (f *Flowkit) GetTransactionByID(
866866
defer f.logger.StopProgress()
867867
}
868868

869-
result, err := f.gateway.GetTransactionResult(ID, waitSeal)
869+
result, err := f.gateway.GetTransactionResult(ctx, ID, waitSeal)
870870
return tx, result, err
871871
}
872872

873873
func (f *Flowkit) GetTransactionsByBlockID(
874-
_ context.Context,
874+
ctx context.Context,
875875
blockID flow.Identifier,
876876
) ([]*flow.Transaction, []*flow.TransactionResult, error) {
877-
tx, err := f.gateway.GetTransactionsByBlockID(blockID)
877+
tx, err := f.gateway.GetTransactionsByBlockID(ctx, blockID)
878878
if err != nil {
879879
return nil, nil, err
880880
}
881881

882-
txRes, err := f.gateway.GetTransactionResultsByBlockID(blockID)
882+
txRes, err := f.gateway.GetTransactionResultsByBlockID(ctx, blockID)
883883
if err != nil {
884884
return nil, nil, err
885885
}
@@ -890,7 +890,7 @@ func (f *Flowkit) GetTransactionsByBlockID(
890890
//
891891
// AddressesRoles type defines the address for each role (payer, proposer, authorizers) and the script defines the transaction content.
892892
func (f *Flowkit) BuildTransaction(
893-
_ context.Context,
893+
ctx context.Context,
894894
addresses transactions.AddressesRoles,
895895
proposerKeyIndex int,
896896
script Script,
@@ -901,12 +901,12 @@ func (f *Flowkit) BuildTransaction(
901901
return nil, err
902902
}
903903

904-
latestBlock, err := f.gateway.GetLatestBlock()
904+
latestBlock, err := f.gateway.GetLatestBlock(ctx)
905905
if err != nil {
906906
return nil, fmt.Errorf("failed to get latest sealed block: %w", err)
907907
}
908908

909-
proposerAccount, err := f.gateway.GetAccount(addresses.Proposer)
909+
proposerAccount, err := f.gateway.GetAccount(ctx, addresses.Proposer)
910910
if err != nil {
911911
return nil, err
912912
}
@@ -986,15 +986,15 @@ func (f *Flowkit) SignTransactionPayload(
986986
//
987987
// You can build the transaction using the BuildTransaction method and then sign it using the SignTranscation method.
988988
func (f *Flowkit) SendSignedTransaction(
989-
_ context.Context,
989+
ctx context.Context,
990990
tx *transactions.Transaction,
991991
) (*flow.Transaction, *flow.TransactionResult, error) {
992-
sentTx, err := f.gateway.SendSignedTransaction(tx.FlowTransaction())
992+
sentTx, err := f.gateway.SendSignedTransaction(ctx, tx.FlowTransaction())
993993
if err != nil {
994994
return nil, nil, err
995995
}
996996

997-
res, err := f.gateway.GetTransactionResult(sentTx.ID(), true)
997+
res, err := f.gateway.GetTransactionResult(ctx, sentTx.ID(), true)
998998
if err != nil {
999999
return nil, nil, err
10001000
}
@@ -1037,7 +1037,7 @@ func (f *Flowkit) SendTransaction(
10371037
f.logger.StartProgress("Sending transaction...")
10381038
defer f.logger.StopProgress()
10391039

1040-
sentTx, err := f.gateway.SendSignedTransaction(tx.FlowTransaction())
1040+
sentTx, err := f.gateway.SendSignedTransaction(ctx, tx.FlowTransaction())
10411041
if err != nil {
10421042
return nil, nil, err
10431043
}
@@ -1046,7 +1046,7 @@ func (f *Flowkit) SendTransaction(
10461046
f.logger.StartProgress("Waiting for transaction to be sealed...")
10471047
defer f.logger.StopProgress()
10481048

1049-
res, err := f.gateway.GetTransactionResult(sentTx.ID(), true)
1049+
res, err := f.gateway.GetTransactionResult(ctx, sentTx.ID(), true)
10501050

10511051
return sentTx, res, err
10521052
}

0 commit comments

Comments
 (0)