Skip to content

Commit 52ad1c9

Browse files
Update examples to include blocks streaming
1 parent d367716 commit 52ad1c9

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

examples/sse/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/tonkeeper/tonapi-go/examples/sse
22

3-
go 1.20
3+
go 1.21.3
44

5-
require github.com/tonkeeper/tonapi-go v0.0.2
5+
require github.com/tonkeeper/tonapi-go v0.0.3
66

77
require (
88
github.com/dlclark/regexp2 v1.10.0 // indirect

examples/sse/go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
5151
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5252
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
5353
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
54-
github.com/tonkeeper/tonapi-go v0.0.1 h1:miD7wFpocflt0seNhJ4kKhiXsWkozwotO2gJGlib4C0=
55-
github.com/tonkeeper/tonapi-go v0.0.1/go.mod h1:2n+LizNjCQbCZhvVSooKGnzxAgdIrgePsh6Kgea0NPY=
56-
github.com/tonkeeper/tonapi-go v0.0.2/go.mod h1:2n+LizNjCQbCZhvVSooKGnzxAgdIrgePsh6Kgea0NPY=
54+
github.com/tonkeeper/tonapi-go v0.0.3 h1:T0C40m4OnGJmR17ijRBFDBqlJm3IktjP3JOi8VRXZWk=
55+
github.com/tonkeeper/tonapi-go v0.0.3/go.mod h1:2n+LizNjCQbCZhvVSooKGnzxAgdIrgePsh6Kgea0NPY=
5756
github.com/tonkeeper/tongo v1.4.1 h1:QMIya/ongFuOyHgVOFmHF+Cu4nt23KumPRLApmp90zE=
5857
github.com/tonkeeper/tongo v1.4.1/go.mod h1:LdOBjpUz6vLp1EdX3E0XLNks9YI5XMSqaQahfOMrBEY=
5958
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=

examples/sse/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"github.com/tonkeeper/tonapi-go"
99
)
1010

11+
func intPointer(x int) *int {
12+
return &x
13+
}
14+
1115
func subscribeToMempool(token string) {
1216
streamingAPI := tonapi.NewStreamingAPI(tonapi.WithStreamingToken(token))
1317
for {
@@ -57,6 +61,19 @@ func subscribeToTraces(token string) {
5761
}
5862
}
5963

64+
func subscribeToBlocks(token string) {
65+
streamingAPI := tonapi.NewStreamingAPI(tonapi.WithStreamingToken(token))
66+
for {
67+
err := streamingAPI.SubscribeToBlocks(context.Background(), intPointer(-1),
68+
func(data tonapi.BlockEventData) {
69+
fmt.Printf("New block: (%v,%v,%v)\n", data.Workchain, data.Shard, data.Seqno)
70+
})
71+
if err != nil {
72+
fmt.Printf("block error: %v, reconnecting...\n", err)
73+
}
74+
}
75+
}
76+
6077
func main() {
6178
// When working with tonapi.io, you should consider getting an API key at https://tonconsole.com/
6279
// because tonapi.io has per-ip limits for sse and websocket connections.
@@ -72,5 +89,6 @@ func main() {
7289
go subscribeToTraces(token)
7390
go subscribeToMempool(token)
7491
go subscribeToTransactions(token)
92+
go subscribeToBlocks(token)
7593
select {}
7694
}

examples/websocket/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/tonkeeper/tonapi-go/examples/websocket
22

3-
go 1.20
3+
go 1.21.3
44

5-
require github.com/tonkeeper/tonapi-go v0.0.2
5+
require github.com/tonkeeper/tonapi-go v0.0.3
66

77
require (
88
github.com/dlclark/regexp2 v1.10.0 // indirect

examples/websocket/go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
5151
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5252
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
5353
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
54-
github.com/tonkeeper/tonapi-go v0.0.1 h1:miD7wFpocflt0seNhJ4kKhiXsWkozwotO2gJGlib4C0=
55-
github.com/tonkeeper/tonapi-go v0.0.1/go.mod h1:2n+LizNjCQbCZhvVSooKGnzxAgdIrgePsh6Kgea0NPY=
56-
github.com/tonkeeper/tonapi-go v0.0.2/go.mod h1:2n+LizNjCQbCZhvVSooKGnzxAgdIrgePsh6Kgea0NPY=
54+
github.com/tonkeeper/tonapi-go v0.0.3 h1:T0C40m4OnGJmR17ijRBFDBqlJm3IktjP3JOi8VRXZWk=
55+
github.com/tonkeeper/tonapi-go v0.0.3/go.mod h1:2n+LizNjCQbCZhvVSooKGnzxAgdIrgePsh6Kgea0NPY=
5756
github.com/tonkeeper/tongo v1.4.1 h1:QMIya/ongFuOyHgVOFmHF+Cu4nt23KumPRLApmp90zE=
5857
github.com/tonkeeper/tongo v1.4.1/go.mod h1:LdOBjpUz6vLp1EdX3E0XLNks9YI5XMSqaQahfOMrBEY=
5958
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=

examples/websocket/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ func main() {
2929
ws.SetTraceHandler(func(data tonapi.TraceEventData) {
3030
fmt.Printf("New trace with hash: %v\n", data.Hash)
3131
})
32+
ws.SetBlockHandler(func(data tonapi.BlockEventData) {
33+
fmt.Printf("New block: (%v,%v,%v)\n", data.Workchain, data.Shard, data.Seqno)
34+
})
3235

33-
if err := ws.SubscribeToMempool(accounts); err != nil {
36+
if err := ws.SubscribeToMempool(nil); err != nil {
3437
return err
3538
}
3639
if err := ws.SubscribeToTransactions(accounts, nil); err != nil {
@@ -39,6 +42,10 @@ func main() {
3942
if err := ws.SubscribeToTraces(accounts); err != nil {
4043
return err
4144
}
45+
masterchain := -1
46+
if err := ws.SubscribeToBlocks(&masterchain); err != nil {
47+
return err
48+
}
4249
// It is possible to run a loop updating subscription on the go:
4350
//
4451
// subscribeCh := make(chan []string) // channel to send accounts to subscribe.

0 commit comments

Comments
 (0)