/
main-eos.go
152 lines (128 loc) · 3.68 KB
/
main-eos.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// CODE:BEGIN:quickstarts_go_eos_section1
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
pb "github.com/EOS-Nation/dfuse-docs/quickstarts/go/pb"
"github.com/tidwall/gjson"
"golang.org/x/oauth2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/oauth"
)
// CODE:END:quickstarts_go_eos_section1
// CODE:BEGIN:quickstarts_go_eos_section2
func getToken(apiKey string) (token string, expiration time.Time, err error) {
reqBody := bytes.NewBuffer([]byte(fmt.Sprintf(`{"api_key":"%s"}`, apiKey)))
resp, err := http.Post("https://auth.eosnation.io/v1/auth/issue", "application/json", reqBody)
if err != nil {
err = fmt.Errorf("unable to obtain token: %s", err)
return
}
if resp.StatusCode != 200 {
err = fmt.Errorf("unable to obtain token, status not 200, got %d: %s", resp.StatusCode, reqBody.String())
return
}
if body, err := ioutil.ReadAll(resp.Body); err == nil {
token = gjson.GetBytes(body, "token").String()
expiration = time.Unix(gjson.GetBytes(body, "expires_at").Int(), 0)
}
return
}
// CODE:END:quickstarts_go_eos_section2
// CODE:BEGIN:quickstarts_go_eos_section3
func createClient(endpoint string) pb.GraphQLClient {
dfuseAPIKey := os.Getenv("DFUSE_API_KEY")
if dfuseAPIKey == "" {
panic("you must specify a DFUSE_API_KEY environment variable")
}
token, _, err := getToken(dfuseAPIKey)
panicIfError(err)
credential := oauth.NewOauthAccess(&oauth2.Token{AccessToken: token, TokenType: "Bearer"})
transportCreds := credentials.NewClientTLSFromCert(nil, "")
conn, err := grpc.Dial(endpoint,
grpc.WithPerRPCCredentials(credential),
grpc.WithTransportCredentials(transportCreds),
)
panicIfError(err)
return pb.NewGraphQLClient(conn)
}
// CODE:END:quickstarts_go_eos_section3
//
/// EOSIO
//
// CODE:BEGIN:quickstarts_go_eos_section4
const operationEOS = `subscription {
searchTransactionsForward(query:"receiver:eosio.token action:transfer -data.quantity:'0.0001 EOS'") {
undo cursor
trace { id matchingActions { json } }
}
}`
type eosioDocument struct {
SearchTransactionsForward struct {
Cursor string
Undo bool
Trace struct {
ID string
MatchingActions []struct {
JSON map[string]interface{}
}
}
}
}
// CODE:END:quickstarts_go_eos_section4
// CODE:BEGIN:quickstarts_go_eos_section5
func streamEOSIO(ctx context.Context) {
/* The client can be re-used for all requests, cache it at the appropriate level */
client := createClient("eos.dfuse.eosnation.io:9000")
executor, err := client.Execute(ctx, &pb.Request{Query: operationEOS})
panicIfError(err)
for {
resp, err := executor.Recv()
panicIfError(err)
if len(resp.Errors) > 0 {
for _, err := range resp.Errors {
fmt.Printf("Request failed: %s\n", err)
}
/* We continue here, but you could take another decision here, like exiting the process */
continue
}
document := &eosioDocument{}
err = json.Unmarshal([]byte(resp.Data), document)
panicIfError(err)
result := document.SearchTransactionsForward
reverted := ""
if result.Undo {
reverted = " REVERTED"
}
for _, action := range result.Trace.MatchingActions {
data := action.JSON
fmt.Printf("Transfer %s -> %s [%s]%s\n", data["from"], data["to"], data["quantity"], reverted)
}
}
}
// CODE:END:quickstarts_go_eos_section5
/* DFUSE_API_KEY="server_abcdef12345678900000000000" go run main.go eosio */
// CODE:BEGIN:quickstarts_go_eos_section6
func main() {
proto := ""
if len(os.Args) >= 2 {
proto = os.Args[1]
}
switch proto {
default:
streamEOSIO(context.Background())
}
}
func panicIfError(err error) {
if err != nil {
panic(err)
}
}
// CODE:END:quickstarts_go_eos_section6