-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (158 loc) · 4.55 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"log"
"net/http"
"flag"
"math/big"
"strconv"
"github.com/ethereum/go-ethereum/core/types"
"encoding/json"
)
var serverMode = flag.String("serverMode","production","Set to 'testing' to enable debug access.")
var server = flag.String("host", "http://localhost:8545", "Set server host.")
//var server = "https://mainnet.infura.io"
var fromBlock = flag.Int("fromBlock", 0, "Set to block to start server queries from.")
func syncHandler(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, "Node sync status requested\n")
cl, err := ethclient.Dial(*server)
if err != nil {
log.Panic("Connection Error: ", err)
}
ctx := context.Background()
prog, err := cl.SyncProgress(ctx)
if err != nil {
log.Panic("Error Fetching Sync Status: ", err)
}
if prog == nil {
fmt.Fprint(w, "Syncing complete!\n")
} else {
fmt.Fprint(w, "Current Block: ", prog.CurrentBlock, "\nHighestBlock: ", prog.HighestBlock)
}
}
func addressHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Address Balance Requested: ", r.URL.Path[6:], "\n")
cl, err := ethclient.Dial(*server)
if err != nil {
log.Panic("Connection Error: ", err)
}
ctx := context.Background()
balance, err := cl.BalanceAt(ctx, common.HexToAddress(r.URL.Path[6:]), nil)
if err != nil {
log.Panic("Error Fetching Balance: ", err)
}
fmt.Fprint(w, balance)
}
func txHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Transaction Requested: ", r.URL.Path[4:], "\n")
cl, err := ethclient.Dial(*server)
if err != nil {
log.Panic("Connection Error: ", err)
}
ctx := context.Background()
tx, pending, err := cl.TransactionByHash(ctx, common.HexToHash(r.URL.Path[4:]))
if err != nil {
log.Panic("Error Fetching Transaction: ", err)
}
if !pending {
fmt.Fprint(w, tx)
} else {
fmt.Fprint(w, "Warning: Transaction Pending\n")
fmt.Fprint(w, tx)
}
}
func blockHandler(w http.ResponseWriter, r *http.Request) {
cl, err := ethclient.Dial(*server)
if err != nil {
log.Panic("Connection Error: ", err)
}
ctx := context.Background()
i, err := strconv.Atoi(r.URL.Path[7:])
if err != nil {
log.Panic("Error Coverting Block Number: ", err)
}
block, err := cl.BlockByNumber(ctx, big.NewInt(int64(i)))
if err != nil {
log.Panic("Block Fetch Error: ", err)
}
fmt.Fprint(w, block)
}
func blockeventsHandler(w http.ResponseWriter, r *http.Request) {
if *serverMode == "testing" {
fmt.Fprint(w, "Block Events Requested: ", r.URL.Path[13:], "\n")
}
cl, err := ethclient.Dial(*server)
if err != nil {
log.Panic("Connection Error: ", err)
}
ctx := context.Background()
i, err := strconv.Atoi(r.URL.Path[13:])
if err != nil {
log.Panic("Error Coverting Block Number: ", err)
}
block, err := cl.BlockByNumber(ctx, big.NewInt(int64(i)))
if err != nil {
log.Panic("Block Fetch Error: ", err)
}
txs := block.Transactions()
for _, t := range txs {
receipt, err := cl.TransactionReceipt(ctx, t.Hash())
if err != nil {
log.Panic("Receipt Error: ", err)
}
for _, lg := range receipt.Logs {
b, err := lg.MarshalJSON()
if err != nil {
log.Panic("JSON Marshalling Error: ", err)
}
_, err = w.Write(b)
if err != nil {
log.Panic("Error Writing Output: ", err)
}
}
}
}
func eventsHandler(w http.ResponseWriter, r *http.Request) {
addr := r.URL.Path[8:]
if *serverMode == "testing" {
fmt.Print("Events Requested: ", addr, "\n")
fmt.Print("Starting from Block: ", *fromBlock, "\n")
}
evtReturner := eventReturner{address: addr}
eventReturnerChan <- &evtReturner
evtReturned := <-eventReturnerChan
if evtReturned.err != nil {
fmt.Fprint(w, "Status of Account: ", evtReturned.err, "/n")
return
}
v, err := json.Marshal(evtReturned.logs)
if err != nil {
log.Panic("Marshal Error: ", err)
}
_, err = w.Write(v)
if err != nil {
log.Panic("Error Writing Logs")
}
//0x4df81d58993ff6f6e3a721b2ac0a08a5cd78ce9e
//0x6090A6e47849629b7245Dfa1Ca21D94cd15878Ef
}
var eventReturnerChan = make(chan *eventReturner)
var newEventsChan = make(chan *types.Log)
func main() {
flag.Parse()
if *serverMode == "testing" {
http.HandleFunc("/addr/", addressHandler)
http.HandleFunc("/tx/", txHandler)
http.HandleFunc("/block/", blockHandler)
http.HandleFunc("/sync", syncHandler)
}
go FileManager(eventReturnerChan, newEventsChan)
go SubListener(newEventsChan)
//go testListener(newEventsChan)
http.HandleFunc("/blockevents/", blockeventsHandler)
http.HandleFunc("/events/", eventsHandler)
http.ListenAndServe(":8080", nil)
}