From 3100cb064f22e2c3237dfb4f9077d0890003ec4b Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Mon, 28 Feb 2022 00:53:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BA=A4=E6=98=93=E6=89=80?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Binance.TradeRobot.API.xml | 19 +++++++ .../Controllers/ExchangeAccountController.cs | 52 +++++++++++++++++ .../Exchange/ExchangeBusiness.cs | 56 +++++++++++++++++++ .../Base/MappingProfiles.cs | 5 ++ .../Binance.TradeRobot.Model.xml | 46 ++++++++++++++- .../Db/Exchange/ExchangeAPIKey.cs | 31 +++++----- .../Db/Exchange/ExchangeAccount.cs | 3 +- .../Exchange/AddExchangeAPIKeyRequest.cs | 11 ++++ .../Exchange/AddExchangeAccountRequest.cs | 16 ++++++ .../Exchange/ExchangeAPIKeyResponse.cs | 9 +++ .../Exchange/ExchangeAccountResponse.cs | 25 +++++++++ 11 files changed, 254 insertions(+), 19 deletions(-) create mode 100644 Binance.TradeRobot.API/Controllers/ExchangeAccountController.cs create mode 100644 Binance.TradeRobot.Business/Exchange/ExchangeBusiness.cs create mode 100644 Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAPIKeyRequest.cs create mode 100644 Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAccountRequest.cs create mode 100644 Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs create mode 100644 Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAccountResponse.cs diff --git a/Binance.TradeRobot.API/Binance.TradeRobot.API.xml b/Binance.TradeRobot.API/Binance.TradeRobot.API.xml index 3f95d9c..ecc2412 100644 --- a/Binance.TradeRobot.API/Binance.TradeRobot.API.xml +++ b/Binance.TradeRobot.API/Binance.TradeRobot.API.xml @@ -4,6 +4,25 @@ Binance.TradeRobot.API + + + 添加交易所账号 + + + + + + 添加交易所APIKey + + + + + + 获取交易所账户列表 + + 交易策略 + + 用户登录 diff --git a/Binance.TradeRobot.API/Controllers/ExchangeAccountController.cs b/Binance.TradeRobot.API/Controllers/ExchangeAccountController.cs new file mode 100644 index 0000000..da80604 --- /dev/null +++ b/Binance.TradeRobot.API/Controllers/ExchangeAccountController.cs @@ -0,0 +1,52 @@ +using Binance.TradeRobot.Business.Exchange; +using Binance.TradeRobot.Model.Base; +using Binance.TradeRobot.Model.Dto; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; + +namespace Binance.TradeRobot.API.Controllers +{ + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + public class ExchangeAccountController : BaseApiController + { + private ExchangeBusiness exchangeBusiness; + + public ExchangeAccountController(ExchangeBusiness exchangeBusiness) + { + this.exchangeBusiness = exchangeBusiness; + } + + /// + /// 添加交易所账号 + /// + /// + [HttpPost] + public void AddExchangeAccount([FromBody] AddExchangeAccountRequest addExchangeAccountRequest) + { + exchangeBusiness.AddExchangeAccount(addExchangeAccountRequest); + } + + /// + /// 添加交易所APIKey + /// + /// + [HttpPost] + public void AddExchangeAPIKey([FromBody] AddExchangeAPIKeyRequest addExchangeAPIKeyRequest) + { + exchangeBusiness.AddExchangeAPIKey(addExchangeAPIKeyRequest); + } + + /// + /// 获取交易所账户列表 + /// + /// 交易策略 + /// + [HttpGet("{tradePolicy}")] + public IList GetExchangeAccountList([FromRoute] Enums.TradePolicy tradePolicy) + { + return exchangeBusiness.GetExchangeAccountList(tradePolicy); + } + } +} diff --git a/Binance.TradeRobot.Business/Exchange/ExchangeBusiness.cs b/Binance.TradeRobot.Business/Exchange/ExchangeBusiness.cs new file mode 100644 index 0000000..3bce41e --- /dev/null +++ b/Binance.TradeRobot.Business/Exchange/ExchangeBusiness.cs @@ -0,0 +1,56 @@ +using Binance.TradeRobot.Common.DI; +using Binance.TradeRobot.Common.Extensions; +using Binance.TradeRobot.Model.Base; +using Binance.TradeRobot.Model.Db; +using Binance.TradeRobot.Model.Dto; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; +using Yitter.IdGenerator; + +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 void AddExchangeAccount(AddExchangeAccountRequest addExchangeAccountRequest) + { + if (addExchangeAccountRequest.Id == 0 || string.IsNullOrEmpty(addExchangeAccountRequest.LoginName)) + throw new BusinessException("交易所账号参数有误"); + if (fsql.Select(addExchangeAccountRequest.Id).Any()) + throw new BusinessException("交易所账号重复"); + + var exchangeAccount = addExchangeAccountRequest.Map(); + if (addExchangeAccountRequest.TradePolicy == Enums.TradePolicy.金字塔) + exchangeAccount.BusinessType = Enums.BusinessType.UPrep; + + if (addExchangeAccountRequest.TradePolicy == Enums.TradePolicy.动量趋势v2) + exchangeAccount.BusinessType = Enums.BusinessType.Spot_Margin; + + fsql.Insert(exchangeAccount).ExecuteAffrows(); + } + + public void AddExchangeAPIKey(AddExchangeAPIKeyRequest addExchangeAPIKeyRequest) + { + if (addExchangeAPIKeyRequest.AccountId == 0 || + string.IsNullOrEmpty(addExchangeAPIKeyRequest.APIKey) || + string.IsNullOrEmpty(addExchangeAPIKeyRequest.SecretKey)) + throw new BusinessException("参数有误"); + + if (fsql.Select().Where(k => k.APIKey == addExchangeAPIKeyRequest.APIKey || k.SecretKey == addExchangeAPIKeyRequest.SecretKey).Any()) + throw new BusinessException("重复的APIKey或SecretKey"); + + var exchangeAPIKey = addExchangeAPIKeyRequest.Map(); + exchangeAPIKey.Id = idGenerator.NewLong(); + fsql.Insert(exchangeAPIKey).ExecuteAffrows(); + } + + public IList GetExchangeAccountList(Enums.TradePolicy tradePolicy) + { + return null; + } + } +} diff --git a/Binance.TradeRobot.Model/Base/MappingProfiles.cs b/Binance.TradeRobot.Model/Base/MappingProfiles.cs index af663e2..2027e10 100644 --- a/Binance.TradeRobot.Model/Base/MappingProfiles.cs +++ b/Binance.TradeRobot.Model/Base/MappingProfiles.cs @@ -10,6 +10,11 @@ namespace Binance.TradeRobot.Model.Base { CreateMap(); CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/Binance.TradeRobot.Model/Binance.TradeRobot.Model.xml b/Binance.TradeRobot.Model/Binance.TradeRobot.Model.xml index 393dbf7..b25f981 100644 --- a/Binance.TradeRobot.Model/Binance.TradeRobot.Model.xml +++ b/Binance.TradeRobot.Model/Binance.TradeRobot.Model.xml @@ -41,17 +41,17 @@ - 业务类型 现货-币币=0,现货-逐仓杠杆=1,U本位合约=2 + 业务类型 币币=0,逐仓杠杆=1,U本位合约=2 - 现货-币币 + 币币 - 现货-逐仓杠杆 + 逐仓杠杆 @@ -74,6 +74,26 @@ 信号周期 1m=0,3m=1,5m=2,15m=3,30m=4,1h=5,2h=6,4h=7,6h=8,8h=9,12h=10,1d=11,3d=12,1w=13,1M=14 + + + 业务类型 + + + + + 账号登录名 + + + + + 交易策略 + + + + + 交易所账号Id + + 运行时长(s) @@ -139,6 +159,11 @@ 用户投资收益 + + + 交易策略 + + 密码需要Md5小写加密 @@ -154,6 +179,21 @@ 每页记录数 + + + 合约USDT资产 + + + + + 币币USDT资产 + + + + + 逐仓杠杆USDT资产 + + 资金变更用户名 diff --git a/Binance.TradeRobot.Model/Db/Exchange/ExchangeAPIKey.cs b/Binance.TradeRobot.Model/Db/Exchange/ExchangeAPIKey.cs index 846876f..3f61ea6 100644 --- a/Binance.TradeRobot.Model/Db/Exchange/ExchangeAPIKey.cs +++ b/Binance.TradeRobot.Model/Db/Exchange/ExchangeAPIKey.cs @@ -5,27 +5,28 @@ namespace Binance.TradeRobot.Model.Db { [Table(DisableSyncStructure = true)] - public partial class ExchangeAPIKey { + public partial class ExchangeAPIKey + { - [Column(IsPrimary = true)] - public long Id { get; set; } + [Column(IsPrimary = true)] + public long Id { get; set; } - /// - /// 交易所账号Id - /// - public long AccountId { get; set; } + /// + /// 交易所账号Id + /// + public long AccountId { get; set; } - [Column(StringLength = 100, IsNullable = false)] - public string APIKey { get; set; } + [Column(StringLength = 100, IsNullable = false)] + public string APIKey { get; set; } - [Column(InsertValueSql = "getdate()")] - public DateTime CreateTime { get; set; } + [Column(InsertValueSql = "getdate()")] + public DateTime CreateTime { get; set; } - public long? RobotId { get; set; } + public long? RobotId { get; set; } - [Column(StringLength = 100, IsNullable = false)] - public string SecretKey { get; set; } + [Column(StringLength = 100, IsNullable = false)] + public string SecretKey { get; set; } - } + } } diff --git a/Binance.TradeRobot.Model/Db/Exchange/ExchangeAccount.cs b/Binance.TradeRobot.Model/Db/Exchange/ExchangeAccount.cs index 9ad792a..fe5d9ad 100644 --- a/Binance.TradeRobot.Model/Db/Exchange/ExchangeAccount.cs +++ b/Binance.TradeRobot.Model/Db/Exchange/ExchangeAccount.cs @@ -15,7 +15,8 @@ namespace Binance.TradeRobot.Model.Db /// /// 业务类型 /// - public int BusinessType { get; set; } + [Column(MapType = typeof(int))] + public Enums.BusinessType BusinessType { get; set; } [Column(InsertValueSql = "getdate()")] public DateTime CreateTime { get; set; } diff --git a/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAPIKeyRequest.cs b/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAPIKeyRequest.cs new file mode 100644 index 0000000..d95f482 --- /dev/null +++ b/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAPIKeyRequest.cs @@ -0,0 +1,11 @@ +namespace Binance.TradeRobot.Model.Dto +{ + public class AddExchangeAPIKeyRequest + { + public long AccountId { get; set; } + + public string APIKey { get; set; } + + public string SecretKey { get; set; } + } +} diff --git a/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAccountRequest.cs b/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAccountRequest.cs new file mode 100644 index 0000000..343e7e0 --- /dev/null +++ b/Binance.TradeRobot.Model/Dto/Request/Exchange/AddExchangeAccountRequest.cs @@ -0,0 +1,16 @@ +using Binance.TradeRobot.Model.Base; + +namespace Binance.TradeRobot.Model.Dto +{ + public class AddExchangeAccountRequest + { + public long Id { get; set; } + + public string LoginName { get; set; } + + /// + /// 交易策略 + /// + public Enums.TradePolicy TradePolicy { get; set; } + } +} diff --git a/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs b/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs new file mode 100644 index 0000000..6fe24be --- /dev/null +++ b/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs @@ -0,0 +1,9 @@ +namespace Binance.TradeRobot.Model.Dto +{ + public class ExchangeAPIKeyResponse : Db.ExchangeAPIKey + { + public decimal SpotMarginUSDT { get; set; } + + public string RobotSymbol { get; set; } + } +} diff --git a/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAccountResponse.cs b/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAccountResponse.cs new file mode 100644 index 0000000..915a0f7 --- /dev/null +++ b/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAccountResponse.cs @@ -0,0 +1,25 @@ +using Binance.TradeRobot.Model.Db; +using System.Collections.Generic; + +namespace Binance.TradeRobot.Model.Dto +{ + public class ExchangeAccountResponse : ExchangeAccount + { + public IList ExchangeAPIKeyList { get; set; } + + /// + /// 合约USDT资产 + /// + public decimal UPrepUSDT { get; set; } + + /// + /// 币币USDT资产 + /// + public decimal SpotUSDT { get; set; } + + /// + /// 逐仓杠杆USDT资产 + /// + public decimal SpotMarginUSDT { get; set; } + } +}