Skip to content

Commit

Permalink
Fixes adshao#615 (AmountToLotSize works incorrect)
Browse files Browse the repository at this point in the history
Add `page` parameter to `GetIncomeHistoryService`
  • Loading branch information
ramilexe committed Jan 4, 2025
1 parent bb34461 commit 41cc41d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
21 changes: 16 additions & 5 deletions v2/common/helpers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package common

import "math"
import "bytes"
import (
"bytes"
"math"
)

// AmountToLotSize converts an amount to a lot sized amount
func AmountToLotSize(lot float64, precision int, amount float64) float64 {
return math.Trunc(math.Floor(amount/lot)*lot*math.Pow10(precision)) / math.Pow10(precision)
// RoundPriceToTickSize rounds the price to the nearest tick size
func RoundPriceToTickSize(price, tickSize float64) float64 {
if tickSize == 0 {
return price // To avoid division by zero
}
// Calculate the factor by which to multiply and divide the price to conform to the tick size.
factor := 1 / tickSize

// Round the price to the nearest tick size.
roundedPrice := math.Round(price*factor) / factor

return roundedPrice
}

// ToJSONList convert v to json list if v is a map
Expand Down
11 changes: 10 additions & 1 deletion v2/common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func TestAmountToLotSize(t *testing.T) {
},
want: 1.389,
},
{
name: "test MEW",
args: args{
lot: 0.0000010,
precision: 7,
amount: 0.003923153000000002,
},
want: 0.003923,
},
{
name: "test with big decimal",
args: args{
Expand All @@ -57,7 +66,7 @@ func TestAmountToLotSize(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(tt.want, AmountToLotSize(tt.args.lot, tt.args.precision, tt.args.amount))
assert.Equal(tt.want, RoundPriceToTickSize(tt.args.amount, tt.args.lot))
})
}
}
10 changes: 10 additions & 0 deletions v2/futures/income_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type GetIncomeHistoryService struct {
incomeType string
startTime *int64
endTime *int64
page *int64
limit *int64
}

Expand Down Expand Up @@ -46,6 +47,12 @@ func (s *GetIncomeHistoryService) Limit(limit int64) *GetIncomeHistoryService {
return s
}

// Page set page
func (s *GetIncomeHistoryService) Page(page int64) *GetIncomeHistoryService {
s.page = &page
return s
}

// Do send request
func (s *GetIncomeHistoryService) Do(ctx context.Context, opts ...RequestOption) (res []*IncomeHistory, err error) {
r := &request{
Expand All @@ -63,6 +70,9 @@ func (s *GetIncomeHistoryService) Do(ctx context.Context, opts ...RequestOption)
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
if s.page != nil {
r.setParam("page", *s.page)
}
if s.limit != nil {
r.setParam("limit", *s.limit)
}
Expand Down

0 comments on commit 41cc41d

Please sign in to comment.