-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdedustApi.go
More file actions
120 lines (94 loc) · 2.55 KB
/
dedustApi.go
File metadata and controls
120 lines (94 loc) · 2.55 KB
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
package dedustApi
import (
"context"
"fmt"
"github.com/machinebox/graphql"
)
const baseUrl = "https://api.dedust.io/v3/graphql"
var defaultVariables = map[string]interface{}{}
func request[T any](schema string, variables map[string]interface{}) (T, error) {
client := graphql.NewClient(baseUrl)
req := graphql.NewRequest(schema)
if len(variables) > 0 {
fmt.Println("Variables:", variables)
for key, value := range variables {
req.Var(key, value)
}
}
var data T
if err := client.Run(context.Background(), req, &data); err != nil {
fmt.Println("Error making GraphQL request:", err)
return data, err
}
return data, nil
}
type ResponseBoost struct {
Boosts []Boost `json:"boosts"`
}
func GetBoost() ([]Boost, error) {
resp, err := request[ResponseBoost](queryGetBoosts, defaultVariables)
return resp.Boosts, err
}
type ResponseAssets struct {
Asset []Asset `json:"assets"`
}
func GetAssets() ([]Asset, error) {
resp, err := request[ResponseAssets](queryGetAssets, defaultVariables)
return resp.Asset, err
}
type ResponseAsset struct {
Asset Asset `json:"asset"`
}
func GetAsset(assetAddress string) (Asset, error) {
query := map[string]interface{}{
"id": assetAddress,
}
resp, err := request[ResponseAsset](queryGetAsset, query)
return resp.Asset, err
}
type ResponsePrice struct {
Price Price `json:"price"`
}
func GetPrice(assetAddress string, decimals int) (Price, error) {
variables := map[string]interface{}{
"id": assetAddress,
"decimals": decimals,
}
resp, err := request[ResponsePrice](queryGetPrices, variables)
return resp.Price, err
}
type ResponsePrices struct {
Price []Price `json:"price"`
}
func GetPrices(assetAddress string, decimals int) ([]Price, error) {
variables := map[string]interface{}{
"id": assetAddress,
"decimals": decimals,
}
resp, err := request[ResponsePrices](queryGetPrices, variables)
return resp.Price, err
}
type ResponsePools struct {
Pools []Pool `json:"pools"`
}
func GetPools() ([]Pool, error) {
resp, err := request[ResponsePools](queryGetPools, defaultVariables)
return resp.Pools, err
}
type ResponsePool struct {
Pool Pool `json:"pool"`
}
func GetPool(poolAddress string) (Pool, error) {
variables := map[string]interface{}{
"address": poolAddress,
}
resp, err := request[ResponsePool](queryGetPool, variables)
return resp.Pool, err
}
type ResponsePromotions struct {
Promotions []Promotions `json:"promotions"`
}
func GetPromotions() ([]Promotions, error) {
resp, err := request[ResponsePromotions](queryGetPromotions, defaultVariables)
return resp.Promotions, err
}