Skip to content

Commit

Permalink
feat: htx usdm apis
Browse files Browse the repository at this point in the history
  • Loading branch information
linstohu committed Dec 20, 2023
1 parent 2bb5525 commit 19fd19c
Show file tree
Hide file tree
Showing 12 changed files with 497 additions and 118 deletions.
135 changes: 135 additions & 0 deletions htx/spot/rest/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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 rest

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

"github.com/linstohu/nexapi/htx/spot/rest/types"
"github.com/linstohu/nexapi/htx/utils"
)

func (scli *SpotClient) GetAccountInfo(ctx context.Context) (*types.GetAccountInfoResponse, error) {
if err := scli.cli.CheckAuth(); err != nil {
return nil, err
}

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
}

{
query := scli.cli.GenAuthParams()

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

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

query.Signature = h

req.Query = query
}

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
}

func (scli *SpotClient) GetAccountValuation(ctx context.Context, param types.GetAccountValuationParam) (*types.GetAccountValuationResp, error) {
if err := scli.cli.CheckAuth(); err != nil {
return nil, err
}

err := scli.validate.Struct(param)
if err != nil {
return nil, err
}

req := utils.HTTPRequest{
BaseURL: scli.cli.GetBaseURL(),
Path: "/v2/account/valuation",
Method: http.MethodGet,
}

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

{
query := types.GetAccountValuationParams{
GetAccountValuationParam: param,
DefaultAuthParam: scli.cli.GenAuthParams(),
}

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

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

query.DefaultAuthParam.Signature = h

req.Query = query
}

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

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

return &ret, nil
}
118 changes: 3 additions & 115 deletions htx/spot/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@
package rest

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

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

Expand All @@ -40,9 +36,9 @@ type SpotClientCfg struct {
// Logger
Logger *slog.Logger

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

func NewSpotClient(cfg *SpotClientCfg) (*SpotClient, error) {
Expand Down Expand Up @@ -70,111 +66,3 @@ func NewSpotClient(cfg *SpotClientCfg) (*SpotClient, error) {
validate: validator,
}, nil
}

func (scli *SpotClient) GetAccountInfo(ctx context.Context) (*types.GetAccountInfoResponse, error) {
if err := scli.cli.CheckAuth(); err != nil {
return nil, err
}

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
}

{
query := scli.cli.GenAuthParams()

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

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

query.Signature = h

req.Query = query
}

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
}

func (scli *SpotClient) GetAccountValuation(ctx context.Context, param types.GetAccountValuationParam) (*types.GetAccountValuationResp, error) {
if err := scli.cli.CheckAuth(); err != nil {
return nil, err
}

err := scli.validate.Struct(param)
if err != nil {
return nil, err
}

req := utils.HTTPRequest{
BaseURL: scli.cli.GetBaseURL(),
Path: "/v2/account/valuation",
Method: http.MethodGet,
}

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

{
query := types.GetAccountValuationParams{
GetAccountValuationParam: param,
DefaultAuthParam: scli.cli.GenAuthParams(),
}

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

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

query.DefaultAuthParam.Signature = h

req.Query = query
}

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

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

return &ret, nil
}
2 changes: 1 addition & 1 deletion htx/spot/rest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

func testNewSpotClient(t *testing.T) *SpotClient {
cli, err := NewSpotClient(&SpotClientCfg{
BaseURL: utils.ProdAWSBaseURL,
BaseURL: utils.SpotAWSBaseURL,
Key: os.Getenv("HTX_KEY"),
Secret: os.Getenv("HTX_SECRET"),
Debug: true,
Expand Down
1 change: 1 addition & 0 deletions htx/spot/rest/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type GetAccountInfoResponse struct {
Status string `json:"status"`
Data []AccountInfo `json:"data"`
}

type AccountInfo struct {
Id int64 `json:"id"`
Type string `json:"type"`
Expand Down
83 changes: 83 additions & 0 deletions htx/usdm/rest/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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 rest

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

"github.com/linstohu/nexapi/htx/usdm/rest/types"
"github.com/linstohu/nexapi/htx/utils"
)

func (ucli *UsdmClient) GetAssetValuation(ctx context.Context, param types.GetAssetValuationParam) (*types.GetAssetValuationResp, error) {
if err := ucli.cli.CheckAuth(); err != nil {
return nil, err
}

err := ucli.validate.Struct(param)
if err != nil {
return nil, err
}

req := utils.HTTPRequest{
BaseURL: ucli.cli.GetBaseURL(),
Path: "/linear-swap-api/v1/swap_balance_valuation",
Method: http.MethodPost,
Body: param,
}

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

{
query := ucli.cli.GenAuthParams()

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

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

query.Signature = h

req.Query = query
}

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

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

return &ret, nil
}
Loading

0 comments on commit 19fd19c

Please sign in to comment.