-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (127 loc) · 3.7 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
package main
// Importing
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
// Define a struct to hold the transaction data
type Transaction struct {
From string `json:"from"`
To string `json:"to"`
Value string `json:"value"`
Block string `json:"blockNumber"`
Time string `json:"timeStamp"`
}
// Define a struct to hold the API response
type ApiResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Result []Transaction `json:"result"`
}
// Endpoint to fetch the data from the Etherscan API
func fetchData() ([]Transaction, error) {
// The API key goes here
apiKey := "XE5JUF29JI5711WR6WKACV2PHVAIRUYFYI"
url := "https://api.etherscan.io/api?module=account&action=tokentx&contractaddress=0x9355372396e3F6daF13359B7b607a3374cc638e0&page=1&sort=asc&apikey=" + apiKey + "&limit=100"
// Making the API call
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
var apiResponse ApiResponse
err = json.NewDecoder(response.Body).Decode(&apiResponse)
if err != nil {
return nil, err
}
return apiResponse.Result, nil
}
var database []Transaction // In-memory database to store the transactions
// Initialize the in-memory database
func initDatabase() error {
data, err := fetchData()
if err != nil {
return err
}
database = make([]Transaction, len(data))
copy(database, data)
return nil
}
// Handler function to return all transacrions
func getAllTransactions(w http.ResponseWriter, r *http.Request) {
var limit int = 10 // Slice the database to return a maximum of 10 transactions
if len(database) > limit {
database = database[:limit]
}
response, err := json.Marshal(database)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
// Handler function to return paginated transactions
func getPaginatedTransactions(w http.ResponseWriter, r *http.Request) {
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
http.Error(w, "Invalid limit parameter", http.StatusBadRequest)
return
}
offset, err := strconv.Atoi(r.URL.Query().Get("offset"))
if err != nil {
http.Error(w, "Invalid offset parameter", http.StatusBadRequest)
return
}
start := offset
end := offset + limit
if end > len(database) {
end = len(database)
}
response, err := json.Marshal(database[start:end])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
// Handler function to return transactions based on from and to addresses
func getTransactionsByAddress(w http.ResponseWriter, r *http.Request) {
from := r.URL.Query().Get("from")
to := r.URL.Query().Get("to")
// Create a slice to hold the matching transactions
matches := make([]Transaction, 0)
for _, tx := range database {
if tx.From == from && tx.To == to {
matches = append(matches, tx)
}
}
response, err := json.Marshal(matches)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
func main() {
err := initDatabase()
if err != nil {
log.Fatal(err)
}
router := mux.NewRouter() // Create a new router
router.HandleFunc("/transactions", getAllTransactions).Methods("GET")
router.HandleFunc("/transactions/paginated", getPaginatedTransactions).Methods("GET")
router.HandleFunc("/transactions/address", getTransactionsByAddress).Methods("GET")
// Start the HTTP server:
fmt.Println("Server listening on port 8001")
err = http.ListenAndServe(":8001", router)
if err != nil {
log.Fatal(err)
}
}