-
Notifications
You must be signed in to change notification settings - Fork 10
/
Exchange.cs
246 lines (213 loc) · 7.71 KB
/
Exchange.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* Copyright (C) 2020-2024 b1f6c1c4
*
* This file is part of ProfessionalAccounting.
*
* ProfessionalAccounting is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3.
*
* ProfessionalAccounting is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ProfessionalAccounting. If not, see
* <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
using AccountingServer.Entities.Util;
using Newtonsoft.Json.Linq;
namespace AccountingServer.BLL.Util;
internal static class ExchangeFactory
{
public static IHistoricalExchange HistoricalInstance { get; set; } = new FixerIoExchange();
public static IExchange Instance { get; set; } =
new CoinMarketCapExchange { Successor = new FixerIoExchange() };
}
/// <summary>
/// 汇率查询
/// </summary>
internal interface IExchange
{
/// <summary>
/// 汇率查询
/// </summary>
/// <example>
/// <c>IExchange.Query("USD", "CNY") == 6.8</c>
/// <c>IExchange.Query("CNY", "USD") == 0.147</c>
/// </example>
/// <param name="from">购汇币种</param>
/// <param name="to">结汇币种</param>
/// <returns>汇率</returns>
ValueTask<double> Query(string from, string to);
}
/// <summary>
/// 历史汇率查询
/// </summary>
public interface IHistoricalExchange
{
/// <summary>
/// 汇率查询
/// </summary>
/// <param name="date">日期</param>
/// <param name="from">购汇币种</param>
/// <param name="to">结汇币种</param>
/// <returns>汇率</returns>
ValueTask<double> Query(DateTime? date, string from, string to);
}
[Serializable]
[XmlRoot("Exchange")]
public class ExchangeInfo
{
[XmlElement("fixer_io")] public List<string> FixerAccessKey;
[XmlElement("coin_mkt")] public List<string> CoinAccessKey;
[XmlArray("currencies")]
[XmlArrayItem("conventional", typeof(ConventionalCurrency))]
[XmlArrayItem("crypto", typeof(CryptoCurrency))]
public List<Currency> Currencies;
}
[Serializable]
public abstract class Currency { }
[Serializable]
public class ConventionalCurrency : Currency
{
[XmlAttribute("id")]
public int CoinMarketCapId { get; set; }
[XmlText]
public string Symbol { get; set; }
}
[Serializable]
public class CryptoCurrency : Currency
{
[XmlAttribute("id")]
public int CoinMarketCapId { get; set; }
[XmlText]
public string Symbol { get; set; }
}
internal class RoundRobinApiKeys
{
private long m_CurrentId;
private long GetCurrentId() => Interlocked.Increment(ref m_CurrentId);
public async ValueTask<TOut> Execute<TOut>(IList<string> keys, Func<string, ValueTask<TOut?>> func)
where TOut : struct
{
if (keys.Count == 0)
throw new UnauthorizedAccessException("No access key found");
var count = keys.Count;
var st = (int)(GetCurrentId() % count);
for (var i = 0; i < count; i++)
if (await func(keys[(i + st) % count]) is var res && res.HasValue)
return res.Value;
throw new UnauthorizedAccessException("No more access keys");
}
}
internal abstract class ExchangeApi : IExchange
{
/// <summary>
/// 汇率API配置
/// </summary>
static ExchangeApi()
=> Cfg.RegisterType<ExchangeInfo>("Exchange");
internal ExchangeApi Successor { private get; init; }
public ValueTask<double> Query(string from, string to)
=> Query(from, to, Enumerable.Empty<Exception>());
private ValueTask<double> Query(string from, string to, IEnumerable<Exception> err)
{
try
{
return Invoke(from, to);
}
catch (Exception e)
{
var ne = err.Prepend(e);
if (Successor != null)
return Successor.Query(from, to, ne);
throw new AggregateException(ne);
}
}
protected abstract ValueTask<double> Invoke(string from, string to);
}
/// <summary>
/// 利用fixer.io查询汇率
/// </summary>
internal class FixerIoExchange : ExchangeApi, IHistoricalExchange
{
private readonly RoundRobinApiKeys m_ApiKeys = new();
public ValueTask<double> Query(DateTime? date, string from, string to)
{
if (!date.HasValue)
return Invoke(from, to);
return m_ApiKeys.Execute(Cfg.Get<ExchangeInfo>().FixerAccessKey,
key => PartialInvoke(from, to, key, date!.Value.ToString("yyyy-MM-dd")));
}
protected override ValueTask<double> Invoke(string from, string to)
=> m_ApiKeys.Execute(Cfg.Get<ExchangeInfo>().FixerAccessKey,
key => PartialInvoke(from, to, key, "latest"));
private static async ValueTask<double?> PartialInvoke(string from, string to, string key, string endpoint)
{
var client = new HttpClient();
var response =
await client.GetAsync($"http://data.fixer.io/api/{endpoint}?access_key={key}&symbols={from},{to}");
response.EnsureSuccessStatusCode();
var json = JObject.Parse(await response.Content.ReadAsStringAsync());
if (json["success"]!.Value<bool>())
return json["rates"]![to]!.Value<double>() / json["rates"]![from]!.Value<double>();
return null;
}
}
/// <summary>
/// 利用coinmartketcap.com查询实时汇率
/// </summary>
internal class CoinMarketCapExchange : ExchangeApi
{
private readonly RoundRobinApiKeys m_ApiKeys = new();
protected override ValueTask<double> Invoke(string from, string to)
{
int? fromId = null, toId = null;
var isCrypto = false;
foreach (var cur in Cfg.Get<ExchangeInfo>().Currencies)
switch (cur)
{
case CryptoCurrency c:
if (c.Symbol == from)
{
fromId = c.CoinMarketCapId;
isCrypto = true;
}
if (c.Symbol == to)
{
toId = c.CoinMarketCapId;
isCrypto = true;
}
break;
case ConventionalCurrency c:
if (c.Symbol == from)
fromId = c.CoinMarketCapId;
if (c.Symbol == to)
toId = c.CoinMarketCapId;
break;
}
if (!fromId.HasValue || !toId.HasValue || !isCrypto)
throw new InvalidOperationException("Not a cryptocurrency");
return m_ApiKeys.Execute(Cfg.Get<ExchangeInfo>().CoinAccessKey,
key => PartialInvoke(fromId.Value, toId.Value, key));
}
private static async ValueTask<double?> PartialInvoke(int fromId, int toId, string key)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-CMC_PRO_API_KEY", key);
client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
var response = await client.GetAsync(
$"https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id={fromId}&convert_id={toId}");
var json = JObject.Parse(await response.Content.ReadAsStringAsync());
return json["data"]?[fromId.ToString()]?["quote"]?[toId.ToString()]?["price"]?.Value<double>();
}
}