This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
forked from dfuse-io/dfuse-eosio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_eos_client_test.go
103 lines (85 loc) · 2.39 KB
/
example_eos_client_test.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
95
96
97
98
99
100
101
102
103
package searchclient_test
import (
"context"
"fmt"
"io"
"os"
"github.com/dfuse-io/dfuse-eosio/trxdb"
searchclient "github.com/dfuse-io/dfuse-eosio/search-client"
"github.com/streamingfast/dgrpc"
"github.com/streamingfast/logging"
pbsearch "github.com/streamingfast/pbgo/dfuse/search/v1"
"go.uber.org/zap"
)
func init() {
if os.Getenv("DEBUG") != "" {
logger, _ := zap.NewDevelopment()
logging.Override(logger)
}
}
// Remove the leading # just before 'Output' at very end of ExampleEOSClient see actual execution results!
func ExampleEOSClient() {
kvdbDSN, searchAddr, err := getEOSConfig()
if err != nil {
fmt.Println(err)
return
}
dbReader, err := trxdb.New(kvdbDSN)
if err != nil {
fmt.Println(fmt.Errorf("unable to create EOS database instance: %w", err))
return
}
searchConn, err := dgrpc.NewInternalClient(searchAddr)
if err != nil {
fmt.Println(fmt.Errorf("unable to create search connection: %w", err))
return
}
client := searchclient.NewEOSClient(searchConn, dbReader)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := client.StreamMatches(ctx, &pbsearch.RouterRequest{
Query: "account:eosio.token action:transfer",
LowBlockNum: 110521022,
HighBlockNum: 110521023,
Limit: 5,
WithReversible: true,
Mode: pbsearch.RouterRequest_STREAMING,
})
if err != nil {
fmt.Println("Unable to stream matches", err)
return
}
for {
match, err := stream.Recv()
if err != nil {
if err != io.EOF {
fmt.Printf("Client error: %s\n", err)
}
return
}
if match.TransactionTrace == nil {
fmt.Printf("Live marker at block %s, continuing\n", match.BlockID)
continue
}
fmt.Printf("Match %s (%s), %d actions\n",
match.TransactionTrace.Id,
match.BlockID,
len(match.MatchingActions),
)
for _, action := range match.MatchingActions {
fmt.Printf(" - Action #%d - %s\n", action.ActionOrdinal, action.SimpleName())
}
}
// #Output: any
}
func getEOSConfig() (kvdbDSN, searchAddr string, err error) {
kvdbDSN = os.Getenv("KVDB_DSN")
if kvdbDSN == "" {
return "", "", fmt.Errorf("the environment variable KVDB_DSN must be set, for example 'bigtable://dfuse-io.dfuse-saas/mainnet-v4'")
}
searchAddr = os.Getenv("SEARCH_ADDR")
if searchAddr == "" {
return "", "", fmt.Errorf("the environment variable SEARCH_ADDR must be set, for example 'localhost:9001'")
}
return
}