Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix V2 Rest book API return funding result error #205

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,6 @@ type FundingOfferRequest struct {
Rate float64
Period int64
Hidden bool

}

func (o *FundingOfferRequest) ToJSON() ([]byte, error) {
Expand All @@ -997,7 +996,7 @@ func (o *FundingOfferRequest) ToJSON() ([]byte, error) {
Type: o.Type,
Symbol: o.Symbol,
Amount: o.Amount,
Rate: o.Rate,
Rate: o.Rate,
Period: o.Period,
}
if o.Hidden {
Expand Down Expand Up @@ -1556,6 +1555,7 @@ type BookUpdate struct {
AmountJsNum json.Number // update amount as json.Number
Side OrderSide // side
Action BookAction // action (add/remove)
Period float64 // Period level (Funding only)
}

type BookUpdateSnapshot struct {
Expand All @@ -1581,9 +1581,14 @@ func IsRawBook(precision string) bool {
return precision == "R0"
}

func IsFunding(symbol string) bool {
return strings.HasPrefix(symbol, "f")
Copy link

@harry830622 harry830622 Aug 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return strings.HasPrefix(symbol, "f")
return strings.HasPrefix(symbol, FundingPrefix)

}

// NewBookUpdateFromRaw creates a new book update object from raw data. Precision determines how
// to interpret the side (baked into Count versus Amount)
// raw book updates [ID, price, qty], aggregated book updates [price, amount, count]
// Trading - raw book updates [ID, price, amount], aggregated book updates [price, count, amount]
// Funding - on raw books [ID, period, rate, amount], aggregated book updates [rate, period, count, amount]
func NewBookUpdateFromRaw(symbol, precision string, data []interface{}, raw_numbers interface{}) (b *BookUpdate, err error) {
if len(data) < 3 {
return b, fmt.Errorf("data slice too short for book update, expected %d got %d: %#v", 5, len(data), data)
Expand All @@ -1592,23 +1597,44 @@ func NewBookUpdateFromRaw(symbol, precision string, data []interface{}, raw_numb
var px_num json.Number
var id, cnt int64
raw_num_array := raw_numbers.([]interface{})
amt := f64ValOrZero(data[2])
amt_num := floatToJsonNumber(raw_num_array[2])

var side OrderSide
var actionCtrl float64
if IsRawBook(precision) {
// [ID, price, amount]
id = i64ValOrZero(data[0])
px = f64ValOrZero(data[1])
px_num = floatToJsonNumber(raw_num_array[1])
actionCtrl = px
var actionCtrl, amt, period float64
var amt_num json.Number
if IsFunding(symbol) {
amt = f64ValOrZero(data[3])
amt_num = floatToJsonNumber(raw_num_array[3])
if IsRawBook(precision) {
// [ID, period, rate, amount]
id = i64ValOrZero(data[0])
px = f64ValOrZero(data[2])
px_num = floatToJsonNumber(raw_num_array[2])
actionCtrl = px
period = f64ValOrZero(data[1])
} else {
// [rate, period, count, amount]
px = f64ValOrZero(data[0])
px_num = floatToJsonNumber(raw_num_array[0])
cnt = i64ValOrZero(data[2])
actionCtrl = float64(cnt)
period = f64ValOrZero(data[1])
}
} else {
// [price, amount, count]
px = f64ValOrZero(data[0])
px_num = floatToJsonNumber(raw_num_array[0])
cnt = i64ValOrZero(data[1])
actionCtrl = float64(cnt)
amt = f64ValOrZero(data[2])
amt_num = floatToJsonNumber(raw_num_array[2])
if IsRawBook(precision) {
// [ID, price, amount]
id = i64ValOrZero(data[0])
px = f64ValOrZero(data[1])
px_num = floatToJsonNumber(raw_num_array[1])
actionCtrl = px
} else {
// [price, count, amount]
px = f64ValOrZero(data[0])
px_num = floatToJsonNumber(raw_num_array[0])
cnt = i64ValOrZero(data[1])
actionCtrl = float64(cnt)
}
}

if amt > 0 {
Expand All @@ -1634,6 +1660,7 @@ func NewBookUpdateFromRaw(symbol, precision string, data []interface{}, raw_numb
Side: side,
Action: action,
ID: id,
Period: period,
}

return
Expand Down