From 9ef347c5ccf1477261500adec2bc89118ccba4ac Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Tue, 1 Mar 2022 05:29:52 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98=E7=B1=BB?= =?UTF-8?q?=202.=E6=B7=BB=E5=8A=A0=E4=BA=A4=E6=98=93=E6=89=80=E8=B4=A6?= =?UTF-8?q?=E6=88=B7=E5=90=8C=E6=97=B6=E6=B7=BB=E5=8A=A0=E4=B8=80=E6=AC=A1?= =?UTF-8?q?APIKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Binance.TradeRobot.API.csproj | 1 + Binance.TradeRobot.API/Startup.cs | 1 + Binance.TradeRobot.Business/BaseBusiness.cs | 33 ++++++++++++++++-- .../Binance.TradeRobot.Business.csproj | 1 + .../Exchange/ExchangeBusiness.cs | 34 ++++++++++++++++--- .../User/UserBusiness.cs | 3 +- .../Exchange/AddExchangeAccountRequest.cs | 4 +++ 7 files changed, 69 insertions(+), 8 deletions(-) diff --git a/Binance.TradeRobot.API/Binance.TradeRobot.API.csproj b/Binance.TradeRobot.API/Binance.TradeRobot.API.csproj index f482d46..e433554 100644 --- a/Binance.TradeRobot.API/Binance.TradeRobot.API.csproj +++ b/Binance.TradeRobot.API/Binance.TradeRobot.API.csproj @@ -15,6 +15,7 @@ + diff --git a/Binance.TradeRobot.API/Startup.cs b/Binance.TradeRobot.API/Startup.cs index 1f8ddf0..0b2fc3d 100644 --- a/Binance.TradeRobot.API/Startup.cs +++ b/Binance.TradeRobot.API/Startup.cs @@ -32,6 +32,7 @@ namespace Binance.TradeRobot.API // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddMemoryCache(); services.AddSingleton(); services.AddSingleton(); diff --git a/Binance.TradeRobot.Business/BaseBusiness.cs b/Binance.TradeRobot.Business/BaseBusiness.cs index 9ae97c7..d8109d9 100644 --- a/Binance.TradeRobot.Business/BaseBusiness.cs +++ b/Binance.TradeRobot.Business/BaseBusiness.cs @@ -1,4 +1,9 @@ -using Yitter.IdGenerator; +using Binance.Net.Clients; +using Binance.Net.Objects; +using CryptoExchange.Net.Authentication; +using Microsoft.Extensions.Caching.Memory; +using System; +using Yitter.IdGenerator; namespace Binance.TradeRobot.Business { @@ -7,11 +12,35 @@ namespace Binance.TradeRobot.Business protected IFreeSql fsql; protected NLogManager logManager; protected IIdGenerator idGenerator; - public BaseBusiness(IFreeSql fsql, NLogManager logManager, IIdGenerator idGenerator) + protected IMemoryCache memoryCache; + private TimeSpan expirationTimeSpan; + + + public BaseBusiness(IFreeSql fsql, NLogManager logManager, IIdGenerator idGenerator, IMemoryCache memoryCache) { this.fsql = fsql; this.logManager = logManager; this.idGenerator = idGenerator; + expirationTimeSpan = TimeSpan.FromDays(1); + } + + protected BinanceClient GetBinanceClient(string apiKey, string secret) + { + if (!memoryCache.TryGetValue(apiKey, out BinanceClient binanceClient)) + { + var apiClientOption = new BinanceApiClientOptions() + { + BaseAddress = "https://fapi.binance.com", + ApiCredentials = new ApiCredentials(apiKey, secret) + }; + binanceClient = new BinanceClient(new BinanceClientOptions() + { + UsdFuturesApiOptions = apiClientOption, + SpotApiOptions = apiClientOption + }); + memoryCache.Set(apiKey, binanceClient, expirationTimeSpan); + } + return binanceClient; } } } diff --git a/Binance.TradeRobot.Business/Binance.TradeRobot.Business.csproj b/Binance.TradeRobot.Business/Binance.TradeRobot.Business.csproj index 0e37ba7..9b7bcf1 100644 --- a/Binance.TradeRobot.Business/Binance.TradeRobot.Business.csproj +++ b/Binance.TradeRobot.Business/Binance.TradeRobot.Business.csproj @@ -10,6 +10,7 @@ + diff --git a/Binance.TradeRobot.Business/Exchange/ExchangeBusiness.cs b/Binance.TradeRobot.Business/Exchange/ExchangeBusiness.cs index a5b5f13..7409949 100644 --- a/Binance.TradeRobot.Business/Exchange/ExchangeBusiness.cs +++ b/Binance.TradeRobot.Business/Exchange/ExchangeBusiness.cs @@ -3,6 +3,7 @@ using Binance.TradeRobot.Common.Extensions; using Binance.TradeRobot.Model.Base; using Binance.TradeRobot.Model.Db; using Binance.TradeRobot.Model.Dto; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; @@ -16,7 +17,7 @@ namespace Binance.TradeRobot.Business.Exchange [BatchRegistration(ServiceLifetime.Singleton, RegistrationType.Self)] public class ExchangeBusiness : BaseBusiness { - public ExchangeBusiness(IFreeSql fsql, NLogManager logManager, IIdGenerator idGenerator) : base(fsql, logManager, idGenerator) { } + public ExchangeBusiness(IFreeSql fsql, NLogManager logManager, IIdGenerator idGenerator, IMemoryCache memoryCache) : base(fsql, logManager, idGenerator, memoryCache) { } public void AddExchangeAccount(AddExchangeAccountRequest addExchangeAccountRequest) { @@ -32,7 +33,12 @@ namespace Binance.TradeRobot.Business.Exchange if (addExchangeAccountRequest.TradePolicy == Enums.TradePolicy.动量趋势v2) exchangeAccount.BusinessType = Enums.BusinessType.Spot_Margin; - fsql.Insert(exchangeAccount).ExecuteAffrows(); + fsql.Transaction(() => + { + + fsql.Insert(exchangeAccount).ExecuteAffrows(); + }); + } public void AddExchangeAPIKey(AddExchangeAPIKeyRequest addExchangeAPIKeyRequest) @@ -71,8 +77,10 @@ namespace Binance.TradeRobot.Business.Exchange foreach (var exchangeAccount in exchangeAccountList) { var currentExchangeAccountAPIKeyList = exchangeAPIKeyList.Where(k => k.AccountId == exchangeAccount.Id); - if (currentExchangeAccountAPIKeyList.Count() > 0) - exchangeAccount.ExchangeAPIKeyList.AddRange(currentExchangeAccountAPIKeyList); + if (currentExchangeAccountAPIKeyList.Count() == 0) + continue; + exchangeAccount.ExchangeAPIKeyList.AddRange(currentExchangeAccountAPIKeyList); + var ewh = new ManualResetEvent(false); waitList.Add(ewh); Task.Factory.StartNew(() => GetExchangeAssets(exchangeAccount, ewh)); @@ -85,7 +93,23 @@ namespace Binance.TradeRobot.Business.Exchange { try { - + var binanceClient = GetBinanceClient(exchangeAccount.ExchangeAPIKeyList[0].APIKey, exchangeAccount.ExchangeAPIKeyList[0].SecretKey); + if (exchangeAccount.BusinessType == Enums.BusinessType.UPrep) + { + var result = binanceClient.UsdFuturesApi.Account.GetBalancesAsync().Result; + if (result.Success) + { + exchangeAccount.UPrepUSDT = result.Data.FirstOrDefault(b => b.Asset == "USDT")?.WalletBalance ?? 0; + } + } + else if (exchangeAccount.BusinessType == Enums.BusinessType.Spot) + { + + } + else if (exchangeAccount.BusinessType == Enums.BusinessType.Spot_Margin) + { + + } } catch (Exception ex) { diff --git a/Binance.TradeRobot.Business/User/UserBusiness.cs b/Binance.TradeRobot.Business/User/UserBusiness.cs index 4ad3c3a..8f1c32d 100644 --- a/Binance.TradeRobot.Business/User/UserBusiness.cs +++ b/Binance.TradeRobot.Business/User/UserBusiness.cs @@ -4,6 +4,7 @@ using Binance.TradeRobot.Common.Extensions; using Binance.TradeRobot.Model.Base; using Binance.TradeRobot.Model.Db; using Binance.TradeRobot.Model.Dto; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; @@ -22,7 +23,7 @@ namespace Binance.TradeRobot.Business { private IConfiguration configuration; - public UserBusiness(IFreeSql fsql, NLogManager logManager, IIdGenerator idGenerator, IConfiguration configuration) : base(fsql, logManager, idGenerator) + public UserBusiness(IFreeSql fsql, NLogManager logManager, IIdGenerator idGenerator, IConfiguration configuration, IMemoryCache memoryCache) : base(fsql, logManager, idGenerator, memoryCache) { this.configuration = configuration; } diff --git a/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAccountRequest.cs b/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAccountRequest.cs index 343e7e0..a4b5f7d 100644 --- a/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAccountRequest.cs +++ b/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAccountRequest.cs @@ -12,5 +12,9 @@ namespace Binance.TradeRobot.Model.Dto /// 交易策略 /// public Enums.TradePolicy TradePolicy { get; set; } + + public string APIKey { get; set; } + + public string SecretKey { get; set; } } }