protected void WithdrawCryptocurrencyButton_Click(object sender, EventArgs e) { if (Page.IsValid) { CryptocurrencySuccessMessagePanel.Visible = false; CryptocurrencyErrorMessagePanel.Visible = false; try { var Cryptocurrency = CryptocurrencyFactory.Get(SelectedCryptocurrency); User.ValidatePIN(CryptoPINTextBox.Text); string address = TryGetWithdrawalAddress(Cryptocurrency); Money moneyAmount = Money.Zero; Money moneyFee = Money.Zero; Money totalAmount = Money.Zero; decimal amountInCryptocurrency = Decimal.Zero; if (Cryptocurrency.WithdrawalFeePolicy == WithdrawalFeePolicy.Packs) { WithdrawalPacksPlaceHolder.Visible = true; WithdrawalPacksLiteral.Text = BitcoinWithdrawalFeePacks.GetPacksText(User.Id); } moneyAmount = new Money(Convert.ToDecimal(WithdrawCryptocurrencyAmountTextBox.Text)).FromMulticurrency(); moneyFee = Cryptocurrency.EstimatedWithdrawalFee(amountInCryptocurrency, address, User.Id, Cryptocurrency.WithdrawalSource); totalAmount = moneyAmount - moneyFee; amountInCryptocurrency = Cryptocurrency.ConvertFromMoney(moneyAmount); if (Cryptocurrency.WithdrawalSource == WithdrawalSourceBalance.Wallet) { amountInCryptocurrency = Convert.ToDecimal(WithdrawCryptocurrencyAmountTextBox.Text); moneyAmount = new CryptocurrencyMoney(Cryptocurrency.Type, amountInCryptocurrency); moneyFee = Cryptocurrency.EstimatedWithdrawalFee(amountInCryptocurrency, address, User.Id, Cryptocurrency.WithdrawalSource); totalAmount = new CryptocurrencyMoney(SelectedCryptocurrency, moneyAmount.ToDecimal() - moneyFee.ToDecimal()); } TryValidateCryptocurrencyWithdrawalByEmailOrPhone(); CryptocurrencyFeeLiteral.Visible = WithdrawTotalCryptocurrencyLiteral.Visible = true; CryptocurrencyFeeLiteral.Text = "</br>" + U3500.CASHOUT_FEES + ": " + moneyFee.ToString() + "</br>"; WithdrawTotalCryptocurrencyLiteral.Text = "<b>" + U5001.TOTAL + ": " + totalAmount + "</b>"; WithdrawCryptocurrencyButton.Visible = false; WithdrawCryptocurrencyConfirmButton.Visible = true; WithdrawCryptocurrencyAmountTextBox.Enabled = false; CryptoPINTextBox.Enabled = false; } catch (Exception ex) { CryptocurrencyErrorMessagePanel.Visible = true; CryptocurrencyErrorMessageLiteral.Text = ex.Message; } } }
private void SubtractFromCryptocurrencyBalance(CryptocurrencyType cryptocurrencyType, CryptocurrencyMoney value, string note, BalanceLogType logType = BalanceLogType.Other) { UserCryptocurrencyBalance.Remove(this.Id, value, cryptocurrencyType); //Works on ONE crypto balance at the moment BalanceLog.Add(this, CryptocurrencyTypeHelper.ConvertToBalanceType(cryptocurrencyType), value.ToDecimal() * -1, note, logType); }
public static void TryPlaceOrder(int OfferId, CryptocurrencyMoney CCAmount, String SellerDescription) { //Loading selected offer var SelectedOffer = new CryptocurrencyTradeOffer(OfferId); var OfferCreator = new Member(SelectedOffer.CreatorId); //Checking Creator's Balance if (SelectedOffer.OfferKind == CryptocurrencyOfferType.Buy) { if (SelectedOffer.CreatorId != Member.CurrentId) { if (OfferCreator.GetCryptocurrencyBalance(CryptocurrencyType.BTC) < CCAmount) { throw new MsgException(U6010.ORDER_CREATORNOBALANCE); } } } else { if (SelectedOffer.CreatorId == Member.CurrentId) { if (OfferCreator.GetCryptocurrencyBalance(CryptocurrencyType.BTC) < CCAmount) { throw new MsgException(U6010.ORDER_YOUNOBALANCE); } } } //If everything is good, creating transaction CryptocurrencyTradeTransaction NewTransaction = new CryptocurrencyTradeTransaction() { OfferId = OfferId, ClientId = Member.CurrentId, ExecutionTime = AppSettings.ServerTime, PaymentStatus = CryptocurrencyTransactionStatus.AwaitingPayment, CCAmount = CCAmount, SellerDescription = SellerDescription }; //Freezing cryptocurrency for transaction time //Current user clicked sell if (SelectedOffer.OfferKind == CryptocurrencyOfferType.Buy) { Member.Current.SubtractFromCryptocurrencyBalance(CryptocurrencyType.BTC, CCAmount.ToDecimal(), "Cryptocurrency trade", BalanceLogType.CryptocurrencyTrade); } //Current user clicked buy else { OfferCreator.SubtractFromCryptocurrencyBalance(CryptocurrencyType.BTC, CCAmount.ToDecimal(), "Cryptocurrency trade", BalanceLogType.CryptocurrencyTrade); } //Descreasing existing offer CC amount left SelectedOffer.AmountLeft = SelectedOffer.AmountLeft - CCAmount; if (SelectedOffer.AmountLeft <= CryptocurrencyMoney.Zero) { SelectedOffer.Status = CryptocurrencyOfferStatus.Finished; } SelectedOffer.Save(); //Saving transaction, ESCROW starts here NewTransaction.Save(true); }