github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/mux/msg/msg_test.go (about)

     1  package msg_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/balanceinfo"
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/book"
     8  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/candle"
     9  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/event"
    10  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingcredit"
    11  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingloan"
    12  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingoffer"
    13  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/margin"
    14  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/notification"
    15  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/order"
    16  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/position"
    17  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/status"
    18  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/ticker"
    19  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/trades"
    20  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/wallet"
    21  	"github.com/bitfinexcom/bitfinex-api-go/pkg/mux/msg"
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestIsEvent(t *testing.T) {
    27  	cases := map[string]struct {
    28  		pld      []byte
    29  		expected bool
    30  	}{
    31  		"event type": {
    32  			pld:      []byte(`{}`),
    33  			expected: true,
    34  		},
    35  		"not event type": {
    36  			pld:      []byte(`[]`),
    37  			expected: false,
    38  		},
    39  	}
    40  
    41  	for k, v := range cases {
    42  		t.Run(k, func(t *testing.T) {
    43  			m := msg.Msg{
    44  				Data: v.pld,
    45  			}
    46  
    47  			got := m.IsEvent()
    48  			assert.Equal(t, v.expected, got)
    49  		})
    50  	}
    51  }
    52  
    53  func TestIsRaw(t *testing.T) {
    54  	cases := map[string]struct {
    55  		pld      []byte
    56  		expected bool
    57  	}{
    58  		"raw msg type": {
    59  			pld:      []byte(`[]`),
    60  			expected: true,
    61  		},
    62  		"raw info type": {
    63  			pld:      []byte(`{}`),
    64  			expected: false,
    65  		},
    66  	}
    67  
    68  	for k, v := range cases {
    69  		t.Run(k, func(t *testing.T) {
    70  			m := msg.Msg{
    71  				Data: v.pld,
    72  			}
    73  
    74  			got := m.IsRaw()
    75  			assert.Equal(t, v.expected, got)
    76  		})
    77  	}
    78  }
    79  
    80  func TestProcessEvent(t *testing.T) {
    81  	m := msg.Msg{
    82  		Data: []byte(`{
    83  			"event": "info",
    84  			"version": 2,
    85  			"serverId": "dbea77ee-4740-4a82-84f3-c6bc1b5abb9a",
    86  			"platform": {
    87  				"status":1
    88  			}
    89  		}`),
    90  	}
    91  
    92  	expected := event.Info{
    93  		Subscribe: event.Subscribe{
    94  			Event: "info",
    95  		},
    96  		Version:  2,
    97  		ServerID: "dbea77ee-4740-4a82-84f3-c6bc1b5abb9a",
    98  		Platform: event.Platform{Status: 1},
    99  	}
   100  
   101  	got, err := m.ProcessEvent()
   102  	assert.NoError(t, err)
   103  	assert.Equal(t, expected, got)
   104  }
   105  
   106  func TestProcessPublic(t *testing.T) {
   107  	cases := map[string]struct {
   108  		pld      []byte
   109  		expected interface{}
   110  		inf      event.Info
   111  	}{
   112  		"info event": {
   113  			pld: []byte(`[123, "hb"]`),
   114  			inf: event.Info{},
   115  			expected: event.Info{
   116  				ChanID:    123,
   117  				Subscribe: event.Subscribe{Event: "hb"},
   118  			},
   119  		},
   120  		"ticker trading pair snapshot": {
   121  			pld: []byte(`[
   122  				111,
   123  				[[
   124  					7616.5,31.89055171,7617.5,43.358118629999986,
   125  					-550.8,-0.0674,7617.1,8314.71200815,8257.8,7500
   126  				]]
   127  			]`),
   128  			inf: event.Info{
   129  				Subscribe: event.Subscribe{
   130  					Channel: "ticker",
   131  					Symbol:  "tBTCUST",
   132  				},
   133  			},
   134  			expected: &ticker.Snapshot{
   135  				Snapshot: []*ticker.Ticker{
   136  					{
   137  						Symbol:          "tBTCUST",
   138  						Bid:             7616.5,
   139  						BidSize:         31.89055171,
   140  						Ask:             7617.5,
   141  						AskSize:         43.358118629999986,
   142  						DailyChange:     -550.8,
   143  						DailyChangePerc: -0.0674,
   144  						LastPrice:       7617.1,
   145  						Volume:          8314.71200815,
   146  						High:            8257.8,
   147  						Low:             7500,
   148  					},
   149  				},
   150  			},
   151  		},
   152  		"ticker trading pair update": {
   153  			pld: []byte(`[
   154  				111,
   155  				[
   156  					7617,52.98726298,7617.1,53.601795929999994,
   157  					-550.9,-0.0674,7617,8318.92961981,8257.8,7500
   158  				]
   159  			]`),
   160  			inf: event.Info{
   161  				Subscribe: event.Subscribe{
   162  					Channel: "ticker",
   163  					Symbol:  "tBTCUST",
   164  				},
   165  			},
   166  			expected: &ticker.Ticker{
   167  				Symbol:          "tBTCUST",
   168  				Frr:             0,
   169  				Bid:             7617,
   170  				BidPeriod:       0,
   171  				BidSize:         52.98726298,
   172  				Ask:             7617.1,
   173  				AskPeriod:       0,
   174  				AskSize:         53.601795929999994,
   175  				DailyChange:     -550.9,
   176  				DailyChangePerc: -0.0674,
   177  				LastPrice:       7617,
   178  				Volume:          8318.92961981,
   179  				High:            8257.8,
   180  				Low:             7500,
   181  			},
   182  		},
   183  		"ticker funding pair snapshot": {
   184  			pld: []byte(`[
   185  				111,
   186  				[[
   187  					0.0003193369863013699,0.0002401,30,3939629.6177260396,0.00019012,2,
   188  					307776.1592138799,-0.00005823,-0.2344,0.00019016,122156333.45260866,
   189  					0.00027397,6.8e-7,null,null,3441851.73330503
   190  				]]
   191  			]`),
   192  			inf: event.Info{
   193  				Subscribe: event.Subscribe{
   194  					Channel: "ticker",
   195  					Symbol:  "fUSD",
   196  				},
   197  			},
   198  			expected: &ticker.Snapshot{
   199  				Snapshot: []*ticker.Ticker{
   200  					{
   201  						Symbol:             "fUSD",
   202  						Frr:                0.0003193369863013699,
   203  						Bid:                0.0002401,
   204  						BidPeriod:          30,
   205  						BidSize:            3.9396296177260396e+06,
   206  						Ask:                0.00019012,
   207  						AskPeriod:          2,
   208  						AskSize:            307776.1592138799,
   209  						DailyChange:        -5.823e-05,
   210  						DailyChangePerc:    -0.2344,
   211  						LastPrice:          0.00019016,
   212  						Volume:             1.2215633345260866e+08,
   213  						High:               0.00027397,
   214  						Low:                6.8e-07,
   215  						FrrAmountAvailable: 3.44185173330503e+06,
   216  					},
   217  				},
   218  			},
   219  		},
   220  		"ticker funding trading pair update": {
   221  			pld: []byte(`[
   222  				111,
   223  				[
   224  					0.0003193315068493151,0.0002401,30,4037829.0804227195,0.000189,4,
   225  					384507.7314462898,-0.00005939,-0.2391,0.000189,122159083.98991197,
   226  					0.00027397,6.8e-7,null,null,3441851.73330503
   227  				]
   228  			]`),
   229  			inf: event.Info{
   230  				Subscribe: event.Subscribe{
   231  					Channel: "ticker",
   232  					Symbol:  "fUSD",
   233  				},
   234  			},
   235  			expected: &ticker.Ticker{
   236  				Symbol:             "fUSD",
   237  				Frr:                0.0003193315068493151,
   238  				Bid:                0.0002401,
   239  				BidPeriod:          30,
   240  				BidSize:            4.0378290804227195e+06,
   241  				Ask:                0.000189,
   242  				AskPeriod:          4,
   243  				AskSize:            384507.7314462898,
   244  				DailyChange:        -5.939e-05,
   245  				DailyChangePerc:    -0.2391,
   246  				LastPrice:          0.000189,
   247  				Volume:             1.2215908398991197e+08,
   248  				High:               0.00027397,
   249  				Low:                6.8e-07,
   250  				FrrAmountAvailable: 3.44185173330503e+06,
   251  			},
   252  		},
   253  		"trades snapshot": {
   254  			pld: []byte(`[111,[[559273857,1609665708633,-0.0048,34113]]]`),
   255  			inf: event.Info{
   256  				Subscribe: event.Subscribe{
   257  					Channel: "trades",
   258  					Symbol:  "tBTCUST",
   259  				},
   260  			},
   261  			expected: trades.TradeSnapshot{
   262  				Snapshot: []trades.Trade{
   263  					{
   264  						Pair:   "tBTCUST",
   265  						ID:     559273857,
   266  						MTS:    1609665708633,
   267  						Amount: -0.0048,
   268  						Price:  34113,
   269  					},
   270  				},
   271  			},
   272  		},
   273  		"trade": {
   274  			pld: []byte(`[111,[559273857,1609665708633,-0.0048,34113]]`),
   275  			inf: event.Info{
   276  				Subscribe: event.Subscribe{
   277  					Channel: "trades",
   278  					Symbol:  "tBTCUST",
   279  				},
   280  			},
   281  			expected: trades.Trade{
   282  				Pair:   "tBTCUST",
   283  				ID:     559273857,
   284  				MTS:    1609665708633,
   285  				Amount: -0.0048,
   286  				Price:  34113,
   287  			},
   288  		},
   289  		"trade execution": {
   290  			pld: []byte(`[17470,"te",[401597395,1574694478808,0.005,7245.3]]`),
   291  			inf: event.Info{
   292  				Subscribe: event.Subscribe{
   293  					Channel: "trades",
   294  					Symbol:  "tBTCUST",
   295  				},
   296  			},
   297  			expected: trades.TradeExecuted{
   298  				Pair:   "tBTCUST",
   299  				ID:     401597395,
   300  				MTS:    1574694478808,
   301  				Amount: 0.005,
   302  				Price:  7245.3,
   303  			},
   304  		},
   305  		"trade execution update": {
   306  			pld: []byte(`[17470,"tu",[401597395,1574694478808,0.005,7245.3]]`),
   307  			inf: event.Info{
   308  				Subscribe: event.Subscribe{
   309  					Channel: "trades",
   310  					Symbol:  "tBTCUST",
   311  				},
   312  			},
   313  			expected: trades.TradeExecutionUpdate{
   314  				Pair:   "tBTCUST",
   315  				ID:     401597395,
   316  				MTS:    1574694478808,
   317  				Amount: 0.005,
   318  				Price:  7245.3,
   319  			},
   320  		},
   321  		"funding trade execution": {
   322  			pld: []byte(`[337371,"fte",[133323543,1574694605000,-59.84,0.00023647,2]]`),
   323  			inf: event.Info{
   324  				Subscribe: event.Subscribe{
   325  					Channel: "trades",
   326  					Symbol:  "fUSD",
   327  				},
   328  			},
   329  			expected: trades.FundingTradeExecuted{
   330  				Symbol: "fUSD",
   331  				ID:     133323543,
   332  				MTS:    1574694605000,
   333  				Amount: -59.84,
   334  				Rate:   0.00023647,
   335  				Period: 2,
   336  			},
   337  		},
   338  		"funding trade execution update": {
   339  			pld: []byte(`[337371,"ftu",[133323543,1574694605000,-59.84,0.00023647,2]]`),
   340  			inf: event.Info{
   341  				Subscribe: event.Subscribe{
   342  					Channel: "trades",
   343  					Symbol:  "fUSD",
   344  				},
   345  			},
   346  			expected: trades.FundingTradeExecutionUpdate{
   347  				Symbol: "fUSD",
   348  				ID:     133323543,
   349  				MTS:    1574694605000,
   350  				Amount: -59.84,
   351  				Rate:   0.00023647,
   352  				Period: 2,
   353  			},
   354  		},
   355  		"book snapshot trading pair bid entry": {
   356  			pld: []byte(`[17082,[[7254.7,3,3.3]]]`),
   357  			inf: event.Info{
   358  				Subscribe: event.Subscribe{
   359  					Channel:   "book",
   360  					Symbol:    "tETHEUR",
   361  					Precision: "P0",
   362  					Frequency: "F0",
   363  				},
   364  			},
   365  			expected: &book.Snapshot{
   366  				Snapshot: []*book.Book{
   367  					{
   368  						Symbol:      "tETHEUR",
   369  						Count:       3,
   370  						Price:       7254.7,
   371  						Amount:      3.3,
   372  						PriceJsNum:  "7254.7",
   373  						AmountJsNum: "3.3",
   374  						Side:        1,
   375  						Action:      0,
   376  					},
   377  				},
   378  			},
   379  		},
   380  		"book snapshot trading pair ask entry": {
   381  			pld: []byte(`[17082,[[7254.7,3,-3.3]]]`),
   382  			inf: event.Info{
   383  				Subscribe: event.Subscribe{
   384  					Channel:   "book",
   385  					Symbol:    "tETHEUR",
   386  					Precision: "P0",
   387  					Frequency: "F0",
   388  				},
   389  			},
   390  			expected: &book.Snapshot{
   391  				Snapshot: []*book.Book{
   392  					{
   393  						Symbol:      "tETHEUR",
   394  						Count:       3,
   395  						Price:       7254.7,
   396  						Amount:      3.3,
   397  						PriceJsNum:  "7254.7",
   398  						AmountJsNum: "-3.3",
   399  						Side:        2,
   400  						Action:      0,
   401  					},
   402  				},
   403  			},
   404  		},
   405  		"book snapshot trading pair exit": {
   406  			pld: []byte(`[17082,[[7254.7,0,3.3]]]`),
   407  			inf: event.Info{
   408  				Subscribe: event.Subscribe{
   409  					Channel:   "book",
   410  					Symbol:    "tETHEUR",
   411  					Precision: "P0",
   412  					Frequency: "F0",
   413  				},
   414  			},
   415  			expected: &book.Snapshot{
   416  				Snapshot: []*book.Book{
   417  					{
   418  						Symbol:      "tETHEUR",
   419  						Count:       0,
   420  						Price:       7254.7,
   421  						Amount:      3.3,
   422  						PriceJsNum:  "7254.7",
   423  						AmountJsNum: "3.3",
   424  						Side:        1,
   425  						Action:      1,
   426  					},
   427  				},
   428  			},
   429  		},
   430  		"book snapshot funding pair": {
   431  			pld: []byte(`[431549,[[0.00023112,30,1,-15190.7005375]]]`),
   432  			inf: event.Info{
   433  				Subscribe: event.Subscribe{
   434  					Channel:   "book",
   435  					Symbol:    "fUSD",
   436  					Precision: "P0",
   437  					Frequency: "F0",
   438  				},
   439  			},
   440  			expected: &book.Snapshot{
   441  				Snapshot: []*book.Book{
   442  					{
   443  						Symbol:      "fUSD",
   444  						Count:       1,
   445  						Period:      30,
   446  						Amount:      -15190.7005375,
   447  						Rate:        0.00023112,
   448  						AmountJsNum: "-15190.7005375",
   449  					},
   450  				},
   451  			},
   452  		},
   453  		"book trading pair update": {
   454  			pld: []byte(`[17082,[7254.7,3,3.3]]`),
   455  			inf: event.Info{
   456  				Subscribe: event.Subscribe{
   457  					Channel:   "book",
   458  					Symbol:    "tETHEUR",
   459  					Precision: "P0",
   460  					Frequency: "F0",
   461  				},
   462  			},
   463  			expected: &book.Book{
   464  				Symbol:      "tETHEUR",
   465  				Count:       3,
   466  				Price:       7254.7,
   467  				Amount:      3.3,
   468  				PriceJsNum:  "7254.7",
   469  				AmountJsNum: "3.3",
   470  				Side:        1,
   471  				Action:      0,
   472  			},
   473  		},
   474  		"book funding pair update": {
   475  			pld: []byte(`[348748,[0.00023157,2,1,66.35007188]]`),
   476  			inf: event.Info{
   477  				Subscribe: event.Subscribe{
   478  					Channel:   "book",
   479  					Symbol:    "fUSD",
   480  					Precision: "P0",
   481  					Frequency: "F0",
   482  				},
   483  			},
   484  			expected: &book.Book{
   485  				Symbol:      "fUSD",
   486  				Count:       1,
   487  				Period:      2,
   488  				Amount:      66.35007188,
   489  				Rate:        0.00023157,
   490  				AmountJsNum: "66.35007188",
   491  			},
   492  		},
   493  		"raw trading pair book snapshot bid entry": {
   494  			pld: []byte(`[869944,[[55804480297,33766,2]]]`),
   495  			inf: event.Info{
   496  				Subscribe: event.Subscribe{
   497  					Channel:   "book",
   498  					Symbol:    "tBTCUSD",
   499  					Precision: "R0",
   500  				},
   501  			},
   502  			expected: &book.Snapshot{
   503  				Snapshot: []*book.Book{
   504  					{
   505  						Symbol:      "tBTCUSD",
   506  						ID:          55804480297,
   507  						Price:       33766,
   508  						Amount:      2,
   509  						PriceJsNum:  "33766",
   510  						AmountJsNum: "2",
   511  						Side:        1,
   512  						Action:      0,
   513  					},
   514  				},
   515  			},
   516  		},
   517  		"raw trading pair book snapshot ask entry": {
   518  			pld: []byte(`[869944,[[55804480297,33766,-2]]]`),
   519  			inf: event.Info{
   520  				Subscribe: event.Subscribe{
   521  					Channel:   "book",
   522  					Symbol:    "tBTCUSD",
   523  					Precision: "R0",
   524  				},
   525  			},
   526  			expected: &book.Snapshot{
   527  				Snapshot: []*book.Book{
   528  					{
   529  						Symbol:      "tBTCUSD",
   530  						ID:          55804480297,
   531  						PriceJsNum:  "33766",
   532  						AmountJsNum: "-2",
   533  						Side:        2,
   534  						Action:      0,
   535  						Price:       33766,
   536  						Amount:      2,
   537  					},
   538  				},
   539  			},
   540  		},
   541  		"raw trading pair book snapshot remove entry": {
   542  			pld: []byte(`[869944,[[55804480297,0,2]]]`),
   543  			inf: event.Info{
   544  				Subscribe: event.Subscribe{
   545  					Channel:   "book",
   546  					Symbol:    "tBTCUSD",
   547  					Precision: "R0",
   548  				},
   549  			},
   550  			expected: &book.Snapshot{
   551  				Snapshot: []*book.Book{
   552  					{
   553  						Symbol:      "tBTCUSD",
   554  						ID:          55804480297,
   555  						Price:       0,
   556  						Amount:      2,
   557  						PriceJsNum:  "0",
   558  						AmountJsNum: "2",
   559  						Side:        1,
   560  						Action:      1,
   561  					},
   562  				},
   563  			},
   564  		},
   565  		"raw funding pair book snapshot": {
   566  			pld: []byte(`[472778,[[658282397,30,0.000233,-530]]]`),
   567  			inf: event.Info{
   568  				Subscribe: event.Subscribe{
   569  					Channel:   "book",
   570  					Symbol:    "fUSD",
   571  					Precision: "R0",
   572  				},
   573  			},
   574  			expected: &book.Snapshot{
   575  				Snapshot: []*book.Book{
   576  					{
   577  						Symbol:      "fUSD",
   578  						ID:          658282397,
   579  						Period:      30,
   580  						Amount:      -530,
   581  						Rate:        0.000233,
   582  						AmountJsNum: "-530",
   583  					},
   584  				},
   585  			},
   586  		},
   587  		"raw trading pair book update": {
   588  			pld: []byte(`[433290,[34753006045,0,-1]]`),
   589  			inf: event.Info{
   590  				Subscribe: event.Subscribe{
   591  					Channel:   "book",
   592  					Symbol:    "tBTCUSD",
   593  					Precision: "R0",
   594  				},
   595  			},
   596  			expected: &book.Book{
   597  				Symbol:      "tBTCUSD",
   598  				ID:          34753006045,
   599  				PriceJsNum:  "0",
   600  				AmountJsNum: "-1",
   601  				Amount:      1,
   602  				Side:        2,
   603  				Action:      1,
   604  			},
   605  		},
   606  		"raw funding pair book update": {
   607  			pld: []byte(`[472778,[658286906,2,0,1]]`),
   608  			inf: event.Info{
   609  				Subscribe: event.Subscribe{
   610  					Channel:   "book",
   611  					Symbol:    "fUSD",
   612  					Precision: "R0",
   613  				},
   614  			},
   615  			expected: &book.Book{
   616  				Symbol:      "fUSD",
   617  				ID:          658286906,
   618  				Period:      2,
   619  				Amount:      1,
   620  				Rate:        0,
   621  				AmountJsNum: "1",
   622  			},
   623  		},
   624  		"candles snapshot": {
   625  			pld: []byte(`[111,[[1609668540000,828.01,827.67,828.42,827.67,2.32080241]]]`),
   626  			inf: event.Info{
   627  				Subscribe: event.Subscribe{
   628  					Channel: "candles",
   629  					Key:     "trade:1m:tETHUST",
   630  				},
   631  			},
   632  			expected: &candle.Snapshot{
   633  				Snapshot: []*candle.Candle{
   634  					{
   635  						Symbol:     "tETHUST",
   636  						Resolution: "1m",
   637  						MTS:        1609668540000,
   638  						Open:       828.01,
   639  						Close:      827.67,
   640  						High:       828.42,
   641  						Low:        827.67,
   642  						Volume:     2.32080241,
   643  					},
   644  				},
   645  			},
   646  		},
   647  		"candle": {
   648  			pld: []byte(`[111,[1609668540000,828.01,827.67,828.42,827.67,2.32080241]]`),
   649  			inf: event.Info{
   650  				Subscribe: event.Subscribe{
   651  					Channel: "candles",
   652  					Key:     "trade:1m:tETHUST",
   653  				},
   654  			},
   655  			expected: &candle.Candle{
   656  				Symbol:     "tETHUST",
   657  				Resolution: "1m",
   658  				MTS:        1609668540000,
   659  				Open:       828.01,
   660  				Close:      827.67,
   661  				High:       828.42,
   662  				Low:        827.67,
   663  				Volume:     2.32080241,
   664  			},
   665  		},
   666  		"derivatives status snapshot": {
   667  			pld: []byte(`[
   668  				799830,
   669  				[[
   670  					1609921474000,null,34568.786626655,34575.5,null,1856521.42387705,
   671  					null,1609948800000,-0.00004348,481,null,0,null,null,34593.64333333333,
   672  					null,null,11153.74635347,null,null,null,null,null
   673  				]]
   674  			]`),
   675  			inf: event.Info{
   676  				Subscribe: event.Subscribe{
   677  					Channel: "status",
   678  					Key:     "deriv:tBTCF0:USTF0",
   679  				},
   680  			},
   681  			expected: &status.DerivativesSnapshot{
   682  				Snapshot: []*status.Derivative{
   683  					{
   684  						Symbol:               "tBTCF0:USTF0",
   685  						MTS:                  1609921474000,
   686  						Price:                34568.786626655,
   687  						SpotPrice:            34575.5,
   688  						InsuranceFundBalance: 1.85652142387705e+06,
   689  						FundingEventMTS:      1609948800000,
   690  						FundingAccrued:       -4.348e-05,
   691  						FundingStep:          481,
   692  						MarkPrice:            34593.64333333333,
   693  						OpenInterest:         11153.74635347,
   694  					},
   695  				},
   696  			},
   697  		},
   698  		"derivatives status": {
   699  			pld: []byte(`[
   700  				799830,
   701  				[
   702  					1609921474000,null,34568.786626655,34575.5,null,1856521.42387705,
   703  					null,1609948800000,-0.00004348,481,null,0,null,null,34593.64333333333,
   704  					null,null,11153.74635347,null,null,null,null,null
   705  				]
   706  			]`),
   707  			inf: event.Info{
   708  				Subscribe: event.Subscribe{
   709  					Channel: "status",
   710  					Key:     "deriv:tBTCF0:USTF0",
   711  				},
   712  			},
   713  			expected: &status.Derivative{
   714  				Symbol:               "tBTCF0:USTF0",
   715  				MTS:                  1609921474000,
   716  				Price:                34568.786626655,
   717  				SpotPrice:            34575.5,
   718  				InsuranceFundBalance: 1.85652142387705e+06,
   719  				FundingEventMTS:      1609948800000,
   720  				FundingAccrued:       -4.348e-05,
   721  				FundingStep:          481,
   722  				MarkPrice:            34593.64333333333,
   723  				OpenInterest:         11153.74635347,
   724  			},
   725  		},
   726  		"liquidation status snapshot": {
   727  			pld: []byte(`[
   728  				521209,
   729  				[[
   730  					"pos",145511476,1609921778489,null,"tBTCF0:USTF0",
   731  					0.12173,34618.82986269,null,1,1,null,34281
   732  				]]
   733  			]`),
   734  			inf: event.Info{
   735  				Subscribe: event.Subscribe{
   736  					Channel: "status",
   737  					Key:     "liq:global",
   738  				},
   739  			},
   740  			expected: &status.LiquidationsSnapshot{
   741  				Snapshot: []*status.Liquidation{
   742  					{
   743  						Symbol:        "tBTCF0:USTF0",
   744  						PositionID:    145511476,
   745  						MTS:           1609921778489,
   746  						Amount:        0.12173,
   747  						BasePrice:     34618.82986269,
   748  						IsMatch:       1,
   749  						IsMarketSold:  1,
   750  						PriceAcquired: 34281,
   751  					},
   752  				},
   753  			},
   754  		},
   755  		"liquidation status": {
   756  			pld: []byte(`[
   757  				521209,
   758  				[
   759  					"pos",145511476,1609921778489,null,"tBTCF0:USTF0",
   760  					0.12173,34618.82986269,null,1,1,null,34281
   761  				]
   762  			]`),
   763  			inf: event.Info{
   764  				Subscribe: event.Subscribe{
   765  					Channel: "status",
   766  					Key:     "liq:global",
   767  				},
   768  			},
   769  			expected: &status.Liquidation{
   770  				Symbol:        "tBTCF0:USTF0",
   771  				PositionID:    145511476,
   772  				MTS:           1609921778489,
   773  				Amount:        0.12173,
   774  				BasePrice:     34618.82986269,
   775  				IsMatch:       1,
   776  				IsMarketSold:  1,
   777  				PriceAcquired: 34281,
   778  			},
   779  		},
   780  	}
   781  
   782  	for k, v := range cases {
   783  		t.Run(k, func(t *testing.T) {
   784  			m := msg.Msg{Data: v.pld}
   785  			raw, pld, chID, _, err := m.PreprocessRaw()
   786  			require.NoError(t, err)
   787  			got, err := m.ProcessPublic(raw, pld, chID, v.inf)
   788  			assert.NoError(t, err)
   789  			assert.Equal(t, v.expected, got)
   790  		})
   791  	}
   792  }
   793  
   794  func TestProcessPrivate(t *testing.T) {
   795  	cases := map[string]struct {
   796  		pld      []byte
   797  		expected interface{}
   798  	}{
   799  		"info event": {
   800  			pld: []byte(`[0, "hb"]`),
   801  			expected: event.Info{
   802  				ChanID:    0,
   803  				Subscribe: event.Subscribe{Event: "hb"},
   804  			},
   805  		},
   806  		"balance info update": {
   807  			pld: []byte(`[0,"bu",[4131,4131.85]]`),
   808  			expected: balanceinfo.Update{
   809  				TotalAUM: 4131,
   810  				NetAUM:   4131.85,
   811  			},
   812  		},
   813  		"position snapshot": {
   814  			pld: []byte(`[
   815  				0,
   816  				"ps",
   817  				[[
   818  					"tETHUST","ACTIVE",0.2,153.71,0,0,null,null,null,
   819  					null,null,142420429,null,null,null,0,null,0,null,
   820  					{
   821  						"reason":"TRADE",
   822  						"order_id":34934099168,
   823  						"order_id_oppo":34934090814,
   824  						"liq_stage":null,
   825  						"trade_price":"153.71",
   826  						"trade_amount":"0.2"
   827  					}
   828  				]]
   829  			]`),
   830  			expected: &position.Snapshot{
   831  				Snapshot: []*position.Position{
   832  					{
   833  						Id:        142420429,
   834  						Symbol:    "tETHUST",
   835  						Status:    "ACTIVE",
   836  						Amount:    0.2,
   837  						BasePrice: 153.71,
   838  						Meta: map[string]interface{}{
   839  							"liq_stage":     nil,
   840  							"order_id":      3.4934099168e+10,
   841  							"order_id_oppo": 3.4934090814e+10,
   842  							"reason":        "TRADE",
   843  							"trade_amount":  "0.2",
   844  							"trade_price":   "153.71",
   845  						},
   846  					},
   847  				},
   848  			},
   849  		},
   850  		"position new": {
   851  			pld: []byte(`[
   852  				0,
   853  				"pn",
   854  				[
   855  					"tETHUST","ACTIVE",0.2,153.71,0,0,-0.07944800000000068,-0.05855181835925015,
   856  					67.52755254906451, 1.409288545397275,null,142420429,null,null,null,0,null,0,0,
   857  					{
   858  						"reason":"TRADE",
   859  						"order_id":34934099168,
   860  						"order_id_oppo":34934090814,
   861  						"liq_stage":null,
   862  						"trade_price":"153.71",
   863  						"trade_amount":"0.2"
   864  					}
   865  				]
   866  			]`),
   867  			expected: position.New{
   868  				Id:                   142420429,
   869  				Symbol:               "tETHUST",
   870  				Status:               "ACTIVE",
   871  				Amount:               0.2,
   872  				BasePrice:            153.71,
   873  				Type:                 "pn",
   874  				ProfitLoss:           -0.07944800000000068,
   875  				ProfitLossPercentage: -0.05855181835925015,
   876  				LiquidationPrice:     67.52755254906451,
   877  				Leverage:             1.409288545397275,
   878  				Meta: map[string]interface{}{
   879  					"liq_stage":     nil,
   880  					"order_id":      3.4934099168e+10,
   881  					"order_id_oppo": 3.4934090814e+10,
   882  					"reason":        "TRADE",
   883  					"trade_amount":  "0.2",
   884  					"trade_price":   "153.71",
   885  				},
   886  			},
   887  		},
   888  		"position update": {
   889  			pld: []byte(`[
   890  				0,
   891  				"pu",
   892  				[
   893  					"tETHUST","ACTIVE",0.2,153.71,0,0,-0.07944800000000068,-0.05855181835925015,
   894  					67.52755254906451, 1.409288545397275,null,142420429,null,null,null,0,null,0,0,
   895  					{
   896  						"reason":"TRADE",
   897  						"order_id":34934099168,
   898  						"order_id_oppo":34934090814,
   899  						"liq_stage":null,
   900  						"trade_price":"153.71",
   901  						"trade_amount":"0.2"
   902  					}
   903  				]
   904  			]`),
   905  			expected: position.Update{
   906  				Id:                   142420429,
   907  				Symbol:               "tETHUST",
   908  				Status:               "ACTIVE",
   909  				Amount:               0.2,
   910  				BasePrice:            153.71,
   911  				Type:                 "pu",
   912  				ProfitLoss:           -0.07944800000000068,
   913  				ProfitLossPercentage: -0.05855181835925015,
   914  				LiquidationPrice:     67.52755254906451,
   915  				Leverage:             1.409288545397275,
   916  				Meta: map[string]interface{}{
   917  					"liq_stage":     nil,
   918  					"order_id":      3.4934099168e+10,
   919  					"order_id_oppo": 3.4934090814e+10,
   920  					"reason":        "TRADE",
   921  					"trade_amount":  "0.2",
   922  					"trade_price":   "153.71",
   923  				},
   924  			},
   925  		},
   926  		"position close": {
   927  			pld: []byte(`[
   928  				0,
   929  				"pc",
   930  				[
   931  					"tETHUST","ACTIVE",0.2,153.71,0,0,-0.07944800000000068,-0.05855181835925015,
   932  					67.52755254906451, 1.409288545397275,null,142420429,null,null,null,0,null,0,0,
   933  					{
   934  						"reason":"TRADE",
   935  						"order_id":34934099168,
   936  						"order_id_oppo":34934090814,
   937  						"liq_stage":null,
   938  						"trade_price":"153.71",
   939  						"trade_amount":"0.2"
   940  					}
   941  				]
   942  			]`),
   943  			expected: position.Cancel{
   944  				Id:                   142420429,
   945  				Symbol:               "tETHUST",
   946  				Status:               "ACTIVE",
   947  				Amount:               0.2,
   948  				BasePrice:            153.71,
   949  				Type:                 "pc",
   950  				ProfitLoss:           -0.07944800000000068,
   951  				ProfitLossPercentage: -0.05855181835925015,
   952  				LiquidationPrice:     67.52755254906451,
   953  				Leverage:             1.409288545397275,
   954  				Meta: map[string]interface{}{
   955  					"liq_stage":     nil,
   956  					"order_id":      3.4934099168e+10,
   957  					"order_id_oppo": 3.4934090814e+10,
   958  					"reason":        "TRADE",
   959  					"trade_amount":  "0.2",
   960  					"trade_price":   "153.71",
   961  				},
   962  			},
   963  		},
   964  		"wallet snapshot": {
   965  			pld: []byte(`[0,"ws",[["exchange","SAN",19.76,0,null,null,null]]]`),
   966  			expected: &wallet.Snapshot{
   967  				Snapshot: []*wallet.Wallet{
   968  					{
   969  						Type:         "exchange",
   970  						Currency:     "SAN",
   971  						Balance:      19.76,
   972  						TradeDetails: nil,
   973  					},
   974  				},
   975  			},
   976  		},
   977  		"wallet update": {
   978  			pld: []byte(`[
   979  				0,
   980  				"wu",
   981  				[
   982  					"exchange","BTC",1.61169184,0,null,"Exchange 0.01 BTC for USD @ 7804.6",
   983  					{
   984  						"reason":"TRADE",
   985  						"order_id":34988418651,
   986  						"order_id_oppo":34990541044,
   987  						"trade_price":"7804.6",
   988  						"trade_amount":"0.01"
   989  					}
   990  				]
   991  			]`),
   992  			expected: wallet.Update{
   993  				Type:              "exchange",
   994  				Currency:          "BTC",
   995  				Balance:           1.61169184,
   996  				UnsettledInterest: 0,
   997  				BalanceAvailable:  0,
   998  				LastChange:        "Exchange 0.01 BTC for USD @ 7804.6",
   999  				TradeDetails: map[string]interface{}{
  1000  					"order_id":      3.4988418651e+10,
  1001  					"order_id_oppo": 3.4990541044e+10,
  1002  					"reason":        "TRADE",
  1003  					"trade_amount":  "0.01",
  1004  					"trade_price":   "7804.6",
  1005  				},
  1006  			},
  1007  		},
  1008  		"order snapshot": {
  1009  			pld: []byte(`[
  1010  				0,
  1011  				"os",
  1012  				[[
  1013  					34930659963,null,1574955083558,"tETHUSD",1574955083558,1574955083573,
  1014  					0.201104,0.201104,"EXCHANGE LIMIT",null,null,null,0,"ACTIVE",null,null,
  1015  					120,0,0,0,null,null,null,0,0,null,null,null,"BFX",null,null,null
  1016  				]]
  1017  			]`),
  1018  			expected: &order.Snapshot{
  1019  				Snapshot: []*order.Order{
  1020  					{
  1021  						ID:            34930659963,
  1022  						GID:           0,
  1023  						CID:           1574955083558,
  1024  						Symbol:        "tETHUSD",
  1025  						MTSCreated:    1574955083558,
  1026  						MTSUpdated:    1574955083573,
  1027  						Amount:        0.201104,
  1028  						AmountOrig:    0.201104,
  1029  						Type:          "EXCHANGE LIMIT",
  1030  						TypePrev:      "",
  1031  						MTSTif:        0,
  1032  						Flags:         0,
  1033  						Status:        "ACTIVE",
  1034  						Price:         120,
  1035  						PriceAvg:      0,
  1036  						PriceTrailing: 0,
  1037  						PriceAuxLimit: 0,
  1038  						Notify:        false,
  1039  						Hidden:        false,
  1040  						PlacedID:      0,
  1041  						Routing:       "BFX",
  1042  						Meta:          nil,
  1043  					},
  1044  				},
  1045  			},
  1046  		},
  1047  		"order new": {
  1048  			pld: []byte(`[
  1049  				0,
  1050  				"on",
  1051  				[
  1052  					34930659963,null,1574955083558,"tETHUSD",1574955083558,1574955354487,
  1053  					0.201104,0.201104,"EXCHANGE LIMIT",null,null,null,0,"CANCELED",null,
  1054  					null,120,0,0,0,null,null,null,0,0,null,null,null,"BFX",null,null,null
  1055  				]
  1056  			]`),
  1057  			expected: order.New{
  1058  				ID:            34930659963,
  1059  				GID:           0,
  1060  				CID:           1574955083558,
  1061  				Symbol:        "tETHUSD",
  1062  				MTSCreated:    1574955083558,
  1063  				MTSUpdated:    1574955354487,
  1064  				Amount:        0.201104,
  1065  				AmountOrig:    0.201104,
  1066  				Type:          "EXCHANGE LIMIT",
  1067  				TypePrev:      "",
  1068  				MTSTif:        0,
  1069  				Flags:         0,
  1070  				Status:        "CANCELED",
  1071  				Price:         120,
  1072  				PriceAvg:      0,
  1073  				PriceTrailing: 0,
  1074  				PriceAuxLimit: 0,
  1075  				Notify:        false,
  1076  				Hidden:        false,
  1077  				PlacedID:      0,
  1078  				Routing:       "BFX",
  1079  				Meta:          nil,
  1080  			},
  1081  		},
  1082  		"order update": {
  1083  			pld: []byte(`[
  1084  				0,
  1085  				"ou",
  1086  				[
  1087  					34930659963,null,1574955083558,"tETHUSD",1574955083558,1574955354487,
  1088  					0.201104,0.201104,"EXCHANGE LIMIT",null,null,null,0,"CANCELED",null,
  1089  					null,120,0,0,0,null,null,null,0,0,null,null,null,"BFX",null,null,null
  1090  				]
  1091  			]`),
  1092  			expected: order.Update{
  1093  				ID:            34930659963,
  1094  				GID:           0,
  1095  				CID:           1574955083558,
  1096  				Symbol:        "tETHUSD",
  1097  				MTSCreated:    1574955083558,
  1098  				MTSUpdated:    1574955354487,
  1099  				Amount:        0.201104,
  1100  				AmountOrig:    0.201104,
  1101  				Type:          "EXCHANGE LIMIT",
  1102  				TypePrev:      "",
  1103  				MTSTif:        0,
  1104  				Flags:         0,
  1105  				Status:        "CANCELED",
  1106  				Price:         120,
  1107  				PriceAvg:      0,
  1108  				PriceTrailing: 0,
  1109  				PriceAuxLimit: 0,
  1110  				Notify:        false,
  1111  				Hidden:        false,
  1112  				PlacedID:      0,
  1113  				Routing:       "BFX",
  1114  				Meta:          nil,
  1115  			},
  1116  		},
  1117  		"order cancel": {
  1118  			pld: []byte(`[
  1119  				0,
  1120  				"oc",
  1121  				[
  1122  					34930659963,null,1574955083558,"tETHUSD",1574955083558,1574955354487,
  1123  					0.201104,0.201104,"EXCHANGE LIMIT",null,null,null,0,"CANCELED",null,
  1124  					null,120,0,0,0,null,null,null,0,0,null,null,null,"BFX",null,null,null
  1125  				]
  1126  			]`),
  1127  			expected: order.Cancel{
  1128  				ID:            34930659963,
  1129  				GID:           0,
  1130  				CID:           1574955083558,
  1131  				Symbol:        "tETHUSD",
  1132  				MTSCreated:    1574955083558,
  1133  				MTSUpdated:    1574955354487,
  1134  				Amount:        0.201104,
  1135  				AmountOrig:    0.201104,
  1136  				Type:          "EXCHANGE LIMIT",
  1137  				TypePrev:      "",
  1138  				MTSTif:        0,
  1139  				Flags:         0,
  1140  				Status:        "CANCELED",
  1141  				Price:         120,
  1142  				PriceAvg:      0,
  1143  				PriceTrailing: 0,
  1144  				PriceAuxLimit: 0,
  1145  				Notify:        false,
  1146  				Hidden:        false,
  1147  				PlacedID:      0,
  1148  				Routing:       "BFX",
  1149  				Meta:          nil,
  1150  			},
  1151  		},
  1152  		"trade execution": {
  1153  			pld: []byte(`[
  1154  				0,
  1155  				"te",
  1156  				[402088407,"tETHUST",1574963975602,34938060782,-0.2,153.57,"MARKET",0,-1,null,null,0]
  1157  			]`),
  1158  			expected: trades.AuthTradeExecution{
  1159  				ID:            402088407,
  1160  				Pair:          "tETHUST",
  1161  				MTS:           1574963975602,
  1162  				OrderID:       34938060782,
  1163  				ExecAmount:    -0.2,
  1164  				ExecPrice:     153.57,
  1165  				OrderType:     "MARKET",
  1166  				OrderPrice:    0,
  1167  				Maker:         -1,
  1168  				ClientOrderID: 0,
  1169  			},
  1170  		},
  1171  		"trade execution update": {
  1172  			pld: []byte(`[
  1173  				0,
  1174  				"tu",
  1175  				[402088407,"tETHUST",1574963975602,34938060782,-0.2,153.57,"MARKET",0,-1,-0.061668,"USD"]
  1176  			]`),
  1177  			expected: trades.AuthTradeExecutionUpdate{
  1178  				ID:          402088407,
  1179  				Pair:        "tETHUST",
  1180  				MTS:         1574963975602,
  1181  				OrderID:     34938060782,
  1182  				ExecAmount:  -0.2,
  1183  				ExecPrice:   153.57,
  1184  				OrderType:   "MARKET",
  1185  				OrderPrice:  0,
  1186  				Maker:       -1,
  1187  				Fee:         -0.061668,
  1188  				FeeCurrency: "USD",
  1189  			},
  1190  		},
  1191  		"funding trade executed": {
  1192  			pld: []byte(`[0,"fte",[636854,"fUSD",1575282446000,41238905,-1000,0.002,7,null]]`),
  1193  			expected: trades.AuthFundingTradeExecuted{
  1194  				ID:         636854,
  1195  				Symbol:     "fUSD",
  1196  				MTSCreated: 1575282446000,
  1197  				OfferID:    41238905,
  1198  				Amount:     -1000,
  1199  				Rate:       0.002,
  1200  				Period:     7,
  1201  				Maker:      0,
  1202  			},
  1203  		},
  1204  		"funding trade update": {
  1205  			pld: []byte(`[0,"ftu",[636854,"fUSD",1575282446000,41238905,-1000,0.002,7,null]]`),
  1206  			expected: trades.AuthFundingTradeUpdate{
  1207  				ID:         636854,
  1208  				Symbol:     "fUSD",
  1209  				MTSCreated: 1575282446000,
  1210  				OfferID:    41238905,
  1211  				Amount:     -1000,
  1212  				Rate:       0.002,
  1213  				Period:     7,
  1214  				Maker:      0,
  1215  			},
  1216  		},
  1217  		"funding offer snapshot": {
  1218  			pld: []byte(`[
  1219  				0,
  1220  				"fos",
  1221  				[[
  1222  					41237920,"fETH",1573912039000,1573912039000,0.5,0.5,"LIMIT",
  1223  					null,null,0,"ACTIVE",null,null,null,0.0024,2,0,0,null,0,null
  1224  				]]
  1225  			]`),
  1226  			expected: &fundingoffer.Snapshot{
  1227  				Snapshot: []*fundingoffer.Offer{
  1228  					{
  1229  						ID:         41237920,
  1230  						Symbol:     "fETH",
  1231  						MTSCreated: 1573912039000,
  1232  						MTSUpdated: 1573912039000,
  1233  						Amount:     0.5,
  1234  						AmountOrig: 0.5,
  1235  						Type:       "LIMIT",
  1236  						Status:     "ACTIVE",
  1237  						Rate:       0.0024,
  1238  						Period:     2,
  1239  					},
  1240  				},
  1241  			},
  1242  		},
  1243  		"funding offer new": {
  1244  			pld: []byte(`[
  1245  				0,
  1246  				"fon",
  1247  				[
  1248  					41238747,"fUST",1575026670000,1575026670000,5000,5000,"LIMIT",null,null,
  1249  					0,"ACTIVE",null,null,null,0.006000000000000001,30,0,0,null,0,null
  1250  				]
  1251  			]`),
  1252  			expected: fundingoffer.New{
  1253  				ID:         41238747,
  1254  				Symbol:     "fUST",
  1255  				MTSCreated: 1575026670000,
  1256  				MTSUpdated: 1575026670000,
  1257  				Amount:     5000,
  1258  				AmountOrig: 5000,
  1259  				Type:       "LIMIT",
  1260  				Flags:      nil,
  1261  				Status:     "ACTIVE",
  1262  				Rate:       0.006000000000000001,
  1263  				Period:     30,
  1264  				Notify:     false,
  1265  				Hidden:     false,
  1266  				Insure:     false,
  1267  				Renew:      false,
  1268  				RateReal:   0,
  1269  			},
  1270  		},
  1271  		"funding offer update": {
  1272  			pld: []byte(`[
  1273  				0,
  1274  				"fou",
  1275  				[
  1276  					41238747,"fUST",1575026670000,1575026670000,5000,5000,"LIMIT",null,null,
  1277  					0,"ACTIVE",null,null,null,0.006000000000000001,30,0,0,null,0,null
  1278  				]
  1279  			]`),
  1280  			expected: fundingoffer.Update{
  1281  				ID:         41238747,
  1282  				Symbol:     "fUST",
  1283  				MTSCreated: 1575026670000,
  1284  				MTSUpdated: 1575026670000,
  1285  				Amount:     5000,
  1286  				AmountOrig: 5000,
  1287  				Type:       "LIMIT",
  1288  				Flags:      nil,
  1289  				Status:     "ACTIVE",
  1290  				Rate:       0.006000000000000001,
  1291  				Period:     30,
  1292  				Notify:     false,
  1293  				Hidden:     false,
  1294  				Insure:     false,
  1295  				Renew:      false,
  1296  				RateReal:   0,
  1297  			},
  1298  		},
  1299  		"funding offer cancel": {
  1300  			pld: []byte(`[
  1301  				0,
  1302  				"foc",
  1303  				[
  1304  					41238747,"fUST",1575026670000,1575026670000,5000,5000,"LIMIT",null,null,
  1305  					0,"ACTIVE",null,null,null,0.006000000000000001,30,0,0,null,0,null
  1306  				]
  1307  			]`),
  1308  			expected: fundingoffer.Cancel{
  1309  				ID:         41238747,
  1310  				Symbol:     "fUST",
  1311  				MTSCreated: 1575026670000,
  1312  				MTSUpdated: 1575026670000,
  1313  				Amount:     5000,
  1314  				AmountOrig: 5000,
  1315  				Type:       "LIMIT",
  1316  				Flags:      nil,
  1317  				Status:     "ACTIVE",
  1318  				Rate:       0.006000000000000001,
  1319  				Period:     30,
  1320  				Notify:     false,
  1321  				Hidden:     false,
  1322  				Insure:     false,
  1323  				Renew:      false,
  1324  				RateReal:   0,
  1325  			},
  1326  		},
  1327  		"funding credits snapshot": {
  1328  			pld: []byte(`[
  1329  				0,
  1330  				"fcs",
  1331  				[[
  1332  					26223578,"fUST",1,1575052261000,1575296187000,350,0,"ACTIVE",null,null,
  1333  					null,0,30,1575052261000,1575293487000,0,0,null,0,null,0,"tBTCUST"
  1334  				]]
  1335  			]`),
  1336  			expected: &fundingcredit.Snapshot{
  1337  				Snapshot: []*fundingcredit.Credit{
  1338  					{
  1339  						ID:            26223578,
  1340  						Symbol:        "fUST",
  1341  						Side:          1,
  1342  						MTSCreated:    1575052261000,
  1343  						MTSUpdated:    1575296187000,
  1344  						Amount:        350,
  1345  						Status:        "ACTIVE",
  1346  						Period:        30,
  1347  						MTSOpened:     1575052261000,
  1348  						MTSLastPayout: 1575293487000,
  1349  						PositionPair:  "tBTCUST",
  1350  					},
  1351  				},
  1352  			},
  1353  		},
  1354  		"funding credit new": {
  1355  			pld: []byte(`[
  1356  				0,
  1357  				"fcn",
  1358  				[
  1359  					26223578,"fUST",1,1575052261000,1575296787000,350,0,"ACTIVE",null,null,
  1360  					null,0,30,1575052261000,1575293487000,0,0,null,0,null,0,"tBTCUST"
  1361  				]
  1362  			]`),
  1363  			expected: fundingcredit.New{
  1364  				ID:            26223578,
  1365  				Symbol:        "fUST",
  1366  				Side:          1,
  1367  				MTSCreated:    1575052261000,
  1368  				MTSUpdated:    1575296787000,
  1369  				Amount:        350,
  1370  				Flags:         map[string]interface{}(nil),
  1371  				Status:        "ACTIVE",
  1372  				RateType:      "",
  1373  				Rate:          0,
  1374  				Period:        30,
  1375  				MTSOpened:     1575052261000,
  1376  				MTSLastPayout: 1575293487000,
  1377  				Notify:        false,
  1378  				Hidden:        false,
  1379  				Insure:        false,
  1380  				Renew:         false,
  1381  				RateReal:      0,
  1382  				NoClose:       false,
  1383  				PositionPair:  "tBTCUST",
  1384  			},
  1385  		},
  1386  		"funding credit update": {
  1387  			pld: []byte(`[
  1388  				0,
  1389  				"fcu",
  1390  				[
  1391  					26223578,"fUST",1,1575052261000,1575296787000,350,0,"ACTIVE",null,null,
  1392  					null,0,30,1575052261000,1575293487000,0,0,null,0,null,0,"tBTCUST"
  1393  				]
  1394  			]`),
  1395  			expected: fundingcredit.Update{
  1396  				ID:            26223578,
  1397  				Symbol:        "fUST",
  1398  				Side:          1,
  1399  				MTSCreated:    1575052261000,
  1400  				MTSUpdated:    1575296787000,
  1401  				Amount:        350,
  1402  				Flags:         map[string]interface{}(nil),
  1403  				Status:        "ACTIVE",
  1404  				RateType:      "",
  1405  				Rate:          0,
  1406  				Period:        30,
  1407  				MTSOpened:     1575052261000,
  1408  				MTSLastPayout: 1575293487000,
  1409  				Notify:        false,
  1410  				Hidden:        false,
  1411  				Insure:        false,
  1412  				Renew:         false,
  1413  				RateReal:      0,
  1414  				NoClose:       false,
  1415  				PositionPair:  "tBTCUST",
  1416  			},
  1417  		},
  1418  		"funding credit close": {
  1419  			pld: []byte(`[
  1420  				0,
  1421  				"fcc",
  1422  				[
  1423  					26223578,"fUST",1,1575052261000,1575296787000,350,0,"ACTIVE",null,null,
  1424  					null,0,30,1575052261000,1575293487000,0,0,null,0,null,0,"tBTCUST"
  1425  				]
  1426  			]`),
  1427  			expected: fundingcredit.Cancel{
  1428  				ID:            26223578,
  1429  				Symbol:        "fUST",
  1430  				Side:          1,
  1431  				MTSCreated:    1575052261000,
  1432  				MTSUpdated:    1575296787000,
  1433  				Amount:        350,
  1434  				Flags:         map[string]interface{}(nil),
  1435  				Status:        "ACTIVE",
  1436  				RateType:      "",
  1437  				Rate:          0,
  1438  				Period:        30,
  1439  				MTSOpened:     1575052261000,
  1440  				MTSLastPayout: 1575293487000,
  1441  				Notify:        false,
  1442  				Hidden:        false,
  1443  				Insure:        false,
  1444  				Renew:         false,
  1445  				RateReal:      0,
  1446  				NoClose:       false,
  1447  				PositionPair:  "tBTCUST",
  1448  			},
  1449  		},
  1450  		"funding loan snapshot": {
  1451  			pld: []byte(`[
  1452  				0,
  1453  				"fls",
  1454  				[[
  1455  					2995442,"fUSD",-1,1575291961000,1575295850000,820,0,"ACTIVE",null,
  1456  					null,null,0.002,7,1575282446000,1575295850000,0,0,null,0,null,0
  1457  				]]
  1458  			]`),
  1459  			expected: &fundingloan.Snapshot{
  1460  				Snapshot: []*fundingloan.Loan{
  1461  					{
  1462  						ID:            2995442,
  1463  						Symbol:        "fUSD",
  1464  						Side:          -1,
  1465  						MTSCreated:    1575291961000,
  1466  						MTSUpdated:    1575295850000,
  1467  						Amount:        820,
  1468  						Status:        "ACTIVE",
  1469  						Rate:          0.002,
  1470  						Period:        7,
  1471  						MTSOpened:     1575282446000,
  1472  						MTSLastPayout: 1575295850000,
  1473  					},
  1474  				},
  1475  			},
  1476  		},
  1477  		"funding loan new": {
  1478  			pld: []byte(`[
  1479  				0,
  1480  				"fln",
  1481  				[
  1482  					2995444,"fUSD",-1,1575298742000,1575298742000,1000,0,"ACTIVE",null,
  1483  					null,null,0.002,7,1575298742000,1575298742000,0,0,null,0,null,0
  1484  				]
  1485  			]`),
  1486  			expected: fundingloan.New{
  1487  				ID:            2995444,
  1488  				Symbol:        "fUSD",
  1489  				Side:          -1,
  1490  				MTSCreated:    1575298742000,
  1491  				MTSUpdated:    1575298742000,
  1492  				Amount:        1000,
  1493  				Status:        "ACTIVE",
  1494  				Rate:          0.002,
  1495  				Period:        7,
  1496  				MTSOpened:     1575298742000,
  1497  				MTSLastPayout: 1575298742000,
  1498  			},
  1499  		},
  1500  		"funding loan update": {
  1501  			pld: []byte(`[
  1502  				0,
  1503  				"flu",
  1504  				[
  1505  					2995444,"fUSD",-1,1575298742000,1575298742000,1000,0,"ACTIVE",null,
  1506  					null,null,0.002,7,1575298742000,1575298742000,0,0,null,0,null,0
  1507  				]
  1508  			]`),
  1509  			expected: fundingloan.Update{
  1510  				ID:            2995444,
  1511  				Symbol:        "fUSD",
  1512  				Side:          -1,
  1513  				MTSCreated:    1575298742000,
  1514  				MTSUpdated:    1575298742000,
  1515  				Amount:        1000,
  1516  				Status:        "ACTIVE",
  1517  				Rate:          0.002,
  1518  				Period:        7,
  1519  				MTSOpened:     1575298742000,
  1520  				MTSLastPayout: 1575298742000,
  1521  			},
  1522  		},
  1523  		"funding loan close": {
  1524  			pld: []byte(`[
  1525  				0,
  1526  				"flc",
  1527  				[
  1528  					2995444,"fUSD",-1,1575298742000,1575298742000,1000,0,"ACTIVE",null,
  1529  					null,null,0.002,7,1575298742000,1575298742000,0,0,null,0,null,0
  1530  				]
  1531  			]`),
  1532  			expected: fundingloan.Cancel{
  1533  				ID:            2995444,
  1534  				Symbol:        "fUSD",
  1535  				Side:          -1,
  1536  				MTSCreated:    1575298742000,
  1537  				MTSUpdated:    1575298742000,
  1538  				Amount:        1000,
  1539  				Status:        "ACTIVE",
  1540  				Rate:          0.002,
  1541  				Period:        7,
  1542  				MTSOpened:     1575298742000,
  1543  				MTSLastPayout: 1575298742000,
  1544  			},
  1545  		},
  1546  		"margin info update symbol calc": {
  1547  			pld: []byte(`[
  1548  				0,
  1549  				"miu",
  1550  				[
  1551  					"sym","tETHUSD",
  1552  					[149361.09689202666,149639.26293509,830.0182168075556,895.0658432466332,null,null,null,null]
  1553  				]
  1554  			]`),
  1555  			expected: &margin.InfoUpdate{
  1556  				Symbol:          "tETHUSD",
  1557  				TradableBalance: 149361.09689202666,
  1558  				GrossBalance:    149639.26293509,
  1559  				Buy:             830.0182168075556,
  1560  				Sell:            895.0658432466332,
  1561  			},
  1562  		},
  1563  		"margin info update base calc": {
  1564  			pld: []byte(`[
  1565  				0,
  1566  				"miu",
  1567  				["base",[-13.014640000000007,0,49331.70267297,49318.68803297,27]]
  1568  			]`),
  1569  			expected: &margin.InfoBase{
  1570  				UserProfitLoss: -13.014640000000007,
  1571  				UserSwaps:      0,
  1572  				MarginBalance:  49331.70267297,
  1573  				MarginNet:      49318.68803297,
  1574  				MarginRequired: 27,
  1575  			},
  1576  		},
  1577  		"order new notification": {
  1578  			pld: []byte(`[
  1579  				0,
  1580  				"n",
  1581  				[
  1582  					1611922089,"on-req",null,null,
  1583  					[
  1584  						1201469553,0,788,"tBTCUSD",1611922089073,1611922089073,0.001,0.001,"EXCHANGE LIMIT",
  1585  						null,null,null,0,"ACTIVE",null,null,33,0,0,0,null,null,null,0,0,null,null,null,
  1586  						"API>BFX",null,null,null
  1587  					],
  1588  					null,"SUCCESS","Submitting exchange limit buy order for 0.001 BTC."
  1589  				]
  1590  			]`),
  1591  			expected: &notification.Notification{
  1592  				MTS:       1611922089,
  1593  				Type:      "on-req",
  1594  				MessageID: 0,
  1595  				NotifyInfo: order.New{
  1596  					ID:            1201469553,
  1597  					GID:           0,
  1598  					CID:           788,
  1599  					Symbol:        "tBTCUSD",
  1600  					MTSCreated:    1611922089073,
  1601  					MTSUpdated:    1611922089073,
  1602  					Amount:        0.001,
  1603  					AmountOrig:    0.001,
  1604  					Type:          "EXCHANGE LIMIT",
  1605  					TypePrev:      "",
  1606  					MTSTif:        0,
  1607  					Flags:         0,
  1608  					Status:        "ACTIVE",
  1609  					Price:         33,
  1610  					PriceAvg:      0,
  1611  					PriceTrailing: 0,
  1612  					PriceAuxLimit: 0,
  1613  					Notify:        false,
  1614  					Hidden:        false,
  1615  					PlacedID:      0,
  1616  					Routing:       "API>BFX",
  1617  					Meta:          nil,
  1618  				},
  1619  				Code:   0,
  1620  				Status: "SUCCESS",
  1621  				Text:   "Submitting exchange limit buy order for 0.001 BTC.",
  1622  			},
  1623  		},
  1624  		"order cancel notification": {
  1625  			pld: []byte(`[
  1626  				0,
  1627  				"n",
  1628  				[
  1629  					1611922089,"oc-req",null,null,
  1630  					[
  1631  						1201469553,0,788,"tBTCUSD",1611922089073,1611922089073,0.001,0.001,"EXCHANGE LIMIT",
  1632  						null,null,null,0,"ACTIVE",null,null,33,0,0,0,null,null,null,0,0,null,null,null,
  1633  						"API>BFX",null,null,null
  1634  					],
  1635  					null,"SUCCESS","Submitting exchange limit buy order for 0.001 BTC."
  1636  				]
  1637  			]`),
  1638  			expected: &notification.Notification{
  1639  				MTS:       1611922089,
  1640  				Type:      "oc-req",
  1641  				MessageID: 0,
  1642  				NotifyInfo: order.Cancel{
  1643  					ID:            1201469553,
  1644  					GID:           0,
  1645  					CID:           788,
  1646  					Symbol:        "tBTCUSD",
  1647  					MTSCreated:    1611922089073,
  1648  					MTSUpdated:    1611922089073,
  1649  					Amount:        0.001,
  1650  					AmountOrig:    0.001,
  1651  					Type:          "EXCHANGE LIMIT",
  1652  					TypePrev:      "",
  1653  					MTSTif:        0,
  1654  					Flags:         0,
  1655  					Status:        "ACTIVE",
  1656  					Price:         33,
  1657  					PriceAvg:      0,
  1658  					PriceTrailing: 0,
  1659  					PriceAuxLimit: 0,
  1660  					Notify:        false,
  1661  					Hidden:        false,
  1662  					PlacedID:      0,
  1663  					Routing:       "API>BFX",
  1664  					Meta:          nil,
  1665  				},
  1666  				Code:   0,
  1667  				Status: "SUCCESS",
  1668  				Text:   "Submitting exchange limit buy order for 0.001 BTC.",
  1669  			},
  1670  		},
  1671  		"order udate notification": {
  1672  			pld: []byte(`[
  1673  				0,
  1674  				"n",
  1675  				[
  1676  					1575289447641,"ou-req",null,null,
  1677  					[
  1678  						1185815100,null,1575289350475,"tETHUSD",1575289351944,1575289351948,-3,
  1679  						-3,"LIMIT",null,null,null,0,"ACTIVE",null,null,240,0,0,0,null,null,null,
  1680  						0,0,null,null,null,"API>BFX",null,null,null
  1681  					],
  1682  					null,"SUCCESS","Submitting update to limit sell order for 3 ETH."
  1683  				]
  1684  			]`),
  1685  			expected: &notification.Notification{
  1686  				MTS:       1575289447641,
  1687  				Type:      "ou-req",
  1688  				MessageID: 0,
  1689  				NotifyInfo: order.Update{
  1690  					ID:            1185815100,
  1691  					GID:           0,
  1692  					CID:           1575289350475,
  1693  					Symbol:        "tETHUSD",
  1694  					MTSCreated:    1575289351944,
  1695  					MTSUpdated:    1575289351948,
  1696  					Amount:        -3,
  1697  					AmountOrig:    -3,
  1698  					Type:          "LIMIT",
  1699  					TypePrev:      "",
  1700  					MTSTif:        0,
  1701  					Flags:         0,
  1702  					Status:        "ACTIVE",
  1703  					Price:         240,
  1704  					PriceAvg:      0,
  1705  					PriceTrailing: 0,
  1706  					PriceAuxLimit: 0,
  1707  					Notify:        false,
  1708  					Hidden:        false,
  1709  					PlacedID:      0,
  1710  					Routing:       "API>BFX",
  1711  					Meta:          nil,
  1712  				},
  1713  				Code:   0,
  1714  				Status: "SUCCESS",
  1715  				Text:   "Submitting update to limit sell order for 3 ETH.",
  1716  			},
  1717  		},
  1718  		"funding offer new notification": {
  1719  			pld: []byte(`[
  1720  				0,
  1721  				"n",
  1722  				[
  1723  					1575282446099,"fon-req",null,null,
  1724  					[
  1725  						41238905,null,null,null,-1000,null,null,null,null,null,
  1726  						null,null,null,null,0.002,2,null,null,null,null,null
  1727  					],
  1728  					null,"SUCCESS","Submitting funding bid of 1000.0 USD at 0.2000 for 2 days."
  1729  				]
  1730  			]`),
  1731  			expected: &notification.Notification{
  1732  				MTS:       1575282446099,
  1733  				Type:      "fon-req",
  1734  				MessageID: 0,
  1735  				NotifyInfo: fundingoffer.New{
  1736  					ID:         41238905,
  1737  					Symbol:     "",
  1738  					MTSCreated: 0,
  1739  					MTSUpdated: 0,
  1740  					Amount:     -1000,
  1741  					AmountOrig: 0,
  1742  					Type:       "",
  1743  					Flags:      map[string]interface{}(nil),
  1744  					Status:     "",
  1745  					Rate:       0.002,
  1746  					Period:     2,
  1747  					Notify:     false,
  1748  					Hidden:     false,
  1749  					Insure:     false,
  1750  					Renew:      false,
  1751  					RateReal:   0,
  1752  				},
  1753  				Code:   0,
  1754  				Status: "SUCCESS",
  1755  				Text:   "Submitting funding bid of 1000.0 USD at 0.2000 for 2 days.",
  1756  			},
  1757  		},
  1758  	}
  1759  
  1760  	for k, v := range cases {
  1761  		t.Run(k, func(t *testing.T) {
  1762  			m := msg.Msg{Data: v.pld}
  1763  			raw, pld, chID, msgType, err := m.PreprocessRaw()
  1764  			require.NoError(t, err)
  1765  			got, err := m.ProcessPrivate(raw, pld, chID, msgType)
  1766  			assert.NoError(t, err)
  1767  			assert.Equal(t, v.expected, got)
  1768  		})
  1769  	}
  1770  }