private void InternalCreate(TransactionalDapperCommand transaction, int userId, int buyId, string transactionHash) { var trans = TransactionBusiness.SetNew(userId, transactionHash); transaction.Insert(trans); var buyTrans = BuyTransactionBusiness.SetNew(buyId, trans.Id); transaction.Insert(buyTrans); }
private void Create(int userId, int buyId, string transactionHash) { TransactionBusiness.ValidateTransaction(transactionHash); using (var transaction = new TransactionalDapperCommand()) { InternalCreate(transaction, userId, buyId, transactionHash); transaction.Commit(); } }
private void HandleLostTransaction(Transaction transaction, int userId, int buyId, string transactionHash) { using (var trans = new TransactionalDapperCommand()) { transaction.TransactionStatus = TransactionStatus.Lost.Value; transaction.ProcessedDate = DateTime.UtcNow; trans.Update(transaction); InternalCreate(trans, userId, buyId, transactionHash); trans.Commit(); } }
public void Cancel(string email, int buyId) { var user = UserBusiness.GetValidUser(email); var buy = BuyBusiness.Get(buyId); if (buy == null || buy.UserId != user.Id || buy.ExpirationDate.HasValue) { throw new ArgumentException("Invalid purchase."); } if (buy.LastTransaction.TransactionStatus == TransactionStatus.Pending.Value) { if (string.IsNullOrEmpty(buy.LastTransaction.TransactionHash)) { TransactionBusiness.Process(buy.LastTransaction, TransactionStatus.Cancel); } else { try { var transaction = Web3.Web3Business.CheckTransaction(buy.LastTransaction.TransactionHash); throw new ArgumentException(transaction.BlockNumber.HasValue ? "The transaction is already minted." : "Pending transaction cannot be canceled."); } catch (DomainObjects.Web3.Web3Exception ex) { if (ex.Code == 404) { TransactionBusiness.Process(buy.LastTransaction, TransactionStatus.Cancel); } else { throw; } } } } else if (buy.LastTransaction.TransactionStatus == TransactionStatus.Error.Value) { using (var transaction = new TransactionalDapperCommand()) { var trans = TransactionBusiness.SetCanceled(user.Id); transaction.Insert(trans); var buyTrans = BuyTransactionBusiness.SetNew(buyId, trans.Id); transaction.Insert(buyTrans); transaction.Commit(); } } else { throw new ArgumentException("Invalid transaction status."); } }
private void HandleLostTransaction(Transaction transaction, int userId, int escrowResultId) { using (var trans = new TransactionalDapperCommand()) { transaction.TransactionStatus = TransactionStatus.Lost.Value; transaction.ProcessedDate = DateTime.UtcNow; trans.Update(transaction); var newTransaction = TransactionBusiness.SetNew(userId); trans.Insert(newTransaction); var escrowResultTrans = SetNew(escrowResultId, newTransaction.Id); trans.Insert(escrowResultTrans); trans.Commit(); } }
internal Projection Create(DomainObjects.Portfolio.Portfolio portfolio, double projectionValue, RiskType risk, double? optimisticProjection, double? pessimisticProjection, Dictionary<int, double> distribution) { var projection = SetNew(portfolio.Id, projectionValue, risk, optimisticProjection, pessimisticProjection); using (var transaction = new TransactionalDapperCommand()) { transaction.Insert(projection); var distributions = DistributionBusiness.SetNew(projection.Id, distribution); foreach (Distribution dist in distributions) transaction.Insert(dist); portfolio.ProjectionId = projection.Id; transaction.Update(portfolio); projection.Distribution = distributions; transaction.Commit(); } return projection; }
public DomainObjects.Portfolio.Portfolio UpdateWithDistribution(string email, int portfolioId, decimal price, string name, string description, Dictionary <int, double> distribution) { var user = UserBusiness.GetValidUser(email); var portfolio = GetValidByOwner(user.Id, portfolioId); if (portfolio == null) { throw new ArgumentException("Invalid portfolio."); } var projection = ProjectionBusiness.Get(portfolio.ProjectionId.Value); if (projection == null) { throw new ArgumentException("Invalid projection."); } var risk = PortfolioBusiness.GetRisk(projection.ProjectionValue, distribution); using (var transaction = new TransactionalDapperCommand()) { var portfolioDetail = PortfolioDetailBusiness.SetNew(portfolioId, price, name, description, true); transaction.Insert(portfolioDetail); var newProjection = ProjectionBusiness.SetNew(portfolio.Id, projection.ProjectionValue, risk, projection.OptimisticProjectionValue, projection.PessimisticProjectionValue); transaction.Insert(newProjection); var distributions = DistributionBusiness.SetNew(newProjection.Id, distribution); foreach (Distribution dist in distributions) { transaction.Insert(dist); } portfolio.ProjectionId = newProjection.Id; transaction.Update(portfolio); newProjection.Distribution = distributions; portfolio.Detail = portfolioDetail; portfolio.Projection = newProjection; transaction.Commit(); } return(portfolio); }
public DomainObjects.Portfolio.Portfolio Create(string email, int advisorId, decimal price, string name, string description, double projectionValue, double?optimisticProjection, double?pessimisticProjection, Dictionary <int, double> distribution) { var advisor = AdvisorBusiness.GetWithOwner(advisorId, email); if (advisor == null) { throw new ArgumentException("Invalid advisor."); } var risk = GetRisk(projectionValue, distribution); var advisorDetail = AdvisorDetailBusiness.GetForAutoEnabled(advisorId); var portfolio = new DomainObjects.Portfolio.Portfolio(); portfolio.AdvisorId = advisorId; portfolio.CreationDate = DateTime.UtcNow; using (var transaction = new TransactionalDapperCommand()) { transaction.Insert(portfolio); var detail = PortfolioDetailBusiness.SetNew(portfolio.Id, price, name, description, true); transaction.Insert(detail); var projection = ProjectionBusiness.SetNew(portfolio.Id, projectionValue, risk, optimisticProjection, pessimisticProjection); transaction.Insert(projection); var distributions = DistributionBusiness.SetNew(projection.Id, distribution); foreach (Distribution dist in distributions) { transaction.Insert(dist); } portfolio.ProjectionId = projection.Id; transaction.Update(portfolio); if (advisorDetail != null) { advisorDetail = AdvisorDetailBusiness.SetNew(advisorId, advisorDetail.Name, advisorDetail.Description, true); transaction.Insert(advisorDetail); } projection.Distribution = distributions; portfolio.Projection = projection; portfolio.Detail = detail; transaction.Commit(); } return(portfolio); }
public DomainObjects.Advisor.Advisor Create(string email, string name, string description) { var user = UserBusiness.GetValidUser(email); var advisor = SimpleGetByOwner(user.Id); if (advisor != null) { throw new ArgumentException("Advisor already registered."); } advisor = new DomainObjects.Advisor.Advisor(); advisor.UserId = user.Id; advisor.Type = AdvisorType.Human.Value; using (var transaction = new TransactionalDapperCommand()) { transaction.Insert(advisor); var detail = AdvisorDetailBusiness.SetNew(advisor.Id, name, description, false); transaction.Insert(detail); advisor.Detail = detail; transaction.Commit(); } return(advisor); }
public async Task <Login> SimpleRegister(string address, string username, string email, string password) { User user; using (var transaction = new TransactionalDapperCommand()) { user = SetBaseUserCreation(username, email, password); transaction.Insert(user); var wallet = SetWalletCreation(user.Id, address); user.Wallet = wallet; transaction.Insert(wallet); transaction.Commit(); } await SendEmailConfirmation(user.Email, user.ConfirmationCode); return(new Model.Login() { Address = user.Wallet.Address, Email = user.Email, Username = user.Username, PendingConfirmation = true }); }
public void Evaluate() { var purchases = BuyBusiness.ListPendingEscrowResult(); foreach (var purchase in purchases) { try { var history = PortfolioHistoryBusiness.ListHistory(purchase.PortfolioId); if (history.Max(c => c.Date) < purchase.ExpirationDate.Value) { continue; } var projection = ProjectionBusiness.Get(purchase.ProjectionId); var dailyEstimatedPercentage = Util.Util.ConvertMonthlyToDailyRate(projection.ProjectionValue); var estimatedPercentage = Math.Pow(1.0 + dailyEstimatedPercentage, purchase.Days) - 1.0; var startDate = purchase.ExpirationDate.Value.AddDays(-purchase.Days); if (startDate < history.Min(c => c.Date)) { startDate = history.Min(c => c.Date); } var historyOfPeriod = history.Where(c => c.Date >= startDate && c.Date <= purchase.ExpirationDate.Value); var historyResult = PortfolioHistoryBusiness.GetHistoryResult(historyOfPeriod); var performedPercentage = historyResult.Value / 100.0; decimal buyerTokenAmount = 0, ownerTokenAmount = 0; if (estimatedPercentage > performedPercentage) { buyerTokenAmount = purchase.Price; } else { ownerTokenAmount = purchase.Price; } using (var transaction = new TransactionalDapperCommand()) { var escrowResult = new EscrowResult() { BuyId = purchase.Id, CreationDate = DateTime.UtcNow, BuyerTokenResult = buyerTokenAmount, OwnerTokenResult = ownerTokenAmount }; transaction.Insert(escrowResult); var trans = TransactionBusiness.SetNew(purchase.UserId); transaction.Insert(trans); var escrowResultTrans = EscrowResultTransactionBusiness.SetNew(escrowResult.Id, trans.Id); transaction.Insert(escrowResultTrans); transaction.Commit(); } } catch (Exception ex) { Logger.LogCritical(ex, $"Error on evaluate purchase {purchase.Id}"); } } }
public Buy Create(string email, string address, int portfolioId, int days, int?goalOptionId = null, int?timeframe = null, int?risk = null, double?targetAmount = null, double?startingAmount = null, double?monthlyContribution = null) { if (string.IsNullOrWhiteSpace(address)) { throw new ArgumentException("Address must be filled."); } if (days <= 0) { throw new ArgumentException("Invalid purchase days."); } var user = UserBusiness.GetValidUser(email, address); if (user?.ConfirmationDate == null) { throw new ArgumentException("User didn't confirmed e-mail."); } var portfolio = PortfolioBusiness.GetWithDetails(portfolioId); if (portfolio == null || !portfolio.Detail.Enabled || !portfolio.Advisor.Detail.Enabled) { throw new ArgumentException("Invalid portfolio."); } if (portfolio.Advisor.UserId == user.Id) { throw new ArgumentException("User is the advisor owner."); } var purchases = ListPurchases(user.Id); if (purchases.Any(c => c.PortfolioId == portfolio.Id)) { throw new ArgumentException("Portfolio already bought."); } Buy buy; using (var transaction = new TransactionalDapperCommand()) { Goal goal = null; if (portfolio.Advisor.Type == AdvisorType.Robo.Value) { if (!goalOptionId.HasValue || !timeframe.HasValue || !risk.HasValue || !startingAmount.HasValue || !monthlyContribution.HasValue) { throw new ArgumentException("Invalid goal data."); } goal = GoalBusiness.SetNew(user.Id, goalOptionId.Value, timeframe.Value, risk.Value, targetAmount, startingAmount.Value, monthlyContribution.Value); transaction.Insert(goal); } var price = Math.Floor(portfolio.Detail.Price * (decimal)(1000000.0 * days) / (decimal)30.0) / (decimal)1000000.0; buy = SetNew(days, price, portfolio.Id, portfolio.ProjectionId.Value, portfolio.Detail.Id, user.Id, goal?.Id); transaction.Insert(buy); var trans = TransactionBusiness.SetNew(user.Id); transaction.Insert(trans); var buyTrans = BuyTransactionBusiness.SetNew(buy.Id, trans.Id); transaction.Insert(buyTrans); buy.Goal = goal; buy.Portfolio = portfolio; buy.PortfolioDetail = portfolio.Detail; transaction.Commit(); } return(buy); }
private TransactionStatus CheckAndProcessTransaction(Transaction buyTransaction, int buyId) { TransactionStatus status = TransactionStatus.Pending; try { var transaction = Web3.Web3Business.CheckTransaction(buyTransaction.TransactionHash, "Escrow(address,uint256)"); if (transaction.Status.HasValue) { if (transaction.Status.Value == 1) { if (transaction.EventData != null && transaction.EventData.Length == 2 && !string.IsNullOrEmpty(transaction.EventData[0]) && !string.IsNullOrEmpty(transaction.EventData[1])) { var wallet = WalletBusiness.GetByUser(buyTransaction.UserId); if (transaction.EventData[0].ToLower().Trim() != wallet.Address) { status = TransactionStatus.Fraud; } else { var purchase = BuyBusiness.GetSimple(buyId); var escrowedValue = Util.Util.ConvertBigNumber(transaction.EventData[1], 18); if (purchase.Price > escrowedValue) { status = TransactionStatus.Fraud; } else { var transactions = ListByUserAndHash(buyTransaction.UserId, buyTransaction.TransactionHash); if (transactions.Count > 1 && (transactions.Any(c => c.BuyId != buyId) || transactions.Any(c => c.Transaction.TransactionStatus != TransactionStatus.Pending.Value))) { status = TransactionStatus.Fraud; } else { status = TransactionStatus.Success; using (var trans = new TransactionalDapperCommand()) { purchase.ExpirationDate = DateTime.UtcNow.Date.AddDays(purchase.Days); trans.Update(purchase); buyTransaction.TransactionStatus = status.Value; buyTransaction.ProcessedDate = DateTime.UtcNow; trans.Update(buyTransaction); trans.Commit(); } } } } } else { status = TransactionStatus.Fraud; } } else { status = TransactionStatus.Error; } if (status != TransactionStatus.Success) { TransactionBusiness.Process(buyTransaction, status); } } } catch (DomainObjects.Web3.Web3Exception ex) { if (ex.Code == 404 && TransactionBusiness.TransactionCanBeConsideredLost(buyTransaction)) { HandleLostTransaction(buyTransaction, buyTransaction.UserId, buyId, null); } else { throw; } } return(status); }