Example #1
0
 private void Create(int userId, int buyId, string transactionHash)
 {
     TransactionBusiness.ValidateTransaction(transactionHash);
     using (var transaction = new TransactionalDapperCommand())
     {
         InternalCreate(transaction, userId, buyId, transactionHash);
         transaction.Commit();
     }
 }
Example #2
0
 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();
     }
 }
Example #3
0
        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.");
            }
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        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
            });
        }
Example #7
0
        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);
        }