This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathExchangeRateAPI.cs
91 lines (79 loc) · 3.72 KB
/
ExchangeRateAPI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using Newtonsoft.Json;
using NiceHashMiner.Configs;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace NiceHashMiner {
class ExchangeRateAPI {
public class Result {
public Object algorithms { get; set; }
public Object servers { get; set; }
public Object idealratios { get; set; }
public List<Dictionary<string, string>> exchanges { get; set; }
public Dictionary<string, double> exchanges_fiat { get; set; }
}
public class ExchangeRateJSON {
public Result result { get; set; }
public string method { get; set; }
}
const string apiUrl = "https://api.nicehash.com/api?method=nicehash.service.info";
private static Dictionary<string, double> exchanges_fiat = null;
private static double USD_BTC_rate = -1;
public static string ActiveDisplayCurrency = "USD";
private static bool ConverterActive {
get { return ConfigManager.GeneralConfig.DisplayCurrency != "USD"; }
}
public static double ConvertToActiveCurrency(double amount) {
if (!ConverterActive) {
return amount;
}
// if we are still null after an update something went wrong. just use USD hopefully itll update next tick
if (exchanges_fiat == null || ActiveDisplayCurrency == "USD") {
Helpers.ConsolePrint("CurrencyConverter", "Unable to retrieve update, Falling back to USD");
return amount;
}
//Helpers.ConsolePrint("CurrencyConverter", "Current Currency: " + ConfigManager.Instance.GeneralConfig.DisplayCurrency);
double usdExchangeRate = 1.0;
if (exchanges_fiat.TryGetValue(ActiveDisplayCurrency, out usdExchangeRate))
return amount * usdExchangeRate;
else {
Helpers.ConsolePrint("CurrencyConverter", "Unknown Currency Tag: " + ActiveDisplayCurrency + " falling back to USD rates");
ActiveDisplayCurrency = "USD";
return amount;
}
}
public static double GetUSDExchangeRate() {
if (USD_BTC_rate > 0) {
return USD_BTC_rate;
}
return 0.0;
}
public static void UpdateAPI(string worker) {
string resp = NiceHashStats.GetNiceHashAPIData(apiUrl, worker);
if (resp != null) {
try {
var LastResponse = JsonConvert.DeserializeObject<ExchangeRateJSON>(resp, Globals.JsonSettings);
// set that we have a response
if (LastResponse != null) {
Result last_result = LastResponse.result;
ActiveDisplayCurrency = ConfigManager.GeneralConfig.DisplayCurrency;
exchanges_fiat = last_result.exchanges_fiat;
// ActiveDisplayCurrency = "USD";
// check if currency avaliable and fill currency list
foreach (var pair in last_result.exchanges) {
if (pair.ContainsKey("USD") && pair.ContainsKey("coin") && pair["coin"] == "BTC" && pair["USD"] != null) {
USD_BTC_rate = Helpers.ParseDouble(pair["USD"]);
break;
}
}
}
} catch(Exception e) {
Helpers.ConsolePrint("ExchangeRateAPI", "UpdateAPI got Exception: " + e.Message);
}
} else {
Helpers.ConsolePrint("ExchangeRateAPI", "UpdateAPI got NULL");
}
}
}
}