Commit 94650068 authored by Clint.Network's avatar Clint.Network

Finishing Automatic Conversion from the Browser Currency

parent 82e71439
using System; using System;
using System.Globalization;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using NBitcoin; using NBitcoin;
using NBitcoin.Networks; using NBitcoin.Networks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp; using RestSharp;
using Stratis.Guru.Models; using Stratis.Guru.Models;
using Stratis.Guru.Settings; using Stratis.Guru.Settings;
...@@ -27,22 +30,46 @@ namespace Stratis.Guru.Controllers ...@@ -27,22 +30,46 @@ namespace Stratis.Guru.Controllers
[HttpGet] [HttpGet]
[Route("price")] [Route("price")]
public ActionResult<object> Price(bool stringify, double amount = 1) public ActionResult<object> Price(bool notApi = false, double amount = 1)
{ {
try try
{ {
dynamic coinmarketcap = JsonConvert.DeserializeObject(_memoryCache.Get("Coinmarketcap").ToString()); dynamic coinmarketcap = JsonConvert.DeserializeObject(_memoryCache.Get("Coinmarketcap").ToString());
if (stringify) if (notApi)
{ {
var rqf = Request.HttpContext.Features.Get<IRequestCultureFeature>();
double displayPrice = 0;
if (rqf.RequestCulture.UICulture.ThreeLetterISOLanguageName.Equals("eng"))
{
displayPrice = coinmarketcap.data.quotes.USD.price;
}
else
{
dynamic fixerApiResponse = JsonConvert.DeserializeObject(_memoryCache.Get("Fixer").ToString());
var dollarRate = fixerApiResponse.rates.USD;
try
{
var regionInfo = new RegionInfo(rqf.RequestCulture.UICulture.Name.ToUpper());
var browserCurrencyRate = (double) ((JObject) fixerApiResponse.rates)[regionInfo.ISOCurrencySymbol];
displayPrice = 1 / (double) dollarRate * (double) coinmarketcap.data.quotes.USD.price * browserCurrencyRate;
}
catch
{
// ignored
}
}
return new TickerApi return new TickerApi
{ {
UsdPrice = (coinmarketcap.data.quotes.USD.price * amount).ToString("C"), UsdPrice = (displayPrice * amount).ToString("C"),
Last24Change = (coinmarketcap.data.quotes.USD.percent_change_24h / 100).ToString("P2") Last24Change = (coinmarketcap.data.quotes.USD.percent_change_24h / 100).ToString("P2")
}; };
} }
return new Ticker return new Ticker
{ {
UsdPrice = coinmarketcap.data.quotes.USD.price * amount, DisplayPrice = coinmarketcap.data.quotes.USD.price * amount,
Last24Change = coinmarketcap.data.quotes.USD.percent_change_24h / 100 Last24Change = coinmarketcap.data.quotes.USD.percent_change_24h / 100
}; };
} }
......
...@@ -4,13 +4,17 @@ using System.Data; ...@@ -4,13 +4,17 @@ using System.Data;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using NBitcoin; using NBitcoin;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using QRCoder; using QRCoder;
using Stratis.Guru.Models; using Stratis.Guru.Models;
using Stratis.Guru.Modules; using Stratis.Guru.Modules;
...@@ -30,11 +34,38 @@ namespace Stratis.Guru.Controllers ...@@ -30,11 +34,38 @@ namespace Stratis.Guru.Controllers
public IActionResult Index() public IActionResult Index()
{ {
var rqf = Request.HttpContext.Features.Get<IRequestCultureFeature>();
double displayPrice = 0;
double last24Change = 0;
dynamic coinmarketcap = JsonConvert.DeserializeObject(_memoryCache.Get("Coinmarketcap").ToString()); dynamic coinmarketcap = JsonConvert.DeserializeObject(_memoryCache.Get("Coinmarketcap").ToString());
last24Change = coinmarketcap.data.quotes.USD.percent_change_24h / 100;
if (rqf.RequestCulture.UICulture.ThreeLetterISOLanguageName.Equals("eng"))
{
displayPrice = coinmarketcap.data.quotes.USD.price;
}
else
{
dynamic fixerApiResponse = JsonConvert.DeserializeObject(_memoryCache.Get("Fixer").ToString());
var dollarRate = fixerApiResponse.rates.USD;
try
{
var regionInfo = new RegionInfo(rqf.RequestCulture.UICulture.Name.ToUpper());
var browserCurrencyRate = (double) ((JObject) fixerApiResponse.rates)[regionInfo.ISOCurrencySymbol];
displayPrice = 1 / (double) dollarRate * (double) coinmarketcap.data.quotes.USD.price * browserCurrencyRate;
}
catch
{
// ignored
}
}
return View(new Ticker return View(new Ticker
{ {
UsdPrice = coinmarketcap.data.quotes.USD.price, DisplayPrice = displayPrice,
Last24Change = coinmarketcap.data.quotes.USD.percent_change_24h/100 Last24Change = last24Change
}); });
} }
......
...@@ -2,7 +2,7 @@ namespace Stratis.Guru.Models ...@@ -2,7 +2,7 @@ namespace Stratis.Guru.Models
{ {
public class Ticker public class Ticker
{ {
public double UsdPrice { get; set; } public double DisplayPrice { get; set; }
public double Last24Change { get; set; } public double Last24Change { get; set; }
} }
} }
\ No newline at end of file
...@@ -2,6 +2,7 @@ using System; ...@@ -2,6 +2,7 @@ using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
...@@ -12,14 +13,14 @@ namespace Stratis.Guru.Services ...@@ -12,14 +13,14 @@ namespace Stratis.Guru.Services
{ {
public class FixerService : IHostedService, IDisposable public class FixerService : IHostedService, IDisposable
{ {
private FixerApiSettings _options;
private IDistributedCache _distributedCache;
private Timer _timer; private Timer _timer;
private readonly FixerApiSettings _options;
private readonly IMemoryCache _memoryCache;
public FixerService(IOptions<FixerApiSettings> options, IDistributedCache distributedCache) public FixerService(IOptions<FixerApiSettings> options, IMemoryCache memoryCache)
{ {
_options = options.Value; _options = options.Value;
_distributedCache = distributedCache; _memoryCache = memoryCache;
} }
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
...@@ -34,7 +35,7 @@ namespace Stratis.Guru.Services ...@@ -34,7 +35,7 @@ namespace Stratis.Guru.Services
var rq = new RestRequest(Method.GET); var rq = new RestRequest(Method.GET);
rs.ExecuteAsync(rq, delegate(IRestResponse response) rs.ExecuteAsync(rq, delegate(IRestResponse response)
{ {
_distributedCache.SetString("Fixer", response.Content); _memoryCache.Set("Fixer", response.Content);
}); });
} }
......
...@@ -7,8 +7,8 @@ using System.Threading; ...@@ -7,8 +7,8 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
...@@ -18,32 +18,31 @@ using Stratis.Guru.Models; ...@@ -18,32 +18,31 @@ using Stratis.Guru.Models;
using Stratis.Guru.Modules; using Stratis.Guru.Modules;
using Stratis.Guru.Services; using Stratis.Guru.Services;
using Stratis.Guru.Settings; using Stratis.Guru.Settings;
using static Microsoft.AspNetCore.Http.SameSiteMode;
namespace Stratis.Guru namespace Stratis.Guru
{ {
public class Startup public class Startup
{ {
public IConfiguration Configuration { get; } private IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) public Startup(IConfiguration configuration)
{ {
Configuration = configuration; Configuration = configuration;
} }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.Configure<CookiePolicyOptions>(options => services.Configure<CookiePolicyOptions>(options =>
{ {
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true; options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None; options.MinimumSameSitePolicy = None;
}); });
services.Configure<NakoApiSettings>(Configuration.GetSection("NakoApi")); services.Configure<NakoApiSettings>(Configuration.GetSection("NakoApi"));
services.Configure<FixerApiSettings>(Configuration.GetSection("FixerApi")); services.Configure<FixerApiSettings>(Configuration.GetSection("FixerApi"));
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); /*Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");*/
services.AddMemoryCache(); services.AddMemoryCache();
...@@ -59,7 +58,6 @@ namespace Stratis.Guru ...@@ -59,7 +58,6 @@ namespace Stratis.Guru
services.AddSignalR(); services.AddSignalR();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ {
if (env.IsDevelopment()) if (env.IsDevelopment())
...@@ -94,6 +92,23 @@ namespace Stratis.Guru ...@@ -94,6 +92,23 @@ namespace Stratis.Guru
DefaultFilesOptions = { DefaultFileNames = {"index.html"}} DefaultFilesOptions = { DefaultFileNames = {"index.html"}}
}); });
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("fr"),
new CultureInfo("ru"),
new CultureInfo("it"),
new CultureInfo("de"),
new CultureInfo("cn")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
app.UseSignalR(routes => app.UseSignalR(routes =>
{ {
routes.MapHub<UpdateHub>("/update"); routes.MapHub<UpdateHub>("/update");
......
@model Ticker @using System.Globalization
@model Ticker
@{ @{
ViewBag.Title = "Online Stratis ($STRAT) price ticker"; ViewBag.Title = "Online Stratis ($STRAT) price ticker";
} }
...@@ -12,8 +13,8 @@ ...@@ -12,8 +13,8 @@
<div class="d-inline-block align-middle"> <div class="d-inline-block align-middle">
<span class="click-edit" spellcheck="false">1</span> STRAT <span class="d-none d-lg-inline-block">=</span> <span class="click-edit" spellcheck="false">1</span> STRAT <span class="d-none d-lg-inline-block">=</span>
</div> </div>
<span class="align-middle display-1 font-weight-bold" id="amount">@(Model.UsdPrice.ToString("C2"))</span> <span class="align-middle display-1 font-weight-bold" id="amount">@(Model.DisplayPrice.ToString("C2"))</span>
<span id="lastchange" class="d-block d-lg-inline-block change-@(Model.Last24Change > 0 ? "success":"danger") font-weight-bold"><sup> <span class="d-none d-lg-inline-block">@((Model.Last24Change > 0 ? "+":""))</span> @(Model.Last24Change.ToString("P2"))</sup></span> <span id="lastchange" class="d-block d-lg-inline-block change-@(Model.Last24Change > 0 ? "success":"danger") font-weight-bold"><sup> <span class="d-none d-lg-inline-block">@((Model.Last24Change > 0 ? "+":""))</span> <span class="inner">@(Model.Last24Change.ToString("P2"))</span></sup></span>
</h1> </h1>
<a asp-controller="BlockExplorer" asp-action="Index" class="btn-secondary-box"><i class="fa fa-cube"></i> Go to Block Explorer</a> <a asp-controller="BlockExplorer" asp-action="Index" class="btn-secondary-box"><i class="fa fa-cube"></i> Go to Block Explorer</a>
</div> </div>
...@@ -26,13 +27,13 @@ ...@@ -26,13 +27,13 @@
<script src="~/npm/@@aspnet/signalr/dist/browser/signalr.min.js"></script> <script src="~/npm/@@aspnet/signalr/dist/browser/signalr.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
var stratisPrice = @Model.UsdPrice; var stratisPrice = @Model.DisplayPrice.ToString("N", new CultureInfo("en-US"));
var stratisAmount = 1; var stratisAmount = 1;
function UpdateTicker() { function UpdateTicker() {
NProgress.start(); NProgress.start();
$.getJSON("/api/price?stringify=true&amount=" + stratisAmount, function(e) { $.getJSON("/api/price?notApi=true&amount=" + stratisAmount, function(e) {
$("#amount").text(e.usdPrice); $("#amount").text(e.usdPrice);
$("#lastchange").text(e.last24Change); $("#lastchange").find(".inner").text(e.last24Change);
NProgress.done(); NProgress.done();
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment