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

Bitfinex: Fix WS trade processing #1754

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gbjk
Copy link
Collaborator

@gbjk gbjk commented Dec 18, 2024

  • Bitfinex
    • Fixes exchange Bitfinex websocket error - unhandled WS trade update event:
    • Add tests for TestWSAllTrades
    • Add handling for public marginFunding trades
    • Fix idiosyncratic naming using TestWs instead of TestWS
    • Fix naming for wsTradeUpdated ( was wsTradeExecutionUpdate )
    • Fix trades not going to DataHandler unless SaveTrade was enabled
    • Enable tradeFeed for tests
  • Linter
    • Disables shadow checks for err. Advocated this before, but had my fill of stylecheck (correctly, IMO) fighting with shadow over this rule. This isn't strictly necessary, but it's nearly christmas again.

Fixes #1746
Progresses #1323 with a prototype

Type of change

  • Bug fix (non-breaking change which fixes an issue)

* Add handling for funding trades

Fixes thrasher-corp#1746
@gbjk gbjk added bug review me This pull request is ready for review labels Dec 18, 2024
@gbjk gbjk self-assigned this Dec 18, 2024
@gbjk gbjk force-pushed the bugfix/bitfinex_ws_trade branch 2 times, most recently from 0dd865c to cb416cc Compare December 19, 2024 01:52
Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

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

nice stuff, not much from me.

exchanges/bitfinex/bitfinex_test.go Outdated Show resolved Hide resolved
exchanges/bitfinex/bitfinex_websocket.go Show resolved Hide resolved
exchanges/bitfinex/bitfinex_websocket.go Outdated Show resolved Hide resolved
exchanges/bitfinex/bitfinex_websocket.go Outdated Show resolved Hide resolved
Copy link

codecov bot commented Dec 27, 2024

Codecov Report

Attention: Patch coverage is 55.69620% with 35 lines in your changes missing coverage. Please review.

Project coverage is 37.37%. Comparing base (105591b) to head (d3406a6).
Report is 28 commits behind head on master.

Files with missing lines Patch % Lines
exchanges/bitfinex/bitfinex_websocket.go 53.33% 26 Missing and 9 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1754      +/-   ##
==========================================
+ Coverage   37.13%   37.37%   +0.23%     
==========================================
  Files         414      415       +1     
  Lines      180198   178586    -1612     
==========================================
- Hits        66925    66746     -179     
+ Misses     105413   104042    -1371     
+ Partials     7860     7798      -62     
Files with missing lines Coverage Δ
exchanges/bitfinex/bitfinex_types.go 100.00% <100.00%> (ø)
exchanges/btse/btse_wrapper.go 43.77% <100.00%> (ø)
exchanges/subscription/template.go 97.70% <100.00%> (-2.30%) ⬇️
exchanges/bitfinex/bitfinex_websocket.go 36.00% <53.33%> (+3.20%) ⬆️

... and 52 files with indirect coverage changes

@gbjk gbjk requested a review from shazbert December 27, 2024 06:45
Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my last nits.

exchanges/bitfinex/bitfinex_types.go Outdated Show resolved Hide resolved
.golangci.yml Outdated Show resolved Hide resolved
exchanges/bitfinex/bitfinex_websocket.go Outdated Show resolved Hide resolved
@gbjk gbjk requested a review from shazbert December 29, 2024 01:29
Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

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

tACK.

It's been a year, and I'm still getting caught out by govet demanding I
don't shadow a var I was deliberately shadowing.
Made worse by an increase in clashes with stylecheck when they both want
opposite things on the same line.
@gbjk gbjk force-pushed the bugfix/bitfinex_ws_trade branch from 48ebaf7 to f86fff4 Compare January 6, 2025 01:47
@@ -1382,17 +1372,44 @@ func TestWsOrderBook(t *testing.T) {
assert.ErrorIs(t, err, errNoSeqNo, "handleWSBookUpdate should send correct error")
}

func TestWsTradeResponse(t *testing.T) {
func TestWSAllTrades(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please fix test

data, ok := d[2].([]interface{})
if !ok {
return errors.New("trade data type assertion error")
var wsTrades []*wsTrade
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please change this shadowed var name/the original string var name

Comment on lines +938 to +939
if valueType != jsonparser.Array {
errs = common.AppendError(errs, fmt.Errorf("%w `tradesSnapshot[1][*]`: %w `%s`", errParsingWSField, jsonparser.UnknownValueTypeError, valueType))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a little bit extra given that there is only one caller to this and its behind a gate checking if its an array. I am normally one for all checks should be within the function, but its that you just checked to get to this function

Copy link
Collaborator

@gloriousCode gloriousCode Jan 7, 2025

Choose a reason for hiding this comment

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

How about this simplification?

func (b *Bitfinex) handleWSPublicTradesSnapshot(respRaw []byte) ([]*wsTrade, error) {
	var trades []*wsTrade
	err := json.Unmarshal(respRaw, &trades)
	return trades, err
}

Where handleWSAllTrades changes with:

k, valueType, _, err := jsonparser.Get(respRaw, "[1]")
...
if wsTrades, err = b.handleWSPublicTradesSnapshot(k); err != nil {

A lot less tangling of additional jsonparser functions, with added bonus of naked return removal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug review me This pull request is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bitfinex: Fix WS Trade stream erroring
3 participants