-
Notifications
You must be signed in to change notification settings - Fork 0
/
price.go
104 lines (87 loc) · 2.6 KB
/
price.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
package vwap
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"time"
)
const priceEndpoint = "http://dev.api.gnoswap.io/v1/tokens/prices"
type TokenPrice struct {
Path string `json:"path"`
USD string `json:"usd"`
PricesBefore PricesBefore `json:"pricesBefore"`
MarketCap string `json:"marketCap"`
LockedTokensUSD string `json:"lockedTokensUsd"`
VolumeUSD24h string `json:"volumeUsd24h"`
FeeUSD24h string `json:"feeUsd24h"`
MostLiquidityPool string `json:"mostLiquidityPool"`
Last7d []Last7d `json:"last7d"`
}
type PricesBefore struct {
LatestPrice string `json:"latestPrice"`
Price1h string `json:"price1h"`
PriceToday string `json:"priceToday"`
Price1d string `json:"price1d"`
Price7d string `json:"price7d"`
Price30d string `json:"price30d"`
Price60d string `json:"price60d"`
Price90d string `json:"price90d"`
}
type Last7d struct {
Date string `json:"date"`
Price string `json:"price"`
}
type PricesResponse struct {
Error json.RawMessage `json:"error"`
Data []TokenPrice `json:"data"`
}
func fetchTokenPrices(endpoint string) ([]TokenPrice, error) {
client := &http.Client{}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error making request: %v\n", err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Received non-OK response: %d\n", resp.StatusCode)
return nil, fmt.Errorf("received non-OK status code: %d", resp.StatusCode)
}
var apiResponse PricesResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResponse); err != nil {
log.Printf("Error decoding response: %v\n", err)
return nil, err
}
return apiResponse.Data, nil
}
func extractTrades(prices []TokenPrice, volumeByToken map[string]float64) map[string][]TradeData {
trades := make(map[string][]TradeData)
for _, price := range prices {
usd, err := strconv.ParseFloat(price.USD, 64)
if err != nil {
fmt.Printf("failed to parse USD price for token %s: %v\n", price.Path, err)
continue
}
volume, ok := volumeByToken[price.Path]
if !ok {
fmt.Printf("volume not found for token %s\n", price.Path)
continue
}
trades[price.Path] = append(trades[price.Path], TradeData{
TokenName: price.Path,
Volume: volume,
Ratio: usd,
Timestamp: int(time.Now().Unix()),
})
}
return trades
}