Skip to content

Commit

Permalink
feat: htx unify account ws update
Browse files Browse the repository at this point in the history
  • Loading branch information
linstohu committed Dec 21, 2023
1 parent a71660f commit f0ebeed
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
24 changes: 24 additions & 0 deletions htx/usdm/accountws/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,27 @@ func TestSubscribeAccountUpdate(t *testing.T) {

select {}
}

func TestSubscribeUnifyAccountUpdate(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

cli := testNewAccountWsClient(ctx, t, GlobalOrderWsBaseURL)

topic, err := cli.GetUnifyAccountUpdateTopic()
assert.Nil(t, err)

cli.AddListener(topic, func(e any) {
acc, ok := e.(*types.UnifyAccount)
if !ok {
return
}

fmt.Printf("Topic: %s, Data: %+v\n",
topic, acc.Data)
})

cli.Subscribe(topic)

select {}
}
12 changes: 12 additions & 0 deletions htx/usdm/accountws/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func (m *AccountWsClient) handle(msg *Message) error {
return err
}
m.GetListeners(msg.Topic, &data)
case strings.HasPrefix(msg.Topic, "accounts_unify"):
var data types.UnifyAccount
err := json.Unmarshal(msg.Raw, &data)
if err != nil {
return err
}
// since the topic in sub is different from the topic returned in ws channel
topic, err := m.GetUnifyAccountUpdateTopic()
if err != nil {
return err
}
m.GetListeners(topic, &data)
default:
return fmt.Errorf("unknown message, topic: %s", msg.Topic)
}
Expand Down
4 changes: 4 additions & 0 deletions htx/usdm/accountws/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ func (m *AccountWsClient) GetCrossAccountUpdateTopic(marginAccount string) (stri

return fmt.Sprintf("accounts_cross.%s", marginAccount), nil
}

func (m *AccountWsClient) GetUnifyAccountUpdateTopic() (string, error) {
return "accounts_unify.USDT", nil
}
55 changes: 55 additions & 0 deletions htx/usdm/accountws/types/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,58 @@ type CrossData struct {
ContractDetail []ContractDetail `json:"contract_detail,omitempty"`
FuturesContractDetail []FuturesContractDetail `json:"futures_contract_detail,omitempty"`
}

type UnifyAccount struct {
Op string `json:"op,omitempty"`
Topic string `json:"topic,omitempty"`
Ts int64 `json:"ts,omitempty"`
Event string `json:"event,omitempty"`
Data []UnifyAccountData `json:"data,omitempty"`
UID string `json:"uid,omitempty"`
}

type CrossSwap struct {
Symbol string `json:"symbol,omitempty"`
ContractCode string `json:"contract_code,omitempty"`
MarginMode string `json:"margin_mode,omitempty"`
MarginAvailable float64 `json:"margin_available,omitempty"`
LeverRate float64 `json:"lever_rate,omitempty"`
ContractType string `json:"contract_type,omitempty"`
BusinessType string `json:"business_type,omitempty"`
CrossMaxAvailable float64 `json:"cross_max_available,omitempty"`
}

type CrossFuture struct {
Symbol string `json:"symbol,omitempty"`
ContractCode string `json:"contract_code,omitempty"`
MarginMode string `json:"margin_mode,omitempty"`
MarginAvailable float64 `json:"margin_available,omitempty"`
LeverRate float64 `json:"lever_rate,omitempty"`
ContractType string `json:"contract_type,omitempty"`
BusinessType string `json:"business_type,omitempty"`
CrossMaxAvailable float64 `json:"cross_max_available,omitempty"`
}

type IsolatedSwap struct {
Symbol string `json:"symbol,omitempty"`
ContractCode string `json:"contract_code,omitempty"`
MarginMode string `json:"margin_mode,omitempty"`
MarginAvailable float64 `json:"margin_available,omitempty"`
WithdrawAvailable float64 `json:"withdraw_available,omitempty"`
LeverRate int `json:"lever_rate,omitempty"`
PositionMode string `json:"position_mode,omitempty"`
}

type UnifyAccountData struct {
MarginAsset string `json:"margin_asset,omitempty"`
MarginStatic float64 `json:"margin_static,omitempty"`
CrossMarginStatic float64 `json:"cross_margin_static,omitempty"`
MarginBalance float64 `json:"margin_balance,omitempty"`
CrossProfitUnreal float64 `json:"cross_profit_unreal,omitempty"`
MarginFrozen float64 `json:"margin_frozen,omitempty"`
WithdrawAvailable float64 `json:"withdraw_available,omitempty"`
CrossRiskRate float64 `json:"cross_risk_rate,omitempty"`
CrossSwap []CrossSwap `json:"cross_swap,omitempty"`
CrossFuture []CrossFuture `json:"cross_future,omitempty"`
IsolatedSwap []IsolatedSwap `json:"isolated_swap,omitempty"`
}

0 comments on commit f0ebeed

Please sign in to comment.