Inheritance: MonoBehaviour
Esempio n. 1
1
        private EntityCollection GenerateRandomAccountCollection()
        {
            var collection = new List<Entity>();
            for (var i = 0; i < 10; i++)
            {
                var rgn = new Random((int)DateTime.Now.Ticks);
                var entity = new Entity("account");
                entity["accountid"] = entity.Id = Guid.NewGuid();
                entity["address1_addressid"] = Guid.NewGuid();
                entity["modifiedon"] = DateTime.Now;
                entity["lastusedincampaign"] = DateTime.Now;
                entity["donotfax"] = rgn.NextBoolean();
                entity["new_verybignumber"] = rgn.NextInt64();
                entity["exchangerate"] = rgn.NextDecimal();
                entity["address1_latitude"] = rgn.NextDouble();
                entity["numberofemployees"] = rgn.NextInt32();
                entity["primarycontactid"] = new EntityReference("contact", Guid.NewGuid());
                entity["revenue"] = new Money(rgn.NextDecimal());
                entity["ownerid"] = new EntityReference("systemuser", Guid.NewGuid());
                entity["industrycode"] = new OptionSetValue(rgn.NextInt32());
                entity["name"] = rgn.NextString(15);
                entity["description"] = rgn.NextString(300);
                entity["statecode"] = new OptionSetValue(rgn.NextInt32());
                entity["statuscode"] = new OptionSetValue(rgn.NextInt32());
                collection.Add(entity);
            }

            return new EntityCollection(collection);
        }
 public void MultiplicationOfMoneyObejcts_ShouldReturnWallet()
 {
     var money1 = new Money<decimal>(42);
     var money2 = new Money<decimal>(1000);
     var actual = money1 * money2;
     actual.ShouldBeAssignableTo<Wallet<decimal>>();
 }
Esempio n. 3
0
        public static Tuple<string, DateTime> ConvertToBaseAmount(decimal quantity, int curFromId, DateTime date, bool isCurrent)
        {
            using (IDalSession session = NHSessionFactory.CreateSession())
            {
                ICurrency curFrom = InstrumentMapper.GetCurrency(session, curFromId);

                Money convAmount = null;
                DateTime rateDate = DateTime.MinValue;
                if (isCurrent || Util.IsNullDate(date))
                {
                    Money amount = new Money(quantity, curFrom);
                    if (curFrom.ExchangeRate != null)
                        rateDate = curFrom.ExchangeRate.RateDate;
                    convAmount = amount.CurrentBaseAmount;
                }
                else
                {
                    IExRate rate = HistoricalExRateMapper.GetNearestHistoricalExRate(session, curFrom, date);
                    if (rate == null)
                        throw new ApplicationException(string.Format("No exchange rate found on {0} for {1}", date.ToString("dd-MM-yyyy"), curFrom.Symbol));
                    Money amount = new Money(quantity, curFrom, rate.Rate);
                    rateDate = rate.RateDate;
                    convAmount = amount.BaseAmount;
                }

                return new Tuple<string,DateTime>(convAmount.DisplayString, rateDate);
            }
        }
 public void DivisionOfMoneyObejcts_ShouldReturnWallet()
 {
     var money1 = new Money<long>(42);
     var money2 = new Money<long>(1000);
     var actual = money1 / money2;
     actual.ShouldBeAssignableTo<Wallet<long>>();
 }
        public void MoneyHasValueEquality()
        {
            var money1 = new Money(101.5M);
            var money2 = new Money(101.5M);

            money2.ShouldBe(money1);
        }
Esempio n. 6
0
        public void MoneyFractionalAmountWithOverflowSubtractionIsCorrect()
        {
            var money1 = new Money(100.5m);
            var money2 = new Money(0.9m);

            Assert.Equal(new Money(99.6m), money1 - money2);
        }
        internal Money Round(Money money, int decimalPlaces)
        {
            decimal amount = money.Amount;
            int factor = (int) Math.Pow(10, decimalPlaces);

            // split the amount into integral and fraction parts
            decimal integral = Math.Truncate(money.Amount);
            decimal fraction = amount-integral;

            // raise the fraction by the number of decimal places to which we want to round,
            // so that we can apply integer rounding
            decimal raisedFraction = Decimal.Multiply(fraction, factor);

            // pass the raisedFraction to a template method to do the rounding according to the RoundingStrategy
            decimal roundedFraction = CalculateRoundedAmount(raisedFraction);

            // shift the decimal to the left again, by the number of decimal places
            decimal resultFraction = Decimal.Divide(roundedFraction, factor);

            // add the original integral and the rounded resultFraction back together
            decimal roundedAmount = integral + resultFraction;

            // return the result
            return new Money(money.Currency, roundedAmount, money.RoundingMode, (DecimalPlaces)money.DecimalPlaces);
        }
Esempio n. 8
0
        public void MoneyFractionalAmountSubtractionIsCorrect()
        {
            var money1 = new Money(100.00m);
            var money2 = new Money(0.01m);

            Assert.Equal(new Money(99.99m), money1 - money2);
        }
Esempio n. 9
0
        public void MoneyFractionalAmountWithOverflowAdditionIsCorrect()
        {
            var money1 = new Money(100.999M);
            var money2 = new Money(0.9M);

            Assert.Equal(new Money(101.899m), money1 + money2);
        }
Esempio n. 10
0
        public void MoneyEqualOperatorIsCorrect()
        {
            var money1 = new Money(100.125m);
            var money2 = new Money(100.125m);

            Assert.True(money1 == money2);
        }
Esempio n. 11
0
        public void MoneyFractionalAmountAdditionIsCorrect()
        {
            var money1 = new Money(100.00m);
            var money2 = new Money(0.01m);

            Assert.Equal(new Money(100.01m), money1 + money2);
        }
    /// <summary>
    /// Runs the code example.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    /// <param name="businessId">The AdWords Express business id.</param>
    /// <param name="promotionId">The promotion id.</param>
    public void Run(AdWordsUser user, long businessId, long promotionId) {
      // Get the ExpressBusinessService.
      ExpressBusinessService businessService = (ExpressBusinessService)
          user.GetService(AdWordsService.v201509.ExpressBusinessService);

      // Get the PromotionService
      PromotionService promotionService = (PromotionService)
          user.GetService(AdWordsService.v201509.PromotionService);

      // Set the business ID to the service.
      promotionService.RequestHeader.expressBusinessId = businessId;

      // Update the budget for the promotion
      Promotion promotion = new Promotion();
      promotion.id = promotionId;
      Money newBudget = new Money();
      newBudget.microAmount = 2000000;
      promotion.budget = newBudget;

      PromotionOperation operation = new PromotionOperation();
      operation.@operator = Operator.SET;
      operation.operand = promotion;

      try {
        Promotion[] updatedPromotions = promotionService.mutate(
            new PromotionOperation[] { operation });

        Console.WriteLine("Promotion ID {0} for business ID {1} now has budget micro " +
            "amount {2}.", promotionId, businessId,
            updatedPromotions[0].budget.microAmount);
      } catch (Exception e) {
        throw new System.ApplicationException("Failed to update promotions.", e);
      }
    }
 public override void DataBind()
 {
     var splitLineItems = Shipment.GetShipmentLineItems(SplitShipment);
     var billingCurrency = CartHelper.Cart.BillingCurrency;
     if (splitLineItems != null)
     {
         OrderSubTotalLineItems.Text = new Money(splitLineItems.ToArray().Sum(x => x.ExtendedPrice) +
             splitLineItems.ToArray().Sum(x => x.OrderLevelDiscountAmount), billingCurrency).ToString();
     }
     if (SplitShipment != null)
     {
         string discountMessage = String.Empty;
         var shippingDiscountsTotal = CalculateShippingDiscounts(SplitShipment, billingCurrency, out discountMessage);
         var shippingCostSubTotal = CalculateShippingCostSubTotal(SplitShipment, CartHelper);
         shippingDiscount.Text = shippingDiscountsTotal.ToString();
         ShippingDiscountsMessage.Text = discountMessage;
         shippingTotal.Text = (shippingCostSubTotal).ToString();
     }
     else
     {
         string zeroMoney = new Money(0, SiteContext.Current.Currency).ToString();
         shippingDiscount.Text = zeroMoney;
         shippingTotal.Text = zeroMoney;
     }
 }
Esempio n. 14
0
 public void ValidateDeposit(Money amount)
 {
     if (amount > _maximumDepositAllowed)
     {
         throw new ArgumentOutOfRangeException(string.Format("Deposits over {0} are not allowed", _maximumDepositAllowed));
     }
 }
 public Transfer makeTransfer(string counterAccount, Money amount)
 {
     // 1. Assuming result is 9-digit bank account number, validate 11-test:
     int sum = 0; // <1>
     for (int i = 0; i < counterAccount.Length; i++)
     {
         sum = sum + (9 - i) * (int)Char.GetNumericValue(
             counterAccount[i]);
     }
     if (sum % 11 == 0)
     {
         // 2. Look up counter account and make transfer object:
         CheckingAccount acct = Accounts.FindAcctByNumber(counterAccount);
         Transfer result = new Transfer(this, acct, amount); // <2>
         // 3. Check whether withdrawal is to registered counter account:
         if (result.CounterAccount.Equals(this.RegisteredCounterAccount))
         {
             return result;
         }
         else
         {
             throw new BusinessException("Counter-account not registered!");
         }
     }
     else
     {
         throw new BusinessException("Invalid account number!!");
     }
 }
Esempio n. 16
0
		public void TestConverter()
		{
			var money1 = new Money(12.34, CurrencyCodes.USD);
			var money2 = new Money(12.34, CurrencyCodes.ZAR);
			Money.Converter = new ConverterMock();

			var money3 = money1.Convert(CurrencyCodes.ZAR);

			Assert.AreEqual(money3.CurrencyCode, money2.CurrencyCode);
			Assert.AreNotEqual(money3, money2);
			Assert.IsTrue(money3 > money2);
			Assert.IsTrue(money1 != money3);
			Assert.AreNotEqual(money3, money1);

            // comparing apples to oranges possible with Converter!
            // Will only return a match if the Converter has the same rate for from -> to and (inverted) to -> from
            Money.AllowImplicitConversion = true;
            var m1To3 = Money.Converter.GetRate(money1.CurrencyCode, money3.CurrencyCode, DateTime.Now);
            var m3To1 = Money.Converter.GetRate(money3.CurrencyCode, money1.CurrencyCode, DateTime.Now);
            if (m1To3 == 1d / m3To1)
            {
                Assert.IsTrue(money3 == money1);
                Assert.IsTrue(money1 == money3);
                Assert.AreEqual(money3, money1);
            }
            else
            {
                Assert.IsFalse(money3 == money1);
                Assert.IsFalse(money1 == money3);
                Assert.AreNotEqual(money3, money1);
            }
        }
Esempio n. 17
0
		public void TestOperations()
		{
			var money1 = new Money(12.34, CurrencyCodes.USD);
			var money2 = new Money(12.34, CurrencyCodes.ZAR);
			Money.Converter = new ConverterMock();
			Money.AllowImplicitConversion = true;

			// adding oranges to apples gives you apples
			var money3 = money1 + money2;
			Assert.AreEqual("USD", money3.CurrencyCode);

            // left side is ZAR and right side is USD, money3 gets converted back to ZAR
            // the same converter should return the same inverted rates
            var m1To3 = Money.Converter.GetRate(money1.CurrencyCode, money3.CurrencyCode, DateTime.Now);
            var m3To1 = Money.Converter.GetRate(money3.CurrencyCode, money1.CurrencyCode, DateTime.Now);
            if (m1To3 == 1d / m3To1)
                Assert.AreEqual(money2, money3 - money1);
            else
                Assert.AreNotEqual(money2, money3 - money1);
            // Mix up ZAR and USD. moneys converted only one way
            Assert.AreEqual(money1, money3 - money2);

			// Should fail if allowImplicitconversion is false (default)
			Money.AllowImplicitConversion = false;
			try
			{
				money3 = money1 + money2;
				Assert.Fail("Money type exception was not thrown:" + money3.Amount);
			}
			catch (InvalidOperationException e)
			{
				Assert.AreEqual("Money type mismatch", e.Message);
			}
		}
Esempio n. 18
0
 private Reservation(Account account, DateTimeOffset expiration, Money amount)
     : this(true)
 {
     this.account = account;
     this.Expiration = expiration;
     this.Amount = amount;
 }
Esempio n. 19
0
        public void AddItem(Sku sku, uint quantity, Money unitPrice)
        {
            /* 1. Guard clauses */

            if (sku == null) throw new ArgumentNullException("sku");
            if (unitPrice == null) throw new ArgumentNullException("unitPrice");

            if (OrderValue != null && unitPrice.Currency != OrderValue.Currency)
            {
                throw new ArgumentException(
                    string.Format(
                        "Unable to mix currencies on an order (SalesOrder value: {0}, supplied unit price: {1}",
                        OrderValue,
                        unitPrice));
            }

            /* 2. Invariants */

            // Would these items take the order over it's max?
            Money itemsValue = quantity * unitPrice;
            if ((OrderValue + itemsValue) > MaxCustomerOrderValue)
            {
                throw new InvalidOperationException(
                    string.Format(
                        "Adding items with value of {0} would take the current order value of {1} over the customer allowed maximum of {2}",
                        itemsValue,
                        OrderValue,
                        MaxCustomerOrderValue));
            }

            /* 3. Update state */

            OrderValue += quantity * unitPrice;
            Lines.Add(new SalesOrderLine(sku, quantity, unitPrice));
        }
Esempio n. 20
0
        public void CompareToTest()
        {
            Currency currency = new Currency("USD", "USD");
            Money target = new Money(100, currency);
            Money other = null;
            int actual;

            try
            {
                actual = target.CompareTo(other);
                Assert.Fail("Expected ArgumentNullException");
            }
            catch (ArgumentNullException) { }

            target = new Money(100, currency);
            other = new Money(100, currency);
            actual = target.CompareTo(other);
            Assert.AreEqual<int>(0, actual);

            target = new Money(50, currency);
            other = new Money(100, currency);
            actual = target.CompareTo(other);
            Assert.AreEqual<int>(-1, actual);

            target = new Money(50, currency);
            other = new Money(20, currency);
            actual = target.CompareTo(other);
            Assert.AreEqual<int>(1, actual);
        }
 internal Account(AccountTypeEnum accountType, Money initialDeposit)
 {
     this.accountType = accountType;
     ledger = new Ledger(accountType);
     this.accountType.ValidateBalance(initialDeposit);
     ledger.DepositMoney(initialDeposit);
 }
Esempio n. 22
0
 public static Money NinetyPercentOf(Money amount)
 {
     return amount
         .Distribute(10)
         .Take(9)
         .Sum(m => m.Amount);
 }
 public void WhenDestinationCurrencyIsEmpty_ShouldThrow()
 {
     var m1 = new Money<decimal>(123, "USD");
     var m2 = new Money<decimal>(1, "AUD");
     var wallet = m1 + m2;
     Should.Throw<ArgumentNullException>(() => wallet.Evaluate(this.currencyConverter, null));
 }
Esempio n. 24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:B4F.TotalGiro.Instruments.Price">Price</see> class.
 /// </summary>
 /// <param name="money">This is the amount that one particular instrument to which the price belongs would cost</param>
 /// <param name="instrument">The instrument to which the price belongs</param>
 public Price(Money money, IInstrument instrument)
 {
     this.quantity = money.Quantity;
     this.underlying = (ICurrency)money.Underlying;
     this.instrument = instrument;
     this.XRate = money.XRate;
 }
 public void WhenCurrencyConverterIsNull_ShouldThrow()
 {
     var m1 = new Money<int>(123, "USD");
     var m2 = new Money<int>(1, "AUD");
     var wallet = m1 + m2;
     Should.Throw<ArgumentNullException>(() => wallet.Evaluate(null, "AUD"));
 }
Esempio n. 26
0
        public void Bid(Guid auctionId, Guid memberId, decimal amount)
        {
            try
            {
                using (DomainEvents.Register(OutBid()))
                using (DomainEvents.Register(BidPlaced()))
                {
                    var auction = _auctionRepository.FindBy(auctionId);

                    var bidAmount = new Money(amount);

                    auction.PlaceBidFor(new Bid(memberId, bidAmount, _clock.Time()), _clock.Time());

                    _auctionRepository.Save(auction);
                }

                _unitOfWork.Commit();
            }
            catch (ConcurrencyException ex)
            {
                _unitOfWork.Clear();

                Bid(auctionId, memberId, amount);
            }            
        }
Esempio n. 27
0
        public void opInequality_Money_Money()
        {
            var obj = new Money(new Currency("€", 2), 1.23m);
            var comparand = new Money(new Currency("£", 2), 1.23m);

            Assert.True(obj != comparand);
        }
Esempio n. 28
0
        public void CurrencyCodeShouldBeCaseIgnorant(string currency)
        {
            const Currency expectedCurrency = Currency.AUD;
            var money = new Money(42, currency);

            money.Currency.ShouldBe(expectedCurrency);
        }
 public void AdditionOfMoneyObejcts_ShouldReturnWallet()
 {
     var money1 = new Money<int>(42);
     var money2 = new Money<int>(1000);
     var actual = money1 + money2;
     actual.ShouldBeAssignableTo<Wallet<int>>();
 }
Esempio n. 30
0
 public Contract(Product product, Money revenue, DateTime whenSigned, long id)
 {
     _product = product;
     Revenue = revenue;
     WhenSigned = whenSigned;
     _id = id;
 }
Esempio n. 31
0
        public async Task<IActionResult> Submit(string cryptoCode,
            long? maxadditionalfeecontribution,
            int? additionalfeeoutputindex,
            decimal minfeerate = -1.0m,
            bool disableoutputsubstitution = false,
            int v = 1)
        {
            var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
            if (network == null)
                return NotFound();

            if (v != 1)
            {
                return BadRequest(new JObject
                {
                    new JProperty("errorCode", "version-unsupported"),
                    new JProperty("supported", new JArray(1)),
                    new JProperty("message", "This version of payjoin is not supported.")
                });
            }

            await using var ctx = new PayjoinReceiverContext(_invoiceRepository, _explorerClientProvider.GetExplorerClient(network), _payJoinRepository);
            ObjectResult CreatePayjoinErrorAndLog(int httpCode, PayjoinReceiverWellknownErrors err, string debug)
            {
                ctx.Logs.Write($"Payjoin error: {debug}", InvoiceEventData.EventSeverity.Error);
                return StatusCode(httpCode, CreatePayjoinError(err, debug));
            }
            var explorer = _explorerClientProvider.GetExplorerClient(network);
            if (Request.ContentLength is long length)
            {
                if (length > 1_000_000)
                    return this.StatusCode(413,
                        CreatePayjoinError("payload-too-large", "The transaction is too big to be processed"));
            }
            else
            {
                return StatusCode(411,
                    CreatePayjoinError("missing-content-length",
                        "The http header Content-Length should be filled"));
            }

            string rawBody;
            using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                rawBody = (await reader.ReadToEndAsync()) ?? string.Empty;
            }

            FeeRate originalFeeRate = null;
            bool psbtFormat = true;

            if (PSBT.TryParse(rawBody, network.NBitcoinNetwork, out var psbt))
            {
                if (!psbt.IsAllFinalized())
                    return BadRequest(CreatePayjoinError("original-psbt-rejected", "The PSBT should be finalized"));
                ctx.OriginalTransaction = psbt.ExtractTransaction();
            }
            // BTCPay Server implementation support a transaction instead of PSBT
            else
            {
                psbtFormat = false;
                if (!Transaction.TryParse(rawBody, network.NBitcoinNetwork, out var tx))
                    return BadRequest(CreatePayjoinError("original-psbt-rejected", "invalid transaction or psbt"));
                ctx.OriginalTransaction = tx;
                psbt = PSBT.FromTransaction(tx, network.NBitcoinNetwork);
                psbt = (await explorer.UpdatePSBTAsync(new UpdatePSBTRequest() { PSBT = psbt })).PSBT;
                for (int i = 0; i < tx.Inputs.Count; i++)
                {
                    psbt.Inputs[i].FinalScriptSig = tx.Inputs[i].ScriptSig;
                    psbt.Inputs[i].FinalScriptWitness = tx.Inputs[i].WitScript;
                }
            }

            FeeRate senderMinFeeRate = minfeerate >= 0.0m ? new FeeRate(minfeerate) : null;
            Money allowedSenderFeeContribution = Money.Satoshis(maxadditionalfeecontribution is long t && t >= 0 ? t : 0);

            var sendersInputType = psbt.GetInputsScriptPubKeyType();
            if (psbt.CheckSanity() is var errors && errors.Count != 0)
            {
                return BadRequest(CreatePayjoinError("original-psbt-rejected", $"This PSBT is insane ({errors[0]})"));
            }
            if (!psbt.TryGetEstimatedFeeRate(out originalFeeRate))
            {
                return BadRequest(CreatePayjoinError("original-psbt-rejected",
                    "You need to provide Witness UTXO information to the PSBT."));
            }

            // This is actually not a mandatory check, but we don't want implementers
            // to leak global xpubs
            if (psbt.GlobalXPubs.Any())
            {
                return BadRequest(CreatePayjoinError("original-psbt-rejected",
                    "GlobalXPubs should not be included in the PSBT"));
            }

            if (psbt.Outputs.Any(o => o.HDKeyPaths.Count != 0) || psbt.Inputs.Any(o => o.HDKeyPaths.Count != 0))
            {
                return BadRequest(CreatePayjoinError("original-psbt-rejected",
                    "Keypath information should not be included in the PSBT"));
            }

            if (psbt.Inputs.Any(o => !o.IsFinalized()))
            {
                return BadRequest(CreatePayjoinError("original-psbt-rejected", "The PSBT Should be finalized"));
            }
            ////////////

            var mempool = await explorer.BroadcastAsync(ctx.OriginalTransaction, true);
            if (!mempool.Success)
            {
                ctx.DoNotBroadcast();
                return BadRequest(CreatePayjoinError("original-psbt-rejected",
                    $"Provided transaction isn't mempool eligible {mempool.RPCCodeMessage}"));
            }
            var enforcedLowR = ctx.OriginalTransaction.Inputs.All(IsLowR);
            var paymentMethodId = new PaymentMethodId(network.CryptoCode, PaymentTypes.BTCLike);
            bool paidSomething = false;
            Money due = null;
            Dictionary<OutPoint, UTXO> selectedUTXOs = new Dictionary<OutPoint, UTXO>();
            PSBTOutput originalPaymentOutput = null;
            BitcoinAddress paymentAddress = null;
            KeyPath paymentAddressIndex = null;
            InvoiceEntity invoice = null;
            DerivationSchemeSettings derivationSchemeSettings = null;
            WalletId walletId = null;
            foreach (var output in psbt.Outputs)
            {
                var walletReceiveMatch =
                    _walletReceiveService.GetByScriptPubKey(network.CryptoCode, output.ScriptPubKey);
                if (walletReceiveMatch is null)
                {

                    var key = output.ScriptPubKey.Hash + "#" + network.CryptoCode.ToUpperInvariant();
                    invoice = (await _invoiceRepository.GetInvoicesFromAddresses(new[] {key})).FirstOrDefault();
                    if (invoice is null)
                        continue;
                    derivationSchemeSettings = invoice
                        .GetSupportedPaymentMethod<DerivationSchemeSettings>(paymentMethodId)
                        .SingleOrDefault();
                    walletId = new WalletId(invoice.StoreId, network.CryptoCode.ToUpperInvariant());
                }
                else
                {
                    var store = await _storeRepository.FindStore(walletReceiveMatch.Item1.StoreId);
                    derivationSchemeSettings = store.GetDerivationSchemeSettings(_btcPayNetworkProvider,
                        walletReceiveMatch.Item1.CryptoCode);
                    
                    walletId = walletReceiveMatch.Item1;
                }
                
                if (derivationSchemeSettings is null)
                    continue;
                var receiverInputsType = derivationSchemeSettings.AccountDerivation.ScriptPubKeyType();
                if (receiverInputsType == ScriptPubKeyType.Legacy)
                {
                    //this should never happen, unless the store owner changed the wallet mid way through an invoice
                    return CreatePayjoinErrorAndLog(503, PayjoinReceiverWellknownErrors.Unavailable, "Our wallet does not support payjoin");
                }
                if (sendersInputType is ScriptPubKeyType t1 && t1 != receiverInputsType)
                {
                    return CreatePayjoinErrorAndLog(503, PayjoinReceiverWellknownErrors.Unavailable, "We do not have any UTXO available for making a payjoin with the sender's inputs type");
                }

                if (walletReceiveMatch is null)
                {
                    var paymentMethod = invoice.GetPaymentMethod(paymentMethodId);
                    var paymentDetails =
                        paymentMethod.GetPaymentMethodDetails() as Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod;
                    if (paymentDetails is null || !paymentDetails.PayjoinEnabled)
                        continue;
                    paidSomething = true;
                    due = paymentMethod.Calculate().TotalDue - output.Value;
                    if (due > Money.Zero)
                    {
                        break;
                    }

                    paymentAddress = paymentDetails.GetDepositAddress(network.NBitcoinNetwork);
                    paymentAddressIndex = paymentDetails.KeyPath;

                    if (invoice.GetAllBitcoinPaymentData(false).Any())
                    {
                        ctx.DoNotBroadcast();
                        return UnprocessableEntity(CreatePayjoinError("already-paid",
                            $"The invoice this PSBT is paying has already been partially or completely paid"));
                    }
                }
                else
                {
                    paidSomething = true;
                    due = Money.Zero;
                    paymentAddress = walletReceiveMatch.Item2.Address;
                    paymentAddressIndex = walletReceiveMatch.Item2.KeyPath;
                }


                if (!await _payJoinRepository.TryLockInputs(ctx.OriginalTransaction.Inputs.Select(i => i.PrevOut).ToArray()))
                {
                    // We do not broadcast, since we might double spend a delayed transaction of a previous payjoin
                    ctx.DoNotBroadcast();
                    return CreatePayjoinErrorAndLog(503, PayjoinReceiverWellknownErrors.Unavailable, "Some of those inputs have already been used to make another payjoin transaction");
                }

                var utxos = (await explorer.GetUTXOsAsync(derivationSchemeSettings.AccountDerivation))
                    .GetUnspentUTXOs(false);
                // In case we are paying ourselves, be need to make sure
                // we can't take spent outpoints.
                var prevOuts = ctx.OriginalTransaction.Inputs.Select(o => o.PrevOut).ToHashSet();
                utxos = utxos.Where(u => !prevOuts.Contains(u.Outpoint)).ToArray();
                Array.Sort(utxos, UTXODeterministicComparer.Instance);
                foreach (var utxo in (await SelectUTXO(network, utxos, psbt.Inputs.Select(input => input.WitnessUtxo.Value.ToDecimal(MoneyUnit.BTC)), output.Value.ToDecimal(MoneyUnit.BTC),
                    psbt.Outputs.Where(psbtOutput => psbtOutput.Index != output.Index).Select(psbtOutput => psbtOutput.Value.ToDecimal(MoneyUnit.BTC)))).selectedUTXO)
                {
                    selectedUTXOs.Add(utxo.Outpoint, utxo);
                }
                ctx.LockedUTXOs = selectedUTXOs.Select(u => u.Key).ToArray();
                originalPaymentOutput = output;
                break;
            }

            if (!paidSomething)
            {
                return BadRequest(CreatePayjoinError("invoice-not-found",
                    "This transaction does not pay any invoice with payjoin"));
            }

            if (due is null || due > Money.Zero)
            {
                return BadRequest(CreatePayjoinError("invoice-not-fully-paid",
                    "The transaction must pay the whole invoice"));
            }

            if (selectedUTXOs.Count == 0)
            {
                return CreatePayjoinErrorAndLog(503, PayjoinReceiverWellknownErrors.Unavailable, "We do not have any UTXO available for contributing to a payjoin");
            }

            var originalPaymentValue = originalPaymentOutput.Value;
            await _broadcaster.Schedule(DateTimeOffset.UtcNow + TimeSpan.FromMinutes(2.0), ctx.OriginalTransaction, network);

            //check if wallet of store is configured to be hot wallet
            var extKeyStr = await explorer.GetMetadataAsync<string>(
                derivationSchemeSettings.AccountDerivation,
                WellknownMetadataKeys.AccountHDKey);
            if (extKeyStr == null)
            {
                // This should not happen, as we check the existance of private key before creating invoice with payjoin
                return CreatePayjoinErrorAndLog(503, PayjoinReceiverWellknownErrors.Unavailable, "The HD Key of the store changed");
            }

            Money contributedAmount = Money.Zero;
            var newTx = ctx.OriginalTransaction.Clone();
            var ourNewOutput = newTx.Outputs[originalPaymentOutput.Index];
            HashSet<TxOut> isOurOutput = new HashSet<TxOut>();
            isOurOutput.Add(ourNewOutput);
            TxOut feeOutput =
                additionalfeeoutputindex is int feeOutputIndex &&
                maxadditionalfeecontribution is long v3 &&
                v3 >= 0 &&
                feeOutputIndex >= 0
                && feeOutputIndex < newTx.Outputs.Count
                && !isOurOutput.Contains(newTx.Outputs[feeOutputIndex])
                ? newTx.Outputs[feeOutputIndex] : null;
            int senderInputCount = newTx.Inputs.Count;
            foreach (var selectedUTXO in selectedUTXOs.Select(o => o.Value))
            {
                contributedAmount += (Money)selectedUTXO.Value;
                var newInput = newTx.Inputs.Add(selectedUTXO.Outpoint);
                newInput.Sequence = newTx.Inputs[(int)(RandomUtils.GetUInt32() % senderInputCount)].Sequence;
            }
            ourNewOutput.Value += contributedAmount;
            var minRelayTxFee = this._dashboard.Get(network.CryptoCode).Status.BitcoinStatus?.MinRelayTxFee ??
                                new FeeRate(1.0m);

            // Remove old signatures as they are not valid anymore
            foreach (var input in newTx.Inputs)
            {
                input.WitScript = WitScript.Empty;
            }

            Money ourFeeContribution = Money.Zero;
            // We need to adjust the fee to keep a constant fee rate
            var txBuilder = network.NBitcoinNetwork.CreateTransactionBuilder();
            var coins = psbt.Inputs.Select(i => i.GetSignableCoin())
                .Concat(selectedUTXOs.Select(o => o.Value.AsCoin(derivationSchemeSettings.AccountDerivation))).ToArray();

            txBuilder.AddCoins(coins);
            Money expectedFee = txBuilder.EstimateFees(newTx, originalFeeRate);
            Money actualFee = newTx.GetFee(txBuilder.FindSpentCoins(newTx));
            Money additionalFee = expectedFee - actualFee;
            if (additionalFee > Money.Zero)
            {
                // If the user overpaid, taking fee on our output (useful if sender dump a full UTXO for privacy)
                for (int i = 0; i < newTx.Outputs.Count && additionalFee > Money.Zero && due < Money.Zero; i++)
                {
                    if (disableoutputsubstitution)
                        break;
                    if (isOurOutput.Contains(newTx.Outputs[i]))
                    {
                        var outputContribution = Money.Min(additionalFee, -due);
                        outputContribution = Money.Min(outputContribution,
                            newTx.Outputs[i].Value - newTx.Outputs[i].GetDustThreshold(minRelayTxFee));
                        newTx.Outputs[i].Value -= outputContribution;
                        additionalFee -= outputContribution;
                        due += outputContribution;
                        ourFeeContribution += outputContribution;
                    }
                }

                // The rest, we take from user's change
                if (feeOutput != null)
                {
                    var outputContribution = Money.Min(additionalFee, feeOutput.Value);
                    outputContribution = Money.Min(outputContribution,
                        feeOutput.Value - feeOutput.GetDustThreshold(minRelayTxFee));
                    outputContribution = Money.Min(outputContribution, allowedSenderFeeContribution);
                    feeOutput.Value -= outputContribution;
                    additionalFee -= outputContribution;
                    allowedSenderFeeContribution -= outputContribution;
                }

                if (additionalFee > Money.Zero)
                {
                    // We could not pay fully the additional fee, however, as long as
                    // we are not under the relay fee, it should be OK.
                    var newVSize = txBuilder.EstimateSize(newTx, true);
                    var newFeePaid = newTx.GetFee(txBuilder.FindSpentCoins(newTx));
                    if (new FeeRate(newFeePaid, newVSize) < (senderMinFeeRate ?? minRelayTxFee))
                    {
                        return CreatePayjoinErrorAndLog(422, PayjoinReceiverWellknownErrors.NotEnoughMoney, "Not enough money is sent to pay for the additional payjoin inputs");
                    }
                }
            }

            var accountKey = ExtKey.Parse(extKeyStr, network.NBitcoinNetwork);
            var newPsbt = PSBT.FromTransaction(newTx, network.NBitcoinNetwork);
            foreach (var selectedUtxo in selectedUTXOs.Select(o => o.Value))
            {
                var signedInput = newPsbt.Inputs.FindIndexedInput(selectedUtxo.Outpoint);
                var coin = selectedUtxo.AsCoin(derivationSchemeSettings.AccountDerivation);
                signedInput.UpdateFromCoin(coin);
                var privateKey = accountKey.Derive(selectedUtxo.KeyPath).PrivateKey;
                signedInput.PSBT.Settings.SigningOptions = new SigningOptions()
                {
                    EnforceLowR = enforcedLowR
                };
                signedInput.Sign(privateKey);
                signedInput.FinalizeInput();
                newTx.Inputs[signedInput.Index].WitScript = newPsbt.Inputs[(int)signedInput.Index].FinalScriptWitness;
            }

            // Add the transaction to the payments with a confirmation of -1.
            // This will make the invoice paid even if the user do not
            // broadcast the payjoin.
            var originalPaymentData = new BitcoinLikePaymentData(paymentAddress,
                originalPaymentOutput.Value,
                new OutPoint(ctx.OriginalTransaction.GetHash(), originalPaymentOutput.Index),
                ctx.OriginalTransaction.RBF, paymentAddressIndex);
            originalPaymentData.ConfirmationCount = -1;
            originalPaymentData.PayjoinInformation = new PayjoinInformation()
            {
                CoinjoinTransactionHash = GetExpectedHash(newPsbt, coins),
                CoinjoinValue = originalPaymentValue - ourFeeContribution,
                ContributedOutPoints = selectedUTXOs.Select(o => o.Key).ToArray()
            };
            if (invoice != null)
            {
                var payment = await _invoiceRepository.AddPayment(invoice.Id, DateTimeOffset.UtcNow, originalPaymentData, network, true);
                if (payment is null)
                {
                    return UnprocessableEntity(CreatePayjoinError("already-paid",
                        $"The original transaction has already been accounted"));
                }
                _eventAggregator.Publish(new InvoiceEvent(invoice,InvoiceEvent.ReceivedPayment) { Payment = payment });
            }

           
            await _btcPayWalletProvider.GetWallet(network).SaveOffchainTransactionAsync(ctx.OriginalTransaction);
            _eventAggregator.Publish(new UpdateTransactionLabel()
            {
                WalletId = walletId,
                TransactionLabels = selectedUTXOs.GroupBy(pair => pair.Key.Hash).Select(utxo =>
                       new KeyValuePair<uint256, List<(string color, Label label)>>(utxo.Key,
                           new List<(string color, Label label)>()
                           {
                                UpdateTransactionLabel.PayjoinExposedLabelTemplate(invoice?.Id)
                           }))
                    .ToDictionary(pair => pair.Key, pair => pair.Value)
            });
Esempio n. 32
0
        public async Task ShouldCreateAndDeleteCartInExternalAmountTaxModeAndTestExternalAmountTaxModeOrientedActions()
        {
            CartDraft       cartDraft = Helper.GetTestCartDraftWithCustomLineItemsUsingExternalAmountTaxMode(_project);
            Response <Cart> response  = await _client.Carts().CreateCartAsync(cartDraft);

            Assert.IsTrue(response.Success);

            Cart cart = response.Result;

            Assert.IsTrue(cart.TaxMode == TaxMode.ExternalAmount, string.Format("Incorrect TaxMode: {0}", cart.TaxMode));
            Assert.NotNull(cart.Id, "Null Cart Id");
            Assert.NotNull(cart.CustomLineItems, "Null Custom Line Items");
            Assert.IsTrue(cart.CustomLineItems.Count > 0);
            Assert.NotNull(cart.CustomLineItems[0].TaxRate, "Null Custom Line Item Tax Rate");
            Assert.AreEqual(cart.Country, cartDraft.Country, "Country not the same");
            Assert.AreEqual(cart.InventoryMode, cartDraft.InventoryMode, "InventoryMode not the same");
            Assert.AreEqual(cart.ShippingAddress, cartDraft.ShippingAddress, "ShippingAddress not the same");
            Assert.AreEqual(cart.BillingAddress, cartDraft.BillingAddress, "BillingAddress not the same");
            SetCustomLineItemTaxAmountAction customLineItemTaxAmountAction = new SetCustomLineItemTaxAmountAction(cart.CustomLineItems[0].Id);
            Money totalGross = new Money();

            totalGross.CentAmount   = 123;
            totalGross.CurrencyCode = _project.Currencies[0];
            List <SubRate> subRates = new List <SubRate>();
            SubRate        subRate  = new SubRate();

            subRate.Name   = "sub-rate";
            subRate.Amount = .5m;
            subRates.Add(subRate);

            //test success of CustomLineItemTaxAmountAction
            customLineItemTaxAmountAction.ExternalTaxAmount = new ExternalTaxAmountDraft(totalGross, new ExternalTaxRateDraft("custom-line-item-ext-tax-rate-draft", cart.Country)
            {
                SubRates = subRates
            });
            response = await _client.Carts().UpdateCartAsync(cart, customLineItemTaxAmountAction);

            Assert.IsTrue(response.Success, "SetCustomLineItemTaxAmountAction Failed");
            cart = response.Result;
            Assert.AreEqual(cart.CustomLineItems[0].TaxRate.Amount, subRate.Amount, string.Format("Mismatching Tax Rates: {0} vs {1}", cart.CustomLineItems[0].TaxRate.Amount, subRate.Amount));

            //test success of adding a standard line item and SetLineItemTaxAmountAction
            response = await _client.Carts().UpdateCartAsync(cart, new AddLineItemAction(_testProduct.Id, _testProduct.MasterData.Current.MasterVariant.Id)
            {
                Quantity = 1
            });

            Assert.IsTrue(response.Success, "AddLineItem Failed");
            cart = response.Result;
            SetLineItemTaxAmountAction setLineItemTaxAmountAction = new SetLineItemTaxAmountAction(cart.LineItems[0].Id);

            setLineItemTaxAmountAction.ExternalTaxAmount = new ExternalTaxAmountDraft(totalGross, new ExternalTaxRateDraft("line-item-ext-tax-rate-draft", cart.Country)
            {
                SubRates = subRates
            });
            response = await _client.Carts().UpdateCartAsync(cart, new AddLineItemAction(_testProduct.Id, _testProduct.MasterData.Current.MasterVariant.Id)
            {
                Quantity = 1
            });

            Assert.IsTrue(response.Success, "SetLineItemTaxAmountAction Failed");
            cart = response.Result;

            //test success of SetShippingMethodTaxAmount
            SetShippingMethodTaxAmountAction setShippingMethodTaxAmountAction = new SetShippingMethodTaxAmountAction();

            response = await _client.Carts().UpdateCartAsync(cart, customLineItemTaxAmountAction);

            Assert.IsTrue(response.Success, "SetShippingMethodTaxAmountAction Failed");
            cart = response.Result;

            //test success of SetCartTotalTaxAction
            Money totalNetPlusTax = new Money();

            totalNetPlusTax.CentAmount   = cart.TotalPrice.CentAmount + 123;
            totalNetPlusTax.CurrencyCode = _project.Currencies[0];
            SetCartTotalTaxAction setCartTotalTaxAction = new SetCartTotalTaxAction(totalNetPlusTax);

            response = await _client.Carts().UpdateCartAsync(cart, setCartTotalTaxAction);

            Assert.IsTrue(response.Success, "SetCartTotalTaxActionResponse Failed");
            cart = response.Result;

            string deletedCartId = cart.Id;

            response = await _client.Carts().DeleteCartAsync(cart);

            Assert.IsTrue(response.Success, "Delete Cart Failed");

            cart = response.Result;

            response = await _client.Carts().GetCartByIdAsync(deletedCartId);

            Assert.IsFalse(response.Success, "Confirm Delete Cart Failed");
        }
 // Update is called once per frame
 void Update()
 {
     Money.Add(1);
 }
Esempio n. 34
0
        //transaction https://live.blockcypher.com/btc-testnet/tx/63ea3b0cac9e9afc188b42785a63f5bc84dd120400a5e18eccbbbb82adeadf2c/
        private static void Transaction2()
        {
            var bitcoinPrivateKey = new BitcoinSecret("cS88F3sJycinEUfGGMQH4w4izaNjEec97VAHsoQZYmDjZJwK1wuf");
            var key = bitcoinPrivateKey.PrivateKey;

            var network = bitcoinPrivateKey.Network;
            var address = bitcoinPrivateKey.GetAddress();

            var client = new QBitNinjaClient(network);

            var transactionId = uint256.Parse("4761929185a3dd80f23670efb854ba3d7ab5a151f296ea7c38015ceb821ee838");

            var transactionResponse = client.GetTransaction(transactionId).Result;

            Console.WriteLine(transactionResponse.TransactionId);

            if (transactionResponse.Block != null)
            {
                Console.WriteLine(transactionResponse.Block.Confirmations);
            }


            #region  detect the correct outpount to use

            var      receivedCoins   = transactionResponse.ReceivedCoins;
            OutPoint outPointToSpend = null;
            foreach (var coin in receivedCoins)
            {
                if (coin.TxOut.ScriptPubKey == bitcoinPrivateKey.ScriptPubKey)
                {
                    outPointToSpend = coin.Outpoint;
                    //amount: {0.11973878 }
                }
            }
            if (outPointToSpend == null)
            {
                throw new Exception("TxOut doesn't contain our WitHash.ScriptPubKey.PaymentScript");
            }

            Console.WriteLine("We want to spend # {0} outpoint", outPointToSpend.N + 1);
            #endregion


            #region How much you want to spend

            // How much you want to spend
            var destinationAmount = new Money(0.00036930m, MoneyUnit.BTC);

            // How much miner fee you want to pay
            var minerFee = new Money(0.00005000m, MoneyUnit.BTC);  //0.00005 BTC

            // How much you want to get back as change
            var txInAmount   = (Money)receivedCoins[(int)outPointToSpend.N].Amount;
            var changeAmount = txInAmount - destinationAmount - minerFee;

            #endregion

            #region create TxOuts
            var destinationAddress = BitcoinAddress.Create("2NA5L1cYrMRQEUYisWCK7bjfM1UFdxumYps", network);

            TxOut destinationTxOut = new TxOut()
            {
                Value        = destinationAmount,
                ScriptPubKey = destinationAddress.ScriptPubKey
            };

            TxOut changeTxOut = new TxOut()
            {
                Value        = changeAmount,
                ScriptPubKey = bitcoinPrivateKey.ScriptPubKey
            };

            var   message   = "Sound me on www.codesounding.org!";
            var   bytes     = Encoding.UTF8.GetBytes(message);
            TxOut msgTxtOut = new TxOut()
            {
                Value        = Money.Zero,
                ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
            };

            #endregion

            #region build transaction
            Transaction transaction = Transaction.Create(network);
            transaction.Inputs.Add(new TxIn()
            {
                PrevOut = outPointToSpend
            });
            transaction.Outputs.Add(destinationTxOut);
            transaction.Outputs.Add(changeTxOut);
            transaction.Outputs.Add(msgTxtOut);

            //see https://live.blockcypher.com/btc/decodetx/
            String txHex = transaction.ToHex();
            #endregion

            //transaction.Inputs[0].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey.PaymentScript;
            transaction.Inputs[0].ScriptSig = bitcoinPrivateKey.ScriptPubKey;

            #region sign transaction
            transaction.Sign(bitcoinPrivateKey, false);
            String txHexPostSign = transaction.ToHex();
            #endregion

            BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result;

            if (!broadcastResponse.Success)
            {
                Console.Error.WriteLine("ErrorCode: " + broadcastResponse.Error.ErrorCode);
                Console.Error.WriteLine("Error message: " + broadcastResponse.Error.Reason);
            }
            else
            {
                var txId = transaction.GetHash();
                Console.WriteLine("Success! You can check out the hash of the transaciton in any block explorer:");
                Console.WriteLine(txId);
            }
        }
Esempio n. 35
0
        //transaction https://live.blockcypher.com/btc-testnet/tx/1fdabe95c10025e1e220fdc72222ec51d6e14d376d172f183b39bfda35c44a6c/
        private static void ColoredTransaction()
        {
            var bitcoinPrivateKey = new BitcoinSecret("cS88F3sJycinEUfGGMQH4w4izaNjEec97VAHsoQZYmDjZJwK1wuf");
            var key = bitcoinPrivateKey.PrivateKey;

            var network = bitcoinPrivateKey.Network;
            var address = bitcoinPrivateKey.GetAddress();

            var client = new QBitNinjaClient(network);

            var transactionId = uint256.Parse("46d8eaffddbe7309a2add03428a7623ce335ce47f5eb65514aa819f57a14dfcd");

            var transactionResponse = client.GetTransaction(transactionId).Result;

            Console.WriteLine(transactionResponse.TransactionId);

            if (transactionResponse.Block != null)
            {
                Console.WriteLine(transactionResponse.Block.Confirmations);
            }


            #region  detect the correct outpount to use

            var      receivedCoins   = transactionResponse.ReceivedCoins;
            OutPoint outPointToSpend = null;
            foreach (var coin in receivedCoins)
            {
                if (coin.TxOut.ScriptPubKey == bitcoinPrivateKey.ScriptPubKey)
                {
                    outPointToSpend = coin.Outpoint;
                }
            }
            if (outPointToSpend == null)
            {
                throw new Exception("TxOut doesn't contain our ScriptPubKey");
            }

            Console.WriteLine("We want to spend # {0} outpoint", outPointToSpend.N + 1);
            #endregion


            #region How much you want to spend

            // How much you want to spend
            var destinationAmount = new Money(0.001m, MoneyUnit.BTC);

            // How much miner fee you want to pay
            var minerFee = new Money(0.00010000m, MoneyUnit.BTC);

            // How much you want to get back as change
            var txInAmount   = (Money)receivedCoins[(int)outPointToSpend.N].Amount;
            var changeAmount = txInAmount - destinationAmount - minerFee;

            #endregion

            #region create TxOuts
            var destinationAddress = BitcoinAddress.Create("mv7SwdxTpJtqgZCwW6hDgdvTud2gDuuAL4", network);

            TxOut destinationTxOut = new TxOut()
            {
                Value        = destinationAmount,
                ScriptPubKey = destinationAddress.ScriptPubKey
            };

            TxOut changeTxOut = new TxOut()
            {
                Value        = changeAmount,
                ScriptPubKey = bitcoinPrivateKey.ScriptPubKey
            };

            #endregion

            #region create meta tag

            String hash = "4565629a158493bb5e9f9319491e3724b5be3445"; //Hasher.hash256Ripemd160(bytes);

            byte[] script   = FromHexString("6a4c18");                //opreturn 6a OP_PUSHDATA1 4c, size 18 = 24
            byte[] protocol = GetProtocol();
            byte[] data     = FromHexString(hash);

            byte[] fullScript = script.Concat(protocol).Concat(data).ToArray();


            TxOut msgTxtOut = new TxOut()
            {
                Value        = Money.Zero,
                ScriptPubKey = new Script(fullScript)
            };

            #endregion

            #region build transaction
            Transaction transaction = Transaction.Create(network);
            transaction.Inputs.Add(new TxIn()
            {
                PrevOut = outPointToSpend
            });
            transaction.Outputs.Add(destinationTxOut);
            transaction.Outputs.Add(changeTxOut);
            transaction.Outputs.Add(msgTxtOut);

            //see https://live.blockcypher.com/btc/decodetx/
            String txHex = transaction.ToHex();
            #endregion

            transaction.Inputs[0].ScriptSig = bitcoinPrivateKey.ScriptPubKey;

            #region sign transaction
            transaction.Sign(bitcoinPrivateKey, false);
            String txHexPostSign = transaction.ToHex();
            #endregion

            try
            {
                RPCClient rc = new RPCClient(Network.TestNet);
                rc.SendRawTransaction(transaction);
            }
            catch (Exception e)
            {
                String msg = e.Message;

                Console.WriteLine("error: " + msg); //error code: -27, msg: transaction already in block chain
            }
        }
Esempio n. 36
0
        private void Fill()
        {
            string strSql = "SELECT * FROM MS_KONTRAK INNER JOIN MS_CUSTOMER ON MS_KONTRAK.NoCustomer = MS_CUSTOMER.NoCustomer"
                            + " WHERE NoKontrak = '" + nomor + "'";
            DataTable rs = Db.Rs(strSql);

            if (rs.Rows.Count != 0)
            {
                nomorl.Text = rs.Rows[0]["NoKontrak"].ToString();
                tgl.Text    = Cf.Day(rs.Rows[0]["TglKontrak"]);

                nama.Text        = rs.Rows[0]["Nama"].ToString();
                alamatsurat.Text = rs.Rows[0]["Alamat1"]
                                   + "<br />" + rs.Rows[0]["Alamat2"]
                                   + "<br />" + rs.Rows[0]["Alamat3"];

                telp.Text = rs.Rows[0]["NoTelp"].ToString();
                hp.Text   = rs.Rows[0]["NoHP"].ToString();

                tipe.Text   = Db.SingleString("SELECT Nama FROM REF_JENIS WHERE Jenis = '" + rs.Rows[0]["Jenis"] + "'");
                lokasi.Text = rs.Rows[0]["Lokasi"].ToString();
                unit.Text   = rs.Rows[0]["NoUnit"].ToString();

                netto.Text  = Cf.Num(Convert.ToDecimal(rs.Rows[0]["NilaiKontrak"]));
                netto2.Text = Money.Str(Convert.ToDecimal(rs.Rows[0]["NilaiKontrak"]));
                skema.Text  = rs.Rows[0]["Skema"].ToString();

                DataTable rsag = Db.Rs("SELECT * FROM MS_AGENT WHERE NoAgent = " + Cf.Pk(rs.Rows[0]["NoAgent"]));
                if (rsag.Rows.Count != 0)
                {
                    ag.Text        = rsag.Rows[0]["Nama"].ToString();
                    agid.Text      = rsag.Rows[0]["NoAgent"].ToString().PadLeft(5, '0');
                    agalamat.Text  = rsag.Rows[0]["Alamat"].ToString();
                    agtelp.Text    = rsag.Rows[0]["Kontak"].ToString();
                    principal.Text = rsag.Rows[0]["Principal"].ToString();
                }

                decimal totalkomisi = Db.SingleDecimal("SELECT ISNULL(SUM(Nilai),0) FROM MS_KOMISIR_DETAIL WHERE NoKomisi IN (SELECT NoKomisi FROM MS_KOMISI WHERE NoKontrak = '" + nomor + "')");
                kom.Text      = Cf.Num(totalkomisi);
                kom2.Text     = Money.Str(totalkomisi);
                skemakom.Text = rs.Rows[0]["SkemaKomisi"].ToString();

                decimal net = Convert.ToDecimal(rs.Rows[0]["NilaiKontrak"]);
                if (net != 0)
                {
                    kompersen.Text = Cf.Num((totalkomisi / net) * 100);
                }

                DataTable rsLunas = Db.Rs("SELECT TOP 1 *"
                                          + ",CASE CaraBayar"
                                          + "		WHEN 'TN' THEN 'TUNAI'"
                                          + "		WHEN 'KK' THEN 'KARTU KREDIT'"
                                          + "		WHEN 'KD' THEN 'KARTU DEBIT'"
                                          + "		WHEN 'TR' THEN 'TRANSFER BANK'"
                                          + "		WHEN 'BG' THEN 'CEK GIRO'"
                                          + "		WHEN 'UJ' THEN 'UANG JAMINAN'"
                                          + "		WHEN 'DN' THEN 'DISKON'"
                                          + " END AS CaraBayar2"
                                          + " FROM MS_PELUNASAN WHERE NoKontrak = '" + nomor + "' AND NoTagihan = 1");
                if (rsLunas.Rows.Count != 0)
                {
                    bftgl.Text     = Cf.Day(rsLunas.Rows[0]["TglPelunasan"]);
                    bf.Text        = Cf.Num(Convert.ToDecimal(rsLunas.Rows[0]["NilaiPelunasan"]));
                    bf2.Text       = Money.Str(Convert.ToDecimal(rsLunas.Rows[0]["NilaiPelunasan"]));
                    carabayar.Text = rsLunas.Rows[0]["CaraBayar2"].ToString()
                                     + " " + rsLunas.Rows[0]["Ket"].ToString();
                }

                tagno.InnerHtml   = nomor;
                tagcs.InnerHtml   = nama.Text;
                tagunit.InnerHtml = unit.Text;
                tagag.InnerHtml   = ag.Text;

                FillTb();
            }
        }
Esempio n. 37
0
        //Created By : Jerome Anthony Gerero, Created On : 3/11/2016
        public Entity SetQuoteTotalDiscountAmount(Entity quoteDiscountEntity, String message)
        {
            _tracingService.Trace("Started SetQuoteTotalDiscountAmount method..");

            Decimal totalDiscountAmount = 0;
            Decimal totalDPAmount = 0;
            Decimal totalAFAmount = 0;
            Decimal totalUPAmount = 0;

            var quoteId = quoteDiscountEntity.GetAttributeValue<EntityReference>("gsc_quoteid") != null
                ? quoteDiscountEntity.GetAttributeValue<EntityReference>("gsc_quoteid").Id
                : Guid.Empty;

            //Retrieve Applied Price List records with the same Quote
            EntityCollection quoteDiscountRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_quotediscount", "gsc_quoteid", quoteId, _organizationService, null, 
                OrderType.Ascending, new[] { "gsc_discountamount", "gsc_applypercentagetodp", "gsc_applyamounttodp", "gsc_applypercentagetoaf", "gsc_applyamounttoaf", "gsc_applypercentagetoup", "gsc_applyamounttoup" });

            _tracingService.Trace("Quote Discount Records Retrieved: " + quoteDiscountRecords.Entities.Count);

            if (quoteDiscountRecords != null && quoteDiscountRecords.Entities.Count > 0)
            {

                //Compute for Total Discount Amount from all retrieved Quote records
                foreach (var appliedPriceList in quoteDiscountRecords.Entities)
                {
                    _tracingService.Trace("Add Created/Updated Discount Amount in Quote..");

                    totalDiscountAmount += appliedPriceList.Contains("gsc_discountamount")
                        ? appliedPriceList.GetAttributeValue<Money>("gsc_discountamount").Value
                        : Decimal.Zero;

                    totalDPAmount += appliedPriceList.Contains("gsc_applyamounttodp")
                        ? appliedPriceList.GetAttributeValue<Money>("gsc_applyamounttodp").Value
                        : Decimal.Zero;

                    totalAFAmount += appliedPriceList.Contains("gsc_applyamounttoaf")
                        ? appliedPriceList.GetAttributeValue<Money>("gsc_applyamounttoaf").Value
                        : Decimal.Zero;

                    totalUPAmount += appliedPriceList.Contains("gsc_applyamounttoup")
                        ? appliedPriceList.GetAttributeValue<Money>("gsc_applyamounttoup").Value
                        : Decimal.Zero;
                }

                if (quoteDiscountEntity.Contains("gsc_discountamount") && message.Equals("Delete"))
                {
                    _tracingService.Trace("Subtract Deleted Discount from Total Discount...");

                    totalDiscountAmount = totalDiscountAmount - (Decimal)quoteDiscountEntity.GetAttributeValue<Money>("gsc_discountamount").Value;

                    totalDPAmount = totalDPAmount - (quoteDiscountEntity.Contains("gsc_applyamounttodp") 
                    ? quoteDiscountEntity.GetAttributeValue<Money>("gsc_applyamounttodp").Value
                    : 0);

                    totalAFAmount = totalAFAmount -  (quoteDiscountEntity.Contains("gsc_applyamounttoaf") 
                    ? quoteDiscountEntity.GetAttributeValue<Money>("gsc_applyamounttoaf").Value
                    : 0);

                    totalUPAmount = totalUPAmount - (quoteDiscountEntity.Contains("gsc_applyamounttoaf") 
                    ? quoteDiscountEntity.GetAttributeValue<Money>("gsc_applyamounttoup").Value
                    : 0);
                }
            }


            //Retrieve Quote record from Quote field value
            EntityCollection quoteRecords = CommonHandler.RetrieveRecordsByOneValue("quote", "quoteid", quoteId, _organizationService, null, OrderType.Ascending,
                new[] { "totaldiscountamount", "gsc_applytodppercentage", "gsc_applytodpamount", "gsc_applytoafpercentage", "gsc_applytoafamount", 
                    "gsc_applytouppercentage", "gsc_applytoupamount", "statecode" });

            if (quoteRecords != null && quoteRecords.Entities.Count > 0 && quoteRecords.Entities[0].GetAttributeValue<OptionSetValue>("statecode").Value == 0)
            {
                _tracingService.Trace("Setting Up Quote to be Updated . . . ");

                Entity quotetoUpdate = quoteRecords.Entities[0];

                quotetoUpdate["totaldiscountamount"] = new Money(totalDiscountAmount);
                quotetoUpdate["gsc_applytodpamount"] = new Money(totalDPAmount);
                quotetoUpdate["gsc_applytoafamount"] = new Money(totalAFAmount);
                quotetoUpdate["gsc_applytoupamount"] = new Money(totalUPAmount);
                quotetoUpdate["gsc_applytodppercentage"] = ComputePercentage(totalDPAmount, totalDiscountAmount);
                quotetoUpdate["gsc_applytoafpercentage"] = ComputePercentage(totalAFAmount, totalDiscountAmount);
                quotetoUpdate["gsc_applytouppercentage"] = ComputePercentage(totalUPAmount, totalDiscountAmount);

                _organizationService.Update(quotetoUpdate);

                _tracingService.Trace("Updated Discounts Amount in Quote..");
                return quotetoUpdate;
            }

            _tracingService.Trace("Ended SetQuoteTotalDiscountAmount method..");

            return quoteDiscountEntity;
        }
Esempio n. 38
0
 public void ToStringRepresentation(Money money, string stringRepresentation)
 {
     Assert.Equal(stringRepresentation, money.ToString());
 }
Esempio n. 39
0
    private string TryMakeManualPayout(Money fee)
    {
        //Manual, add to payoutrequests
        PayoutRequest req = new PayoutRequest();

        //Calculate fees for CustomPP
        Money Fees = fee;

        if (IsCustomPayoutProcessor)
        {
            CustomPayoutProcessor CPP = new CustomPayoutProcessor(CustomPayoutProcessorId);
            Fees = Fees + CPP.MoneyFee;
            Fees = Fees + (AmountToPayout * (CPP.PercentageFee * new Money(0.01)));

            if (string.IsNullOrWhiteSpace(TargetAccount))
            {
                throw new MsgException(U4200.ACCOUNTFIELDNOTBLANK);
            }
        }

        req.Amount      = AmountToPayout - Fees;
        req.IsPaid      = false;
        req.RequestDate = DateTime.Now;
        req.Username    = User.Name;
        req.IsRequest   = true;
        req.BalanceType = BalanceType.MainBalance;

        if (IsCustomPayoutProcessor)
        {
            //CustomPP
            CustomPayoutProcessor CPP = new CustomPayoutProcessor(CustomPayoutProcessorId);
            req.PaymentAddress   = TargetAccount;
            req.PaymentProcessor = CPP.Name;

            //MPesa check
            if (CPP.Name.ToLower() == "mpesa" && (TargetAccount.Length != 10 || !TargetAccount.StartsWith("07")))
            {
                throw new MsgException("Check your phone number format. The number should be 07******** (10 length)");
            }
        }
        else
        {
            string paymentAddress = PaymentAccountDetails.GetPaymentProcessorUserAccount(User, TargetPaymentProcessor);

            if (String.IsNullOrEmpty(paymentAddress))
            {
                throw new MsgException(U5004.YOUMUST);
            }

            if (TargetPaymentProcessor == "Payeer" && paymentAddress.Contains("@"))
            {
                throw new MsgException(U6006.VALIDPAYEER);
            }

            req.PaymentAddress   = paymentAddress;
            req.PaymentProcessor = TargetPaymentProcessor;
        }

        req.Save();

        User.SubtractFromMainBalance(AmountToPayout, "Payout");
        User.MoneyCashout += (AmountToPayout - Fees);
        User.IsPhoneVerifiedBeforeCashout = false;
        User.CashoutsProceed++;
        User.Save();

        //Update payout proportions
        if (!IsCustomPayoutProcessor)
        {
            PaymentProportionsManager.MemberPaidOut(AmountToPayout - Fees, PaymentAccountDetails.GetFromStringType(TargetPaymentProcessor), User, IsCustomPayoutProcessor);
        }

        if (IsAutomaticButAvoveTheLimit)
        {
            return(U3501.CASHOUTSUCC + ": " + U3500.CASHOUT_APPROVE.Replace("%n%", TheLimit.ToString()));
        }
        else
        {
            return(U3501.CASHOUTSUCC + ": " + U3500.CASHOUT_MESSAGE.Replace("%n1%", (AmountToPayout - Fees).ToString()).Replace("%n2%", Fees.ToString()));
        }
    }
Esempio n. 40
0
        public void HandlePacket(GameClient client, GSPacketIn packet)
        {
            byte isok    = (byte)packet.ReadByte();
            byte repair  = (byte)packet.ReadByte();
            byte combine = (byte)packet.ReadByte();

            packet.ReadByte();             // unknown

            ITradeWindow trade = client.Player.TradeWindow;

            if (trade == null)
            {
                return;
            }
            if (isok == 0)
            {
                trade.CloseTrade();
            }
            else if (isok == 1)
            {
                if (trade.Repairing != (repair == 1))
                {
                    trade.Repairing = (repair == 1);
                }
                if (trade.Combine != (combine == 1))
                {
                    trade.Combine = (combine == 1);
                }

                ArrayList tradeSlots = new ArrayList(10);
                for (int i = 0; i < 10; i++)
                {
                    int           slotPosition = packet.ReadByte();
                    InventoryItem item         = client.Player.Inventory.GetItem((eInventorySlot)slotPosition);
                    if (item != null &&
                        ((item.IsDropable && item.IsTradable) || (client.Player.CanTradeAnyItem ||
                                                                  trade is SelfCraftWindow ||
                                                                  (trade.Partner != null && trade.Partner.CanTradeAnyItem))))
                    {
                        tradeSlots.Add(item);
                    }
                }
                trade.TradeItems = tradeSlots;

                packet.ReadShort();

                int[] tradeMoney = new int[5];
                for (int i = 0; i < 5; i++)
                {
                    tradeMoney[i] = packet.ReadShort();
                }

                long money = Money.GetMoney(tradeMoney[0], tradeMoney[1], tradeMoney[2], tradeMoney[3], tradeMoney[4]);
                trade.TradeMoney = money;

                trade.TradeUpdate();
            }
            else if (isok == 2)
            {
                trade.AcceptTrade();
            }
        }
Esempio n. 41
0
    private string TryMakeInstantPayout(Money fee)
    {
        // payoutRequest --> change property to false (it isn't request)
        PayoutRequest req = new PayoutRequest();

        req.Amount      = AmountToPayout - fee;
        req.IsPaid      = true;
        req.RequestDate = DateTime.Now;
        req.Username    = User.Name;
        req.IsRequest   = false;
        req.BalanceType = BalanceType.MainBalance;

        //Check if daily limit is not reached
        if (AppSettings.Payments.GlobalCashoutsToday + AmountToPayout - fee > AppSettings.Payments.GlobalCashoutLimitPerDay)
        {
            throw new MsgException(L1.TOOMANYCASHOUTS + " " + AppSettings.Payments.GlobalCashoutLimitPerDay.ToString());
        }

        //User payment address
        string paymentAddress = PaymentAccountDetails.GetPaymentProcessorUserAccount(User, TargetPaymentProcessor);

        if (String.IsNullOrEmpty(paymentAddress))
        {
            throw new MsgException(U5004.YOUMUST);
        }

        request = new TransactionRequest(User.Name, paymentAddress, AmountToPayout - fee);
        Transaction transaction = TransactionFactory.CreateTransaction(request, TargetPaymentProcessor);

        response = transaction.Commit();

        req.PaymentAddress   = paymentAddress;
        req.PaymentProcessor = TargetPaymentProcessor;

        if (!response.IsSuccess)
        {
            if (request != null && response != null)
            {
                PayoutManager.logPayout("Payout unsuccessful", request, response, req.PaymentProcessor);
            }
            throw new MsgException(response.Note);
        }

        req.Save();

        History.AddCashout(User.Name, AmountToPayout);

        User.SubtractFromMainBalance(AmountToPayout, "Payout");
        User.MoneyCashout += AmountToPayout - fee;
        User.IsPhoneVerifiedBeforeCashout = false;
        User.CashoutsProceed++;
        User.Save();

        //Update payout proportions
        PaymentProportionsManager.MemberPaidOut(AmountToPayout - fee, PaymentAccountDetails.GetFromStringType(TargetPaymentProcessor), User, IsCustomPayoutProcessor);

        //Add to daily cashout
        AppSettings.Payments.GlobalCashoutsToday += AmountToPayout - fee;
        AppSettings.Payments.Save();

        //Add outcome to stats (Data2);
        var stats = new Statistics(StatisticsType.Cashflow);

        stats.AddToData2(AmountToPayout);
        stats.Save();

        //Add paymentproof
        PaymentProof.Add(User.Id, AmountToPayout, PaymentType.Instant, PaymentAccountDetails.GetFromStringType(TargetPaymentProcessor));

        // Log because payment may have response status SuccessWithWarning
        // More on this: https://developer.paypal.com/webapps/developer/docs/classic/api/NVPAPIOverview/
        logPayout("Payout successful", request, response, req.PaymentProcessor);

        return(U3501.AUTOMATICCASHOUTSUCC + ": " + response.Note);
    }
Esempio n. 42
0
    public static Money GetMaxPossiblePayout(PaymentProcessor processor, Member user, out string errorNote)
    {
        PaymentProportionsManager manager = new PaymentProportionsManager(user);

        decimal maxDailyPayout = user.Membership.MaxDailyCashout.ToDecimal() - user.PaidOutToday.ToDecimal();

        if (maxDailyPayout < 0)
        {
            maxDailyPayout = 0;
        }

        decimal maxGlobalPayout = user.Membership.MaxGlobalCashout.ToDecimal() - user.MoneyCashout.ToDecimal();

        decimal maxFromProcessor  = 0m;
        decimal maxSinglePayout   = 0m;
        decimal maxBasedOnAdPacks = 0m;

        if (AppSettings.Payments.MaximumPayoutPolicy == MaximumPayoutPolicy.Constant)
        {
            maxSinglePayout = AppSettings.Payments.MaximumPayoutConstant.ToDecimal();
        }
        else if (AppSettings.Payments.MaximumPayoutPolicy == MaximumPayoutPolicy.Percentage)
        {
            maxSinglePayout = Money.MultiplyPercent(user.MainBalance, AppSettings.Payments.MaximumPayoutPercentage).ToDecimal();
        }

        Dictionary <string, decimal> dic = new Dictionary <string, decimal>();

        dic.Add(L1.MEMBERSHIP, maxDailyPayout);
        dic.Add(U6011.PAYMENTS, maxSinglePayout);
        dic.Add(U5002.YOUCANNOTCASHOUTLIMIT, maxGlobalPayout);

        if (AppSettings.Payments.ProportionalPayoutLimitsEnabled && processor != PaymentProcessor.CustomPayoutProcessor)
        {
            maxFromProcessor = manager.GetMaximum(processor).ToDecimal();
            dic.Add(string.Format(U6011.PROPORTIONALLIMITSPROCESSOR), maxFromProcessor);
        }

        if (AppSettings.Payments.AdPackTypeWithdrawLimitEnabled)
        {
            maxBasedOnAdPacks = (AdPackTypeManager.GetWithdrawalLimit(user.Id) - user.MoneyCashout).ToDecimal();

            if (maxBasedOnAdPacks < 0m)
            {
                maxBasedOnAdPacks = 0m;
            }

            dic.Add(AppSettings.RevShare.AdPack.AdPackNamePlural, maxBasedOnAdPacks);
        }

        //Maximum withdrawal of deposited amount %
        if (user.Membership.MaxWithdrawalAllowedPerInvestmentPercent < 1000000000)
        {
            PaymentProportionsManager ppm = new PaymentProportionsManager(user);
            Money invested    = ppm.TotalPaidIn;
            Money withdrawn   = ppm.TotalPaidOut;
            Money canwithdraw = Money.MultiplyPercent(invested, user.Membership.MaxWithdrawalAllowedPerInvestmentPercent);

            dic.Add(U6011.MEMBERSHIPMAXWITDRAWAL, (canwithdraw - withdrawn).ToDecimal());
        }

        var min   = dic.OrderBy(x => x.Value).First();
        var money = new Money(min.Value);

        errorNote = string.Format(U6011.PAYOUTREQUESTTOOHIGH, money.ToString(), min.Key);

        if (money > user.MainBalance)
        {
            errorNote = string.Format(U6012.PAYOUTREQUESTBALANCEERROR, user.MainBalance.ToString());
            money     = user.MainBalance;
        }

        return(money > Money.Zero ? money : Money.Zero);
    }
Esempio n. 43
0
 public void CreateSubClass(String code, String name, List <Airport> hub_airports, Money startmoney = null)
 {
     this._Code = code;
     this._Name = Name;
     foreach (Airport hub_airport in hub_airports)
     {
         Hub hub = new Hub(hub_airport, this);
         this._Hubs.Add(hub.Airport.Iata, hub);
         this._Bases.Add(hub);
     }
 }
Esempio n. 44
0
    public string TryMakePayout()
    {
        Money withdrawalFee = Money.Zero;

        if (!IsCustomPayoutProcessor)
        {
            var processor = PaymentAccountDetails.GetFirstGateway(TargetPaymentProcessor);
            withdrawalFee = Money.MultiplyPercent(AmountToPayout, processor.WithdrawalFeePercent);

            var address = UsersPaymentProcessorsAddress.GetAddress(User.Id, new PaymentProcessorInfo((PaymentProcessor)Enum.Parse(typeof(PaymentProcessor), processor.AccountType)));
            if (address != null)
            {
                if (TargetAccount != address.PaymentAddress)
                {
                    throw new MsgException("Don't try to use account ID which is different from your current one. To change account ID go to user settings panel.");
                }

                if (address.LastChanged.AddDays(processor.DaysToBlockWithdrawalsAfterAccountChangename) > AppSettings.ServerTime)
                {
                    throw new MsgException(string.Format(U6007.CANTWITHDRAWDAYSLIMIT, (address.LastChanged.AddDays(processor.DaysToBlockWithdrawalsAfterAccountChangename).DayOfYear - AppSettings.ServerTime.DayOfYear)));
                }
            }
            else
            {
                User.SetPaymentAddress(processor.Id, TargetAccount);
            }
        }
        else
        {
            var blockingDays = new CustomPayoutProcessor(CustomPayoutProcessorId).DaysToBlockWithdrawalsAfterAccounChange;
            var address      = UsersPaymentProcessorsAddress.GetAddress(User.Id, new PaymentProcessorInfo(CustomPayoutProcessorId));
            if (address != null)
            {
                if (TargetAccount != address.PaymentAddress)
                {
                    throw new MsgException("Don't try to use account ID which is different from your current one. To change account ID go to user settings panel.");
                }

                if (address.LastChanged.AddDays(blockingDays) > AppSettings.ServerTime)
                {
                    throw new MsgException(string.Format(U6007.CANTWITHDRAWDAYSLIMIT, (address.LastChanged.AddDays(blockingDays).DayOfYear - AppSettings.ServerTime.DayOfYear)));
                }
            }
            else
            {
                User.SetPaymentAddress(CustomPayoutProcessorId, TargetAccount);
            }
        }

        ValidatePayout(User, AmountToPayout);

        //Check the gateway limit (global limit, pp custom limit)
        CheckBaseLimits();

        //CLP ---> extension for private client CLP
        User = Titan.CLP.CLPManager.CheckCashoutBonus(User, AmountToPayout);

        if (IsManualPayout()) //Decide if we go with automatic or manual
        {
            return(TryMakeManualPayout(withdrawalFee));
        }
        else
        {
            return(TryMakeInstantPayout(withdrawalFee));
        }

        return("");
    }
Esempio n. 45
0
 // Change the default value
 public TestMemPoolEntryHelper Fee(Money fee)
 {
     this.nFee = fee; return(this);
 }
 public SmartContractTransactionPolicy(Network network) : base(network)
 {
     // Allow bigger fees to be sent as we include gas as well.
     this.MaxTxFee = new FeeRate(Money.Coins(10));
 }
 public AutoSpotContractPricingComponent(Money tradeVolume, Money settlementVolume, Pricing.Rate customerSpotRate, Pricing.Rate costSpotRate, RateDirection rateDirection, TradeDirection tradeDirection, DateRange startEndDates, bool isAmountInSettlementCurrency) : base(tradeVolume, settlementVolume, customerSpotRate, costSpotRate, rateDirection, tradeDirection, startEndDates, isAmountInSettlementCurrency)
 {
 }
        /// <summary>
        /// This method deserialize xml data to object. Use this method when you don't know the type yet.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        static internal object ObjectFromXml(XElement item)
        {
            if (item == null || item.Value == "")
            {
                return(null);
            }
            Object value = "";
            // Obtain CrmType. I haven't cover all types yet.
            string CrmType = item.Attribute(Util.ns.i + "type").Value.Substring(2);

            switch (CrmType)
            {
            case "long":
                value = Util.LoadFromXml <long>(item);
                break;

            case "decimal":
                value = Util.LoadFromXml <decimal>(item);
                break;

            case "string":
                value = item.Value;
                break;

            case "base64Binary":
                value = Convert.FromBase64String(item.Value);
                break;

            case "int":
                value = Util.LoadFromXml <int>(item);
                break;

            case "double":
                value = Util.LoadFromXml <double>(item);
                break;

            case "dateTime":
                value = Util.LoadFromXml <DateTime>(item);
                break;

            case "guid":
                value = Util.LoadFromXml <Guid>(item);
                break;

            case "boolean":
                value = Util.LoadFromXml <bool>(item);
                break;

            case "EntityReference":
                value = EntityReference.LoadFromXml(item);
                break;

            case "AliasedValue":
                value = AliasedValue.LoadFromXml(item);
                break;

            case "OptionSetValue":
                value = OptionSetValue.LoadFromXml(item);
                break;

            case "Money":
                value = Money.LoadFromXml(item);
                break;

            case "EntityCollection":
                value = EntityCollection.LoadFromXml(item);
                break;

            default:
                break;
            }
            return(value);
        }
 /// <summary>
 /// Creates the transaction build context.
 /// </summary>
 /// <param name="network">The network that the context is for.</param>
 /// <param name="accountReference">The wallet account providing the funds.</param>
 /// <param name="password">the wallet password.</param>
 /// <param name="destinationScript">The destination script where we are sending the funds to.</param>
 /// <param name="amount">the amount of money to send.</param>
 /// <param name="feeType">The fee type.</param>
 /// <param name="minConfirmations">The minimum number of confirmations.</param>
 /// <returns>The transaction build context.</returns>
 private static TransactionBuildContext CreateContext(Network network, WalletAccountReference accountReference, string password,
                                                      Script destinationScript, Money amount, FeeType feeType, int minConfirmations)
 {
     return(new TransactionBuildContext(network)
     {
         AccountReference = accountReference,
         MinConfirmations = minConfirmations,
         FeeType = feeType,
         WalletPassword = password,
         Recipients = new[] { new Recipient {
                                  Amount = amount, ScriptPubKey = destinationScript
                              } }.ToList()
     });
 }
Esempio n. 50
0
        public void MempoolAncestorIndexingTest()
        {
            NodeSettings settings = NodeSettings.Default();
            var          pool     = new TxMempool(DateTimeProvider.Default, new BlockPolicyEstimator(new MempoolSettings(settings), settings.LoggerFactory, settings), settings.LoggerFactory, settings);
            var          entry    = new TestMemPoolEntryHelper();

            /* 3rd highest fee */
            var tx1 = new Transaction();

            tx1.AddOutput(new TxOut(new Money(10 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx1.GetHash(), entry.Fee(new Money(10000L)).Priority(10.0).FromTx(tx1));

            /* highest fee */
            var tx2 = new Transaction();

            tx2.AddOutput(new TxOut(new Money(2 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx2.GetHash(), entry.Fee(new Money(20000L)).Priority(9.0).FromTx(tx2));
            int tx2Size = tx2.GetVirtualSize();

            /* lowest fee */
            var tx3 = new Transaction();

            tx3.AddOutput(new TxOut(new Money(5 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx3.GetHash(), entry.Fee(new Money(0L)).Priority(100.0).FromTx(tx3));

            /* 2nd highest fee */
            var tx4 = new Transaction();

            tx4.AddOutput(new TxOut(new Money(6 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx4.GetHash(), entry.Fee(new Money(15000L)).Priority(1.0).FromTx(tx4));

            /* equal fee rate to tx1, but newer */
            var tx5 = new Transaction();

            tx5.AddOutput(new TxOut(new Money(11 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx5.GetHash(), entry.Fee(new Money(10000L)).Priority(1.0).FromTx(tx5));

            // assert size
            Assert.Equal(5, pool.Size);

            var sortedOrder = new List <string>(5);

            sortedOrder.Insert(0, tx2.GetHash().ToString()); // 20000
            sortedOrder.Insert(1, tx4.GetHash().ToString()); // 15000

            // tx1 and tx5 are both 10000
            // Ties are broken by hash, not timestamp, so determine which
            // hash comes first.
            if (tx1.GetHash() < tx5.GetHash())
            {
                sortedOrder.Insert(2, tx1.GetHash().ToString());
                sortedOrder.Insert(3, tx5.GetHash().ToString());
            }
            else
            {
                sortedOrder.Insert(2, tx5.GetHash().ToString());
                sortedOrder.Insert(3, tx1.GetHash().ToString());
            }
            sortedOrder.Insert(4, tx3.GetHash().ToString()); // 0
            this.CheckSort(pool, pool.MapTx.AncestorScore.ToList(), sortedOrder);

            /* low fee parent with high fee child */
            /* tx6 (0) -> tx7 (high) */
            var tx6 = new Transaction();

            tx6.AddOutput(new TxOut(new Money(20 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            pool.AddUnchecked(tx6.GetHash(), entry.Fee(new Money(0L)).FromTx(tx6));
            int tx6Size = tx6.GetVirtualSize();

            Assert.Equal(6, pool.Size);
            // Ties are broken by hash
            if (tx3.GetHash() < tx6.GetHash())
            {
                sortedOrder.Add(tx6.GetHash().ToString());
            }
            else
            {
                sortedOrder.Insert(sortedOrder.Count - 1, tx6.GetHash().ToString());
            }
            this.CheckSort(pool, pool.MapTx.AncestorScore.ToList(), sortedOrder);

            var tx7 = new Transaction();

            tx7.AddInput(new TxIn(new OutPoint(tx6.GetHash(), 0), new Script(OpcodeType.OP_11)));
            tx7.AddOutput(new TxOut(new Money(10 * Money.COIN), new Script(OpcodeType.OP_11, OpcodeType.OP_EQUAL)));
            int   tx7Size = tx7.GetVirtualSize();
            Money fee     = (20000 / tx2Size) * (tx7Size + tx6Size) - 1;

            pool.AddUnchecked(tx7.GetHash(), entry.Fee(fee).FromTx(tx7));
            Assert.Equal(7, pool.Size);
            sortedOrder.Insert(1, tx7.GetHash().ToString());
            this.CheckSort(pool, pool.MapTx.AncestorScore.ToList(), sortedOrder);

            /* after tx6 is mined, tx7 should move up in the sort */
            var vtx = new List <Transaction>(new[] { tx6 });

            pool.RemoveForBlock(vtx, 1);

            sortedOrder.RemoveAt(1);
            // Ties are broken by hash
            if (tx3.GetHash() < tx6.GetHash())
            {
                sortedOrder.Remove(sortedOrder.Last());
            }
            else
            {
                sortedOrder.RemoveAt(sortedOrder.Count - 2);
            }
            sortedOrder.Insert(0, tx7.GetHash().ToString());
            this.CheckSort(pool, pool.MapTx.AncestorScore.ToList(), sortedOrder);
        }
Esempio n. 51
0
        public void Charge_money_will_throw_exception_when_no_enough_balance()
        {
            var chargeMoney = new Money(_currencyInPeso, 2000M);

            Assert.Throws <InsufficientBalanceException>(() => balance.ChargeMoney(chargeMoney));
        }
Esempio n. 52
0
        public async Task TestServicesAsync(string networkString)
        {
            await RuntimeParams.LoadAsync();

            var network          = Network.GetNetwork(networkString);
            var blocksToDownload = new HashSet <uint256>();

            if (network == Network.Main)
            {
                blocksToDownload.Add(new uint256("00000000000000000037c2de35bd85f3e57f14ddd741ce6cee5b28e51473d5d0"));
                blocksToDownload.Add(new uint256("000000000000000000115315a43cb0cdfc4ea54a0e92bed127f4e395e718d8f9"));
                blocksToDownload.Add(new uint256("00000000000000000011b5b042ad0522b69aae36f7de796f563c895714bbd629"));
            }
            else if (network == Network.TestNet)
            {
                blocksToDownload.Add(new uint256("0000000097a664c4084b49faa6fd4417055cb8e5aac480abc31ddc57a8208524"));
                blocksToDownload.Add(new uint256("000000009ed5b82259ecd2aa4cd1f119db8da7a70e7ea78d9c9f603e01f93bcc"));
                blocksToDownload.Add(new uint256("00000000e6da8c2da304e9f5ad99c079df2c3803b49efded3061ecaf206ddc66"));
            }
            else
            {
                throw new NotSupportedNetworkException(network);
            }

            var            addressManagerFolderPath = Path.Combine(Global.Instance.DataDir, "AddressManager");
            var            addressManagerFilePath   = Path.Combine(addressManagerFolderPath, $"AddressManager{network}.dat");
            var            blocksFolderPath         = Path.Combine(Global.Instance.DataDir, "Blocks", network.ToString());
            var            connectionParameters     = new NodeConnectionParameters();
            AddressManager addressManager           = null;

            try
            {
                addressManager = await NBitcoinHelpers.LoadAddressManagerFromPeerFileAsync(addressManagerFilePath);

                Logger.LogInfo($"Loaded {nameof(AddressManager)} from `{addressManagerFilePath}`.");
            }
            catch (DirectoryNotFoundException)
            {
                addressManager = new AddressManager();
            }
            catch (FileNotFoundException)
            {
                addressManager = new AddressManager();
            }
            catch (OverflowException)
            {
                File.Delete(addressManagerFilePath);
                addressManager = new AddressManager();
            }
            catch (FormatException)
            {
                File.Delete(addressManagerFilePath);
                addressManager = new AddressManager();
            }

            connectionParameters.TemplateBehaviors.Add(new AddressManagerBehavior(addressManager));
            var mempoolService = new MempoolService();

            connectionParameters.TemplateBehaviors.Add(new MempoolBehavior(mempoolService));

            var nodes = new NodesGroup(network, connectionParameters, requirements: Constants.NodeRequirements);

            BitcoinStore bitcoinStore = new BitcoinStore();
            await bitcoinStore.InitializeAsync(Path.Combine(Global.Instance.DataDir, EnvironmentHelpers.GetMethodName()), network);

            KeyManager         keyManager    = KeyManager.CreateNew(out _, "password");
            WasabiSynchronizer syncer        = new WasabiSynchronizer(network, bitcoinStore, new Uri("http://localhost:12345"), Global.Instance.TorSocks5Endpoint);
            WalletService      walletService = new WalletService(
                bitcoinStore,
                keyManager,
                syncer,
                new CcjClient(syncer, network, keyManager, new Uri("http://localhost:12345"), Global.Instance.TorSocks5Endpoint),
                mempoolService,
                nodes,
                Global.Instance.DataDir,
                new ServiceConfiguration(50, 2, 21, 50, new IPEndPoint(IPAddress.Loopback, network.DefaultPort), Money.Coins(0.0001m)));

            Assert.True(Directory.Exists(blocksFolderPath));

            try
            {
                mempoolService.TransactionReceived += MempoolService_TransactionReceived;

                nodes.Connect();
                var times = 0;
                while (nodes.ConnectedNodes.Count < 3)
                {
                    if (times > 4200)                     // 7 minutes
                    {
                        throw new TimeoutException("Connection test timed out.");
                    }
                    await Task.Delay(100);

                    times++;
                }

                times = 0;
                while (Interlocked.Read(ref _mempoolTransactionCount) < 3)
                {
                    if (times > 3000)                     // 3 minutes
                    {
                        throw new TimeoutException($"{nameof(MempoolService)} test timed out.");
                    }
                    await Task.Delay(100);

                    times++;
                }

                foreach (var hash in blocksToDownload)
                {
                    using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)))
                    {
                        var block = await walletService.FetchBlockAsync(hash, cts.Token);

                        Assert.True(File.Exists(Path.Combine(blocksFolderPath, hash.ToString())));
                        Logger.LogInfo($"Full block is downloaded: {hash}.");
                    }
                }
            }
            finally
            {
                nodes.ConnectedNodes.Added         -= ConnectedNodes_Added;
                nodes.ConnectedNodes.Removed       -= ConnectedNodes_Removed;
                mempoolService.TransactionReceived -= MempoolService_TransactionReceived;

                // So next test will download the block.
                foreach (var hash in blocksToDownload)
                {
                    await walletService?.DeleteBlockAsync(hash);
                }
                if (walletService != null)
                {
                    await walletService.StopAsync();
                }

                if (Directory.Exists(blocksFolderPath))
                {
                    Directory.Delete(blocksFolderPath, recursive: true);
                }

                IoHelpers.EnsureContainingDirectoryExists(addressManagerFilePath);
                addressManager?.SavePeerFile(addressManagerFilePath, network);
                Logger.LogInfo($"Saved {nameof(AddressManager)} to `{addressManagerFilePath}`.");
                nodes?.Dispose();

                await syncer?.StopAsync();
            }
        }
Esempio n. 53
0
        public void confirmbuyvehicle_cmd(Client player)
        {
            Character character = player.GetCharacter();
            Account   account   = player.GetAccount();
            var       data      = API.GetEntityData(player, "sellcar_buying");

            if (data != null)
            {
                Character buyingFrom = data[0];
                Vehicle   veh        = data[1];
                int       price      = data[2];
                //Make sure near him.
                var buyingPos = buyingFrom.Client.position;
                if (player.position.DistanceTo(buyingPos) <= 5f)
                {
                    //make sure still have slots.
                    if (character.OwnedVehicles.Count < VehicleManager.GetMaxOwnedVehicles(character.Client))
                    {
                        //make sure have money.
                        if (Money.GetCharacterMoney(character) >= price)
                        {
                            //Do actual process.
                            InventoryManager.GiveInventoryItem(buyingFrom, new Money(), price);
                            InventoryManager.DeleteInventoryItem(character, typeof(Money), price);
                            veh.OwnerId   = character.Id;
                            veh.OwnerName = character.CharacterName;
                            veh.Save();

                            //DONE, now spawn if hes vip.
                            if (!veh.IsSpawned)
                            {
                                //He won't be able to buy it anyways if he wasn't VIP... so I THINK he can now have it spawned, right ? :/
                                if (VehicleManager.spawn_vehicle(veh) != 1)
                                {
                                    API.ConsoleOutput(
                                        $"There was an error spawning vehicle #{veh.Id} of {character.CharacterName}.");
                                }
                            }

                            //Tell.
                            API.SendChatMessageToPlayer(player, "You have sucessfully bought the car.");
                            API.SendChatMessageToPlayer(buyingFrom.Client, "You have successfully sold the car.");
                            API.SetEntityData(player, "sellcar_buying", null);
                        }
                        else
                        {
                            API.SendChatMessageToPlayer(player, "You don't have enough money.");
                            API.SendChatMessageToPlayer(buyingFrom.Client, "The buyer doesn't have enough money.");
                            API.SetEntityData(player, "sellcar_buying", null);
                        }
                    }
                    else
                    {
                        API.SendChatMessageToPlayer(player, "You can't own anymore vehicles.");
                        API.SendChatMessageToPlayer(buyingFrom.Client, "The buyer can't own anymore vehicles.");
                        API.SetEntityData(player, "sellcar_buying", null);
                    }
                }
                else
                {
                    API.SendChatMessageToPlayer(player, "You must be near the buyer.");
                    API.SendChatMessageToPlayer(buyingFrom.Client, "The buyer must be near you.");
                    API.SetEntityData(player, "sellcar_buying", null);
                }
            }
            else
            {
                API.SendChatMessageToPlayer(player, "You aren't buying any car.");
            }
        }
        public async Task WalletCanMineWithColdWalletCoinsAsync()
        {
            using (var builder = NodeBuilder.Create(this))
            {
                var network = new StraxRegTest();

                CoreNode stratisSender    = CreatePowPosMiningNode(builder, network, TestBase.CreateTestDir(this), coldStakeNode: false);
                CoreNode stratisHotStake  = CreatePowPosMiningNode(builder, network, TestBase.CreateTestDir(this), coldStakeNode: true);
                CoreNode stratisColdStake = CreatePowPosMiningNode(builder, network, TestBase.CreateTestDir(this), coldStakeNode: true);

                stratisSender.WithReadyBlockchainData(ReadyBlockchain.StraxRegTest150Miner).Start();
                stratisHotStake.WithWallet().Start();
                stratisColdStake.WithWallet().Start();

                var senderWalletManager = stratisSender.FullNode.WalletManager() as ColdStakingManager;
                var coldWalletManager   = stratisColdStake.FullNode.WalletManager() as ColdStakingManager;
                var hotWalletManager    = stratisHotStake.FullNode.WalletManager() as ColdStakingManager;

                // Set up cold staking account on cold wallet.
                coldWalletManager.GetOrCreateColdStakingAccount(WalletName, true, Password, null);
                HdAddress coldWalletAddress = coldWalletManager.GetFirstUnusedColdStakingAddress(WalletName, true);

                // Set up cold staking account on hot wallet.
                hotWalletManager.GetOrCreateColdStakingAccount(WalletName, false, Password, null);
                HdAddress hotWalletAddress = hotWalletManager.GetFirstUnusedColdStakingAddress(WalletName, false);

                var  walletAccountReference = new WalletAccountReference(WalletName, Account);
                long total2 = stratisSender.FullNode.WalletManager().GetSpendableTransactionsInAccount(walletAccountReference, 1).Sum(s => s.Transaction.Amount);

                // Sync all nodes
                TestHelper.ConnectAndSync(stratisHotStake, stratisSender);
                TestHelper.ConnectAndSync(stratisHotStake, stratisColdStake);
                TestHelper.Connect(stratisSender, stratisColdStake);

                // Send coins to hot wallet.
                Money     amountToSend = total2 - network.Consensus.ProofOfWorkReward;
                HdAddress sendto       = hotWalletManager.GetUnusedAddress(new WalletAccountReference(WalletName, Account));

                Transaction transaction1 = stratisSender.FullNode.WalletTransactionHandler().BuildTransaction(CreateContext(stratisSender.FullNode.Network, new WalletAccountReference(WalletName, Account), Password, sendto.ScriptPubKey, amountToSend, FeeType.Medium, 1));

                // Broadcast to the other node
                await stratisSender.FullNode.NodeController <WalletController>().SendTransactionAsync(new SendTransactionRequest(transaction1.ToHex()));

                // Wait for the transaction to arrive
                TestBase.WaitLoop(() => stratisHotStake.CreateRPCClient().GetRawMempool().Length > 0);
                Assert.NotNull(stratisHotStake.CreateRPCClient().GetRawTransaction(transaction1.GetHash(), null, false));
                TestBase.WaitLoop(() => stratisHotStake.FullNode.WalletManager().GetSpendableTransactionsInWallet(WalletName).Any());

                long receiveTotal = stratisHotStake.FullNode.WalletManager().GetSpendableTransactionsInWallet(WalletName).Sum(s => s.Transaction.Amount);
                Assert.Equal(amountToSend, (Money)receiveTotal);
                Assert.Null(stratisHotStake.FullNode.WalletManager().GetSpendableTransactionsInWallet(WalletName).First().Transaction.BlockHeight);

                // Setup cold staking from the hot wallet.
                Money amountToSend2 = receiveTotal - network.Consensus.ProofOfWorkReward;
                (Transaction transaction2, _) = hotWalletManager.GetColdStakingSetupTransaction(stratisHotStake.FullNode.WalletTransactionHandler(),
                                                                                                coldWalletAddress.Address, hotWalletAddress.Address, WalletName, Account, Password, amountToSend2, new Money(0.02m, MoneyUnit.BTC), false, false, 1, false);

                // Broadcast to the other node
                await stratisHotStake.FullNode.NodeController <WalletController>().SendTransactionAsync(new SendTransactionRequest(transaction2.ToHex()));

                // Wait for the transaction to arrive
                TestBase.WaitLoop(() => coldWalletManager.GetSpendableTransactionsInColdWallet(WalletName, true).Any());

                long receivetotal2 = coldWalletManager.GetSpendableTransactionsInColdWallet(WalletName, true).Sum(s => s.Transaction.Amount);
                Assert.Equal(amountToSend2, (Money)receivetotal2);
                Assert.Null(coldWalletManager.GetSpendableTransactionsInColdWallet(WalletName, true).First().Transaction.BlockHeight);

                // Allow coins to reach maturity
                int stakingMaturity = ((PosConsensusOptions)network.Consensus.Options).GetStakeMinConfirmations(0, network);
                TestHelper.MineBlocks(stratisSender, stakingMaturity, true);

                // Start staking.
                var hotMiningFeature = stratisHotStake.FullNode.NodeFeature <MiningFeature>();
                hotMiningFeature.StartStaking(WalletName, Password);

                TestBase.WaitLoop(() =>
                {
                    var stakingInfo = stratisHotStake.FullNode.NodeService <IPosMinting>().GetGetStakingInfoModel();
                    return(stakingInfo.Staking);
                });

                // Wait for money from staking.
                var cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(3)).Token;
                TestBase.WaitLoop(() =>
                {
                    // Keep mining to ensure that staking outputs reach maturity.
                    TestHelper.MineBlocks(stratisSender, 1, true);
                    return(coldWalletManager.GetSpendableTransactionsInColdWallet(WalletName, true).Sum(s => s.Transaction.Amount) > receivetotal2);
                }, cancellationToken: cancellationToken);
            }
        }
Esempio n. 55
0
        public void InvalidCredentialRequests()
        {
            var numberOfCredentials = 3;
            var rnd = new SecureRandom();
            var sk  = new CoordinatorSecretKey(rnd);

            var issuer = new CredentialIssuer(sk, numberOfCredentials, rnd);
            {
                var client = new WabiSabiClient(sk.ComputeCoordinatorParameters(), numberOfCredentials, rnd);

                // Null request. This requests `numberOfCredentials` zero-value credentials.
                var(credentialRequest, validationData) = client.CreateRequestForZeroAmount();

                var credentialResponse = issuer.HandleRequest(credentialRequest);
                client.HandleResponse(credentialResponse, validationData);

                var(validCredentialRequest, _) = client.CreateRequest(new Money[0], client.Credentials.ZeroValue.Take(1));

                // Test incorrect number of presentations (one instead of 3)
                var presented = validCredentialRequest.Presented.ToArray();
                var invalidCredentialRequest = new RegistrationRequest(
                    validCredentialRequest.DeltaAmount,
                    new[] { presented[0] },                     // Should present 3 credentials
                    validCredentialRequest.Requested,
                    validCredentialRequest.Proofs);

                var ex = Assert.Throws <WabiSabiException>(() => issuer.HandleRequest(invalidCredentialRequest));
                Assert.Equal(WabiSabiErrorCode.InvalidNumberOfPresentedCredentials, ex.ErrorCode);
                Assert.Equal("3 credential presentations were expected but 1 were received.", ex.Message);

                // Test incorrect number of presentations (0 instead of 3)
                presented = credentialRequest.Presented.ToArray();
                invalidCredentialRequest = new RegistrationRequest(
                    Money.Coins(2),
                    new CredentialPresentation[0],                     // Should present 3 credentials
                    validCredentialRequest.Requested,
                    validCredentialRequest.Proofs);

                ex = Assert.Throws <WabiSabiException>(() => issuer.HandleRequest(invalidCredentialRequest));
                Assert.Equal(WabiSabiErrorCode.InvalidNumberOfPresentedCredentials, ex.ErrorCode);
                Assert.Equal("3 credential presentations were expected but 0 were received.", ex.Message);

                (validCredentialRequest, _) = client.CreateRequest(new Money[0], client.Credentials.All);

                // Test incorrect number of credential requests
                invalidCredentialRequest = new RegistrationRequest(
                    validCredentialRequest.DeltaAmount,
                    validCredentialRequest.Presented,
                    validCredentialRequest.Requested.Take(1),
                    validCredentialRequest.Proofs);

                ex = Assert.Throws <WabiSabiException>(() => issuer.HandleRequest(invalidCredentialRequest));
                Assert.Equal(WabiSabiErrorCode.InvalidNumberOfRequestedCredentials, ex.ErrorCode);
                Assert.Equal("3 credential requests were expected but 1 were received.", ex.Message);

                // Test incorrect number of credential requests
                invalidCredentialRequest = new RegistrationRequest(
                    Money.Coins(2),
                    new CredentialPresentation[0],
                    validCredentialRequest.Requested.Take(1),
                    validCredentialRequest.Proofs);

                ex = Assert.Throws <WabiSabiException>(() => issuer.HandleRequest(invalidCredentialRequest));
                Assert.Equal(WabiSabiErrorCode.InvalidNumberOfRequestedCredentials, ex.ErrorCode);
                Assert.Equal("3 credential requests were expected but 1 were received.", ex.Message);

                // Test invalid range proof
                var requested = validCredentialRequest.Requested.ToArray();

                invalidCredentialRequest = new RegistrationRequest(
                    validCredentialRequest.DeltaAmount,
                    validCredentialRequest.Presented,
                    new[] { requested[0], requested[1], new IssuanceRequest(requested[2].Ma, new[] { GroupElement.Infinity }) },
                    validCredentialRequest.Proofs);

                ex = Assert.Throws <WabiSabiException>(() => issuer.HandleRequest(invalidCredentialRequest));
                Assert.Equal(WabiSabiErrorCode.InvalidBitCommitment, ex.ErrorCode);
            }

            {
                var client = new WabiSabiClient(sk.ComputeCoordinatorParameters(), numberOfCredentials, rnd);
                var(validCredentialRequest, validationData) = client.CreateRequestForZeroAmount();

                // Test invalid proofs
                var proofs = validCredentialRequest.Proofs.ToArray();
                proofs[0] = proofs[1];
                var invalidCredentialRequest = new RegistrationRequest(
                    validCredentialRequest.DeltaAmount,
                    validCredentialRequest.Presented,
                    validCredentialRequest.Requested,
                    proofs);

                var ex = Assert.Throws <WabiSabiException>(() => issuer.HandleRequest(invalidCredentialRequest));
                Assert.Equal(WabiSabiErrorCode.CoordinatorReceivedInvalidProofs, ex.ErrorCode);
            }

            {
                var client = new WabiSabiClient(sk.ComputeCoordinatorParameters(), numberOfCredentials, rnd);
                var(validCredentialRequest, validationData) = client.CreateRequestForZeroAmount();

                var credentialResponse = issuer.HandleRequest(validCredentialRequest);
                client.HandleResponse(credentialResponse, validationData);

                (validCredentialRequest, validationData) = client.CreateRequest(Enumerable.Empty <Money>(), client.Credentials.All);

                issuer.HandleRequest(validCredentialRequest);
                var ex = Assert.Throws <WabiSabiException>(() => issuer.HandleRequest(validCredentialRequest));
                Assert.Equal(WabiSabiErrorCode.SerialNumberAlreadyUsed, ex.ErrorCode);
            }
        }
Esempio n. 56
0
 public BuildTransactionResult(SmartTransaction transaction, bool spendsUnconfirmed, Money fee, decimal feePercentOfSent, IEnumerable <SmartCoin> outerWalletOutputs, IEnumerable <SmartCoin> innerWalletOutputs, IEnumerable <SmartCoin> spentCoins)
 {
     Transaction       = Guard.NotNull(nameof(transaction), transaction);
     SpendsUnconfirmed = spendsUnconfirmed;
     Fee = fee ?? Money.Zero;
     FeePercentOfSent   = feePercentOfSent;
     OuterWalletOutputs = outerWalletOutputs ?? new List <SmartCoin>();
     InnerWalletOutputs = innerWalletOutputs ?? new List <SmartCoin>();
     SpentCoins         = Guard.NotNullOrEmpty(nameof(spentCoins), spentCoins);
 }
Esempio n. 57
0
        public void CanGetTransactionInfo()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode();
                var rpc  = node.CreateRPCClient();
                builder.StartAll();

                var blocks          = node.Generate(101);
                var secondBlockHash = blocks.First();
                var secondBlock     = rpc.GetBlock(secondBlockHash);
                var firstTx         = secondBlock.Transactions.First();

                var txInfo = rpc.GetRawTransactionInfo(firstTx.GetHash());

                Assert.Equal(101U, txInfo.Confirmations);
                Assert.Equal(secondBlockHash, txInfo.BlockHash);
                Assert.Equal(firstTx.GetHash(), txInfo.TransactionId);
                Assert.Equal(secondBlock.Header.BlockTime, txInfo.BlockTime);
                Assert.Equal(firstTx.Version, txInfo.Version);
                Assert.Equal(firstTx.LockTime, txInfo.LockTime);
                Assert.Equal(firstTx.GetWitHash(), txInfo.Hash);
                Assert.Equal((uint)firstTx.GetSerializedSize(), txInfo.Size);
                Assert.Equal((uint)firstTx.GetVirtualSize(), txInfo.VirtualSize);

                // unconfirmed tx doesn't have blockhash, blocktime nor transactiontime.
                var mempoolTxId = rpc.SendToAddress(new Key().PubKey.GetAddress(builder.Network), Money.Coins(1));
                txInfo = rpc.GetRawTransactionInfo(mempoolTxId);
                Assert.Null(txInfo.TransactionTime);
                Assert.Null(txInfo.BlockHash);
                Assert.Null(txInfo.BlockTime);
                Assert.Equal(0U, txInfo.Confirmations);
            }
        }
Esempio n. 58
0
        public void CredentialIssuance()
        {
            var numberOfCredentials = 3;
            var rnd = new SecureRandom();
            var sk  = new CoordinatorSecretKey(rnd);

            var client = new WabiSabiClient(sk.ComputeCoordinatorParameters(), numberOfCredentials, rnd);

            {
                // Null request. This requests `numberOfCredentials` zero-value credentials.
                var(credentialRequest, validationData) = client.CreateRequestForZeroAmount();

                Assert.True(credentialRequest.IsNullRequest);
                Assert.Equal(numberOfCredentials, credentialRequest.Requested.Count());
                var requested = credentialRequest.Requested.ToArray();
                Assert.Empty(requested[0].BitCommitments);
                Assert.Empty(requested[1].BitCommitments);
                Assert.Empty(requested[2].BitCommitments);
                Assert.Equal(Money.Zero, credentialRequest.DeltaAmount);

                // Issuer part
                var issuer = new CredentialIssuer(sk, numberOfCredentials, rnd);

                var credentialResponse = issuer.HandleRequest(credentialRequest);
                client.HandleResponse(credentialResponse, validationData);
                Assert.Equal(numberOfCredentials, client.Credentials.ZeroValue.Count());
                Assert.Empty(client.Credentials.Valuable);
                var issuedCredential = client.Credentials.ZeroValue.First();
                Assert.True(issuedCredential.Amount.IsZero);
            }

            {
                var present = client.Credentials.ZeroValue.Take(numberOfCredentials);
                var(credentialRequest, validationData) = client.CreateRequest(new[] { Money.Coins(1) }, present);

                Assert.False(credentialRequest.IsNullRequest);
                var credentialRequested = credentialRequest.Requested.ToArray();
                Assert.Equal(numberOfCredentials, credentialRequested.Count());
                Assert.NotEmpty(credentialRequested[0].BitCommitments);
                Assert.NotEmpty(credentialRequested[1].BitCommitments);

                // Issuer part
                var issuer = new CredentialIssuer(sk, numberOfCredentials, rnd);

                var credentialResponse = issuer.HandleRequest(credentialRequest);
                client.HandleResponse(credentialResponse, validationData);
                var issuedCredential = Assert.Single(client.Credentials.Valuable);
                Assert.Equal(new Scalar(100_000_000), issuedCredential.Amount);

                Assert.Equal(2, client.Credentials.ZeroValue.Count());
                Assert.Equal(3, client.Credentials.All.Count());
            }

            {
                var valuableCredential = client.Credentials.Valuable.Take(1);
                var amounts            = Enumerable.Repeat(Money.Coins(0.5m), 2);
                var(credentialRequest, validationData) = client.CreateRequest(amounts, valuableCredential);

                Assert.False(credentialRequest.IsNullRequest);
                var requested = credentialRequest.Requested.ToArray();
                Assert.Equal(numberOfCredentials, requested.Count());
                Assert.NotEmpty(requested[0].BitCommitments);
                Assert.NotEmpty(requested[1].BitCommitments);
                Assert.Equal(Money.Zero, credentialRequest.DeltaAmount);

                // Issuer part
                var issuer = new CredentialIssuer(sk, numberOfCredentials, rnd);

                var credentialResponse = issuer.HandleRequest(credentialRequest);
                client.HandleResponse(credentialResponse, validationData);
                var credentials = client.Credentials.All.ToArray();
                Assert.NotEmpty(credentials);
                Assert.Equal(3, credentials.Count());

                var valuableCredentials = client.Credentials.Valuable.ToArray();
                Assert.Equal(new Scalar(50_000_000), valuableCredentials[0].Amount);
                Assert.Equal(new Scalar(50_000_000), valuableCredentials[1].Amount);
            }

            {
                var client0 = new WabiSabiClient(sk.ComputeCoordinatorParameters(), numberOfCredentials, rnd);
                var(credentialRequest, validationData) = client0.CreateRequestForZeroAmount();

                var issuer             = new CredentialIssuer(sk, numberOfCredentials, rnd);
                var credentialResponse = issuer.HandleRequest(credentialRequest);
                client0.HandleResponse(credentialResponse, validationData);

                (credentialRequest, validationData) = client0.CreateRequest(new[] { Money.Coins(1m) }, Enumerable.Empty <Credential>());

                credentialResponse = issuer.HandleRequest(credentialRequest);
                client0.HandleResponse(credentialResponse, validationData);

                (credentialRequest, validationData) = client0.CreateRequest(new Money[0], client0.Credentials.Valuable);

                credentialResponse = issuer.HandleRequest(credentialRequest);
                client0.HandleResponse(credentialResponse, validationData);
                Assert.NotEmpty(client0.Credentials.All);
                Assert.Equal(numberOfCredentials, client0.Credentials.All.Count());
            }
        }
Esempio n. 59
0
        public void CanGetMemPool()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode();
                var rpc  = node.CreateRPCClient();
                builder.StartAll();
                node.Generate(101);

                var txid        = rpc.SendToAddress(new Key().PubKey.GetAddress(rpc.Network), Money.Coins(1.0m), "hello", "world");
                var memPoolInfo = rpc.GetMemPool();
                Assert.NotNull(memPoolInfo);
                Assert.Equal(1, memPoolInfo.Size);
            }
        }
Esempio n. 60
0
        public void GetRawMemPoolWithValidTxThenReturnsSameTx()
        {
            using (NodeBuilder builder = NodeBuilder.Create())
            {
                CoreNode node = builder.CreateNode();
                builder.StartAll();

                RPCClient rpcClient = node.CreateRPCClient();

                // generate 101 blocks
                node.GenerateAsync(101).GetAwaiter().GetResult();

                uint256   txid = rpcClient.SendToAddress(new Key().PubKey.GetAddress(rpcClient.Network), Money.Coins(1.0m), "hello", "world");
                uint256[] ids  = rpcClient.GetRawMempool();
                Assert.Single(ids);
                Assert.Equal(txid, ids[0]);
            }
        }