From f62f2bbe46269e142acf3672d1416b1a365d703d Mon Sep 17 00:00:00 2001 From: Light Date: Sat, 8 Oct 2022 13:41:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E8=B4=A7=E5=B8=81=EF=BC=8Cbug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BgServices/OrderCheckERC20Service.cs | 2 -- .../BgServices/OrderCheckETHService.cs | 2 -- .../BgServices/OrderCheckTRC20Service.cs | 2 -- .../BgServices/OrderCheckTRXService.cs | 2 -- .../BgServices/OrderExpiredService.cs | 4 +-- src/TokenPay/BgServices/OrderNotifyService.cs | 2 -- src/TokenPay/BgServices/UpdateRateService.cs | 29 ++++++++++--------- src/TokenPay/Controllers/HomeController.cs | 22 +++++++++----- src/TokenPay/Domains/TokenRate.cs | 10 +++++-- src/TokenPay/appsettings.Example.json | 1 + 10 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/TokenPay/BgServices/OrderCheckERC20Service.cs b/src/TokenPay/BgServices/OrderCheckERC20Service.cs index 4de6597..590d325 100644 --- a/src/TokenPay/BgServices/OrderCheckERC20Service.cs +++ b/src/TokenPay/BgServices/OrderCheckERC20Service.cs @@ -79,8 +79,6 @@ private async Task ERC20(IBaseRepository _repository, Currency Curr .Where(x => x.Currency == Currency) .Distinct() .ToListAsync(x => x.ToAddress); - if (Address.Count > 0) - _logger.LogInformation($"{jobName},待检测地址数:{{c}}", Address.Count); var BaseUrl = "https://api.etherscan.io"; if (!env.IsProduction()) { diff --git a/src/TokenPay/BgServices/OrderCheckETHService.cs b/src/TokenPay/BgServices/OrderCheckETHService.cs index 4c3c828..be14938 100644 --- a/src/TokenPay/BgServices/OrderCheckETHService.cs +++ b/src/TokenPay/BgServices/OrderCheckETHService.cs @@ -46,8 +46,6 @@ protected override async Task ExecuteAsync() .Where(x => x.Currency == Currency.ETH) .Distinct() .ToListAsync(x => x.ToAddress); - if (Address.Count > 0) - _logger.LogInformation($"{jobName},待检测地址数:{{c}}", Address.Count); var BaseUrl = "https://api.etherscan.io"; if (!env.IsProduction()) { diff --git a/src/TokenPay/BgServices/OrderCheckTRC20Service.cs b/src/TokenPay/BgServices/OrderCheckTRC20Service.cs index 48cb74d..acf73aa 100644 --- a/src/TokenPay/BgServices/OrderCheckTRC20Service.cs +++ b/src/TokenPay/BgServices/OrderCheckTRC20Service.cs @@ -39,8 +39,6 @@ protected override async Task ExecuteAsync() .Where(x => x.Currency == Currency.USDT_TRC20) .Distinct() .ToListAsync(x => x.ToAddress); - if (Address.Count > 0) - _logger.LogInformation($"{jobName},待检测地址数:{{c}}", Address.Count); var ContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; var BaseUrl = "https://api.trongrid.io"; if (!env.IsProduction()) diff --git a/src/TokenPay/BgServices/OrderCheckTRXService.cs b/src/TokenPay/BgServices/OrderCheckTRXService.cs index b49a8f3..557435a 100644 --- a/src/TokenPay/BgServices/OrderCheckTRXService.cs +++ b/src/TokenPay/BgServices/OrderCheckTRXService.cs @@ -39,8 +39,6 @@ protected override async Task ExecuteAsync() .Where(x => x.Currency == Currency.TRX) .Distinct() .ToListAsync(x => x.ToAddress); - if (Address.Count > 0) - _logger.LogInformation($"{jobName},待检测地址数:{{c}}", Address.Count); var BaseUrl = "https://api.trongrid.io"; if (!env.IsProduction()) { diff --git a/src/TokenPay/BgServices/OrderExpiredService.cs b/src/TokenPay/BgServices/OrderExpiredService.cs index 3c36ddf..a5674f9 100644 --- a/src/TokenPay/BgServices/OrderExpiredService.cs +++ b/src/TokenPay/BgServices/OrderExpiredService.cs @@ -1,4 +1,4 @@ -using FreeSql; +using FreeSql; using TokenPay.Domains; namespace TokenPay.BgServices @@ -26,8 +26,6 @@ protected override async Task ExecuteAsync() var ExpireTime = _configuration.GetValue("ExpireTime", 10 * 60); var ExpireDateTime = DateTime.Now.AddSeconds(-1 * ExpireTime); var ExpiredOrders = await _repository.Where(x => x.CreateTime < ExpireDateTime && x.Status == OrderStatus.Pending).ToListAsync(); - if (ExpiredOrders.Count > 0) - _logger.LogInformation("过期订单检测,订单数:{c}", ExpiredOrders.Count); foreach (var order in ExpiredOrders) { _logger.LogInformation("订单[{c}]过期了!", order.Id); diff --git a/src/TokenPay/BgServices/OrderNotifyService.cs b/src/TokenPay/BgServices/OrderNotifyService.cs index d782ba9..435a01e 100644 --- a/src/TokenPay/BgServices/OrderNotifyService.cs +++ b/src/TokenPay/BgServices/OrderNotifyService.cs @@ -50,8 +50,6 @@ protected override async Task ExecuteAsync() .Where(x => x.LastNotifyTime == null || x.LastNotifyTime < start) //未通知过,或通知失败5分钟后的 .Where(x => x.NotifyUrl.StartsWith("http")) .ToListAsync(); - if (Orders.Count > 0) - _logger.LogInformation("待通知订单检测,订单数:{c}", Orders.Count); foreach (var order in Orders) { _logger.LogInformation("开始异步通知订单: {c}", order.Id); diff --git a/src/TokenPay/BgServices/UpdateRateService.cs b/src/TokenPay/BgServices/UpdateRateService.cs index 24e69c5..4b1f9a2 100644 --- a/src/TokenPay/BgServices/UpdateRateService.cs +++ b/src/TokenPay/BgServices/UpdateRateService.cs @@ -14,6 +14,7 @@ public class UpdateRateService : BaseScheduledService private readonly IServiceProvider _serviceProvider; private readonly ILogger _logger; private readonly FlurlClient client; + private FiatCurrency BaseCurrency => Enum.Parse(_configuration.GetValue("BaseCurrency", "CNY")); public UpdateRateService( IConfiguration configuration, IServiceProvider serviceProvider, @@ -61,7 +62,7 @@ protected override async Task ExecuteAsync() .SetQueryParams(new { side = side, - quoteCurrency = FiatCurrency.CNY.ToString(), + quoteCurrency = BaseCurrency.ToString(), baseCurrency = "USDT", }) .GetJsonAsync(); @@ -69,17 +70,17 @@ protected override async Task ExecuteAsync() { list.Add(new TokenRate { - Id = $"{Currency.USDT_TRC20}_{FiatCurrency.CNY}", + Id = $"{Currency.USDT_TRC20}_{BaseCurrency}", Currency = Currency.USDT_TRC20, - FiatCurrency = FiatCurrency.CNY, + FiatCurrency = BaseCurrency, LastUpdateTime = DateTime.Now, Rate = result.data.First(x => x.bestOption).price, }); list.Add(new TokenRate { - Id = $"{Currency.USDT_ERC20}_{FiatCurrency.CNY}", + Id = $"{Currency.USDT_ERC20}_{BaseCurrency}", Currency = Currency.USDT_ERC20, - FiatCurrency = FiatCurrency.CNY, + FiatCurrency = BaseCurrency, LastUpdateTime = DateTime.Now, Rate = result.data.First(x => x.bestOption).price, }); @@ -105,7 +106,7 @@ protected override async Task ExecuteAsync() .SetQueryParams(new { side = side, - quoteCurrency = FiatCurrency.CNY.ToString(), + quoteCurrency = BaseCurrency.ToString(), baseCurrency = "TRX", }) .GetJsonAsync(); @@ -113,9 +114,9 @@ protected override async Task ExecuteAsync() { list.Add(new TokenRate { - Id = $"{Currency.TRX}_{FiatCurrency.CNY}", + Id = $"{Currency.TRX}_{BaseCurrency}", Currency = Currency.TRX, - FiatCurrency = FiatCurrency.CNY, + FiatCurrency = BaseCurrency, LastUpdateTime = DateTime.Now, Rate = result.data.First(x => x.bestOption).price, }); @@ -142,7 +143,7 @@ protected override async Task ExecuteAsync() .SetQueryParams(new { side = side, - quoteCurrency = FiatCurrency.CNY.ToString(), + quoteCurrency = BaseCurrency.ToString(), baseCurrency = "ETH", }) .GetJsonAsync(); @@ -150,9 +151,9 @@ protected override async Task ExecuteAsync() { list.Add(new TokenRate { - Id = $"{Currency.ETH}_{FiatCurrency.CNY}", + Id = $"{Currency.ETH}_{BaseCurrency}", Currency = Currency.ETH, - FiatCurrency = FiatCurrency.CNY, + FiatCurrency = BaseCurrency, LastUpdateTime = DateTime.Now, Rate = result.data.First(x => x.bestOption).price, }); @@ -178,7 +179,7 @@ protected override async Task ExecuteAsync() .SetQueryParams(new { side = side, - quoteCurrency = FiatCurrency.CNY.ToString(), + quoteCurrency = BaseCurrency.ToString(), baseCurrency = "USDC", }) .GetJsonAsync(); @@ -186,9 +187,9 @@ protected override async Task ExecuteAsync() { list.Add(new TokenRate { - Id = $"{Currency.USDC_ERC20}_{FiatCurrency.CNY}", + Id = $"{Currency.USDC_ERC20}_{BaseCurrency}", Currency = Currency.USDC_ERC20, - FiatCurrency = FiatCurrency.CNY, + FiatCurrency = BaseCurrency, LastUpdateTime = DateTime.Now, Rate = result.data.First(x => x.bestOption).price, }); diff --git a/src/TokenPay/Controllers/HomeController.cs b/src/TokenPay/Controllers/HomeController.cs index ec92935..b86d2c2 100644 --- a/src/TokenPay/Controllers/HomeController.cs +++ b/src/TokenPay/Controllers/HomeController.cs @@ -20,9 +20,10 @@ public class HomeController : Controller private readonly IBaseRepository _rateRepository; private readonly IBaseRepository _tokenRepository; private readonly IConfiguration _configuration; + private FiatCurrency BaseCurrency => Enum.Parse(_configuration.GetValue("BaseCurrency", "CNY")); private int DecimalsUSDT => _configuration.GetValue("Decimals:USDT", 4); private int DecimalsTRX => _configuration.GetValue("Decimals:TRX", 2); - private int DecimalsETH => _configuration.GetValue("Decimals:TRX", 5); + private int DecimalsETH => _configuration.GetValue("Decimals:ETH", 5); private int GetDecimals(Currency currency) { var decimals = currency switch @@ -39,11 +40,13 @@ private int GetDecimals(Currency currency) private decimal RateUSDT => _configuration.GetValue("Rate:USDT", 0m); private decimal RateUSDC => _configuration.GetValue("Rate:USDC", 0m); private decimal RateTRX => _configuration.GetValue("Rate:TRX", 0m); + private decimal RateETH => _configuration.GetValue("Rate:ETH", 0m); private decimal GetRate(Currency currency) { var value = currency switch { Currency.TRX => RateTRX, + Currency.ETH => RateETH, Currency.USDT_TRC20 => RateUSDT, Currency.USDT_ERC20 => RateUSDT, Currency.USDC_ERC20 => RateUSDC, @@ -226,7 +229,7 @@ private async Task<(string, decimal)> GetUseTokenDynamicAdress(CreateOrderViewMo var rate = GetRate(model.Currency); if (rate <= 0) { - rate = await _rateRepository.Where(x => x.Currency == model.Currency && x.FiatCurrency == FiatCurrency.CNY).FirstAsync(x => x.Rate); + rate = await _rateRepository.Where(x => x.Currency == model.Currency && x.FiatCurrency == BaseCurrency).FirstAsync(x => x.Rate); } if (rate <= 0) { @@ -245,14 +248,19 @@ private async Task<(string, string)> CreateAddress(string OrderUserKey, Currency { throw new TokenPayException("动态地址需传递用户标识!"); } + var BaseCurrency = Currency.TRX; + if (currency == Currency.USDT_ERC20 || currency == Currency.USDC_ERC20 || currency == Currency.ETH) + { + BaseCurrency = Currency.ETH; + } - var token = await _tokenRepository.Where(x => x.Id == OrderUserKey).FirstAsync(); + var token = await _tokenRepository.Where(x => x.Id == OrderUserKey && x.Currency == BaseCurrency).FirstAsync(); if (token == null) { var ecKey = Nethereum.Signer.EthECKey.GenerateKey(); var rawPrivateKey = ecKey.GetPrivateKeyAsBytes(); var hex = Convert.ToHexString(rawPrivateKey); - if (currency == Currency.USDT_ERC20 || currency == Currency.ETH) + if (BaseCurrency == Currency.ETH) { var Address = ecKey.GetPublicAddress(); token = new Tokens @@ -260,7 +268,7 @@ private async Task<(string, string)> CreateAddress(string OrderUserKey, Currency Id = OrderUserKey, Address = Address, Key = hex, - Currency = currency + Currency = Currency.ETH }; await _tokenRepository.InsertAsync(token); } @@ -273,7 +281,7 @@ private async Task<(string, string)> CreateAddress(string OrderUserKey, Currency Id = OrderUserKey, Address = Address, Key = hex, - Currency = currency + Currency = Currency.TRX }; await _tokenRepository.InsertAsync(token); } @@ -307,7 +315,7 @@ private async Task<(string, decimal)> GetUseTokenStaticAdress(CreateOrderViewMod var rate = GetRate(model.Currency); if (rate <= 0) { - rate = await _rateRepository.Where(x => x.Currency == model.Currency && x.FiatCurrency == FiatCurrency.CNY).FirstAsync(x => x.Rate); + rate = await _rateRepository.Where(x => x.Currency == model.Currency && x.FiatCurrency == BaseCurrency).FirstAsync(x => x.Rate); } if (rate <= 0) { diff --git a/src/TokenPay/Domains/TokenRate.cs b/src/TokenPay/Domains/TokenRate.cs index 38659d8..d713b96 100644 --- a/src/TokenPay/Domains/TokenRate.cs +++ b/src/TokenPay/Domains/TokenRate.cs @@ -1,4 +1,4 @@ -using FreeSql.DataAnnotations; +using FreeSql.DataAnnotations; using System.ComponentModel.DataAnnotations; namespace TokenPay.Domains @@ -30,6 +30,12 @@ public class TokenRate public enum FiatCurrency { CNY = 10, - USD + USD, + EUR, + GBP, + AUD, + HKD, + TWD, + SGD } } diff --git a/src/TokenPay/appsettings.Example.json b/src/TokenPay/appsettings.Example.json index 4755edb..c2feaad 100644 --- a/src/TokenPay/appsettings.Example.json +++ b/src/TokenPay/appsettings.Example.json @@ -14,6 +14,7 @@ }, "TRON-PRO-API-KEY": "xxxxxx-xxxx-xxxx-xxxxxxxxxxxx", // 避免接口请求频繁被限制,此处申请 https://www.trongrid.io/dashboard/keys "ETH-API-KEY": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // 避免接口请求频繁被限制,此处申请 https://etherscan.io/myapikey + "BaseCurrency": "CNY", //默认货币,支持 CNY、USD、EUR、GBP、AUD、HKD、TWD、SGD "Rate": { //汇率 设置0将使用自动汇率 "USDT": 0, "TRX": 0,