Skip to content

Commit 69047bb

Browse files
authored
Merge pull request #16 from terwey/fix/vaultaddr-response-parsing
Fix/vaultaddr response parsing
2 parents 76cbb8a + ab00c31 commit 69047bb

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

api.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ func (r *APIResponse[T]) UnmarshalJSON(data []byte) error {
4646

4747
// When status is "ok", "response" contains "type" and "data"
4848
r.Type = string(parsed.GetStringBytes("response", "type"))
49-
responseData := parsed.GetStringBytes("response", "data")
49+
50+
// GetStringBytes() only works on string, we should do a Get instead
51+
responseData := parsed.Get("response", "data")
5052

5153
if responseData == nil {
5254
return fmt.Errorf("missing response.data field in successful response")
5355
}
5456

57+
b := responseData.MarshalTo(nil)
58+
5559
// Use fastjson's built-in unmarshaling if possible, fallback to json.Unmarshal
56-
if err := json.Unmarshal(responseData, &r.Data); err != nil {
60+
if err := json.Unmarshal(b, &r.Data); err != nil {
5761
return fmt.Errorf("failed to unmarshal response data: %w", err)
5862
}
5963

exchange.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,19 @@ func (e *Exchange) postAction(
7070
"signature": signature,
7171
}
7272

73-
// Handle vault address based on action type
74-
if actionMap, ok := action.(map[string]any); ok {
75-
if actionMap["type"] != "usdClassTransfer" {
76-
payload["vaultAddress"] = e.vault
73+
if e.vault != "" {
74+
// Handle vault address based on action type
75+
if actionMap, ok := action.(map[string]any); ok {
76+
if actionMap["type"] != "usdClassTransfer" {
77+
payload["vaultAddress"] = e.vault
78+
} else {
79+
payload["vaultAddress"] = nil
80+
}
7781
} else {
78-
payload["vaultAddress"] = nil
82+
// For struct types, we need to use reflection or type assertion
83+
// For now, assume it's not usdClassTransfer
84+
payload["vaultAddress"] = e.vault
7985
}
80-
} else {
81-
// For struct types, we need to use reflection or type assertion
82-
// For now, assume it's not usdClassTransfer
83-
payload["vaultAddress"] = e.vault
8486
}
8587

8688
// Add expiration time if set

exchange_orders.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hyperliquid
22

33
import (
4+
"encoding/json"
45
"fmt"
56
)
67

@@ -39,7 +40,12 @@ type OrderStatusFilled struct {
3940
type OrderStatus struct {
4041
Resting *OrderStatusResting `json:"resting,omitempty"`
4142
Filled *OrderStatusFilled `json:"filled,omitempty"`
42-
Error *error `json:"error,omitempty"`
43+
Error *string `json:"error,omitempty"`
44+
}
45+
46+
func (s *OrderStatus) String() string {
47+
data, _ := json.Marshal(s)
48+
return string(data)
4349
}
4450

4551
type OrderResponse struct {

0 commit comments

Comments
 (0)