Skip to content

Commit

Permalink
[Feature] Fundings Service (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
dougdellolio authored Dec 24, 2017
1 parent e62de20 commit de4ef26
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 0 deletions.
3 changes: 3 additions & 0 deletions GDAXClient.Specs/GDAXClient.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="JsonFixtures\Deposits\CoinbaseDepositResponseFixture.cs" />
<Compile Include="JsonFixtures\Deposits\DepositsResponseFixture.cs" />
<Compile Include="JsonFixtures\Fills\FillsResponseFixture.cs" />
<Compile Include="JsonFixtures\Fundings\FundingsResponseFixture.cs" />
<Compile Include="JsonFixtures\HttpResponseMessage\HttpResponseMessageFixture.cs" />
<Compile Include="JsonFixtures\Orders\CancelOrderResponseFixture.cs" />
<Compile Include="JsonFixtures\Orders\OrderResponseFixture.cs" />
Expand All @@ -102,13 +103,15 @@
<Compile Include="Services\Currencies\CurrenciesServiceSpecs.cs" />
<Compile Include="Services\Deposits\DepositsServiceSpecs.cs" />
<Compile Include="Services\Fills\FillsServiceSpecs.cs" />
<Compile Include="Services\Fundings\FundingsServiceSpecs.cs" />
<Compile Include="Services\HttpRequest\HttpRequestMessageServiceSpecs.cs" />
<Compile Include="Services\Orders\OrdersServiceSpecs.cs" />
<Compile Include="Services\Payments\PaymentsServiceSpecs.cs" />
<Compile Include="Services\Products\ProductsServiceSpecs.cs" />
<Compile Include="Services\Withdrawals\WithdrawalsServiceSpecs.cs" />
<Compile Include="Utilities\Extensions\DateExtensionsSpecs.cs" />
<Compile Include="Utilities\Extensions\ProductTypeExtensionsSpecs.cs" />
<Compile Include="Utilities\QueryBuilderSpecs.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
36 changes: 36 additions & 0 deletions GDAXClient.Specs/JsonFixtures/Fundings/FundingsResponseFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace GDAXClient.Specs.JsonFixtures.Fills
{
class FundingsResponseFixture
{
public static string Create()
{
var json = @"
[
{
""id"": ""b93d26cd-7193-4c8d-bfcc-446b2fe18f71"",
""order_id"": ""b93d26cd-7193-4c8d-bfcc-446b2fe18f71"",
""profile_id"": ""d881e5a6-58eb-47cd-b8e2-8d9f2e3ec6f6"",
""amount"": ""1057.6519956381537500"",
""status"": ""settled"",
""created_at"": ""2017-03-17T23:46:16.663397Z"",
""currency"": ""USD"",
""repaid_amount"": ""1057.6519956381537500"",
""default_amount"": ""0"",
""repaid_default"": false
},
{
""id"": ""280c0a56-f2fa-4d3b-a199-92df76fff5cd"",
""order_id"": ""280c0a56-f2fa-4d3b-a199-92df76fff5cd"",
""profile_id"": ""d881e5a6-58eb-47cd-b8e2-8d9f2e3ec6f6"",
""amount"": ""545.2400000000000000"",
""status"": ""outstanding"",
""created_at"": ""2017-03-18T00:34:34.270484Z"",
""currency"": ""USD"",
""repaid_amount"": ""532.7580047716682500""
},
]";

return json;
}
}
}
74 changes: 74 additions & 0 deletions GDAXClient.Specs/Services/Fundings/FundingsServiceSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using GDAXClient.Authentication;
using GDAXClient.HttpClient;
using Machine.Fakes;
using Machine.Specifications;
using GDAXClient.Services.Fills;
using GDAXClient.Services.Fills.Models.Responses;
using GDAXClient.Services.HttpRequest;
using GDAXClient.Services.Orders;
using GDAXClient.Specs.JsonFixtures.Fills;
using GDAXClient.Specs.JsonFixtures.HttpResponseMessage;
using GDAXClient.Utilities.Extensions;
using GDAXClient.Services.Fundings;
using GDAXClient.Services.Fundings.Models;

namespace GDAXClient.Specs.Services
{
[Subject("FundingsService")]
public class FundingsServiceSpecs : WithSubject<FundingsService>
{
static Authenticator authenticator;

static IList<IList<Funding>> fundings_response;

Establish context = () =>
authenticator = new Authenticator("apiKey", new string('2', 100), "passPhrase");

class when_requesting_all_fundings
{
Establish context = () =>
{
The<IHttpRequestMessageService>().WhenToldTo(p => p.CreateHttpRequestMessage(Param.IsAny<HttpMethod>(), Param.IsAny<Authenticator>(), Param.IsAny<string>(), Param.IsAny<string>()))
.Return(new HttpRequestMessage());

The<IHttpClient>().WhenToldTo(p => p.SendASync(Param.IsAny<HttpRequestMessage>()))
.Return(Task.FromResult(HttpResponseMessageFixture.CreateWithEmptyValue()));

The<IHttpClient>().WhenToldTo(p => p.ReadAsStringAsync(Param.IsAny<HttpResponseMessage>()))
.Return(Task.FromResult(FundingsResponseFixture.Create()));
};

Because of = () =>
fundings_response = Subject.GetAllFundingsAsync().Result;

It should_return_a_response = () =>
fundings_response.ShouldNotBeNull();

It should_return_a_correct_response = () =>
{
fundings_response.First().First().Id.ShouldEqual(new Guid("b93d26cd-7193-4c8d-bfcc-446b2fe18f71"));
fundings_response.First().First().Order_id.ShouldEqual("b93d26cd-7193-4c8d-bfcc-446b2fe18f71");
fundings_response.First().First().Profile_id.ShouldEqual("d881e5a6-58eb-47cd-b8e2-8d9f2e3ec6f6");
fundings_response.First().First().Amount.ShouldEqual(1057.6519956381537500M);
fundings_response.First().First().Status.ShouldEqual("settled");
fundings_response.First().First().Currency.ShouldEqual("USD");
fundings_response.First().First().Repaid_amount.ShouldEqual(1057.6519956381537500M);
fundings_response.First().First().Default_amount.ShouldEqual(0);
fundings_response.First().First().Repaid_default.ShouldBeFalse();

fundings_response.First().Skip(1).First().Id.ShouldEqual(new Guid("280c0a56-f2fa-4d3b-a199-92df76fff5cd"));
fundings_response.First().Skip(1).First().Order_id.ShouldEqual("280c0a56-f2fa-4d3b-a199-92df76fff5cd");
fundings_response.First().Skip(1).First().Profile_id.ShouldEqual("d881e5a6-58eb-47cd-b8e2-8d9f2e3ec6f6");
fundings_response.First().Skip(1).First().Amount.ShouldEqual(545.2400000000000000M);
fundings_response.First().Skip(1).First().Status.ShouldEqual("outstanding");
fundings_response.First().Skip(1).First().Currency.ShouldEqual("USD");
fundings_response.First().Skip(1).First().Repaid_amount.ShouldEqual(532.7580047716682500M);
};
}
}
}
45 changes: 45 additions & 0 deletions GDAXClient.Specs/Utilities/QueryBuilderSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using GDAXClient.Utilities;
using Machine.Fakes;
using Machine.Specifications;
using System.Collections.Generic;

namespace GDAXClient.Specs.Utilities
{
[Subject("QueryBuilder")]
public class QueryBuilderSpecs : WithSubject<QueryBuilder>
{
static string result;

class when_requesting_with_multiple_parameters
{
Because of = () =>
{
result = Subject.BuildQuery(
new KeyValuePair<string, string>("limit", "10"),
new KeyValuePair<string, string>("status", "approved"),
new KeyValuePair<string, string>("product_id", "eth-usd"));
};

It should_have_built_the_correct_query = () =>
{
result.ShouldEqual("?limit=10&status=approved&product_id=eth-usd");
};
}

class when_requesting_with_an_optional_parameter_and_empty_string_is_passed
{
Because of = () =>
{
result = Subject.BuildQuery(
new KeyValuePair<string, string>("limit", "10"),
new KeyValuePair<string, string>("status", "approved"),
new KeyValuePair<string, string>("product_id", ""));
};

It should_have_built_the_correct_query = () =>
{
result.ShouldEqual("?limit=10&status=approved");
};
}
}
}
5 changes: 5 additions & 0 deletions GDAXClient/GDAXClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using GDAXClient.Services.Currencies;
using GDAXClient.Services.Deposits;
using GDAXClient.Services.Fills;
using GDAXClient.Services.Fundings;
using GDAXClient.Services.Orders;
using GDAXClient.Services.Payments;
using GDAXClient.Services.WithdrawalsService;
Expand All @@ -23,6 +24,7 @@ public GDAXClient(Authenticator authenticator, bool sandBox = false)
var httpClient = new HttpClient.HttpClient();
var clock = new Clock();
var httpRequestMessageService = new Services.HttpRequest.HttpRequestMessageService(clock, sandBox);
var queryBuilder = new QueryBuilder();

AccountsService = new AccountsService(httpClient, httpRequestMessageService, authenticator);
CoinbaseAccountsService = new CoinbaseAccountsService(httpClient, httpRequestMessageService, authenticator);
Expand All @@ -33,6 +35,7 @@ public GDAXClient(Authenticator authenticator, bool sandBox = false)
ProductsService = new ProductsService(httpClient, httpRequestMessageService, authenticator);
CurrenciesService = new CurrenciesService(httpClient, httpRequestMessageService, authenticator);
FillsService = new FillsService(httpClient, httpRequestMessageService, authenticator);
FundingsService = new FundingsService(httpClient, httpRequestMessageService, authenticator, queryBuilder);
}

public AccountsService AccountsService { get; }
Expand All @@ -52,5 +55,7 @@ public GDAXClient(Authenticator authenticator, bool sandBox = false)
public CurrenciesService CurrenciesService { get; }

public FillsService FillsService { get; }

public FundingsService FundingsService { get; }
}
}
5 changes: 5 additions & 0 deletions GDAXClient/GDAXClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
<Compile Include="Services\Fills\FillsService.cs" />
<Compile Include="Services\Fills\Models\Fill.cs" />
<Compile Include="Services\Fills\Models\Responses\FillResponse.cs" />
<Compile Include="Services\Fundings\FundingsService.cs" />
<Compile Include="Services\Fundings\FundingStatus.cs" />
<Compile Include="Services\Fundings\Models\Funding.cs" />
<Compile Include="Services\Products\Models\Product.cs" />
<Compile Include="Services\Products\Models\ProductStats.cs" />
<Compile Include="Services\Products\Models\ProductTicker.cs" />
Expand Down Expand Up @@ -100,6 +103,8 @@
<Compile Include="Utilities\Extensions\DateExtensions.cs" />
<Compile Include="Utilities\Extensions\ProductTypeExtensions.cs" />
<Compile Include="Utilities\IClock.cs" />
<Compile Include="Utilities\IQueryBuilder.cs" />
<Compile Include="Utilities\QueryBuilder.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
9 changes: 9 additions & 0 deletions GDAXClient/Services/Fundings/FundingStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace GDAXClient.Services.Fundings
{
public enum FundingStatus
{
Outstanding,
Settled,
Rejected
}
}
46 changes: 46 additions & 0 deletions GDAXClient/Services/Fundings/FundingsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using GDAXClient.HttpClient;
using GDAXClient.Services.Accounts;
using GDAXClient.Services.Fundings.Models;
using GDAXClient.Services.HttpRequest;
using GDAXClient.Utilities;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace GDAXClient.Services.Fundings
{
public class FundingsService : AbstractService
{
private readonly IHttpRequestMessageService httpRequestMessageService;

private readonly IHttpClient httpClient;

private readonly IAuthenticator authenticator;

private readonly IQueryBuilder queryBuilder;

public FundingsService(
IHttpClient httpClient,
IHttpRequestMessageService httpRequestMessageService,
IAuthenticator authenticator,
IQueryBuilder queryBuilder)
: base(httpClient, httpRequestMessageService, authenticator)
{
this.httpRequestMessageService = httpRequestMessageService;
this.httpClient = httpClient;
this.authenticator = authenticator;
this.queryBuilder = queryBuilder;
}

public async Task<IList<IList<Funding>>> GetAllFundingsAsync(int limit = 100, FundingStatus? status = null)
{
var queryString = queryBuilder.BuildQuery(
new KeyValuePair<string, string>("limit", limit.ToString()),
new KeyValuePair<string, string>("status", status?.ToString()));

var httpResponseMessage = await SendHttpRequestMessagePagedAsync<Funding>(HttpMethod.Get, authenticator, $"/funding" + queryString);

return httpResponseMessage;
}
}
}
27 changes: 27 additions & 0 deletions GDAXClient/Services/Fundings/Models/Funding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace GDAXClient.Services.Fundings.Models
{
public class Funding
{
public Guid Id { get; set; }

public string Order_id { get; set; }

public string Profile_id { get; set; }

public decimal Amount { get; set; }

public string Status { get; set; }

public DateTime Created_at { get; set; }

public string Currency { get; set; }

public decimal Repaid_amount { get; set; }

public decimal Default_amount { get; set; }

public bool Repaid_default { get; set; }
}
}
9 changes: 9 additions & 0 deletions GDAXClient/Utilities/IQueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace GDAXClient.Utilities
{
public interface IQueryBuilder
{
string BuildQuery(params KeyValuePair<string, string>[] queryParameters);
}
}
23 changes: 23 additions & 0 deletions GDAXClient/Utilities/QueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Text;

namespace GDAXClient.Utilities
{
public class QueryBuilder : IQueryBuilder
{
public string BuildQuery(params KeyValuePair<string, string>[] queryParameters)
{
var queryString = new StringBuilder("?");

foreach(var queryParameter in queryParameters)
{
if(queryParameter.Value != string.Empty)
{
queryString.Append(queryParameter.Key.ToLower() + "=" + queryParameter.Value.ToLower() + "&");
}
}

return queryString.ToString().TrimEnd('&');
}
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ var allAccounts = await gdaxClient.AccountsService.GetAllAccountsAsync();
- GetFillsByOrderIdAsync(orderId, limit) - gets a list of all recent fills by order id (paged response)
- GetFillsByProductIdAsync(productType, limit) - gets a list of all recent fills by product type (paged response)

###### Fundings ######
- GetAllFundingsAsync(limit, fundingStatus) - gets a list of all orders placed with a margin profile that draws funding (paged response)

<h1>Sandbox Support</h1>

<i>Generate your key at https://public.sandbox.gdax.com/settings/api</i>
Expand Down

0 comments on commit de4ef26

Please sign in to comment.