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; }
}
}