-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
94 lines (85 loc) · 2.88 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/tonkeeper/tonapi-go"
)
func intPointer(x int) *int {
return &x
}
func subscribeToMempool(token string) {
streamingAPI := tonapi.NewStreamingAPI(tonapi.WithStreamingToken(token))
for {
err := streamingAPI.SubscribeToMempool(context.Background(),
// this "accounts" parameter is optional,
// if not set, you will receive all mempool events.
// if defined, you will receive only mempool events that involve these accounts.
[]string{"-1:5555555555555555555555555555555555555555555555555555555555555555"},
func(data tonapi.MempoolEventData) {
value, _ := json.Marshal(data)
fmt.Printf("mempool event: %#v\n", value)
})
if err != nil {
fmt.Printf("mempool error: %v, reconnecting...\n", err)
}
}
}
func subscribeToTransactions(token string) {
streamingAPI := tonapi.NewStreamingAPI(tonapi.WithStreamingToken(token))
for {
err := streamingAPI.SubscribeToTransactions(context.Background(),
[]string{"-1:5555555555555555555555555555555555555555555555555555555555555555"},
// this "operations" is optional,
// if not set, you will receive all transactions.
// if defined, you will receive only transactions with these operations.
nil,
func(data tonapi.TransactionEventData) {
fmt.Printf("New tx with hash: %v\n", data.TxHash)
})
if err != nil {
fmt.Printf("tx error: %v, reconnecting...\n", err)
}
}
}
func subscribeToTraces(token string) {
streamingAPI := tonapi.NewStreamingAPI(tonapi.WithStreamingToken(token))
for {
err := streamingAPI.SubscribeToTraces(context.Background(), []string{"-1:5555555555555555555555555555555555555555555555555555555555555555"},
func(data tonapi.TraceEventData) {
fmt.Printf("New trace with hash: %v\n", data.Hash)
})
if err != nil {
fmt.Printf("trace error: %v, reconnecting...\n", err)
}
}
}
func subscribeToBlocks(token string) {
streamingAPI := tonapi.NewStreamingAPI(tonapi.WithStreamingToken(token))
for {
err := streamingAPI.SubscribeToBlocks(context.Background(), intPointer(-1),
func(data tonapi.BlockEventData) {
fmt.Printf("New block: (%v,%v,%v)\n", data.Workchain, data.Shard, data.Seqno)
})
if err != nil {
fmt.Printf("block error: %v, reconnecting...\n", err)
}
}
}
func main() {
// When working with tonapi.io, you should consider getting an API key at https://tonconsole.com/
// because tonapi.io has per-ip limits for sse and websocket connections.
//
// You can configure it with:
// streamingAPI = tonapi.NewStreamingAPI(tonapi.WithStreamingToken("<private-key>"))
//
// To work with a local version of tonapi.io (opentonapi) use:
// streamingAPI = tonapi.NewStreamingAPI(tonapi.WithStreamingEndpoint("http://127.0.0.1:8081"))
//
token := ""
go subscribeToTraces(token)
go subscribeToMempool(token)
go subscribeToTransactions(token)
go subscribeToBlocks(token)
select {}
}