forked from Enclave-Markets/enclave-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (149 loc) · 4.33 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
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/Enclave-Markets/enclave-go/apiclient"
"github.com/Enclave-Markets/enclave-go/models"
"github.com/shopspring/decimal"
)
func main() {
// the trading pair that will be used, and balance symbol to check
symbol := models.Symbol("AVAX")
market := models.Market("AVAX-USDC")
client, err := apiclient.NewApiClientFromEnv("sandbox")
if err != nil {
fmt.Println("failed to create client", err)
return
}
client.WithApiKey(
os.Getenv("ENCLAVE_KEY"),
os.Getenv("ENCLAVE_SECRET"),
)
client.WaitForEndpoint()
_, err = client.AuthedHello()
if err != nil {
fmt.Println("authed-hello failed:", err)
return
}
// get the balance of AVAX
balanceResp, err := client.GetBalance(models.GetBalanceReq{Symbol: symbol})
if err != nil {
fmt.Println("failed to get balance:", err)
return
}
fmt.Println(symbol, "balance:", balanceResp.Result.TotalBalance)
// get the AVAX-USDC trading pair to find the min order sizes
marketsResp, err := client.Markets()
if err != nil {
fmt.Println("failed to get markets", err, marketsResp.Success)
return
}
var baseMin, quoteMin decimal.Decimal
for _, pair := range marketsResp.Result.Spot.TradingPairs {
if pair.Market != market {
continue
}
baseMin, quoteMin = pair.BaseIncrement, pair.QuoteIncrement
}
fmt.Println("base-min:", baseMin, "quote-min:", quoteMin)
// get top of book for avax usdc
book, err := client.GetSpotDepthBook(market)
if err != nil {
fmt.Println("failed to get market depth:", err)
return
}
if len(book.Result.Asks) == 0 {
fmt.Println("no asks are resting on the book")
return
}
bestAsk := book.Result.Asks[0]
fmt.Println("best-ask-price:", bestAsk.Price, "best-ask-size:", bestAsk.Quantity)
// place a sell limit order of the smallest size one tick above the top of book (so we don't get filled)
orderResp, err := client.AddSpotOrder(
models.AddOrderReq{
Market: market,
Side: models.Ask,
Price: bestAsk.Price.Add(quoteMin),
Size: baseMin,
Type: models.OrderTypeLimit,
},
)
if err != nil {
fmt.Println("failed to place order:", err)
return
}
fmt.Println("order placed, current state:", orderResp.Result.State)
// cancel all orders in the market
if err := client.CancelAllSpotOrders(); err != nil {
fmt.Println("failed to cancel spot orders:", err)
return
}
// get the order state
orderResp, err = client.GetSpotOrder(orderResp.Result.OrderID)
if err != nil {
fmt.Println("failed to get spot order:", err)
return
}
fmt.Println("order state:", orderResp.Result.State)
var side models.BidAsk
switch {
case len(book.Result.Asks) != 0:
side = models.Bid
case len(book.Result.Bids) != 0:
side = models.Ask
default:
fmt.Println("orderbook is empty -- cannot trigger a fill")
return
}
clientOrderID := models.OrderID(strconv.FormatUint(uint64(time.Now().UnixNano()), 10))
orderResp, err = client.AddSpotOrder(
models.AddOrderReq{
Market: market,
Side: side,
Size: baseMin,
Type: models.OrderTypeMarket,
ClientOrderID: clientOrderID,
},
)
if err != nil {
fmt.Println("failed to place market order:", err)
return
}
if orderResp.Result.State != models.FullyFilled {
fmt.Println("market order did not fill:", orderResp.Result.State)
return
}
fmt.Println("market order placed, current state:", orderResp.Result.State)
// lets find the fills
fillsResp, err := client.GetSpotFillsByOrderID(orderResp.Result.OrderID)
if err != nil {
fmt.Println("unable to find fill:", err)
return
}
if len(fillsResp.Result) == 0 {
fmt.Println("could not find fills!")
}
fmt.Println("found n-fills for market order:", len(fillsResp.Result))
// lets find the fills by client order ID this time
fillsResp, err = client.GetSpotFillsByClientOrderID(orderResp.Result.ClientOrderID)
if err != nil {
fmt.Println("unable to find fill:", err)
return
}
if len(fillsResp.Result) == 0 {
fmt.Println("could not find fills!")
}
fmt.Println("found n-fills by client order ID for market order:", len(fillsResp.Result))
// now find all fills
allFillsResp, err := client.GetSpotFills(models.FillParams{})
if err != nil {
fmt.Println("unable to find all fills:", err)
return
}
if len(allFillsResp.Result) == 0 {
fmt.Println("could not find any fills!")
}
fmt.Println("found n-fills any orders:", len(allFillsResp.Result))
}