-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
252 lines (230 loc) · 8.25 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/fatih/color"
"github.com/valyala/fasthttp"
)
const baseEndpoint = "https://indodax.com"
type ServerTimeResponse struct {
ServerTime int64 `json:"server_time"`
Timezone string `json:"timezone"`
}
type TickerResponse struct {
Ticker struct {
High float64 `json:"high"`
Low float64 `json:"low"`
Volume float64 `json:"volume"`
Last float64 `json:"last"`
Buy float64 `json:"buy"`
Sell float64 `json:"sell"`
ServerTime int64 `json:"server_time"`
} `json:"ticker"`
}
type Pair struct {
ID string `json:"id"`
Symbol string `json:"symbol"`
BaseCurrency string `json:"base_currency"`
Description string `json:"description"`
}
type Trade struct {
Date int64 `json:"date,string"`
Price string `json:"price"`
Amount string `json:"amount"`
Tid string `json:"tid"`
Type string `json:"type"`
}
type DepthResponse struct {
Buy [][]interface{} `json:"buy"`
Sell [][]interface{} `json:"sell"`
}
func parseFloat64(s string) (float64, error) {
return strconv.ParseFloat(s, 64)
}
func processResponse(statusCode int, body []byte) ([]byte, error) {
if statusCode == 200 {
return body, nil
}
return nil, fmt.Errorf("unable to fetch data. status code: %d", statusCode)
}
func formatServerTime(serverTime int64) string {
return time.Unix(serverTime/1000, 0).UTC().Format("2006-01-02 15:04:05")
}
func clearScreen() {
os.Stdout.WriteString("\x1b[3;J\x1b[H\x1b[2J")
}
func main() {
info := color.New(color.FgYellow).SprintFunc()
success := color.New(color.FgGreen).SprintFunc()
prompt := color.New(color.FgCyan).SprintFunc()
errorMsg := color.New(color.FgRed).SprintFunc()
header := color.New(color.FgBlue).Add(color.Underline).SprintFunc()
fmt.Printf("[%s] This program is using %s!\n", info("i"), success("Official IndoDax API"))
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("Use %s if you don't know the commands.\n--> ", prompt("help"))
user, _ := reader.ReadString('\n')
user = strings.TrimSpace(user)
switch user {
case "help":
fmt.Println(header("[i] Command List"))
fmt.Printf("[%s] %s | Clear prompt\n", header("*"), "clear")
fmt.Printf("[%s] %s | Provide server time on exchange\n", header("*"), "servertime")
fmt.Printf("[%s] %s | Provide available pairs on exchange\n", header("*"), "pairs")
fmt.Printf("[%s] %s | Provide ticker information for a pair\n", header("="), "ticker")
fmt.Printf("[%s] %s | Provide recent trades for a pair\n", header("="), "trades")
fmt.Printf("[%s] %s | Provide order book depth for a pair\n", header("="), "depth")
case "clear":
fmt.Printf("[%s] Clearing prompt.\n", success("+"))
time.Sleep(1 * time.Second)
clearScreen()
case "servertime":
statusCode, body, err := fasthttp.Get(nil, baseEndpoint+"/api/server_time")
if err != nil {
fmt.Printf("[%s] Unable to fetch data: %s\n", errorMsg("!"), err)
continue
}
body, err = processResponse(statusCode, body)
if err != nil {
fmt.Printf("[%s] %s\n", errorMsg("!"), err)
continue
}
var serverTimeData ServerTimeResponse
json.Unmarshal(body, &serverTimeData)
formattedTime := formatServerTime(serverTimeData.ServerTime)
fmt.Printf("[%s] Server Time: %s %s\n", success("+"), formattedTime, serverTimeData.Timezone)
case "pairs":
statusCode, body, err := fasthttp.Get(nil, baseEndpoint+"/api/pairs")
if err != nil {
fmt.Printf("[%s] Unable to fetch data: %s\n", errorMsg("!"), err)
continue
}
body, err = processResponse(statusCode, body)
if err != nil {
fmt.Printf("[%s] %s\n", errorMsg("!"), err)
continue
}
var pairsData []Pair
json.Unmarshal(body, &pairsData)
for _, pair := range pairsData {
fmt.Printf("[%s] ID: %s\n", success("+"), pair.ID)
fmt.Printf("[%s] Symbol: %s\n", header("-"), pair.Symbol)
fmt.Printf("[%s] Base Currency: %s\n", header("-"), pair.BaseCurrency)
fmt.Printf("[%s] Description: %s\n\n", header("-"), pair.Description)
}
case "ticker":
fmt.Print("Enter pair symbol (e.g., btcidr): ")
pairSymbol, _ := reader.ReadString('\n')
pairSymbol = strings.TrimSpace(pairSymbol)
statusCode, body, err := fasthttp.Get(nil, baseEndpoint+"/api/ticker/"+pairSymbol)
if err != nil {
fmt.Printf("[%s] Unable to fetch data: %s\n", errorMsg("!"), err)
continue
}
body, err = processResponse(statusCode, body)
if err != nil {
fmt.Printf("[%s] %s\n", errorMsg("!"), err)
continue
}
var tickerData struct {
Ticker struct {
High string `json:"high"`
Low string `json:"low"`
VolTen string `json:"vol_ten"`
VolIDR string `json:"vol_idr"`
Last string `json:"last"`
Buy string `json:"buy"`
Sell string `json:"sell"`
ServerTime int64 `json:"server_time"`
} `json:"ticker"`
}
err = json.Unmarshal(body, &tickerData)
if err != nil {
fmt.Printf("[%s] Error parsing ticker data: %s\n", errorMsg("!"), err)
continue
}
fmt.Printf("[%s] Last Price: %s\n", success("+"), tickerData.Ticker.Last)
fmt.Printf("[%s] High: %s\n", header("-"), tickerData.Ticker.High)
fmt.Printf("[%s] Low: %s\n", header("-"), tickerData.Ticker.Low)
fmt.Printf("[%s] Volume in TEN: %s\n", header("-"), tickerData.Ticker.VolTen)
fmt.Printf("[%s] Volume in IDR: %s\n", header("-"), tickerData.Ticker.VolIDR)
fmt.Printf("[%s] Buy: %s\n", header("-"), tickerData.Ticker.Buy)
fmt.Printf("[%s] Sell: %s\n\n", header("-"), tickerData.Ticker.Sell)
case "trades":
fmt.Print("Enter pair symbol (e.g., btcidr): ")
pairSymbol, _ := reader.ReadString('\n')
pairSymbol = strings.TrimSpace(pairSymbol)
statusCode, body, err := fasthttp.Get(nil, baseEndpoint+"/api/trades/"+pairSymbol)
if err != nil {
fmt.Printf("[%s] Unable to fetch data: %s\n", errorMsg("!"), err)
continue
}
body, err = processResponse(statusCode, body)
if err != nil {
fmt.Printf("[%s] %s\n", errorMsg("!"), err)
continue
}
var tradesData []Trade
err = json.Unmarshal(body, &tradesData)
if err != nil {
fmt.Printf("[%s] Error parsing trades data: %s\n", errorMsg("!"), err)
continue
}
for _, trade := range tradesData {
tradeTime := time.Unix(trade.Date, 0).UTC().Format("2006-01-02 15:04:05")
fmt.Printf("[%s] Trade ID: %s\n", success("+"), trade.Tid)
fmt.Printf("[%s] Date: %s\n", header("-"), tradeTime)
fmt.Printf("[%s] Type: %s\n", header("-"), trade.Type)
fmt.Printf("[%s] Price: %s\n", header("-"), trade.Price)
fmt.Printf("[%s] Amount: %s\n\n", header("-"), trade.Amount)
}
case "depth":
fmt.Print("Enter pair symbol (e.g., btcidr): ")
pairSymbol, _ := reader.ReadString('\n')
pairSymbol = strings.TrimSpace(pairSymbol)
statusCode, body, err := fasthttp.Get(nil, baseEndpoint+"/api/depth/"+pairSymbol)
if err != nil {
fmt.Printf("[%s] Unable to fetch data: %s\n", errorMsg("!"), err)
continue
}
body, err = processResponse(statusCode, body)
if err != nil {
fmt.Printf("[%s] %s\n", errorMsg("!"), err)
continue
}
var depthData DepthResponse
err = json.Unmarshal(body, &depthData)
if err != nil {
fmt.Printf("[%s] Error parsing depth data: %s\n", errorMsg("!"), err)
continue
}
fmt.Println(header("[Buy Orders]"))
for _, order := range depthData.Buy {
price, err := parseFloat64(order[0].(string))
if err != nil {
fmt.Printf("[%s] Error parsing price: %s\n", errorMsg("!"), err)
continue
}
amount := order[1].(string)
fmt.Printf("[%s] Price: %.2f, Amount: %s\n", success("+"), price, amount)
}
fmt.Println(header("\n[Sell Orders]"))
for _, order := range depthData.Sell {
price, err := parseFloat64(order[0].(string))
if err != nil {
fmt.Printf("[%s] Error parsing price: %s\n", errorMsg("!"), err)
continue
}
amount := order[1].(string)
fmt.Printf("[%s] Price: %.2f, Amount: %s\n", success("+"), price, amount)
}
default:
fmt.Printf("[%s] Unknown command\n", errorMsg("!"))
}
}
}