-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap_filter.go
84 lines (69 loc) · 1.73 KB
/
swap_filter.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
package vwap
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
// temporary filtering func. should move to a router later.
type SwapToken struct {
Symbol string `json:"symbol"`
}
type Swap struct {
Time string `json:"time"`
TokenA SwapToken `json:"tokenA"`
TokenAAmount string `json:"tokenAAmount"`
TokenB SwapToken `json:"tokenB"`
TokenBAmount string `json:"tokenBAmount"`
TotalUsd string `json:"totalUsd"`
}
type ActivitySwapResponse struct {
Data []Swap `json:"data"`
}
const (
ActivitySwapEndpoint = "http://dev.api.gnoswap.io/v1/activity?type=%s"
QueryTypeSwap = "SWAP"
)
func FetchActivitySwap(endpoint, queryType string) ([]Swap, error) {
client := &http.Client{}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
rpc := fmt.Sprintf(endpoint, queryType)
req, err := http.NewRequestWithContext(ctx, "GET", rpc, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
log.Printf("error: %v", err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: %v", resp.Status)
}
var apiResponse ActivitySwapResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResponse); err != nil {
log.Printf("error: %v", err)
return nil, err
}
return apiResponse.Data, nil
}
var filterToken = map[string]bool{
"USDC": true,
"GNOT": true,
"GNS": true,
}
func FilterSwaps(swaps []Swap) []Swap {
filteredSwaps := make([]Swap, 0)
for _, swap := range swaps {
_, okA := filterToken[swap.TokenA.Symbol]
_, okB := filterToken[swap.TokenB.Symbol]
if okA || okB {
filteredSwaps = append(filteredSwaps, swap)
}
}
return filteredSwaps
}