Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CoinGecko.Net/Clients/CoinGeckoRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
namespace CoinGecko.Net.Clients
{
/// <inheritdoc />
public class CoinGeckoRestClient: BaseRestClient<CoinGeckoEnvironment, CoinGeckoCredentials>, ICoinGeckoRestClient
public class CoinGeckoRestClient : BaseRestClient<CoinGeckoEnvironment, CoinGeckoCredentials>, ICoinGeckoRestClient
{
/// <inheritdoc />
public ICoinGeckoRestClientApi Api { get; }

/// <inheritdoc />
public ICoinGeckoRestClientDexApi DexApi { get; }

#region constructor/destructor
/// <summary>
/// Create a new instance of the CoinGeckoClient using provided options
Expand All @@ -37,6 +40,7 @@ public CoinGeckoRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory
Initialize(options.Value);

Api = AddApiClient(new CoinGeckoRestClientApi(this, _logger, httpClient, options.Value));
DexApi = AddApiClient(new CoinGeckoRestClientDexApi(this, _logger, httpClient, options.Value));
}
#endregion

Expand Down
93 changes: 93 additions & 0 deletions CoinGecko.Net/Clients/CoinGeckoRestClientDexApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using CoinGecko.Net.Clients.MessageHandlers;
using CoinGecko.Net.Interfaces;
using CoinGecko.Net.Objects.Models;
using CoinGecko.Net.Objects.Options;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Converters.MessageParsing.DynamicConverters;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Objects.Errors;
using CryptoExchange.Net.SharedApis;
using Microsoft.Extensions.Logging;

namespace CoinGecko.Net.Clients
{
/// <inheritdoc />
internal class CoinGeckoRestClientDexApi : RestApiClient<CoinGeckoEnvironment, CoinGeckoAuthenticationProvider, CoinGeckoCredentials>, ICoinGeckoRestClientDexApi
{
private static readonly RequestDefinitionCache _definitions = new RequestDefinitionCache();
private readonly CoinGeckoRestOptions _options;

protected override IRestMessageHandler MessageHandler { get; } = new CoinGeckoRestMessageHandler(new ErrorMapping([]));

internal CoinGeckoRestClientDexApi(CoinGeckoRestClient baseClient, ILogger logger, HttpClient? httpClient, CoinGeckoRestOptions options)
: base(logger, httpClient, options.Environment.RestApiAddressPublic, options, options.ApiOptions)
{
_options = options;
StandardRequestHeaders = new Dictionary<string, string>
{
{ "User-Agent", "CryptoExchange.Net/" + baseClient.CryptoExchangeLibVersion.ToString() }
};
}

/// <inheritdoc />
protected override IMessageSerializer CreateSerializer() => new SystemTextJsonMessageSerializer(SerializerOptions.WithConverters(CoinGeckoApi.SerializationContext));

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset, TradingMode tradingMode, DateTime? deliverTime = null) => throw new NotImplementedException();

#region Search Pools

/// <inheritdoc />
public Task<WebCallResult<CoinGeckoDexSearchPoolsResponse>> SearchPoolsAsync(string query, string? network = null, IEnumerable<string>? include = null, CancellationToken ct = default)
{
var parameters = new ParameterCollection();
parameters.Add("query", query);
parameters.AddOptional("network", network);
parameters.AddOptionalCommaSeparated("include", include);

var request = _definitions.GetOrCreate(HttpMethod.Get, "/api/v3/onchain/search/pools", CoinGeckoApi.RateLimiter.CoinGecko, 1, false);
return SendAsync<CoinGeckoDexSearchPoolsResponse>(GetBaseAddress(), request, parameters, ct);
}

#endregion

#region Get Networks

/// <inheritdoc />
public Task<WebCallResult<CoinGeckoDexNetworksResponse>> GetDexNetworksAsync(int page = 1, CancellationToken ct = default)
{
var parameters = new ParameterCollection();
parameters.Add("page", page);

var request = _definitions.GetOrCreate(HttpMethod.Get, "/api/v3/onchain/networks", CoinGeckoApi.RateLimiter.CoinGecko, 1, false);
return SendAsync<CoinGeckoDexNetworksResponse>(GetBaseAddress(), request, parameters, ct);
}

#endregion



private string GetBaseAddress()
{
if (AuthenticationProvider != null)
{
if (((CoinGeckoAuthenticationProvider)AuthenticationProvider).IsDemo)
return _options.Environment.RestApiAddressPublic;

return _options.Environment.RestApiAddressPro;
}

return _options.Environment.RestApiAddressPublic;
}

/// <inheritdoc />
protected override CoinGeckoAuthenticationProvider CreateAuthenticationProvider(CoinGeckoCredentials credentials)
=> new CoinGeckoAuthenticationProvider(credentials);
}
}
2 changes: 2 additions & 0 deletions CoinGecko.Net/Converters/CoinGeckoSourceGenerationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ namespace CoinGecko.Net.Converters
[JsonSerializable(typeof(CoinGeckoTrendingNftData[]))]
[JsonSerializable(typeof(CoinGeckoTrendingCategory[]))]
[JsonSerializable(typeof(CoinGeckoTrendingCategoryData[]))]
[JsonSerializable(typeof(CoinGeckoDexNetworksResponse))]
[JsonSerializable(typeof(CoinGeckoDexSearchPoolsResponse))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(int?))]
[JsonSerializable(typeof(int))]
Expand Down
6 changes: 6 additions & 0 deletions CoinGecko.Net/Interfaces/ICoinGeckoRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ public interface ICoinGeckoRestClient : IRestClient<CoinGeckoCredentials>
/// </summary>
/// <see cref="ICoinGeckoRestClientApi"/>
ICoinGeckoRestClientApi Api { get; }

/// <summary>
/// Dex Api endpoints
/// </summary>
/// <see cref="ICoinGeckoRestClientDexApi"/>
ICoinGeckoRestClientDexApi DexApi { get; }
}
}
47 changes: 47 additions & 0 deletions CoinGecko.Net/Interfaces/ICoinGeckoRestClientDexApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using CoinGecko.Net.Objects.Models;
using CryptoExchange.Net.Interfaces.Clients;
using CryptoExchange.Net.Objects;

namespace CoinGecko.Net.Interfaces
{
/// <summary>
/// CoinGecko API endpoints
/// </summary>
public interface ICoinGeckoRestClientDexApi : IRestApiClient<CoinGeckoCredentials>
{
/// <summary>
/// Get dex networks
/// <para>
/// Docs:<br />
/// <a href="https://docs.coingecko.com/reference/networks-list" /><br />
/// Endpoint:<br />
/// GET /api/v3/onchain/networks
/// </para>
/// </summary>
/// <param name="page">Page number</param>
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
Task<WebCallResult<CoinGeckoDexNetworksResponse>> GetDexNetworksAsync(int page = 1, CancellationToken ct = default);

/// <summary>
/// Search pools
/// <para>
/// Docs:<br />
/// <a href="https://docs.coingecko.com/reference/search-pools" /><br />
/// Endpoint:<br />
/// GET /api/v3/onchain/search/pools
/// </para>
/// </summary>
/// <param name="query">Search query, can be pool contract address, token name, token symbol, or token contract address</param>
/// <param name="network">The network id</param>
/// <param name="include">Attributes to include. Available values: base_token, quote_token, dex</param>
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
Task<WebCallResult<CoinGeckoDexSearchPoolsResponse>> SearchPoolsAsync(string query, string? network = null, IEnumerable<string>? include = null, CancellationToken ct = default);


}
}
49 changes: 49 additions & 0 deletions CoinGecko.Net/Objects/Models/CoinGeckoDexItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Text.Json.Serialization;

namespace CoinGecko.Net.Objects.Models
{
/// <summary>
/// Dex item with attributes and relationships
/// </summary>
[SerializationModel]
public record CoinGeckoDexItem<T, TRelationShip> : CoinGeckoDexItem
{
/// <summary>
/// ["<c>relationships</c>"] Relationships
/// </summary>
[JsonPropertyName("relationships")]
public TRelationShip? Details { get; set; }
}

/// <summary>
/// Dex item with attributes
/// </summary>
[SerializationModel]
public record CoinGeckoDexItem<T> : CoinGeckoDexItem
{
/// <summary>
/// ["<c>attributes</c>"] Attributes
/// </summary>
[JsonPropertyName("attributes")]
public T? Details { get; set; }
}

/// <summary>
/// Dex item
/// </summary>
[SerializationModel]
public record CoinGeckoDexItem
{
/// <summary>
/// ["<c>id</c>"] Id
/// </summary>
[JsonPropertyName("id")]
public string Id { get; set; } = string.Empty;

/// <summary>
/// ["<c>type</c>"] Type
/// </summary>
[JsonPropertyName("type")]
public string Type { get; set; } = string.Empty;
}
}
9 changes: 9 additions & 0 deletions CoinGecko.Net/Objects/Models/CoinGeckoDexNetwork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CoinGecko.Net.Objects.Models
{
/// <summary>
/// Dex network
/// </summary>
[SerializationModel]
public record CoinGeckoDexNetwork : CoinGeckoDexItem<CoinGeckoDexNetworkDetail>
{ }
}
23 changes: 23 additions & 0 deletions CoinGecko.Net/Objects/Models/CoinGeckoDexNetworkDetail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json.Serialization;

namespace CoinGecko.Net.Objects.Models
{
/// <summary>
/// Dex network details
/// </summary>
[SerializationModel]
public record CoinGeckoDexNetworkDetail
{
/// <summary>
/// ["<c>name</c>"] Name
/// </summary>
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;

/// <summary>
/// ["<c>coingecko_asset_platform_id</c>"] CoinGecko asset platform id
/// </summary>
[JsonPropertyName("coingecko_asset_platform_id")]
public string? CoinGeckoAssetPlatformId { get; set; }
}
}
9 changes: 9 additions & 0 deletions CoinGecko.Net/Objects/Models/CoinGeckoDexNetworksResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CoinGecko.Net.Objects.Models
{
/// <summary>
/// Dex networks response
/// </summary>
[SerializationModel]
public record CoinGeckoDexNetworksResponse : CoinGeckoDexResponsePaged<CoinGeckoDexNetwork>
{ }
}
35 changes: 35 additions & 0 deletions CoinGecko.Net/Objects/Models/CoinGeckoDexPageLinks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Text.Json.Serialization;

namespace CoinGecko.Net.Objects.Models
{
/// <summary>
/// Dex pagination links
/// </summary>
[SerializationModel]
public record CoinGeckoDexPageLinks
{
/// <summary>
/// ["<c>first</c>"] First page link
/// </summary>
[JsonPropertyName("first")]
public string First { get; set; } = string.Empty;

/// <summary>
/// ["<c>last</c>"] Last page link
/// </summary>
[JsonPropertyName("last")]
public string Last { get; set; } = string.Empty;

/// <summary>
/// ["<c>prev</c>"] Previous page link
/// </summary>
[JsonPropertyName("prev")]
public string? Prev { get; set; }

/// <summary>
/// ["<c>next</c>"] Next page link
/// </summary>
[JsonPropertyName("next")]
public string? Next { get; set; }
}
}
9 changes: 9 additions & 0 deletions CoinGecko.Net/Objects/Models/CoinGeckoDexPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CoinGecko.Net.Objects.Models
{
/// <summary>
/// Dex pool
/// </summary>
[SerializationModel]
public record CoinGeckoDexPool : CoinGeckoDexItem<CoinGeckoDexPoolDetails, CoinGeckoDexPoolRelationShip>
{ }
}
Loading
Loading