This repository has been archived by the owner on Jun 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
76 lines (62 loc) · 1.78 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
package main
import (
"fmt"
"github.com/vmpartner/bitmex/bitmex"
"github.com/vmpartner/bitmex/config"
"github.com/vmpartner/bitmex/rest"
"github.com/vmpartner/bitmex/tools"
"github.com/vmpartner/bitmex/websocket"
"strings"
)
// Usage example
func main() {
// Load config
cfg := config.LoadConfig("config.json")
ctx := rest.MakeContext(cfg.Key, cfg.Secret, cfg.Host, cfg.Timeout)
// Get wallet
w, response, err := rest.GetWallet(ctx)
tools.CheckErr(err)
fmt.Printf("Status: %v, wallet amount: %v\n", response.StatusCode, w.Amount)
// Connect to WS
conn := websocket.Connect(cfg.Host)
defer conn.Close()
// Listen read WS
chReadFromWS := make(chan []byte, 100)
go websocket.ReadFromWSToChannel(conn, chReadFromWS)
// Listen write WS
chWriteToWS := make(chan interface{}, 100)
go websocket.WriteFromChannelToWS(conn, chWriteToWS)
// Authorize
chWriteToWS <- websocket.GetAuthMessage(cfg.Key, cfg.Secret)
// Read first response message
message := <-chReadFromWS
if !strings.Contains(string(message), "Welcome to the BitMEX") {
fmt.Println(string(message))
panic("No welcome message")
}
// Read auth response success
message = <-chReadFromWS
res, err := bitmex.DecodeMessage(message)
tools.CheckErr(err)
if res.Success != true || res.Request.(map[string]interface{})["op"] != "authKey" {
panic("No auth response success")
}
// Listen websocket before subscribe
go func() {
for {
message := <-chReadFromWS
res, err := bitmex.DecodeMessage(message)
tools.CheckErr(err)
// Your logic here
fmt.Printf("%+v\n", res)
}
}()
// Subscribe
messageWS := websocket.Message{Op: "subscribe"}
messageWS.AddArgument("orderBookL2:XBTUSD")
messageWS.AddArgument("order")
messageWS.AddArgument("position")
chWriteToWS <- messageWS
// Loop forever
select {}
}