From 4267879951cd8929a9ea6201849a1fb92cd94d25 Mon Sep 17 00:00:00 2001 From: Yuri Kovalov Date: Sat, 17 Apr 2021 16:17:28 +0800 Subject: [PATCH] Fix and test spot cancel --- .../vcr_cassettes/spot/private/cancel_ok.json | 86 +++++++++++++++++++ lib/ex_okex/spot/private/cancel_orders.ex | 2 +- .../spot/private/cancel_orders_test.exs | 43 ++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 fixture/vcr_cassettes/spot/private/cancel_ok.json create mode 100644 test/ex_okex/spot/private/cancel_orders_test.exs diff --git a/fixture/vcr_cassettes/spot/private/cancel_ok.json b/fixture/vcr_cassettes/spot/private/cancel_ok.json new file mode 100644 index 0000000..1b93c65 --- /dev/null +++ b/fixture/vcr_cassettes/spot/private/cancel_ok.json @@ -0,0 +1,86 @@ +[ + { + "request": { + "body": "{\"price\":\"61000.0\",\"product_id\":\"BTC-USDT\",\"side\":\"buy\",\"size\":\"0.001\"}", + "headers": { + "Content-Type": "application/json", + "OK-ACCESS-KEY": "***", + "OK-ACCESS-SIGN": "***", + "OK-ACCESS-TIMESTAMP": "***", + "OK-ACCESS-PASSPHRASE": "***" + }, + "method": "post", + "options": [], + "request_body": "", + "url": "https://www.okex.com/api/spot/v3/orders" + }, + "response": { + "binary": false, + "body": "{\"client_oid\":\"\",\"code\":\"0\",\"error_code\":\"0\",\"error_message\":\"\",\"message\":\"\",\"order_id\":\"6809883785387008\",\"result\":true}", + "headers": { + "Date": "Sat, 17 Apr 2021 08:03:00 GMT", + "Content-Type": "application/json", + "Content-Length": "121", + "Connection": "keep-alive", + "Set-Cookie": "__cfduid=d9c03df6fb00e6ce9b161159f7dc7255e1618646580; expires=Mon, 17-May-21 08:03:00 GMT; path=/; domain=.okex.com; HttpOnly; SameSite=Lax", + "X-Content-Type-Options": "nosniff", + "X-XSS-Protection": "1; mode=block", + "Cache-Control": "no-cache, no-store, max-age=0, must-revalidate", + "Pragma": "no-cache", + "Expires": "0", + "X-Frame-Options": "DENY", + "X-BrokerID": "0", + "Strict-Transport-Security": "max-age=63072000; includeSubdomains; preload", + "CF-Cache-Status": "DYNAMIC", + "cf-request-id": "098073d3c500001936370a0000000001", + "Expect-CT": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", + "Server": "cloudflare", + "CF-RAY": "64142265faaa1936-SIN" + }, + "status_code": 200, + "type": "ok" + } + }, + { + "request": { + "body": "[{\"instrument_id\":\"BTC-USDT\",\"order_ids\":[\"6809883785387008\"]}]", + "headers": { + "Content-Type": "application/json", + "OK-ACCESS-KEY": "***", + "OK-ACCESS-SIGN": "***", + "OK-ACCESS-TIMESTAMP": "***", + "OK-ACCESS-PASSPHRASE": "***" + }, + "method": "post", + "options": [], + "request_body": "", + "url": "https://www.okex.com/api/spot/v3/cancel_batch_orders" + }, + "response": { + "binary": false, + "body": "{\"btc-usdt\":[{\"client_oid\":\"\",\"code\":\"0\",\"error_code\":\"0\",\"error_message\":\"\",\"message\":\"\",\"order_id\":\"6809883785387008\",\"result\":true}]}", + "headers": { + "Date": "Sat, 17 Apr 2021 08:03:00 GMT", + "Content-Type": "application/json", + "Content-Length": "136", + "Connection": "keep-alive", + "Set-Cookie": "__cfduid=d9c03df6fb00e6ce9b161159f7dc7255e1618646580; expires=Mon, 17-May-21 08:03:00 GMT; path=/; domain=.okex.com; HttpOnly; SameSite=Lax", + "X-Content-Type-Options": "nosniff", + "X-XSS-Protection": "1; mode=block", + "Cache-Control": "no-cache, no-store, max-age=0, must-revalidate", + "Pragma": "no-cache", + "Expires": "0", + "X-Frame-Options": "DENY", + "X-BrokerID": "0", + "Strict-Transport-Security": "max-age=63072000; includeSubdomains; preload", + "CF-Cache-Status": "DYNAMIC", + "cf-request-id": "098073d43000001936370ae000000001", + "Expect-CT": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", + "Server": "cloudflare", + "CF-RAY": "64142266ac9c1936-SIN" + }, + "status_code": 200, + "type": "ok" + } + } +] \ No newline at end of file diff --git a/lib/ex_okex/spot/private/cancel_orders.ex b/lib/ex_okex/spot/private/cancel_orders.ex index 9d46c68..88ba737 100644 --- a/lib/ex_okex/spot/private/cancel_orders.ex +++ b/lib/ex_okex/spot/private/cancel_orders.ex @@ -8,6 +8,6 @@ defmodule ExOkex.Spot.Private.CancelOrders do new_params = params |> Map.merge(%{instrument_id: instrument_id, order_ids: order_ids}) @path - |> post(new_params, config) + |> post([new_params], config) end end diff --git a/test/ex_okex/spot/private/cancel_orders_test.exs b/test/ex_okex/spot/private/cancel_orders_test.exs new file mode 100644 index 0000000..710ea9e --- /dev/null +++ b/test/ex_okex/spot/private/cancel_orders_test.exs @@ -0,0 +1,43 @@ +defmodule ExOkex.Spot.Private.CancelOrdersTest do + use ExUnit.Case, async: false + use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + alias ExOkex.Spot + + @config %ExOkex.Config{ + api_key: "OKEX_API_KEY", + api_secret: Base.encode64("OKEX_API_SECRET"), + api_passphrase: "OKEX_API_PASSPHRASE" + } + + test ".cancel_orders" do + use_cassette "spot/private/cancel_ok" do + {:ok, %{"order_id" => order_id}} = + Spot.Private.create_order( + %{ + side: "buy", + product_id: "BTC-USDT", + size: "0.001", + price: "61000.0" + }, + @config + ) + + assert {:ok, response} = Spot.Private.cancel_orders("BTC-USDT", [order_id], %{}, @config) + + assert %{ + "btc-usdt" => [ + %{ + "client_oid" => "", + "code" => "0", + "error_code" => "0", + "error_message" => "", + "message" => "", + "order_id" => ^order_id, + "result" => true + } + | _ + ] + } = response + end + end +end