-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (106 loc) · 2.41 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
package main
import (
"context"
"fmt"
"strconv"
"time"
"github.com/machinebox/graphql"
)
type UniswapResponse struct {
PoolDayDatas []PoolDayData
}
type PoolDayData struct {
FeesUSD string `json:"feesUSD"`
Id string `json:"id"`
Pool struct {
Id string `json:"id"`
} `json:"pool"`
TvlUSD string `json:"tvlUSD"`
}
const startDay = "2022-01-01T00:00:00Z"
const endDay = "2022-02-28T00:00:00Z"
const graphEndpoint = "https://api.thegraph.com/subgraphs/name/ianlapham/uniswap-v3-alt"
func main() {
//Start a timer!
start := time.Now()
status := "."
fmt.Println("Looking for Moons...")
client := graphql.NewClient(graphEndpoint)
var result UniswapResponse
var lastID string
var veryMoon string
rates := make(map[string]float64)
for {
fmt.Println(status)
err := client.Run(
context.Background(),
buildRequest(lastID, convertTime(startDay), convertTime(endDay)),
&result)
if err != nil {
panic(err)
}
if len(result.PoolDayDatas) == 0 {
break
}
status = status + "."
for _, data := range result.PoolDayDatas {
//Handle pagination
lastID = data.Id
//Because we can't unmarshal integers, I need to convert strings to float64
fees := parseFloat(data.FeesUSD)
tvl := parseFloat(data.TvlUSD)
//Only include active pools
if fees == 0 || tvl == 0 {
continue
}
//Calculate
rate := fees / tvl
//Add rates to pool.id --> rate map
if _, ok := rates[data.Pool.Id]; !ok {
rates[data.Pool.Id] = rate
} else {
rates[data.Pool.Id] += rate
}
//Start looking for the moon
if veryMoon == "" {
veryMoon = data.Pool.Id
}
if rates[data.Pool.Id] > rates[veryMoon] {
veryMoon = data.Pool.Id
}
}
}
//Report execution time
duration := time.Since(start)
fmt.Println(duration)
fmt.Println(veryMoon)
fmt.Println(rates[veryMoon])
}
func buildRequest(lastID string, sDate int64, eDate int64) *graphql.Request {
query := fmt.Sprintf(`
{
poolDayDatas(first: 1000, where: {id_gt:"%s", date_gte:%v, date_lte:%v}) {
id
pool {
id
}
tvlUSD
feesUSD
}
}`, lastID, sDate, eDate)
return graphql.NewRequest(query)
}
func convertTime(datetime string) int64 {
timestamp, err := time.Parse(time.RFC3339, datetime)
if err != nil {
panic(err)
}
return timestamp.Unix()
}
func parseFloat(jsonString string) float64 {
float, err := strconv.ParseFloat(jsonString, 64)
if err != nil {
panic(err)
}
return float
}