-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
65 lines (58 loc) · 1.93 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
package main
import (
"context"
"fmt"
"github.com/tonkeeper/opentonapi/tonapi"
)
func subscribeToMempool(token string) {
streamingAPI := tonapi.NewStreamingAPI(tonapi.WithStreamingToken(token))
for {
err := streamingAPI.SubscribeToMempool(context.Background(),
func(data tonapi.MempoolEventData) {
fmt.Printf("mempool event: %#v\n", len(data.BOC))
})
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"},
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 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)
select {}
}