Skip to content

Commit

Permalink
feat: add htx least api codes
Browse files Browse the repository at this point in the history
  • Loading branch information
linstohu committed Nov 13, 2023
1 parent c70cdb7 commit 7b88c49
Show file tree
Hide file tree
Showing 10 changed files with 630 additions and 6 deletions.
15 changes: 15 additions & 0 deletions htx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# HTX

## About HTX

Huobi Global is an international digital asset exchange with a presence in over 100 countries. Founded in China in 2013, the platform works with global clients and provides trading and investment services to users around the world, handling over $4 billion in daily trading volume.

The ecosystem includes spot trading, margin trading, futures trading, derivatives trading, staking, crypto loans and more. Active traders and investors have the opportunity to enter OTC trading platforms and gain access to custom trading instruments.

## Official Site

- homepage: https://www.htx.com/

## API Documents

https://www.htx.com/en-us/opend/newApiPages/
121 changes: 121 additions & 0 deletions htx/rest/spot/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2023, LinstoHu
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package spot

import (
"context"
"errors"
"log/slog"
"net/http"

"github.com/go-playground/validator"
"github.com/linstohu/nexapi/htx/rest/spot/types"
"github.com/linstohu/nexapi/htx/rest/utils"
)

type SpotClient struct {
cli *utils.HTXClient

// validate struct fields
validate *validator.Validate
}

type SpotClientCfg struct {
Debug bool
// Logger
Logger *slog.Logger

BaseURL string `validate:"required"`
Key string `validate:"required"`
Secret string `validate:"required"`
SignVersion string `validate:"required"`
}

func NewSpotClient(cfg *SpotClientCfg) (*SpotClient, error) {
validator := validator.New()

err := validator.Struct(cfg)
if err != nil {
return nil, err
}

cli, err := utils.NewHTXRestClient(&utils.HTXClientCfg{
Debug: cfg.Debug,
Logger: cfg.Logger,
BaseURL: cfg.BaseURL,
Key: cfg.Key,
Secret: cfg.Secret,
SignVersion: cfg.SignVersion,
})
if err != nil {
return nil, err
}

return &SpotClient{
cli: cli,
validate: validator,
}, nil
}

func (scli *SpotClient) GetAccountInfo(ctx context.Context) (*types.GetAccountInfoResponse, error) {
req := utils.HTTPRequest{
BaseURL: scli.cli.GetBaseURL(),
Path: "/v1/account/accounts",
Method: http.MethodGet,
}

{
headers, err := scli.cli.GetHeaders()
if err != nil {
return nil, err
}
req.Headers = headers
}

{
values, err := scli.cli.GenSignatureValues(req)
if err != nil {
return nil, err
}

signStr, err := scli.cli.NormalizeRequestContent(req, values)
if err != nil {
return nil, err
}

h := scli.cli.Sign([]byte(signStr))
if err != nil {
return nil, err
}

values.Add("Signature", h)
req.Query = values
}

resp, err := scli.cli.SendHTTPRequest(ctx, req)
if err != nil {
return nil, err
}

var ret types.GetAccountInfoResponse
if err := resp.ReadJsonBody(&ret); err != nil {
return nil, errors.New(resp.Error())
}

return &ret, nil
}
51 changes: 51 additions & 0 deletions htx/rest/spot/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023, LinstoHu
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package spot

import (
"context"
"os"
"testing"

"github.com/linstohu/nexapi/htx/rest/utils"
"github.com/stretchr/testify/assert"
)

func testNewSpotClient(t *testing.T) *SpotClient {
cli, err := NewSpotClient(&SpotClientCfg{
BaseURL: utils.ProdAWSBaseURL,
Key: os.Getenv("HTX_KEY"),
Secret: os.Getenv("HTX_SECRET"),
SignVersion: utils.ApiKeyVersionV2,
Debug: true,
})

if err != nil {
t.Fatalf("Could not create htx client, %s", err)
}

return cli
}

func TestGetAccountInfo(t *testing.T) {
cli := testNewSpotClient(t)

_, err := cli.GetAccountInfo(context.TODO())

assert.Nil(t, err)
}
29 changes: 29 additions & 0 deletions htx/rest/spot/types/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023, LinstoHu
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package types

type GetAccountInfoResponse struct {
Status string `json:"status"`
Data []AccountInfo `json:"data"`
}
type AccountInfo struct {
Id int64 `json:"id"`
Type string `json:"type"`
Subtype string `json:"subtype"`
State string `json:"state"`
}
Loading

0 comments on commit 7b88c49

Please sign in to comment.