Exemplo n.º 1
0
 private bool IsSameTransaction(BlockChainTransaction tnx1, BlockChainTransaction tnx2)
 {
     if (tnx1 == null || tnx2 == null)
     {
         return(false);
     }
     return(tnx1.Amount == tnx2.Amount &&
            (tnx1.ReceiverId.Equals(tnx2.ReceiverId)) &&
            (tnx1.SenderId.Equals(tnx2.SenderId)));
 }
Exemplo n.º 2
0
        public void Transfer()
        {
            Console.Clear();
            Console.Out.Flush();
            Program._BL_CurrentLoggedIn = (BlockChain)_model.FindByUsernameAndPassword(Program._BL_CurrentLoggedIn.WalletId, Program._BL_CurrentLoggedIn.Password);
            Console.WriteLine("-------------------------");
            Console.WriteLine("Số dư của bạn :" + Program._BL_CurrentLoggedIn.Balance + " bitcoin");
            Console.WriteLine("-------------------------");
            Console.WriteLine("Vui lòng nhập Wallet Id người nhận.");
            var walletId = Console.ReadLine();
            var account  = (BlockChain)_model.GetAccountWithAccountNumber(walletId);

            if (account == null)
            {
                Console.WriteLine($"Khong tim thay tai khoan voi so tai khoan la: {walletId}");
                Console.WriteLine("An enter de tiep tuc!");
                Console.ReadLine();
                return;
            }
            Program._BL_CurrentReceiverAccountNumber = account;
            Console.WriteLine("Thông tin người nhận.");
            Console.WriteLine("----------------------------------");
            Console.WriteLine("Wallet Id : " + Program._BL_CurrentReceiverAccountNumber.WalletId);
            Console.WriteLine("------------------------------------");
            Console.WriteLine("Vui lòng nhập số tiền bạn muốn chuyển: ");
            var amount = Double.Parse(Console.ReadLine());

            Console.WriteLine("Lời nhắn: ");
            var content            = Console.ReadLine();
            var historyTransaction = new BlockChainTransaction()
            {
                TransactionId    = Guid.NewGuid().ToString(),
                CreatedAt        = DateTime.Now,
                UpdatedAt        = DateTime.Now,
                Amount           = amount,
                Type             = Transaction.TransactionType.Transfer,
                SenderWalletId   = Program._BL_CurrentLoggedIn.WalletId,
                ReceiverWalletId = Program._BL_CurrentReceiverAccountNumber.WalletId,
                Status           = 1
            };

            Console.WriteLine(_model.UpdateBalanceWhenTransfer(historyTransaction)
                ? "Giao dịch thành công!"
                : "Giao dịch thất bại, và thử lại 1 lần nữa!");
            Program._BL_CurrentLoggedIn =
                (BlockChain)_model.FindByUsernameAndPassword(Program._BL_CurrentLoggedIn.WalletId, Program._BL_CurrentLoggedIn.Password);
            Console.WriteLine("Số tiền hiện tại: " + Program._BL_CurrentLoggedIn.Balance + " BitCoin");
            Console.WriteLine("Ấn enter để tiếp tục.");
            Console.ReadLine();
        }
Exemplo n.º 3
0
        public void WithDraw()
        {
            Console.Clear();
            Console.Out.Flush();
            // lay lai thong tin moi nhat cua Account;
            Program._BL_CurrentLoggedIn =
                (BlockChain)_model.FindByUsernameAndPassword(Program._BL_CurrentLoggedIn.WalletId, Program._BL_CurrentLoggedIn.Password);
            Console.WriteLine("Rút tiền. \t \t Số dư của bạn: " + Program._BL_CurrentLoggedIn.Balance);
            Console.WriteLine("---------------------------------");
            Console.WriteLine("Vui lòng nhập số tiền bạn muốn rút: ");
            var amount = Double.Parse(Console.ReadLine());

            Console.WriteLine("Lời nhắn: ");
            var content = Console.ReadLine();
//            Program.currentLoggedIn = model.GetAccountByUserName(Program.currentLoggedIn.Username);
            var historyTransaction = new BlockChainTransaction()
            {
                TransactionId    = Guid.NewGuid().ToString(),
                Type             = Transaction.TransactionType.Withdraw,
                CreatedAt        = DateTime.Now,
                UpdatedAt        = DateTime.Now,
                Amount           = amount,
                SenderWalletId   = Program._BL_CurrentLoggedIn.WalletId,
                ReceiverWalletId = Program._BL_CurrentLoggedIn.WalletId,
            };

            Console.WriteLine(_model.UpdateBalance(Program._SHB_CurrentLoggedIn, historyTransaction)
                ? "Giao dịch thành công!"
                : "Giao dịch thất bại, vui lòng thử lại!");
            Program._SHB_CurrentLoggedIn =
                (SHBAccount)_model.FindByUsernameAndPassword(Program._SHB_CurrentLoggedIn.Username,
                                                             Program._SHB_CurrentLoggedIn.Password);
            Console.WriteLine("Số dư hiện tại: " + Program._SHB_CurrentLoggedIn.Balance);
            Console.WriteLine("Ấn enter để tiếp tục!");
            Console.ReadLine();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Does both upgrading and recycling
        /// </summary>
        /// <returns></returns>
        public Operation <BitLevel> Upgrade()
        => _authorizer.AuthorizeAccess(UserContext.CurrentProcessPermissionProfile(), () =>
        {
            var maxLevelSettings = _settingsManager.GetSetting(Constants.Settings_MaxBitLevel).Resolve();
            var maxLevel         = (int)maxLevelSettings.ParseData <long>();
            var targetUser       = UserContext.CurrentUser();
            var currentLevel     = _query.CurrentBitLevel(targetUser);

            if (currentLevel != null)
            {
                ConfirmUpgradeDonation().Resolve();
            }

            else
            {
                currentLevel = new BitLevel
                {
                    Level    = maxLevel,
                    Cycle    = 0,
                    Donation = new BlockChainTransaction {
                        Status = BlockChainTransactionStatus.Verified
                    }
                }
            };


            if (currentLevel.Donation.Status == BlockChainTransactionStatus.Unverified)
            {
                throw new Exception("Current level donnation is still unverified");
            }

            var myAddress = _query.GetActiveBitcoinAddress(targetUser)
                            .ThrowIfNull("You do not have a valid block chain transaction address yet.");

            if (currentLevel.Cycle == int.MaxValue && currentLevel.Level == maxLevel)
            {
                throw new Exception("You cannot upgrade past the maximum cycle");
            }

            var nextLevel = (currentLevel.Level + 1) % (maxLevel + 1);
            var nextCycle = nextLevel == 0 ? currentLevel.Cycle + 1 : currentLevel.Cycle;

            var nextUpgradeBeneficiary = NextUpgradeBeneficiary(nextLevel == 0 ? targetUser : currentLevel.Donation.Receiver.Owner, nextLevel, nextCycle).ThrowIfNull("Could not find a valid beneficiary");
            var beneficiaryAddress     = _query.GetActiveBitcoinAddress(nextUpgradeBeneficiary);

            var bl = new BitLevel
            {
                Level         = nextLevel,
                Cycle         = nextCycle,
                DonationCount = 0,
                SkipCount     = 0,
                UserId        = targetUser.UserId
            };
            _pcommand.Add(bl).Resolve();

            var donation = new BlockChainTransaction
            {
                Amount      = nextLevel == maxLevel ? 0 : GetUpgradeAmount(nextLevel + 1),
                LedgerCount = nextLevel == maxLevel ? int.MaxValue : 0,
                CreatedOn   = DateTime.Now,
                Sender      = myAddress,
                Receiver    = beneficiaryAddress,
                ContextType = Constants.TransactionContext_UpgradeBitLevel,
                ContextId   = bl.Id.ToString(),
                Status      = nextLevel == maxLevel ?
                              BlockChainTransactionStatus.Verified :
                              BlockChainTransactionStatus.Unverified
            };
            _pcommand.Add(donation).Resolve();

            bl.DonationId = donation.Id;
            _pcommand.Update(bl);

            bl.Donation = donation;

            //notify user
            _notifier.NotifyUser(new Notification
            {
                Type     = NotificationType.Success,
                TargetId = targetUser.UserId,
                Title    = "Congratulations!",
                Message  = $"Well done, {targetUser.UserId}!! You are now <strong>proudly</strong> at Cycle-{nextCycle} / Level-{nextLevel}, no easy feat!<br/>" +
                           @"Now you can receive even more donations from your downlines. 
                  <p>Remember though, that this is a race to the <strong class='text-primary'>top</strong>, and as such
                  you may miss the donations of any of your downlines who upgrades to levels higher than yours. So dont waste much time here, Upgrade as soon as you can!</p>"
            })
            .Resolve();

            return(bl);
        });
Exemplo n.º 5
0
        public Operation <BitLevel> Promote(string userRef, int units, string securityHash)
        => _authorizer.AuthorizeAccess(this.PermissionProfile(UserContext.CurrentUser()), () =>
        {
            //verify the securityHash
            Haxher.IsValidHash(securityHash).ThrowIf(_v => !_v, "Access Denied");

            //do promotion logic here
            var @ref       = _refQuery.GetReferalNode(userRef);
            var targetUser = new User {
                EntityId = @ref.UserId
            };
            var currentLevel = _query.CurrentBitLevel(targetUser);
            var newLevel     = BitCycle.Create(currentLevel.Cycle, currentLevel.Level).Increment(units);
            var beneficiary  = NextUpgradeBeneficiary(new User {
                UserId = currentLevel.Donation.Receiver.OwnerId
            }, newLevel.Level, newLevel.Cycle)
                               .ThrowIfNull("could not find a suitable beneficiary");

            var beneficiaryAddress = _query.GetActiveBitcoinAddress(beneficiary);
            var targetUserAddress  = _query.GetActiveBitcoinAddress(targetUser);

            //close off the old level
            currentLevel.Donation.Amount = 0;
            currentLevel.Donation.Status = BlockChainTransactionStatus.Verified;
            _pcommand.Update(currentLevel.Donation);

            var bl = new BitLevel
            {
                Level         = newLevel.Level,
                Cycle         = newLevel.Cycle,
                DonationCount = 0,
                SkipCount     = 0,
                UserId        = @ref.UserId
            };
            _pcommand.Add(bl).Resolve();

            var maxLevelSettings = _settingsManager.GetSetting(Constants.Settings_MaxBitLevel).Resolve();
            var maxLevel         = (int)maxLevelSettings.ParseData <long>();

            var donation = new BlockChainTransaction
            {
                Amount      = newLevel.Level == maxLevel ? 0 : GetUpgradeAmount(newLevel.Level + 1),
                LedgerCount = newLevel.Level == maxLevel ? int.MaxValue : 0,
                CreatedOn   = DateTime.Now,
                Sender      = targetUserAddress,
                Receiver    = beneficiaryAddress,
                ContextType = Constants.TransactionContext_UpgradeBitLevel,
                ContextId   = bl.Id.ToString(),
                Status      = newLevel.Level == maxLevel ?
                              BlockChainTransactionStatus.Verified :
                              BlockChainTransactionStatus.Unverified
            };
            _pcommand.Add(donation).Resolve();

            bl.DonationId = donation.Id;
            _pcommand.Update(bl);

            bl.Donation = donation;

            //notify user
            _notifier.NotifyUser(new Notification
            {
                Type     = NotificationType.Success,
                TargetId = targetUser.UserId,
                Title    = "Congratulations!",
                Message  = $"Well done, {targetUser.UserId}!! You are now <strong>proudly</strong> at Cycle-{newLevel.Cycle} / Level-{newLevel.Level}, no easy feat!<br/>" +
                           @"Now you can receive even more donations from your downlines. 
                  <p>Remember though, that this is a race to the <strong class='text-primary'>top</strong>, and as such
                  you may miss the donations of any of your downlines who upgrades to levels higher than yours. So dont waste much time here, Upgrade as soon as you can!</p>"
            })
            .Resolve();

            return(bl);
        });