github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/orders_test.go (about)

     1  package rest
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  
    11  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/common"
    12  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/order"
    13  	"github.com/bitfinexcom/bitfinex-api-go/v2"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestOrdersAll(t *testing.T) {
    19  	httpDo := func(_ *http.Client, req *http.Request) (*http.Response, error) {
    20  		msg := `
    21  				[
    22  					[33961681942,"1227",1337,"tBTCUSD",1573482478000,1573485373000,0.001,0.001,"EXCHANGE LIMIT",null,null,null,"0","CANCELED",null,null,15,0,0,0,null,null,null,0,0,null,null,null,"API>BFX",null,null,null],
    23  					[33961681943,"1227",1337,"tBTCUSD",1573482478000,1573485373000,0.001,0.001,"EXCHANGE LIMIT",null,null,null,"0","CANCELED",null,null,15,0,0,0,null,null,null,0,0,null,null,null,"API>BFX",null,null,null],
    24  					[33961681944,"1227",1337,"tBTCUSD",1573482478000,1573485373000,0.001,0.001,"EXCHANGE LIMIT",null,null,null,"0","CANCELED",null,null,15,0,0,0,null,null,null,0,0,null,null,null,"API>BFX",null,null,null]
    25  				]`
    26  		resp := http.Response{
    27  			Body:       ioutil.NopCloser(bytes.NewBufferString(msg)),
    28  			StatusCode: 200,
    29  		}
    30  		return &resp, nil
    31  	}
    32  
    33  	orders, err := NewClientWithHttpDo(httpDo).Orders.All()
    34  
    35  	if err != nil {
    36  		t.Error(err)
    37  	}
    38  
    39  	if len(orders.Snapshot) != 3 {
    40  		t.Fatalf("expected three orders but got %d", len(orders.Snapshot))
    41  	}
    42  }
    43  
    44  func TestOrdersHistory(t *testing.T) {
    45  	httpDo := func(_ *http.Client, req *http.Request) (*http.Response, error) {
    46  		msg := `
    47  				[
    48  					[33961681942,"1227",1337,"tBTCUSD",1573482478000,1573485373000,0.001,0.001,"EXCHANGE LIMIT",null,null,null,"0","CANCELED",null,null,15,0,0,0,null,null,null,0,0,null,null,null,"API>BFX",null,null,null],
    49  					[33961681943,"1227",1337,"tBTCUSD",1573482478000,1573485373000,0.001,0.001,"EXCHANGE LIMIT",null,null,null,"0","CANCELED",null,null,15,0,0,0,null,null,null,0,0,null,null,null,"API>BFX",null,null,null],
    50  					[33961681944,"1227",1337,"tBTCUSD",1573482478000,1573485373000,0.001,0.001,"EXCHANGE LIMIT",null,null,null,"0","CANCELED",null,null,15,0,0,0,null,null,null,0,0,null,null,null,"API>BFX",null,null,null]
    51  				]`
    52  		resp := http.Response{
    53  			Body:       ioutil.NopCloser(bytes.NewBufferString(msg)),
    54  			StatusCode: 200,
    55  		}
    56  		return &resp, nil
    57  	}
    58  
    59  	orders, err := NewClientWithHttpDo(httpDo).Orders.GetHistoryBySymbol(common.TradingPrefix + bitfinex.IOTBTC)
    60  
    61  	if err != nil {
    62  		t.Error(err)
    63  	}
    64  
    65  	if len(orders.Snapshot) != 3 {
    66  		t.Errorf("expected three orders but got %d", len(orders.Snapshot))
    67  	}
    68  }
    69  
    70  func TestCancelOrderMulti(t *testing.T) {
    71  	t.Run("calls correct resource with correct payload", func(t *testing.T) {
    72  		handler := func(w http.ResponseWriter, r *http.Request) {
    73  			assert.Equal(t, "/auth/w/order/cancel/multi", r.RequestURI)
    74  			assert.Equal(t, "POST", r.Method)
    75  
    76  			gotReqPld := CancelOrderMultiRequest{}
    77  			err := json.NewDecoder(r.Body).Decode(&gotReqPld)
    78  			require.Nil(t, err)
    79  
    80  			expectedReqPld := CancelOrderMultiRequest{
    81  				OrderIDs:      OrderIDs{123},
    82  				GroupOrderIDs: GroupOrderIDs{234},
    83  				All:           1,
    84  			}
    85  			assert.Equal(t, expectedReqPld, gotReqPld)
    86  
    87  			respMock := []interface{}{1568711312683, nil, nil, nil, nil, nil, nil, nil}
    88  			payload, _ := json.Marshal(respMock)
    89  			_, err = w.Write(payload)
    90  			require.Nil(t, err)
    91  		}
    92  
    93  		server := httptest.NewServer(http.HandlerFunc(handler))
    94  		defer server.Close()
    95  
    96  		c := NewClientWithURL(server.URL)
    97  		pld := CancelOrderMultiRequest{
    98  			OrderIDs:      OrderIDs{123},
    99  			GroupOrderIDs: GroupOrderIDs{234},
   100  			All:           1,
   101  		}
   102  
   103  		rsp, err := c.Orders.CancelOrderMulti(pld)
   104  		require.Nil(t, err)
   105  		assert.Equal(t, int64(1568711312683), rsp.MTS)
   106  	})
   107  }
   108  
   109  func TestCancelOrdersMultiOp(t *testing.T) {
   110  	t.Run("calls correct resource with correct payload", func(t *testing.T) {
   111  		handler := func(w http.ResponseWriter, r *http.Request) {
   112  			assert.Equal(t, "/auth/w/order/multi", r.RequestURI)
   113  			assert.Equal(t, "POST", r.Method)
   114  
   115  			gotReqPld := OrderMultiOpsRequest{}
   116  			err := json.NewDecoder(r.Body).Decode(&gotReqPld)
   117  			require.Nil(t, err)
   118  
   119  			expectedReqPld := []interface{}{
   120  				"oc_multi",
   121  				map[string]interface{}{
   122  					"id": []interface{}{
   123  						float64(1189428429),
   124  						float64(1189428430),
   125  					},
   126  				},
   127  			}
   128  			assert.Equal(t, expectedReqPld, gotReqPld.Ops[0])
   129  
   130  			respMock := []interface{}{1568711312683, nil, nil, nil, nil, nil, nil, nil}
   131  			payload, _ := json.Marshal(respMock)
   132  			_, err = w.Write(payload)
   133  			require.Nil(t, err)
   134  		}
   135  
   136  		server := httptest.NewServer(http.HandlerFunc(handler))
   137  		defer server.Close()
   138  
   139  		c := NewClientWithURL(server.URL)
   140  		rsp, err := c.Orders.CancelOrdersMultiOp(OrderIDs{1189428429, 1189428430})
   141  		require.Nil(t, err)
   142  		assert.Equal(t, int64(1568711312683), rsp.MTS)
   143  	})
   144  }
   145  
   146  func TestCancelOrderMultiOp(t *testing.T) {
   147  	t.Run("calls correct resource with correct payload", func(t *testing.T) {
   148  		handler := func(w http.ResponseWriter, r *http.Request) {
   149  			assert.Equal(t, "/auth/w/order/multi", r.RequestURI)
   150  			assert.Equal(t, "POST", r.Method)
   151  
   152  			gotReqPld := OrderMultiOpsRequest{}
   153  			err := json.NewDecoder(r.Body).Decode(&gotReqPld)
   154  			require.Nil(t, err)
   155  
   156  			expectedReqPld := []interface{}{
   157  				"oc",
   158  				map[string]interface{}{"id": float64(1189428429)},
   159  			}
   160  			assert.Equal(t, expectedReqPld, gotReqPld.Ops[0])
   161  
   162  			respMock := []interface{}{1568711312683, nil, nil, nil, nil, nil, nil, nil}
   163  			payload, _ := json.Marshal(respMock)
   164  			_, err = w.Write(payload)
   165  			require.Nil(t, err)
   166  		}
   167  
   168  		server := httptest.NewServer(http.HandlerFunc(handler))
   169  		defer server.Close()
   170  
   171  		c := NewClientWithURL(server.URL)
   172  		rsp, err := c.Orders.CancelOrderMultiOp(1189428429)
   173  		require.Nil(t, err)
   174  		assert.Equal(t, int64(1568711312683), rsp.MTS)
   175  	})
   176  }
   177  
   178  func TestOrderNewMultiOp(t *testing.T) {
   179  	t.Run("calls correct resource with correct payload", func(t *testing.T) {
   180  		handler := func(w http.ResponseWriter, r *http.Request) {
   181  			assert.Equal(t, "/auth/w/order/multi", r.RequestURI)
   182  			assert.Equal(t, "POST", r.Method)
   183  
   184  			gotReqPld := OrderMultiOpsRequest{}
   185  			err := json.NewDecoder(r.Body).Decode(&gotReqPld)
   186  			require.Nil(t, err)
   187  
   188  			expectedReqPld := []interface{}{
   189  				"on",
   190  				map[string]interface{}{
   191  					"amount": "0.002",
   192  					"cid":    float64(119),
   193  					"gid":    float64(118),
   194  					"price":  "12",
   195  					"symbol": "tBTCUSD",
   196  					"type":   "EXCHANGE LIMIT",
   197  					"flags":  float64(512),
   198  					"meta": map[string]interface{}{
   199  						"aff_code": "abc",
   200  					},
   201  				},
   202  			}
   203  			assert.Equal(t, expectedReqPld, gotReqPld.Ops[0])
   204  
   205  			respMock := []interface{}{1568711312683, nil, nil, nil, nil, nil, nil, nil}
   206  			payload, _ := json.Marshal(respMock)
   207  			_, err = w.Write(payload)
   208  			require.Nil(t, err)
   209  		}
   210  
   211  		server := httptest.NewServer(http.HandlerFunc(handler))
   212  		defer server.Close()
   213  
   214  		c := NewClientWithURL(server.URL)
   215  		o := order.NewRequest{
   216  			CID:           119,
   217  			GID:           118,
   218  			Type:          "EXCHANGE LIMIT",
   219  			Symbol:        "tBTCUSD",
   220  			Price:         12,
   221  			Amount:        0.002,
   222  			AffiliateCode: "abc",
   223  			Close:         true,
   224  		}
   225  
   226  		rsp, err := c.Orders.OrderNewMultiOp(o)
   227  		require.Nil(t, err)
   228  		assert.Equal(t, int64(1568711312683), rsp.MTS)
   229  	})
   230  }
   231  
   232  func TestOrderUpdateMultiOp(t *testing.T) {
   233  	t.Run("calls correct resource with correct payload", func(t *testing.T) {
   234  		handler := func(w http.ResponseWriter, r *http.Request) {
   235  			assert.Equal(t, "/auth/w/order/multi", r.RequestURI)
   236  			assert.Equal(t, "POST", r.Method)
   237  
   238  			gotReqPld := OrderMultiOpsRequest{}
   239  			err := json.NewDecoder(r.Body).Decode(&gotReqPld)
   240  			require.Nil(t, err)
   241  
   242  			expectedReqPld := []interface{}{
   243  				"ou",
   244  				map[string]interface{}{
   245  					"amount": "0.002",
   246  					"price":  "12",
   247  					"id":     float64(1189503586),
   248  					"flags":  float64(64),
   249  				},
   250  			}
   251  			assert.Equal(t, expectedReqPld, gotReqPld.Ops[0])
   252  
   253  			respMock := []interface{}{1568711312683, nil, nil, nil, nil, nil, nil, nil}
   254  			payload, _ := json.Marshal(respMock)
   255  			_, err = w.Write(payload)
   256  			require.Nil(t, err)
   257  		}
   258  
   259  		server := httptest.NewServer(http.HandlerFunc(handler))
   260  		defer server.Close()
   261  
   262  		c := NewClientWithURL(server.URL)
   263  		o := order.UpdateRequest{
   264  			ID:     1189503586,
   265  			Price:  12,
   266  			Amount: 0.002,
   267  			Hidden: true,
   268  		}
   269  
   270  		rsp, err := c.Orders.OrderUpdateMultiOp(o)
   271  		require.Nil(t, err)
   272  		assert.Equal(t, int64(1568711312683), rsp.MTS)
   273  	})
   274  }
   275  
   276  func TestOrderMultiOp(t *testing.T) {
   277  	t.Run("calls correct resource with correct payload", func(t *testing.T) {
   278  		handler := func(w http.ResponseWriter, r *http.Request) {
   279  			assert.Equal(t, "/auth/w/order/multi", r.RequestURI)
   280  			assert.Equal(t, "POST", r.Method)
   281  
   282  			gotReqPld := OrderMultiOpsRequest{}
   283  			err := json.NewDecoder(r.Body).Decode(&gotReqPld)
   284  			require.Nil(t, err)
   285  
   286  			expectedReqPld := map[string][]interface{}{
   287  				"on": {
   288  					"on",
   289  					map[string]interface{}{
   290  						"amount": "0.001",
   291  						"cid":    float64(987),
   292  						"flags":  float64(4096),
   293  						"gid":    float64(876),
   294  						"meta": map[string]interface{}{
   295  							"aff_code": "abc",
   296  						},
   297  						"price":  "13",
   298  						"symbol": "tBTCUSD",
   299  						"type":   "EXCHANGE LIMIT",
   300  					},
   301  				},
   302  				"ou": {
   303  					"ou",
   304  					map[string]interface{}{
   305  						"amount": "0.002",
   306  						"price":  "15",
   307  						"id":     float64(1189503342),
   308  						"flags":  float64(64),
   309  					},
   310  				},
   311  				"oc": {
   312  					"oc",
   313  					map[string]interface{}{"id": float64(1189502430)},
   314  				},
   315  				"oc_multi": {
   316  					"oc_multi",
   317  					map[string]interface{}{
   318  						"id": []interface{}{
   319  							float64(1189502431),
   320  							float64(1189502432),
   321  						},
   322  					},
   323  				},
   324  			}
   325  
   326  			for _, v := range gotReqPld.Ops {
   327  				key := v[0].(string)
   328  				assert.Equal(t, expectedReqPld[key][1], v[1])
   329  			}
   330  
   331  			respMock := []interface{}{1568711312683, nil, nil, nil, nil, nil, nil, nil}
   332  			payload, _ := json.Marshal(respMock)
   333  			_, err = w.Write(payload)
   334  			require.Nil(t, err)
   335  		}
   336  
   337  		server := httptest.NewServer(http.HandlerFunc(handler))
   338  		defer server.Close()
   339  
   340  		c := NewClientWithURL(server.URL)
   341  		pld := OrderOps{
   342  			{
   343  				"on",
   344  				order.NewRequest{
   345  					CID:           987,
   346  					GID:           876,
   347  					Type:          "EXCHANGE LIMIT",
   348  					Symbol:        "tBTCUSD",
   349  					Price:         13,
   350  					Amount:        0.001,
   351  					PostOnly:      true,
   352  					AffiliateCode: "abc",
   353  				},
   354  			},
   355  			{
   356  				"oc",
   357  				map[string]int{"id": 1189502430},
   358  			},
   359  			{
   360  				"oc_multi",
   361  				map[string][]int{"id": OrderIDs{1189502431, 1189502432}},
   362  			},
   363  			{
   364  				"ou",
   365  				order.UpdateRequest{
   366  					ID:     1189503342,
   367  					Price:  15,
   368  					Amount: 0.002,
   369  					Hidden: true,
   370  				},
   371  			},
   372  		}
   373  		rsp, err := c.Orders.OrderMultiOp(pld)
   374  		require.Nil(t, err)
   375  		assert.Equal(t, int64(1568711312683), rsp.MTS)
   376  	})
   377  }