-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.go
70 lines (62 loc) · 1.87 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
package main
import (
"context"
"fmt"
"log"
"github.com/tonkeeper/tonapi-go"
)
func main() {
accounts := []string{"-1:5555555555555555555555555555555555555555555555555555555555555555"}
// 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.
//
token := ""
streamingAPI := tonapi.NewStreamingAPI(tonapi.WithStreamingToken(token))
err := streamingAPI.WebsocketHandleRequests(context.Background(), func(ws tonapi.Websocket) error {
ws.SetMempoolHandler(func(data tonapi.MempoolEventData) {
fmt.Printf("new mempool event\n")
})
ws.SetTransactionHandler(func(data tonapi.TransactionEventData) {
fmt.Printf("New tx with hash: %v\n", data.TxHash)
})
ws.SetTraceHandler(func(data tonapi.TraceEventData) {
fmt.Printf("New trace with hash: %v\n", data.Hash)
})
ws.SetBlockHandler(func(data tonapi.BlockEventData) {
fmt.Printf("New block: (%v,%v,%v)\n", data.Workchain, data.Shard, data.Seqno)
})
if err := ws.SubscribeToMempool(nil); err != nil {
return err
}
if err := ws.SubscribeToTransactions(accounts, nil); err != nil {
return err
}
if err := ws.SubscribeToTraces(accounts); err != nil {
return err
}
masterchain := -1
if err := ws.SubscribeToBlocks(&masterchain); err != nil {
return err
}
// It is possible to run a loop updating subscription on the go:
//
// subscribeCh := make(chan []string) // channel to send accounts to subscribe.
// for {
// select {
// case accounts := <-subscribeCh:
// if err := ws.SubscribeToTransactions(accounts); err != nil {
// return err
// }
// if err := ws.SubscribeToTraces(accounts); err != nil {
// return err
// }
// case <-ctx.Done():
// return nil
// }
//}
return nil
})
if err != nil {
log.Fatalf("connection error: %v", err)
}
}