forked from Logarithm-Labs/go-hyperliquid
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhyperliquid_test.go
More file actions
170 lines (152 loc) · 4.66 KB
/
hyperliquid_test.go
File metadata and controls
170 lines (152 loc) · 4.66 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
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
//go:build integration
// +build integration
package hyperliquid
import (
"os"
"sync"
"testing"
)
func GetHyperliquidAPI(t *testing.T) *Hyperliquid {
testAddress := os.Getenv("TEST_ADDRESS")
testPrivateKey := os.Getenv("TEST_PRIVATE_KEY")
if testAddress == "" || testPrivateKey == "" {
t.Fatalf("TEST_ADDRESS or TEST_PRIVATE_KEY is not set; provide credentials via .test.env to run integration tests")
}
hl := NewHyperliquid(&HyperliquidClientConfig{
IsMainnet: false,
AccountAddress: testAddress,
PrivateKey: testPrivateKey,
})
if GLOBAL_DEBUG {
hl.SetDebugActive()
}
return hl
}
func TestHyperliquid_CheckFieldsConsistency(t *testing.T) {
hl := GetHyperliquidAPI(t)
if hl.ExchangeAPI.baseEndpoint != "/exchange" {
t.Errorf("baseEndpoint = %v, want %v", hl.ExchangeAPI.baseEndpoint, "/exchange")
}
if hl.InfoAPI.baseEndpoint != "/info" {
t.Errorf("baseEndpoint = %v, want %v", hl.InfoAPI.baseEndpoint, "/info")
}
var apiUrl string
if hl.ExchangeAPI.IsMainnet() {
apiUrl = MAINNET_API_URL
} else {
apiUrl = TESTNET_API_URL
}
if hl.InfoAPI.baseUrl != apiUrl {
t.Errorf("baseUrl = %v, want %v", hl.InfoAPI.baseUrl, apiUrl)
}
hl.SetDebugActive()
if hl.InfoAPI.Debug != hl.ExchangeAPI.Debug {
t.Errorf("debug = %v, want %v", hl.InfoAPI.Debug, hl.ExchangeAPI.Debug)
}
savedAddress := hl.AccountAddress()
newAddress := "0x1234567890"
hl.SetAccountAddress(newAddress)
if hl.InfoAPI.AccountAddress() != newAddress {
t.Errorf("InfoAPI.AccountAddress = %v, want %v", hl.InfoAPI.AccountAddress(), newAddress)
}
if hl.ExchangeAPI.AccountAddress() != newAddress {
t.Errorf("ExchangeAPI.AccountAddress = %v, want %v", hl.ExchangeAPI.AccountAddress(), newAddress)
}
if hl.AccountAddress() != newAddress {
t.Errorf("gl.AccountAddress = %v, want %v", hl.AccountAddress(), newAddress)
}
hl.SetAccountAddress(savedAddress)
}
func TestHyperliquid_MakeSomeTradingLogic(t *testing.T) {
client := GetHyperliquidAPI(t)
// Make limit order
res1, err := client.LimitOrder(TifGtc, "ETH", 0.01, 1234.1, false)
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("LimitOrder(TifIoc, ETH, 0.01, 1234.1, false): %v", res1)
res2, err := client.LimitOrder(TifGtc, "ETH", 0.01, 1200.1, true)
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("LimitOrder(TifGtc, ETH, 0.01, 1200.1, true): %v", res2)
res3, err := client.LimitOrder(TifGtc, "ETH", -0.01, 5000.1, true)
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("LimitOrder(TifGtc, ETH, -0.01, 5000.1, true): %v", res3)
res4, err := client.LimitOrder(TifGtc, "ETH", 0.01, 1234.1, false, "0x1234567890abcdef1234567890abcdef")
if err != nil {
if err != nil {
t.Errorf("Error: %v", err)
}
}
t.Logf("LimitOrder(TifIoc, ETH, 0.01, 1234.1, false, 0x1234567890abcdef1234567890abcdef): %v", res4)
// Get all ordres
res5, err := client.GetAccountOpenOrders()
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("GetAccountOpenOrders(): %v", res5)
// Close all orders
res6, err := client.CancelAllOrders()
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("CancelAllOrders(): %v", res6)
// Make market order
res7, err := client.MarketOrder("ETH", 0.01, nil)
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("MarketOrder(ETH, 0.01, nil): %v", res7)
// Close position
res8, err := client.ClosePosition("ETH")
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("ClosePosition(ETH): %v", res8)
// Get account balance
res9, err := client.GetAccountState()
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("GetAccountState(): %v", res9)
}
func TestHyperliquid_MarketOrder(t *testing.T) {
client := GetHyperliquidAPI(t)
order, err := client.MarketOrder("ADA", 100, nil)
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("MarketOrder(ADA, 100, nil): %+v", order)
}
func TestHyperliquid_LimitOrder(t *testing.T) {
client := GetHyperliquidAPI(t)
order1, err := client.LimitOrder(TifGtc, "BTC", 0.01, 70000, false)
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("LimitOrder(TifGtc, BTC, 0.01, 70000, false): %+v", order1)
order2, err := client.LimitOrder(TifGtc, "BTC", -0.01, 120000, false)
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("LimitOrder(TifGtc, BTC, -0.01, 120000, false): %+v", order2)
}
func TestHyperliquid_GoLimitOrders(t *testing.T) {
client := GetHyperliquidAPI(t)
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
order, err := client.LimitOrder(TifGtc, "BTC", 0.001, 60000, false)
if err != nil {
t.Errorf("Error: %v", err)
}
t.Logf("LimitOrder(TifGtc, BTC, 0.01, 70000, false): %+v", order)
}(wg)
}
wg.Wait()
}