Skip to content

Commit

Permalink
自定义默认货币,bug修复
Browse files Browse the repository at this point in the history
  • Loading branch information
Light committed Oct 8, 2022
1 parent 9991314 commit f62f2bb
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 36 deletions.
2 changes: 0 additions & 2 deletions src/TokenPay/BgServices/OrderCheckERC20Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ private async Task ERC20(IBaseRepository<TokenOrders> _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())
{
Expand Down
2 changes: 0 additions & 2 deletions src/TokenPay/BgServices/OrderCheckETHService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
2 changes: 0 additions & 2 deletions src/TokenPay/BgServices/OrderCheckTRC20Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 0 additions & 2 deletions src/TokenPay/BgServices/OrderCheckTRXService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
4 changes: 1 addition & 3 deletions src/TokenPay/BgServices/OrderExpiredService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FreeSql;
using FreeSql;
using TokenPay.Domains;

namespace TokenPay.BgServices
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/TokenPay/BgServices/OrderNotifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 15 additions & 14 deletions src/TokenPay/BgServices/UpdateRateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class UpdateRateService : BaseScheduledService
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<UpdateRateService> _logger;
private readonly FlurlClient client;
private FiatCurrency BaseCurrency => Enum.Parse<FiatCurrency>(_configuration.GetValue("BaseCurrency", "CNY"));
public UpdateRateService(
IConfiguration configuration,
IServiceProvider serviceProvider,
Expand Down Expand Up @@ -61,25 +62,25 @@ protected override async Task ExecuteAsync()
.SetQueryParams(new
{
side = side,
quoteCurrency = FiatCurrency.CNY.ToString(),
quoteCurrency = BaseCurrency.ToString(),
baseCurrency = "USDT",
})
.GetJsonAsync<Root>();
if (result.code == 0)
{
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,
});
Expand All @@ -105,17 +106,17 @@ protected override async Task ExecuteAsync()
.SetQueryParams(new
{
side = side,
quoteCurrency = FiatCurrency.CNY.ToString(),
quoteCurrency = BaseCurrency.ToString(),
baseCurrency = "TRX",
})
.GetJsonAsync<Root>();
if (result.code == 0)
{
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,
});
Expand All @@ -142,17 +143,17 @@ protected override async Task ExecuteAsync()
.SetQueryParams(new
{
side = side,
quoteCurrency = FiatCurrency.CNY.ToString(),
quoteCurrency = BaseCurrency.ToString(),
baseCurrency = "ETH",
})
.GetJsonAsync<Root>();
if (result.code == 0)
{
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,
});
Expand All @@ -178,17 +179,17 @@ protected override async Task ExecuteAsync()
.SetQueryParams(new
{
side = side,
quoteCurrency = FiatCurrency.CNY.ToString(),
quoteCurrency = BaseCurrency.ToString(),
baseCurrency = "USDC",
})
.GetJsonAsync<Root>();
if (result.code == 0)
{
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,
});
Expand Down
22 changes: 15 additions & 7 deletions src/TokenPay/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public class HomeController : Controller
private readonly IBaseRepository<TokenRate> _rateRepository;
private readonly IBaseRepository<Tokens> _tokenRepository;
private readonly IConfiguration _configuration;
private FiatCurrency BaseCurrency => Enum.Parse<FiatCurrency>(_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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
{
Expand All @@ -245,22 +248,27 @@ 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
{
Id = OrderUserKey,
Address = Address,
Key = hex,
Currency = currency
Currency = Currency.ETH
};
await _tokenRepository.InsertAsync(token);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down
10 changes: 8 additions & 2 deletions src/TokenPay/Domains/TokenRate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FreeSql.DataAnnotations;
using FreeSql.DataAnnotations;
using System.ComponentModel.DataAnnotations;

namespace TokenPay.Domains
Expand Down Expand Up @@ -30,6 +30,12 @@ public class TokenRate
public enum FiatCurrency
{
CNY = 10,
USD
USD,
EUR,
GBP,
AUD,
HKD,
TWD,
SGD
}
}
1 change: 1 addition & 0 deletions src/TokenPay/appsettings.Example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f62f2bb

Please sign in to comment.