-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
221 lines (200 loc) · 6.36 KB
/
utils.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
const DMARKET_API_URL = "https://api.dmarket.com"
const PRICES_URL = "https://prices.csgotrader.app/latest/prices_v6.json"
var marketPrices = make(map[string]MarketPrices)
func GetPrivateKey(s string) *[64]byte {
b, _ := hex.DecodeString(s)
var privateKey [64]byte
copy(privateKey[:], b[:64])
return &privateKey
}
func Sign(pk, msg string) string {
b := GetPrivateKey(pk)
return hex.EncodeToString(ed25519.Sign((*b)[:], []byte(msg)))
}
func SendSignedDmarketRequest(method string, path string, body string) (*http.Response, error) {
timestamp := strconv.Itoa(int(time.Now().UTC().Unix()))
unsigned := method + path + body + timestamp
signature := Sign(config.DmarketPrivateKey, unsigned)
req, _ := http.NewRequest(method, DMARKET_API_URL+path, ioutil.NopCloser(strings.NewReader(body)))
req.Header.Set("X-Sign-Date", timestamp)
req.Header.Set("X-Request-Sign", "dmar ed25519 "+signature)
req.Header.Set("X-Api-Key", config.DmarketPublicKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
return http.DefaultClient.Do(req)
}
func HandleError(response *http.Response) {
ErrorLogger.Println("Erroneous response received: " + strconv.Itoa(response.StatusCode))
body, _ := ioutil.ReadAll(response.Body)
response.Body.Close()
var errorObj DmarketError
json.Unmarshal(body, &errorObj)
ReportError(errors.New(errorObj.Message))
}
func fetchPrices() {
response, _ := http.DefaultClient.Get(PRICES_URL)
if response.StatusCode != 200 {
panic("Failed to fetch prices")
}
body, _ := ioutil.ReadAll(response.Body)
response.Body.Close()
json.Unmarshal(body, &marketPrices)
}
func PercentageDifference(cost float64, revenue float64) float64 {
return ((revenue - cost) / revenue) * 100
}
type DmarketError struct {
Error string `json:"error"`
Code int `json:"code"`
Message string `json:"message"`
Details []struct {
TypeURL string `json:"type_url"`
Value string `json:"value"`
} `json:"details"`
}
type MarketPrices struct {
Steam struct {
Last24H float64 `json:"last_24h"`
Last7D float64 `json:"last_7d"`
Last30D float64 `json:"last_30d"`
Last90D float64 `json:"last_90d"`
} `json:"steam"`
Bitskins struct {
Price string `json:"price"`
InstantSalePrice interface{} `json:"instant_sale_price"`
} `json:"bitskins"`
Lootfarm float64 `json:"lootfarm"`
Csgotm string `json:"csgotm"`
Csmoney struct {
Price float64 `json:"price"`
Doppler struct {
Phase1 float64 `json:"Phase 1"`
Ruby int `json:"Ruby"`
Phase3 float64 `json:"Phase 3"`
Phase4 float64 `json:"Phase 4"`
Phase2 int `json:"Phase 2"`
Sapphire float64 `json:"Sapphire"`
BlackPearl int `json:"Black Pearl"`
FactoryNew int `json:"(Factory New)"`
} `json:"doppler"`
} `json:"csmoney"`
Skinport struct {
SuggestedPrice float64 `json:"suggested_price"`
StartingAt float64 `json:"starting_at"`
} `json:"skinport"`
Csgotrader struct {
Price float64 `json:"price"`
Doppler struct {
Phase1 float64 `json:"Phase 1"`
Ruby float64 `json:"Ruby"`
Phase3 float64 `json:"Phase 3"`
Phase4 float64 `json:"Phase 4"`
Phase2 float64 `json:"Phase 2"`
Sapphire float64 `json:"Sapphire"`
BlackPearl float64 `json:"Black Pearl"`
FactoryNew interface{} `json:"(Factory New)"`
} `json:"doppler"`
} `json:"csgotrader"`
Csgoempire float64 `json:"csgoempire"`
Swapgg float64 `json:"swapgg"`
Csgoexo float64 `json:"csgoexo"`
Cstrade struct {
Doppler struct {
Phase2 float64 `json:"Phase 2"`
Phase4 float64 `json:"Phase 4"`
Phase1 float64 `json:"Phase 1"`
Phase3 float64 `json:"Phase 3"`
} `json:"doppler"`
Price float64 `json:"price"`
} `json:"cstrade"`
Skinwallet float64 `json:"skinwallet"`
Buff163 struct {
StartingAt struct {
Price float64 `json:"price"`
Doppler map[string]float64 `json:"doppler"`
} `json:"starting_at"`
HighestOrder struct {
Price float64 `json:"price"`
Doppler map[string]float64 `json:"doppler"`
} `json:"highest_order"`
} `json:"buff163"`
}
type ItemPrices struct {
Steam struct {
Last24H float64 `json:"last_24h"`
Last7D float64 `json:"last_7d"`
Last30D float64 `json:"last_30d"`
Last90D float64 `json:"last_90d"`
} `json:"steam"`
Bitskins struct {
Price string `json:"price"`
InstantSalePrice interface{} `json:"instant_sale_price"`
} `json:"bitskins"`
Lootfarm float64 `json:"lootfarm"`
Csgotm string `json:"csgotm"`
Csmoney struct {
Price float64 `json:"price"`
Doppler struct {
Phase1 float64 `json:"Phase 1"`
Ruby int `json:"Ruby"`
Phase3 float64 `json:"Phase 3"`
Phase4 float64 `json:"Phase 4"`
Phase2 int `json:"Phase 2"`
Sapphire float64 `json:"Sapphire"`
BlackPearl int `json:"Black Pearl"`
FactoryNew int `json:"(Factory New)"`
} `json:"doppler"`
} `json:"csmoney"`
Skinport struct {
SuggestedPrice float64 `json:"suggested_price"`
StartingAt float64 `json:"starting_at"`
} `json:"skinport"`
Csgotrader struct {
Price float64 `json:"price"`
Doppler struct {
Phase1 float64 `json:"Phase 1"`
Ruby float64 `json:"Ruby"`
Phase3 float64 `json:"Phase 3"`
Phase4 float64 `json:"Phase 4"`
Phase2 float64 `json:"Phase 2"`
Sapphire float64 `json:"Sapphire"`
BlackPearl float64 `json:"Black Pearl"`
FactoryNew interface{} `json:"(Factory New)"`
} `json:"doppler"`
} `json:"csgotrader"`
Csgoempire float64 `json:"csgoempire"`
Swapgg float64 `json:"swapgg"`
Csgoexo float64 `json:"csgoexo"`
Cstrade struct {
Doppler struct {
Phase2 float64 `json:"Phase 2"`
Phase4 float64 `json:"Phase 4"`
Phase1 float64 `json:"Phase 1"`
Phase3 float64 `json:"Phase 3"`
} `json:"doppler"`
Price float64 `json:"price"`
} `json:"cstrade"`
Skinwallet float64 `json:"skinwallet"`
Buff163 struct {
StartingAt struct {
Price float64 `json:"price"`
Doppler map[string]float64 `json:"doppler"`
} `json:"starting_at"`
HighestOrder struct {
Price float64 `json:"price"`
Doppler map[string]float64 `json:"doppler"`
} `json:"highest_order"`
} `json:"buff163"`
}