Skip to content

Commit

Permalink
Refactor binance package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
dasbd72 committed Mar 3, 2024
1 parent ffeaa45 commit c63bc2d
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 116 deletions.
10 changes: 10 additions & 0 deletions binance/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/http"
"net/url"
"time"
)

type Client struct {
Expand Down Expand Up @@ -149,3 +150,12 @@ func sign(secret, message string) string {
mac.Write([]byte(message))
return fmt.Sprintf("%x", mac.Sum(nil))
}

func currentTimestamp() int64 {
return formatTimestamp(time.Now())
}

// formatTimestamp formats a time into Unix timestamp in milliseconds, as requested by Binance.
func formatTimestamp(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}
28 changes: 28 additions & 0 deletions binance/time_test.go → binance/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ import (
"time"
)

func Test_sign(t *testing.T) {
type args struct {
secret string
message string
}
tests := []struct {
name string
args args
want string
}{
{
name: "testcase 1",
args: args{
secret: "secret",
message: "message",
},
want: "8b5f48702995c1598c573db1e21866a9b825d4a794d169d7060a03605796360b",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sign(tt.args.secret, tt.args.message); got != tt.want {
t.Errorf("sign() = %v, want %v", got, tt.want)
}
})
}
}

func TestFormatTimestamp(t *testing.T) {
t.Parallel()

Expand Down
97 changes: 13 additions & 84 deletions binance/market_data_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,11 @@ import (
"context"
"encoding/json"
"net/http"
)

type (
GetPingResponse struct {
}

GetServerTimeResponse struct {
ServerTime int64 `json:"serverTime"`
}

GetExchangeInfoResponse struct {
Timezone string `json:"timezone"`
ServerTime int64 `json:"serverTime"`
RateLimits []interface{}
ExchangeFilters []interface{}
Symbols []struct {
Symbol string `json:"symbol"`
Status string `json:"status"`
BaseAsset string `json:"baseAsset"`
BaseAssetPrecision int `json:"baseAssetPrecision"`
QuoteAsset string `json:"quoteAsset"`
QuotePrecision int `json:"quotePrecision"`
QuoteAssetPrecision int `json:"quoteAssetPrecision"`
OrderTypes []string `json:"orderTypes"`
IcebergAllowed bool `json:"icebergAllowed"`
OcoAllowed bool `json:"ocoAllowed"`
QuoteOrderQtyMarketAllowed bool `json:"quoteOrderQtyMarketAllowed"`
AllowTrailingStop bool `json:"allowTrailingStop"`
CancelReplaceAllowed bool `json:"cancelReplaceAllowed"`
IsSpotTradingAllowed bool `json:"isSpotTradingAllowed"`
IsMarginTradingAllowed bool `json:"isMarginTradingAllowed"`
Filters []interface{} `json:"filters"`
Permissions []string `json:"permissions"`
DefaultSelfTradePreventionMode string `json:"defaultSelfTradePreventionMode"`
AllowedSelfTradePreventionModes []string `json:"allowedSelfTradePreventionModes"`
}
}

GetOrderBookRequest struct {
params map[string]interface{}
}

GetOrderBookResponse struct {
LastUpdateID int64 `json:"lastUpdateId"`
Bids [][]JSONFloat64 `json:"bids"`
Asks [][]JSONFloat64 `json:"asks"`
}

GetAveragePriceRequest struct {
params map[string]interface{}
}

GetAveragePriceResponse struct {
Mins int `json:"mins"`
Price JSONFloat64 `json:"price"`
CloseTime int64 `json:"closeTime"`
}
"github.com/dasbd72/go-exchange-sdk/binance/pkg/models"
)

func (c *Client) GetPing(ctx context.Context, opts ...RequestOption) (*GetPingResponse, error) {
func (c *Client) GetPing(ctx context.Context, opts ...RequestOption) (*models.GetPingResponse, error) {
res, err := c.CallAPI(ctx, Request_builder{
Method: http.MethodGet,
Endpoint: "/api/v3/ping",
Expand All @@ -72,15 +17,15 @@ func (c *Client) GetPing(ctx context.Context, opts ...RequestOption) (*GetPingRe
if err != nil {
return nil, err
}
data := &GetPingResponse{}
data := &models.GetPingResponse{}
err = json.Unmarshal(res, data)
if err != nil {
return nil, err
}
return data, nil
}

func (c *Client) GetServerTime(ctx context.Context, opts ...RequestOption) (*GetServerTimeResponse, error) {
func (c *Client) GetServerTime(ctx context.Context, opts ...RequestOption) (*models.GetServerTimeResponse, error) {
res, err := c.CallAPI(ctx, Request_builder{
Method: http.MethodGet,
Endpoint: "/api/v3/time",
Expand All @@ -89,15 +34,15 @@ func (c *Client) GetServerTime(ctx context.Context, opts ...RequestOption) (*Get
if err != nil {
return nil, err
}
data := &GetServerTimeResponse{}
data := &models.GetServerTimeResponse{}
err = json.Unmarshal(res, data)
if err != nil {
return nil, err
}
return data, nil
}

func (c *Client) GetExchangeInfo(ctx context.Context, opts ...RequestOption) (*GetExchangeInfoResponse, error) {
func (c *Client) GetExchangeInfo(ctx context.Context, opts ...RequestOption) (*models.GetExchangeInfoResponse, error) {
res, err := c.CallAPI(ctx, Request_builder{
Method: http.MethodGet,
Endpoint: "/api/v3/exchangeInfo",
Expand All @@ -106,59 +51,43 @@ func (c *Client) GetExchangeInfo(ctx context.Context, opts ...RequestOption) (*G
if err != nil {
return nil, err
}
data := &GetExchangeInfoResponse{}
data := &models.GetExchangeInfoResponse{}
err = json.Unmarshal(res, data)
if err != nil {
return nil, err
}
return data, nil
}

func NewGetOrderBookRequest(symbol string) *GetOrderBookRequest {
return &GetOrderBookRequest{
params: map[string]interface{}{
"symbol": symbol,
},
}
}

func (c *Client) GetOrderBook(ctx context.Context, req *GetOrderBookRequest, opts ...RequestOption) (*GetOrderBookResponse, error) {
func (c *Client) GetOrderBook(ctx context.Context, req *models.GetOrderBookRequest, opts ...RequestOption) (*models.GetOrderBookResponse, error) {
res, err := c.CallAPI(ctx, Request_builder{
Method: http.MethodGet,
Endpoint: "/api/v3/depth",
SecType: SecTypeNone,
Params: req.params,
Params: req.Params(),
}.Build(), opts...)
if err != nil {
return nil, err
}
data := &GetOrderBookResponse{}
data := &models.GetOrderBookResponse{}
err = json.Unmarshal(res, data)
if err != nil {
return nil, err
}
return data, nil
}

func NewGetAveragePriceRequest(symbol string) *GetAveragePriceRequest {
return &GetAveragePriceRequest{
params: map[string]interface{}{
"symbol": symbol,
},
}
}

func (c *Client) GetAveragePrice(ctx context.Context, req *GetAveragePriceRequest, opts ...RequestOption) (*GetAveragePriceResponse, error) {
func (c *Client) GetAveragePrice(ctx context.Context, req *models.GetAveragePriceRequest, opts ...RequestOption) (*models.GetAveragePriceResponse, error) {
res, err := c.CallAPI(ctx, Request_builder{
Method: http.MethodGet,
Endpoint: "/api/v3/avgPrice",
SecType: SecTypeNone,
Params: req.params,
Params: req.Params(),
}.Build(), opts...)
if err != nil {
return nil, err
}
data := &GetAveragePriceResponse{}
data := &models.GetAveragePriceResponse{}
err = json.Unmarshal(res, data)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion binance/definition.go → binance/pkg/cast/cast.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package binance
package cast

import (
"strconv"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package binance
package cast

import (
"strconv"
Expand Down
84 changes: 84 additions & 0 deletions binance/pkg/models/market_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package models

import "github.com/dasbd72/go-exchange-sdk/binance/pkg/cast"

type (
GetPingResponse struct {
}

GetServerTimeResponse struct {
ServerTime int64 `json:"serverTime"`
}

GetExchangeInfoResponse struct {
Timezone string `json:"timezone"`
ServerTime int64 `json:"serverTime"`
RateLimits []interface{}
ExchangeFilters []interface{}
Symbols []struct {
Symbol string `json:"symbol"`
Status string `json:"status"`
BaseAsset string `json:"baseAsset"`
BaseAssetPrecision int `json:"baseAssetPrecision"`
QuoteAsset string `json:"quoteAsset"`
QuotePrecision int `json:"quotePrecision"`
QuoteAssetPrecision int `json:"quoteAssetPrecision"`
OrderTypes []string `json:"orderTypes"`
IcebergAllowed bool `json:"icebergAllowed"`
OcoAllowed bool `json:"ocoAllowed"`
QuoteOrderQtyMarketAllowed bool `json:"quoteOrderQtyMarketAllowed"`
AllowTrailingStop bool `json:"allowTrailingStop"`
CancelReplaceAllowed bool `json:"cancelReplaceAllowed"`
IsSpotTradingAllowed bool `json:"isSpotTradingAllowed"`
IsMarginTradingAllowed bool `json:"isMarginTradingAllowed"`
Filters []interface{} `json:"filters"`
Permissions []string `json:"permissions"`
DefaultSelfTradePreventionMode string `json:"defaultSelfTradePreventionMode"`
AllowedSelfTradePreventionModes []string `json:"allowedSelfTradePreventionModes"`
}
}

GetOrderBookRequest struct {
params map[string]interface{}
}

GetOrderBookResponse struct {
LastUpdateID int64 `json:"lastUpdateId"`
Bids [][]cast.JSONFloat64 `json:"bids"`
Asks [][]cast.JSONFloat64 `json:"asks"`
}

GetAveragePriceRequest struct {
params map[string]interface{}
}

GetAveragePriceResponse struct {
Mins int `json:"mins"`
Price cast.JSONFloat64 `json:"price"`
CloseTime int64 `json:"closeTime"`
}
)

func NewGetOrderBookRequest(symbol string) *GetOrderBookRequest {
return &GetOrderBookRequest{
params: map[string]interface{}{
"symbol": symbol,
},
}
}

func (data *GetOrderBookRequest) Params() map[string]interface{} {
return data.params
}

func NewGetAveragePriceRequest(symbol string) *GetAveragePriceRequest {
return &GetAveragePriceRequest{
params: map[string]interface{}{
"symbol": symbol,
},
}
}

func (data *GetAveragePriceRequest) Params() map[string]interface{} {
return data.params
}
16 changes: 16 additions & 0 deletions binance/pkg/models/wallet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package models

import "github.com/dasbd72/go-exchange-sdk/binance/pkg/cast"

type (
GetWalletStatusResponse struct {
Status int `json:"status"`
Msg string `json:"msg"`
}

GetUserWalletBalanceResponse []struct {
Activate bool `json:"activate"`
Balance cast.JSONFloat64 `json:"balance"` // in BTC
WalletName string `json:"walletName"`
}
)
12 changes: 0 additions & 12 deletions binance/time.go

This file was deleted.

Loading

0 comments on commit c63bc2d

Please sign in to comment.