-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTradeData.go
126 lines (101 loc) · 3.08 KB
/
getTradeData.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/joho/godotenv"
)
type sv struct {
Address string
Value float64
}
func getTradeData() []sv {
if err := godotenv.Load(); err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
OPENSEA_API_KEY := os.Getenv("OPENSEA_API_KEY")
OPENSEA_NFT_COLLECTION_SLUG := os.Getenv("OPENSEA_NFT_COLLECTION_SLUG")
AIRDROP_START := os.Getenv("AIRDROP_START")
AIRDROP_END := os.Getenv("AIRDROP_END")
event_type := "sale"
url := fmt.Sprintf("https://api.opensea.io/api/v2/events/collection/%s?event_type=%s&after=%s&before=%s", OPENSEA_NFT_COLLECTION_SLUG, event_type, AIRDROP_START, AIRDROP_END)
var allEvents []map[string]interface{}
for {
fmt.Println("Fetching...", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return nil
}
req.Header.Add("accept", "application/json")
req.Header.Add("x-api-key", OPENSEA_API_KEY)
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return nil
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return nil
}
var response map[string]interface{}
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Println("Error unmarshalling response:", err)
return nil
}
events, found := response["asset_events"].([]interface{})
if !found {
fmt.Println("No events found in response, ending pagination")
break
}
// Print count of events fetched
fmt.Printf("Fetched %d events\n", len(events))
for _, event := range events {
allEvents = append(allEvents, event.(map[string]interface{}))
}
if len(events) >= 50 {
// Check for the next cursor
next, found := response["next"]
if !found || next == nil {
fmt.Println("No more pages.")
break
}
url = fmt.Sprintf("https://api.opensea.io/api/v2/events/collection/%s?event_type=%s&after=%s&before=%s&next=%s", OPENSEA_NFT_COLLECTION_SLUG, event_type, AIRDROP_START, AIRDROP_END, next)
time.Sleep(1 * time.Second) // Sleep to avoid hitting rate limit
} else {
fmt.Println("No more events to fetch.")
break
}
}
fmt.Printf("Total events fetched: %d\n", len(allEvents))
// Get seller and sum of quantity of payment from all events
sellerQuantity := make(map[string]float64)
for _, event := range allEvents {
seller := event["seller"].(string)
quantity := event["payment"].(map[string]interface{})["quantity"].(string)
quantityFloat, err := strconv.ParseFloat(quantity, 64)
if err != nil {
fmt.Println("Error converting quantity to float:", err)
return nil
}
if _, ok := sellerQuantity[seller]; ok {
sellerQuantity[seller] += quantityFloat
} else {
sellerQuantity[seller] = quantityFloat
}
}
// Sort sellerQuantity by sum of quantity
var listingUsers []sv
for seller, quantity := range sellerQuantity {
listingUsers = append(listingUsers, sv{Address: seller, Value: quantity})
}
return listingUsers
}