Skip to content

Commit bbbded5

Browse files
Merge pull request #794 from The-K-R-O-K/illia-malachyn/784-add-more-examples
Add examples for new grpc endpoints
2 parents 1ba8047 + 7c952ed commit bbbded5

File tree

11 files changed

+410
-53
lines changed

11 files changed

+410
-53
lines changed

examples/Makefile

+20-1
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,23 @@ stream-events-reconnect:
111111

112112
.PHONY: stream-execution-data
113113
stream-execution-data:
114-
go run ./stream_execution_data/main.go
114+
go run ./stream_execution_data/main.go
115+
.PHONY: stream-blocks
116+
stream-blocks:
117+
go run ./stream_blocks/main.go
118+
119+
.PHONY: stream-block-headers
120+
stream-block-headers:
121+
go run ./stream_block_headers/main.go
122+
123+
.PHONY: stream-block-digests
124+
stream-block-digests:
125+
go run ./stream_block_digests/main.go
126+
127+
.PHONY: stream-account-statuses
128+
stream-account-statuses:
129+
go run ./stream_account_statuses/main.go
130+
131+
.PHONY: send-and-subscribe-transaction-statuses
132+
send-and-subscribe-transaction-statuses:
133+
go run ./send_and_subscribe_transaction_statuses/main.go

examples/examples.go

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"log"
2626
"os"
27+
"reflect"
2728
"strings"
2829
"time"
2930

@@ -302,3 +303,8 @@ func WaitForSeal(ctx context.Context, c access.Client, id flow.Identifier) *flow
302303
fmt.Printf("Transaction %s sealed\n", id)
303304
return result
304305
}
306+
307+
func Print[T any](object T) {
308+
fmt.Printf("Got new %s:\n", reflect.TypeOf(object).Name())
309+
fmt.Printf("%+v\n\n", object)
310+
}

examples/go.mod

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ require (
1111
github.com/onflow/flow-go-sdk v1.2.2
1212
github.com/onflow/flowkit v1.19.0
1313
github.com/spf13/afero v1.11.0
14-
google.golang.org/grpc v1.63.2
14+
google.golang.org/grpc v1.64.1
1515
)
1616

1717
require (
18-
cloud.google.com/go/compute v1.24.0 // indirect
19-
cloud.google.com/go/compute/metadata v0.2.3 // indirect
18+
cloud.google.com/go/compute/metadata v0.5.0 // indirect
2019
cloud.google.com/go/iam v1.1.6 // indirect
2120
cloud.google.com/go/kms v1.15.7 // indirect
2221
github.com/SaveTheRbtz/mph v0.1.1-0.20240117162131-4166ec7869bc // indirect
@@ -101,8 +100,8 @@ require (
101100
go.uber.org/goleak v1.3.0 // indirect
102101
golang.org/x/crypto v0.28.0 // indirect
103102
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
104-
golang.org/x/net v0.25.0 // indirect
105-
golang.org/x/oauth2 v0.17.0 // indirect
103+
golang.org/x/net v0.26.0 // indirect
104+
golang.org/x/oauth2 v0.18.0 // indirect
106105
golang.org/x/sync v0.8.0 // indirect
107106
golang.org/x/sys v0.26.0 // indirect
108107
golang.org/x/text v0.19.0 // indirect
@@ -112,9 +111,9 @@ require (
112111
google.golang.org/api v0.162.0 // indirect
113112
google.golang.org/appengine v1.6.8 // indirect
114113
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
115-
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
116-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
117-
google.golang.org/protobuf v1.33.0 // indirect
114+
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
115+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
116+
google.golang.org/protobuf v1.34.2 // indirect
118117
gopkg.in/ini.v1 v1.67.0 // indirect
119118
gopkg.in/yaml.v3 v3.0.1 // indirect
120119
lukechampine.com/blake3 v1.3.0 // indirect

examples/go.sum

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
cloud.google.com/go v0.112.0 h1:tpFCD7hpHFlQ8yPwT3x+QeXqc2T6+n6T+hmABHfDUSM=
33
cloud.google.com/go v0.112.0/go.mod h1:3jEEVwZ/MHU4djK5t5RHuKOA/GbLddgTdVubX1qnPD4=
4-
cloud.google.com/go/compute v1.24.0 h1:phWcR2eWzRJaL/kOiJwfFsPs4BaKq1j6vnpZrc1YlVg=
5-
cloud.google.com/go/compute v1.24.0/go.mod h1:kw1/T+h/+tK2LJK0wiPPx1intgdAM3j/g3hFDlscY40=
6-
cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
7-
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
4+
cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY=
5+
cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY=
86
cloud.google.com/go/iam v1.1.6 h1:bEa06k05IO4f4uJonbB5iAgKTPpABy1ayxaIZV/GHVc=
97
cloud.google.com/go/iam v1.1.6/go.mod h1:O0zxdPeGBoFdWW3HWmBxJsk0pfvNM/p/qa82rWOGTwI=
108
cloud.google.com/go/kms v1.15.7 h1:7caV9K3yIxvlQPAcaFffhlT7d1qpxjB1wHBtjWa13SM=
@@ -35,8 +33,8 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
3533
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
3634
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
3735
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
38-
github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa h1:jQCWAUqqlij9Pgj2i/PB79y4KOPYVyFYdROxgaCwdTQ=
39-
github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa/go.mod h1:x/1Gn8zydmfq8dk6e9PdstVsDgu9RuyIIJqAaF//0IM=
36+
github.com/cncf/xds/go v0.0.0-20240318125728-8a4994d93e50 h1:DBmgJDC9dTfkVyGgipamEh2BpGYxScCH1TOF1LL1cXc=
37+
github.com/cncf/xds/go v0.0.0-20240318125728-8a4994d93e50/go.mod h1:5e1+Vvlzido69INQaVO6d87Qn543Xr6nooe9Kz7oBFM=
4038
github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I=
4139
github.com/cockroachdb/errors v1.11.3/go.mod h1:m4UIW4CDjx+R5cybPsNrRbreomiFqt8o1h1wUVazSd8=
4240
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce h1:giXvy4KSc/6g/esnpM7Geqxka4WSqI1SZc7sMJFd3y4=
@@ -477,11 +475,11 @@ golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLL
477475
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
478476
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
479477
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
480-
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
481-
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
478+
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
479+
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
482480
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
483-
golang.org/x/oauth2 v0.17.0 h1:6m3ZPmLEFdVxKKWnKq4VqZ60gutO35zm+zrAHVmHyDQ=
484-
golang.org/x/oauth2 v0.17.0/go.mod h1:OzPDGQiuQMguemayvdylqddI7qcD9lnSDb+1FiwQ5HA=
481+
golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI=
482+
golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8=
485483
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
486484
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
487485
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -548,18 +546,18 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
548546
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
549547
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de h1:F6qOa9AZTYJXOUEr4jDysRDLrm4PHePlge4v4TGAlxY=
550548
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:VUhTRKeHn9wwcdrk73nvdC9gF178Tzhmt/qyaFcPLSo=
551-
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de h1:jFNzHPIeuzhdRwVhbZdiym9q0ory/xY3sA+v2wPg8I0=
552-
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:5iCWqnniDlqZHrd3neWVTOwvh/v6s3232omMecelax8=
553-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
554-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
549+
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 h1:wKguEg1hsxI2/L3hUYrpo1RVi48K+uTyzKqprwLXsb8=
550+
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142/go.mod h1:d6be+8HhtEtucleCbxpPW9PA9XwISACu8nvpPqF0BVo=
551+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs=
552+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
555553
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
556554
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
557555
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
558556
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
559557
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
560558
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
561-
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
562-
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
559+
google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA=
560+
google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0=
563561
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
564562
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
565563
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
@@ -571,8 +569,8 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
571569
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
572570
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
573571
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
574-
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
575-
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
572+
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
573+
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
576574
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
577575
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
578576
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Flow Go SDK
3+
*
4+
* Copyright Flow Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package main
20+
21+
import (
22+
"context"
23+
"fmt"
24+
25+
"github.com/onflow/flow-go-sdk/access/grpc"
26+
27+
"github.com/onflow/flow-go-sdk"
28+
"github.com/onflow/flow-go-sdk/examples"
29+
)
30+
31+
func main() {
32+
demo()
33+
}
34+
35+
func demo() {
36+
ctx := context.Background()
37+
flowClient, err := grpc.NewClient(grpc.EmulatorHost)
38+
examples.Handle(err)
39+
40+
serviceAcctAddr, serviceAcctKey, serviceSigner := examples.ServiceAccount(flowClient)
41+
42+
tx := flow.NewTransaction().
43+
SetPayer(serviceAcctAddr).
44+
SetProposalKey(serviceAcctAddr, serviceAcctKey.Index, serviceAcctKey.SequenceNumber).
45+
SetScript([]byte(`
46+
transaction {
47+
prepare(acc: &Account) {}
48+
execute {
49+
log("test")
50+
}
51+
}
52+
`)).
53+
AddAuthorizer(serviceAcctAddr).
54+
SetReferenceBlockID(examples.GetReferenceBlockId(flowClient))
55+
56+
err = tx.SignEnvelope(serviceAcctAddr, serviceAcctKey.Index, serviceSigner)
57+
examples.Handle(err)
58+
59+
txResultChan, errChan, initErr := flowClient.SendAndSubscribeTransactionStatuses(ctx, *tx)
60+
examples.Handle(initErr)
61+
62+
select {
63+
case <-ctx.Done():
64+
return
65+
case txResult, ok := <-txResultChan:
66+
if !ok {
67+
panic("transaction result channel is closed")
68+
}
69+
examples.Print(txResult)
70+
case err, ok := <-errChan:
71+
if !ok {
72+
panic("error channel is closed")
73+
}
74+
fmt.Printf("~~~ ERROR: %s ~~~\n", err.Error())
75+
}
76+
}
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Flow Go SDK
3+
*
4+
* Copyright Flow Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package main
20+
21+
import (
22+
"context"
23+
"fmt"
24+
25+
"github.com/onflow/flow-go-sdk/access/grpc"
26+
27+
"github.com/onflow/flow-go-sdk"
28+
"github.com/onflow/flow-go-sdk/examples"
29+
)
30+
31+
func main() {
32+
demo()
33+
}
34+
35+
func demo() {
36+
ctx := context.Background()
37+
flowClient, err := grpc.NewClient("access.testnet.nodes.onflow.org:9000")
38+
examples.Handle(err)
39+
40+
header, err := flowClient.GetLatestBlockHeader(ctx, true)
41+
examples.Handle(err)
42+
fmt.Printf("Latest block height: %d\n", header.Height)
43+
fmt.Printf("Latest block ID: %s\n", header.ID)
44+
45+
flowEVMTestnetAddress := "0x8c5303eaa26202d6"
46+
filter := flow.AccountStatusFilter{
47+
EventFilter: flow.EventFilter{
48+
Addresses: []string{flowEVMTestnetAddress},
49+
},
50+
}
51+
accountStatusesChan, errChan, initErr := flowClient.SubscribeAccountStatusesFromStartBlockID(ctx, header.ID, filter)
52+
examples.Handle(initErr)
53+
54+
for {
55+
select {
56+
case <-ctx.Done():
57+
return
58+
case accountStatus, ok := <-accountStatusesChan:
59+
if !ok {
60+
panic("account statuses channel is closed")
61+
}
62+
examples.Print(accountStatus)
63+
case err, ok := <-errChan:
64+
if !ok {
65+
panic("error channel is closed")
66+
}
67+
fmt.Printf("~~~ ERROR: %s ~~~\n", err.Error())
68+
}
69+
}
70+
}

examples/stream_block_digests/main.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Flow Go SDK
3+
*
4+
* Copyright Flow Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package main
20+
21+
import (
22+
"context"
23+
"fmt"
24+
25+
"github.com/onflow/flow-go-sdk/access/grpc"
26+
27+
"github.com/onflow/flow-go-sdk"
28+
"github.com/onflow/flow-go-sdk/examples"
29+
)
30+
31+
func main() {
32+
demo()
33+
}
34+
35+
func demo() {
36+
ctx := context.Background()
37+
flowClient, err := grpc.NewClient("access.testnet.nodes.onflow.org:9000")
38+
examples.Handle(err)
39+
40+
header, err := flowClient.GetLatestBlockHeader(ctx, true)
41+
examples.Handle(err)
42+
fmt.Printf("Block Height: %d\n", header.Height)
43+
fmt.Printf("Block ID: %s\n", header.ID)
44+
45+
blockDigestsChan, errChan, initErr := flowClient.SubscribeBlockDigestsFromStartBlockID(ctx, header.ID, flow.BlockStatusFinalized)
46+
examples.Handle(initErr)
47+
48+
for {
49+
select {
50+
case <-ctx.Done():
51+
return
52+
case blockDigest, ok := <-blockDigestsChan:
53+
if !ok {
54+
panic("block digest channel is closed")
55+
}
56+
examples.Print(blockDigest)
57+
case err, ok := <-errChan:
58+
if !ok {
59+
panic("error channel is closed")
60+
}
61+
fmt.Printf("~~~ ERROR: %s ~~~\n", err.Error())
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)