diff --git a/CoinbasePro.Specs/Services/Orders/OrdersServiceSpecs.cs b/CoinbasePro.Specs/Services/Orders/OrdersServiceSpecs.cs index 90147798..a725569a 100644 --- a/CoinbasePro.Specs/Services/Orders/OrdersServiceSpecs.cs +++ b/CoinbasePro.Specs/Services/Orders/OrdersServiceSpecs.cs @@ -294,50 +294,101 @@ class when_getting_all_orders class when_getting_all_orders_and_specifying_order_status { - Establish context = () => - The().WhenToldTo(p => p.ReadAsStringAsync(Param.IsAny())) - .Return(Task.FromResult(OrderResponseFixture.CreateLimitOrderMany(OrderStatus.Active))); - - Because of = () => - order_many_response_result = Subject.GetAllOrdersAsync(OrderStatus.Active).Result; - - It should_have_correct_number_of_orders = () => - order_many_response_result.First().Count.ShouldEqual(2); - - It should_have_correct_orders = () => + class with_single_status { - order_many_response_result.First().First().Id.ShouldEqual(new Guid("d0c5340b-6d6c-49d9-b567-48c4bfca13d2")); - order_many_response_result.First().First().Price.ShouldEqual(0.10000000M); - order_many_response_result.First().First().Size.ShouldEqual(0.01000000M); - order_many_response_result.First().First().ProductId.ShouldEqual(ProductType.BtcUsd); - order_many_response_result.First().First().Side.ShouldEqual(OrderSide.Buy); - order_many_response_result.First().First().Stp.ShouldEqual("dc"); - order_many_response_result.First().First().OrderType.ShouldEqual(OrderType.Limit); - order_many_response_result.First().First().TimeInForce.ShouldEqual(TimeInForce.Gtc); - order_many_response_result.First().First().PostOnly.ShouldBeFalse(); - order_many_response_result.First().First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9)); - order_many_response_result.First().First().FillFees.ShouldEqual(0.0000000000000000M); - order_many_response_result.First().First().FilledSize.ShouldEqual(0.00000000M); - order_many_response_result.First().First().ExecutedValue.ShouldEqual(0.0000000000000000M); - order_many_response_result.First().First().Status.ShouldEqual(OrderStatus.Active); - order_many_response_result.First().First().Settled.ShouldBeFalse(); - - order_many_response_result.First().Skip(1).First().Id.ShouldEqual(new Guid("8b99b139-58f2-4ab2-8e7a-c11c846e3022")); - order_many_response_result.First().Skip(1).First().Price.ShouldEqual(0.10000000M); - order_many_response_result.First().Skip(1).First().Size.ShouldEqual(0.01000000M); - order_many_response_result.First().Skip(1).First().ProductId.ShouldEqual(ProductType.EthUsd); - order_many_response_result.First().Skip(1).First().Side.ShouldEqual(OrderSide.Buy); - order_many_response_result.First().Skip(1).First().Stp.ShouldEqual("dc"); - order_many_response_result.First().Skip(1).First().OrderType.ShouldEqual(OrderType.Limit); - order_many_response_result.First().Skip(1).First().TimeInForce.ShouldEqual(TimeInForce.Gtc); - order_many_response_result.First().Skip(1).First().PostOnly.ShouldBeFalse(); - order_many_response_result.First().Skip(1).First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9)); - order_many_response_result.First().Skip(1).First().FillFees.ShouldEqual(0.0000000000000000M); - order_many_response_result.First().Skip(1).First().FilledSize.ShouldEqual(0.00000000M); - order_many_response_result.First().Skip(1).First().ExecutedValue.ShouldEqual(0.0000000000000000M); - order_many_response_result.First().Skip(1).First().Status.ShouldEqual(OrderStatus.Active); - order_many_response_result.First().Skip(1).First().Settled.ShouldBeFalse(); - }; + Establish context = () => + The().WhenToldTo(p => p.ReadAsStringAsync(Param.IsAny())) + .Return(Task.FromResult(OrderResponseFixture.CreateLimitOrderMany(OrderStatus.Active))); + + Because of = () => + order_many_response_result = Subject.GetAllOrdersAsync(OrderStatus.Active).Result; + + It should_have_correct_number_of_orders = () => + order_many_response_result.First().Count.ShouldEqual(2); + + It should_have_correct_orders = () => + { + order_many_response_result.First().First().Id.ShouldEqual(new Guid("d0c5340b-6d6c-49d9-b567-48c4bfca13d2")); + order_many_response_result.First().First().Price.ShouldEqual(0.10000000M); + order_many_response_result.First().First().Size.ShouldEqual(0.01000000M); + order_many_response_result.First().First().ProductId.ShouldEqual(ProductType.BtcUsd); + order_many_response_result.First().First().Side.ShouldEqual(OrderSide.Buy); + order_many_response_result.First().First().Stp.ShouldEqual("dc"); + order_many_response_result.First().First().OrderType.ShouldEqual(OrderType.Limit); + order_many_response_result.First().First().TimeInForce.ShouldEqual(TimeInForce.Gtc); + order_many_response_result.First().First().PostOnly.ShouldBeFalse(); + order_many_response_result.First().First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9)); + order_many_response_result.First().First().FillFees.ShouldEqual(0.0000000000000000M); + order_many_response_result.First().First().FilledSize.ShouldEqual(0.00000000M); + order_many_response_result.First().First().ExecutedValue.ShouldEqual(0.0000000000000000M); + order_many_response_result.First().First().Status.ShouldEqual(OrderStatus.Active); + order_many_response_result.First().First().Settled.ShouldBeFalse(); + + order_many_response_result.First().Skip(1).First().Id.ShouldEqual(new Guid("8b99b139-58f2-4ab2-8e7a-c11c846e3022")); + order_many_response_result.First().Skip(1).First().Price.ShouldEqual(0.10000000M); + order_many_response_result.First().Skip(1).First().Size.ShouldEqual(0.01000000M); + order_many_response_result.First().Skip(1).First().ProductId.ShouldEqual(ProductType.EthUsd); + order_many_response_result.First().Skip(1).First().Side.ShouldEqual(OrderSide.Buy); + order_many_response_result.First().Skip(1).First().Stp.ShouldEqual("dc"); + order_many_response_result.First().Skip(1).First().OrderType.ShouldEqual(OrderType.Limit); + order_many_response_result.First().Skip(1).First().TimeInForce.ShouldEqual(TimeInForce.Gtc); + order_many_response_result.First().Skip(1).First().PostOnly.ShouldBeFalse(); + order_many_response_result.First().Skip(1).First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9)); + order_many_response_result.First().Skip(1).First().FillFees.ShouldEqual(0.0000000000000000M); + order_many_response_result.First().Skip(1).First().FilledSize.ShouldEqual(0.00000000M); + order_many_response_result.First().Skip(1).First().ExecutedValue.ShouldEqual(0.0000000000000000M); + order_many_response_result.First().Skip(1).First().Status.ShouldEqual(OrderStatus.Active); + order_many_response_result.First().Skip(1).First().Settled.ShouldBeFalse(); + }; + } + + class with_multiple_statuses + { + Establish context = () => + The().WhenToldTo(p => p.ReadAsStringAsync(Param.IsAny())) + .Return(Task.FromResult(OrderResponseFixture.CreateLimitOrderMany(OrderStatus.Active))); + + Because of = () => + order_many_response_result = Subject.GetAllOrdersAsync(new [] { OrderStatus.Active, OrderStatus.Done }).Result; + + It should_have_correct_number_of_orders = () => + order_many_response_result.First().Count.ShouldEqual(2); + + It should_have_correct_orders = () => + { + order_many_response_result.First().First().Id.ShouldEqual(new Guid("d0c5340b-6d6c-49d9-b567-48c4bfca13d2")); + order_many_response_result.First().First().Price.ShouldEqual(0.10000000M); + order_many_response_result.First().First().Size.ShouldEqual(0.01000000M); + order_many_response_result.First().First().ProductId.ShouldEqual(ProductType.BtcUsd); + order_many_response_result.First().First().Side.ShouldEqual(OrderSide.Buy); + order_many_response_result.First().First().Stp.ShouldEqual("dc"); + order_many_response_result.First().First().OrderType.ShouldEqual(OrderType.Limit); + order_many_response_result.First().First().TimeInForce.ShouldEqual(TimeInForce.Gtc); + order_many_response_result.First().First().PostOnly.ShouldBeFalse(); + order_many_response_result.First().First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9)); + order_many_response_result.First().First().FillFees.ShouldEqual(0.0000000000000000M); + order_many_response_result.First().First().FilledSize.ShouldEqual(0.00000000M); + order_many_response_result.First().First().ExecutedValue.ShouldEqual(0.0000000000000000M); + order_many_response_result.First().First().Status.ShouldEqual(OrderStatus.Active); + order_many_response_result.First().First().Settled.ShouldBeFalse(); + + order_many_response_result.First().Skip(1).First().Id.ShouldEqual(new Guid("8b99b139-58f2-4ab2-8e7a-c11c846e3022")); + order_many_response_result.First().Skip(1).First().Price.ShouldEqual(0.10000000M); + order_many_response_result.First().Skip(1).First().Size.ShouldEqual(0.01000000M); + order_many_response_result.First().Skip(1).First().ProductId.ShouldEqual(ProductType.EthUsd); + order_many_response_result.First().Skip(1).First().Side.ShouldEqual(OrderSide.Buy); + order_many_response_result.First().Skip(1).First().Stp.ShouldEqual("dc"); + order_many_response_result.First().Skip(1).First().OrderType.ShouldEqual(OrderType.Limit); + order_many_response_result.First().Skip(1).First().TimeInForce.ShouldEqual(TimeInForce.Gtc); + order_many_response_result.First().Skip(1).First().PostOnly.ShouldBeFalse(); + order_many_response_result.First().Skip(1).First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9)); + order_many_response_result.First().Skip(1).First().FillFees.ShouldEqual(0.0000000000000000M); + order_many_response_result.First().Skip(1).First().FilledSize.ShouldEqual(0.00000000M); + order_many_response_result.First().Skip(1).First().ExecutedValue.ShouldEqual(0.0000000000000000M); + order_many_response_result.First().Skip(1).First().Status.ShouldEqual(OrderStatus.Active); + order_many_response_result.First().Skip(1).First().Settled.ShouldBeFalse(); + }; + } } class when_getting_order_by_id diff --git a/CoinbasePro/CoinbaseProClient.cs b/CoinbasePro/CoinbaseProClient.cs index e195d111..5d6f5614 100644 --- a/CoinbasePro/CoinbaseProClient.cs +++ b/CoinbasePro/CoinbaseProClient.cs @@ -49,7 +49,7 @@ public CoinbaseProClient( AccountsService = new AccountsService(httpClient, httpRequestMessageService); CoinbaseAccountsService = new CoinbaseAccountsService(httpClient, httpRequestMessageService); - OrdersService = new OrdersService(httpClient, httpRequestMessageService); + OrdersService = new OrdersService(httpClient, httpRequestMessageService, queryBuilder); PaymentsService = new PaymentsService(httpClient, httpRequestMessageService); WithdrawalsService = new WithdrawalsService(httpClient, httpRequestMessageService); DepositsService = new DepositsService(httpClient, httpRequestMessageService); diff --git a/CoinbasePro/Network/Authentication/Authenticator.cs b/CoinbasePro/Network/Authentication/Authenticator.cs index 97511b9a..11321175 100644 --- a/CoinbasePro/Network/Authentication/Authenticator.cs +++ b/CoinbasePro/Network/Authentication/Authenticator.cs @@ -36,7 +36,7 @@ public string ComputeSignature( return HashString(prehash, convertedString); } - public string HashString(string str, byte[] secret) + private string HashString(string str, byte[] secret) { var bytes = Encoding.UTF8.GetBytes(str); using (var hmaccsha = new HMACSHA256(secret)) diff --git a/CoinbasePro/Network/Authentication/IAuthenticator.cs b/CoinbasePro/Network/Authentication/IAuthenticator.cs index acc45bb9..76f273ec 100644 --- a/CoinbasePro/Network/Authentication/IAuthenticator.cs +++ b/CoinbasePro/Network/Authentication/IAuthenticator.cs @@ -16,8 +16,5 @@ string ComputeSignature( double timestamp, string requestUri, string contentBody = ""); - - string HashString(string str, byte[] secret); - } } diff --git a/CoinbasePro/Services/Orders/OrdersService.cs b/CoinbasePro/Services/Orders/OrdersService.cs index 1bad1fb3..8af65c29 100644 --- a/CoinbasePro/Services/Orders/OrdersService.cs +++ b/CoinbasePro/Services/Orders/OrdersService.cs @@ -1,6 +1,7 @@ using CoinbasePro.Shared.Utilities.Extensions; using System; using System.Collections.Generic; +using System.Linq; using System.Net.Http; using System.Threading.Tasks; using CoinbasePro.Network.HttpClient; @@ -10,16 +11,21 @@ using CoinbasePro.Services.Orders.Types; using CoinbasePro.Shared.Types; using CoinbasePro.Shared.Utilities; +using CoinbasePro.Shared.Utilities.Queries; namespace CoinbasePro.Services.Orders { public class OrdersService : AbstractService { + private readonly QueryBuilder queryBuilder; + public OrdersService( IHttpClient httpClient, - IHttpRequestMessageService httpRequestMessageService) + IHttpRequestMessageService httpRequestMessageService, + QueryBuilder queryBuilder) : base(httpClient, httpRequestMessageService) { + this.queryBuilder = queryBuilder; } public async Task PlaceMarketOrderAsync( @@ -171,6 +177,19 @@ public async Task>> GetAllOrdersAsync( return httpResponseMessage; } + public async Task>> GetAllOrdersAsync( + OrderStatus[] orderStatus, + int limit = 100, + int numberOfPages = 0) + { + var queryKeyValuePairs = orderStatus.Select(p => new KeyValuePair("status", p.GetEnumMemberValue())).ToArray(); + var query = queryBuilder.BuildQuery(queryKeyValuePairs); + + var httpResponseMessage = await SendHttpRequestMessagePagedAsync(HttpMethod.Get, $"/orders{query}&limit={limit}", numberOfPages: numberOfPages); + + return httpResponseMessage; + } + public async Task GetOrderByIdAsync(string id) { return await SendServiceCall(HttpMethod.Get, $"/orders/{id}").ConfigureAwait(false); diff --git a/README.md b/README.md index c5c96278..c784f35c 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ var allAccounts = await coinbaseProClient.AccountsService.GetAllAccountsAsync(); - CancelAllOrdersAsync() - cancel all orders - CancelOrderByIdAsync(id) - cancel order by id - GetAllOrdersAsync(orderStatus, limit, numberOfPages) - get all, active or pending orders (paged response) +- GetAllOrdersAsync(orderStatus[], limit, numberOfPages) - get orders by multiple statuses (paged response) - GetOrderByIdAsync(id) - get order by id ###### Payments ######