Skip to content

Commit

Permalink
fix: explicitly state that it is int64 (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokisan authored Mar 8, 2023
1 parent ab3969d commit d374129
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
32 changes: 16 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,25 @@ func (c *Client) hasAuth() bool {
}

func (c *Client) populateSignature(src url.Values) url.Values {
intNow := int(time.Now().UTC().UnixNano() / int64(time.Millisecond))
now := strconv.Itoa(intNow)

if src == nil {
src = url.Values{}
}

src.Add("api_key", c.key)
src.Add("timestamp", now)
src.Add("timestamp", strconv.FormatInt(c.getTimestamp(), 10))
src.Add("sign", getSignature(src, c.secret))

return src
}

func (c *Client) populateSignatureForBody(src []byte) []byte {
intNow := int(time.Now().UTC().UnixNano() / int64(time.Millisecond))
now := strconv.Itoa(intNow)

body := map[string]interface{}{}
if err := json.Unmarshal(src, &body); err != nil {
panic(err)
}

body["api_key"] = c.key
body["timestamp"] = now
body["timestamp"] = strconv.FormatInt(c.getTimestamp(), 10)
body["sign"] = getSignatureForBody(body, c.secret)

result, err := json.Marshal(body)
Expand All @@ -150,25 +144,25 @@ func (c *Client) populateSignatureForBody(src []byte) []byte {
}

func getV5Signature(
timestamp int,
timestamp int64,
key string,
queryString string,
secret string,
) string {
val := strconv.Itoa(timestamp) + key
val := strconv.FormatInt(timestamp, 10) + key
val = val + queryString
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(val))
return hex.EncodeToString(h.Sum(nil))
}

func getV5SignatureForBody(
timestamp int,
timestamp int64,
key string,
body []byte,
secret string,
) string {
val := strconv.Itoa(timestamp) + key
val := strconv.FormatInt(timestamp, 10) + key
val = val + string(body)
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(val))
Expand Down Expand Up @@ -276,15 +270,15 @@ func (c *Client) getV5Privately(path string, query url.Values, dst interface{})
u.Path = path
u.RawQuery = query.Encode()

timestamp := int(time.Now().UTC().UnixNano() / int64(time.Millisecond))
timestamp := c.getTimestamp()
sign := getV5Signature(timestamp, c.key, query.Encode(), c.secret)

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return err
}
req.Header.Set("X-BAPI-API-KEY", c.key)
req.Header.Set("X-BAPI-TIMESTAMP", strconv.Itoa(timestamp))
req.Header.Set("X-BAPI-TIMESTAMP", strconv.FormatInt(timestamp, 10))
req.Header.Set("X-BAPI-SIGN", sign)

if err := c.Request(req, &dst); err != nil {
Expand Down Expand Up @@ -331,7 +325,7 @@ func (c *Client) postV5JSON(path string, body []byte, dst interface{}) error {
}
u.Path = path

timestamp := int(time.Now().UTC().UnixNano() / int64(time.Millisecond))
timestamp := c.getTimestamp()
sign := getV5SignatureForBody(timestamp, c.key, body, c.secret)

req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(body))
Expand All @@ -340,7 +334,7 @@ func (c *Client) postV5JSON(path string, body []byte, dst interface{}) error {
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-BAPI-API-KEY", c.key)
req.Header.Set("X-BAPI-TIMESTAMP", strconv.Itoa(timestamp))
req.Header.Set("X-BAPI-TIMESTAMP", strconv.FormatInt(timestamp, 10))
req.Header.Set("X-BAPI-SIGN", sign)

if err := c.Request(req, &dst); err != nil {
Expand Down Expand Up @@ -403,3 +397,9 @@ func (c *Client) deletePrivately(path string, query url.Values, dst interface{})

return nil
}

func (c *Client) getTimestamp() int64 {
now := time.Now()
unixNano := now.UnixNano()
return unixNano / 1000000
}
6 changes: 3 additions & 3 deletions future_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *FutureCommonService) OrderBook(symbol SymbolFuture) (*OrderBookResponse
type ListKlineParam struct {
Symbol SymbolFuture `url:"symbol"`
Interval Interval `url:"interval"`
From int `url:"from"`
From int64 `url:"from"`

Limit *int `url:"limit,omitempty"`
}
Expand Down Expand Up @@ -335,7 +335,7 @@ type MarkPriceKlineResult struct {
type MarkPriceKlineParam struct {
Symbol SymbolFuture `url:"symbol"`
Interval Interval `url:"interval"`
From int `url:"from"`
From int64 `url:"from"`

Limit *int `url:"limit,omitempty"`
}
Expand Down Expand Up @@ -377,7 +377,7 @@ type IndexPriceKlineResult struct {
type IndexPriceKlineParam struct {
Symbol SymbolFuture `url:"symbol"`
Interval Interval `url:"interval"`
From int `url:"from"`
From int64 `url:"from"`

Limit *int `url:"limit,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion future_inverse_perpetual.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type PremiumIndexKlineResult struct {
type PremiumIndexKlineParam struct {
Symbol SymbolFuture `url:"symbol"`
Interval Interval `url:"interval"`
From int `url:"from"`
From int64 `url:"from"`

Limit *int `url:"limit,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion future_usdt_perpetual.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type FutureUSDTPerpetualService struct {
type ListLinearKlineParam struct {
Symbol SymbolFuture `url:"symbol"`
Interval Interval `url:"interval"`
From int `url:"from"`
From int64 `url:"from"`

Limit *int `url:"limit,omitempty"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestListKline(t *testing.T) {
res, err := client.Future().InverseFuture().ListKline(bybit.ListKlineParam{
Symbol: bybit.SymbolFutureBTCUSD,
Interval: bybit.Interval120,
From: int(time.Now().AddDate(0, 0, -1).Unix()),
From: time.Now().AddDate(0, 0, -1).Unix(),
})
require.NoError(t, err)
{
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestMarkPriceKline(t *testing.T) {
res, err := client.Future().InverseFuture().MarkPriceKline(bybit.MarkPriceKlineParam{
Symbol: bybit.SymbolFutureBTCUSD,
Interval: bybit.IntervalD,
From: int(time.Now().AddDate(0, 0, -1).Unix()),
From: time.Now().AddDate(0, 0, -1).Unix(),
})
require.NoError(t, err)
{
Expand All @@ -132,7 +132,7 @@ func TestIndexPriceKline(t *testing.T) {
res, err := client.Future().InverseFuture().IndexPriceKline(bybit.IndexPriceKlineParam{
Symbol: bybit.SymbolFutureBTCUSD,
Interval: bybit.IntervalD,
From: int(time.Now().AddDate(0, 0, -1).Unix()),
From: time.Now().AddDate(0, 0, -1).Unix(),
})
require.NoError(t, err)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestListKline(t *testing.T) {
res, err := client.Future().InversePerpetual().ListKline(bybit.ListKlineParam{
Symbol: bybit.SymbolFutureBTCUSD,
Interval: bybit.Interval120,
From: int(time.Now().AddDate(0, 0, -1).Unix()),
From: time.Now().AddDate(0, 0, -1).Unix(),
})
require.NoError(t, err)
{
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestMarkPriceKline(t *testing.T) {
res, err := client.Future().InverseFuture().MarkPriceKline(bybit.MarkPriceKlineParam{
Symbol: bybit.SymbolFutureBTCUSD,
Interval: bybit.IntervalD,
From: int(time.Now().AddDate(0, 0, -1).Unix()),
From: time.Now().AddDate(0, 0, -1).Unix(),
})
require.NoError(t, err)
{
Expand All @@ -132,7 +132,7 @@ func TestIndexPriceKline(t *testing.T) {
res, err := client.Future().InversePerpetual().IndexPriceKline(bybit.IndexPriceKlineParam{
Symbol: bybit.SymbolFutureBTCUSD,
Interval: bybit.IntervalD,
From: int(time.Now().AddDate(0, 0, -1).Unix()),
From: time.Now().AddDate(0, 0, -1).Unix(),
})
require.NoError(t, err)
{
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestPremiumIndexKline(t *testing.T) {
res, err := client.Future().InversePerpetual().PremiumIndexKline(bybit.PremiumIndexKlineParam{
Symbol: bybit.SymbolFutureBTCUSD,
Interval: bybit.Interval120,
From: int(time.Now().AddDate(0, 0, -1).Unix()),
From: time.Now().AddDate(0, 0, -1).Unix(),
})
require.NoError(t, err)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestListLinearKline(t *testing.T) {
res, err := client.Future().USDTPerpetual().ListLinearKline(bybit.ListLinearKlineParam{
Symbol: bybit.SymbolFutureBTCUSDT,
Interval: bybit.Interval120,
From: int(time.Now().AddDate(0, 0, -1).Unix()),
From: time.Now().AddDate(0, 0, -1).Unix(),
})
require.NoError(t, err)
{
Expand Down

0 comments on commit d374129

Please sign in to comment.