diff --git a/Examples/Examples.sln b/Examples/Examples.sln new file mode 100644 index 0000000..6422812 --- /dev/null +++ b/Examples/Examples.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mexc.Examples.Api", "Mexc.Examples.Api\Mexc.Examples.Api.csproj", "{ECA139DF-CA98-4E98-89DA-C594FEBC0865}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mexc.Examples.Console", "Mexc.Examples.Console\Mexc.Examples.Console.csproj", "{FD4F95C8-D9B7-4F81-9245-4CE667DFD421}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {ECA139DF-CA98-4E98-89DA-C594FEBC0865}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ECA139DF-CA98-4E98-89DA-C594FEBC0865}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ECA139DF-CA98-4E98-89DA-C594FEBC0865}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ECA139DF-CA98-4E98-89DA-C594FEBC0865}.Release|Any CPU.Build.0 = Release|Any CPU + {FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {84A4E6CE-9D3A-43FF-97B1-D91CE93B7E8F} + EndGlobalSection +EndGlobal diff --git a/Examples/Mexc.Examples.Api/Mexc.Examples.Api.csproj b/Examples/Mexc.Examples.Api/Mexc.Examples.Api.csproj new file mode 100644 index 0000000..2e9d208 --- /dev/null +++ b/Examples/Mexc.Examples.Api/Mexc.Examples.Api.csproj @@ -0,0 +1,16 @@ + + + + net7.0 + enable + enable + true + + + + + + + + + diff --git a/Examples/Mexc.Examples.Api/Program.cs b/Examples/Mexc.Examples.Api/Program.cs new file mode 100644 index 0000000..729849f --- /dev/null +++ b/Examples/Mexc.Examples.Api/Program.cs @@ -0,0 +1,45 @@ +using Mexc.Net.Interfaces.Clients; +using CryptoExchange.Net.Authentication; +using Microsoft.AspNetCore.Mvc; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +// Add the Mexc services +builder.Services.AddMexc(); + +// OR to provide API credentials for accessing private endpoints, or setting other options: +/* +builder.Services.AddMexc(restOptions => +{ + restOptions.ApiCredentials = new ApiCredentials("", ""); + restOptions.RequestTimeout = TimeSpan.FromSeconds(5); +}, socketOptions => +{ + socketOptions.ApiCredentials = new ApiCredentials("", ""); +}); +*/ + +var app = builder.Build(); +app.UseSwagger(); +app.UseSwaggerUI(); +app.UseHttpsRedirection(); + +// Map the endpoints and inject the Mexc rest client +app.MapGet("/{Symbol}", async ([FromServices] IMexcRestClient client, string symbol) => +{ + var result = await client.SpotApi.ExchangeData.GetTickerAsync(symbol); + return (object)(result.Success ? result.Data : result.Error!); +}) +.WithOpenApi(); + +app.MapGet("/Balances", async ([FromServices] IMexcRestClient client) => +{ + var result = await client.SpotApi.Account.GetAccountInfoAsync(); + return (object)(result.Success ? result.Data : result.Error!); +}) +.WithOpenApi(); + +app.Run(); \ No newline at end of file diff --git a/Examples/Mexc.Examples.Api/Properties/launchSettings.json b/Examples/Mexc.Examples.Api/Properties/launchSettings.json new file mode 100644 index 0000000..066e9d1 --- /dev/null +++ b/Examples/Mexc.Examples.Api/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:23442", + "sslPort": 44376 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5114", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7266;http://localhost:5114", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Examples/Mexc.Examples.Api/appsettings.Development.json b/Examples/Mexc.Examples.Api/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Examples/Mexc.Examples.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Examples/Mexc.Examples.Api/appsettings.json b/Examples/Mexc.Examples.Api/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Examples/Mexc.Examples.Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Examples/Mexc.Examples.Console/Mexc.Examples.Console.csproj b/Examples/Mexc.Examples.Console/Mexc.Examples.Console.csproj new file mode 100644 index 0000000..11d2955 --- /dev/null +++ b/Examples/Mexc.Examples.Console/Mexc.Examples.Console.csproj @@ -0,0 +1,14 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + diff --git a/Examples/Mexc.Examples.Console/Program.cs b/Examples/Mexc.Examples.Console/Program.cs new file mode 100644 index 0000000..3e45543 --- /dev/null +++ b/Examples/Mexc.Examples.Console/Program.cs @@ -0,0 +1,25 @@ +using Mexc.Net.Clients; +using CryptoExchange.Net.Objects; +using Microsoft.Extensions.Logging; + +// REST +var restClient = new MexcRestClient(); +var ticker = await restClient.SpotApi.ExchangeData.GetTickerAsync("ETHUSDT"); +Console.WriteLine($"Rest client ticker price for ETHUSDT: {ticker.Data.LastPrice}"); + +Console.WriteLine(); +Console.WriteLine("Press enter to start websocket subscription"); +Console.ReadLine(); + +// Websocket +// Optional, manually add logging +var logFactory = new LoggerFactory(); +logFactory.AddProvider(new TraceLoggerProvider()); + +var socketClient = new MexcSocketClient(logFactory); +var subscription = await socketClient.SpotApi.SubscribeToMiniTickerUpdatesAsync("ETHUSDT", update => +{ + Console.WriteLine($"Websocket client ticker price for ETHUSDT: {update.Data.LastPrice}"); +}); + +Console.ReadLine(); diff --git a/Examples/README.md b/Examples/README.md new file mode 100644 index 0000000..e35542e --- /dev/null +++ b/Examples/README.md @@ -0,0 +1,7 @@ +# Examples + +### Mexc.Examples.Api +A minimal API showing how to integrate Mexc.Net in a web API project + +### Mexc.Examples.Console +A simple console client demonstrating basic usage \ No newline at end of file