2017-09-13 08:47:34 +02:00
|
|
|
using BTCPayServer.Tests.Logging;
|
|
|
|
using System.Linq;
|
|
|
|
using NBitcoin;
|
|
|
|
using NBitcoin.DataEncoders;
|
|
|
|
using NBitcoin.Payment;
|
|
|
|
using NBitpayClient;
|
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
using Xunit.Sdk;
|
2017-10-20 21:06:37 +02:00
|
|
|
using BTCPayServer.Services.Invoices;
|
2017-09-25 18:31:43 +02:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System.IO;
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-10-11 05:20:44 +02:00
|
|
|
using BTCPayServer.Controllers;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2017-10-13 11:06:46 +02:00
|
|
|
using BTCPayServer.Authentication;
|
2017-10-20 21:00:38 +02:00
|
|
|
using System.Diagnostics;
|
2017-10-24 18:41:01 +02:00
|
|
|
using Microsoft.EntityFrameworkCore.Extensions;
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-10-27 04:39:11 +02:00
|
|
|
using BTCPayServer.Services.Rates;
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2018-01-09 02:54:19 +01:00
|
|
|
using System.Collections.Generic;
|
2018-01-17 07:59:31 +01:00
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
2018-01-18 10:12:01 +01:00
|
|
|
using System.Threading.Tasks;
|
2018-02-17 05:18:16 +01:00
|
|
|
using System.Globalization;
|
2018-02-18 18:38:03 +01:00
|
|
|
using BTCPayServer.Payments;
|
2018-02-19 10:54:21 +01:00
|
|
|
using BTCPayServer.Payments.Bitcoin;
|
|
|
|
using BTCPayServer.HostedServices;
|
2018-02-25 16:48:12 +01:00
|
|
|
using BTCPayServer.Payments.Lightning;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
public class UnitTest1
|
|
|
|
{
|
|
|
|
public UnitTest1(ITestOutputHelper helper)
|
|
|
|
{
|
|
|
|
Logs.Tester = new XUnitLog(helper) { Name = "Tests" };
|
|
|
|
Logs.LogProvider = new XUnitLogProvider(helper);
|
|
|
|
}
|
|
|
|
|
2018-02-19 10:54:21 +01:00
|
|
|
[Fact]
|
|
|
|
public void CanCalculateCryptoDue2()
|
|
|
|
{
|
2018-03-13 07:28:39 +01:00
|
|
|
var dummy = new Key().PubKey.GetAddress(Network.RegTest).ToString();
|
2018-02-19 10:54:21 +01:00
|
|
|
#pragma warning disable CS0618
|
|
|
|
InvoiceEntity invoiceEntity = new InvoiceEntity();
|
|
|
|
invoiceEntity.Payments = new System.Collections.Generic.List<PaymentEntity>();
|
|
|
|
invoiceEntity.ProductInformation = new ProductInformation() { Price = 100 };
|
|
|
|
PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary();
|
|
|
|
paymentMethods.Add(new PaymentMethod()
|
|
|
|
{
|
|
|
|
CryptoCode = "BTC",
|
|
|
|
Rate = 10513.44m,
|
|
|
|
}.SetPaymentMethodDetails(new BTCPayServer.Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod()
|
|
|
|
{
|
|
|
|
TxFee = Money.Coins(0.00000100m),
|
|
|
|
DepositAddress = dummy
|
|
|
|
}));
|
|
|
|
paymentMethods.Add(new PaymentMethod()
|
|
|
|
{
|
|
|
|
CryptoCode = "LTC",
|
|
|
|
Rate = 216.79m
|
|
|
|
}.SetPaymentMethodDetails(new BTCPayServer.Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod()
|
|
|
|
{
|
|
|
|
TxFee = Money.Coins(0.00010000m),
|
|
|
|
DepositAddress = dummy
|
|
|
|
}));
|
|
|
|
invoiceEntity.SetPaymentMethods(paymentMethods);
|
|
|
|
|
|
|
|
var btc = invoiceEntity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
|
|
|
|
var accounting = btc.Calculate();
|
|
|
|
|
|
|
|
invoiceEntity.Payments.Add(new PaymentEntity() { Accounted = true, CryptoCode = "BTC" }.SetCryptoPaymentData(new BitcoinLikePaymentData()
|
|
|
|
{
|
|
|
|
Output = new TxOut() { Value = Money.Coins(0.00151263m) }
|
|
|
|
}));
|
|
|
|
accounting = btc.Calculate();
|
|
|
|
invoiceEntity.Payments.Add(new PaymentEntity() { Accounted = true, CryptoCode = "BTC" }.SetCryptoPaymentData(new BitcoinLikePaymentData()
|
|
|
|
{
|
|
|
|
Output = new TxOut() { Value = accounting.Due }
|
|
|
|
}));
|
|
|
|
accounting = btc.Calculate();
|
|
|
|
Assert.Equal(Money.Zero, accounting.Due);
|
|
|
|
Assert.Equal(Money.Zero, accounting.DueUncapped);
|
|
|
|
|
|
|
|
var ltc = invoiceEntity.GetPaymentMethod(new PaymentMethodId("LTC", PaymentTypes.BTCLike), null);
|
|
|
|
accounting = ltc.Calculate();
|
|
|
|
|
|
|
|
Assert.Equal(Money.Zero, accounting.Due);
|
|
|
|
// LTC might have over paid due to BTC paying above what it should (round 1 satoshi up)
|
|
|
|
Assert.True(accounting.DueUncapped < Money.Zero);
|
|
|
|
|
|
|
|
var paymentMethod = InvoiceWatcher.GetNearestClearedPayment(paymentMethods, out var accounting2, null);
|
|
|
|
Assert.Equal(btc.CryptoCode, paymentMethod.CryptoCode);
|
|
|
|
#pragma warning restore CS0618
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
[Fact]
|
|
|
|
public void CanCalculateCryptoDue()
|
|
|
|
{
|
|
|
|
var entity = new InvoiceEntity();
|
2017-12-21 07:52:04 +01:00
|
|
|
#pragma warning disable CS0618
|
2017-10-27 10:53:04 +02:00
|
|
|
entity.TxFee = Money.Coins(0.1m);
|
|
|
|
entity.Rate = 5000;
|
2017-12-21 07:52:04 +01:00
|
|
|
|
2018-01-12 09:04:47 +01:00
|
|
|
entity.Payments = new System.Collections.Generic.List<PaymentEntity>();
|
|
|
|
entity.ProductInformation = new ProductInformation() { Price = 5000 };
|
|
|
|
|
|
|
|
// Some check that handling legacy stuff does not break things
|
2018-02-19 07:09:05 +01:00
|
|
|
var paymentMethod = entity.GetPaymentMethods(null, true).TryGet("BTC", PaymentTypes.BTCLike);
|
|
|
|
paymentMethod.Calculate();
|
|
|
|
Assert.NotNull(paymentMethod);
|
|
|
|
Assert.Null(entity.GetPaymentMethods(null, false).TryGet("BTC", PaymentTypes.BTCLike));
|
|
|
|
entity.SetPaymentMethod(new PaymentMethod() { ParentEntity = entity, Rate = entity.Rate, CryptoCode = "BTC", TxFee = entity.TxFee });
|
|
|
|
Assert.NotNull(entity.GetPaymentMethods(null, false).TryGet("BTC", PaymentTypes.BTCLike));
|
|
|
|
Assert.NotNull(entity.GetPaymentMethods(null, true).TryGet("BTC", PaymentTypes.BTCLike));
|
2018-01-12 09:04:47 +01:00
|
|
|
////////////////////
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
var accounting = paymentMethod.Calculate();
|
2017-12-21 10:01:26 +01:00
|
|
|
Assert.Equal(Money.Coins(1.1m), accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.1m), accounting.TotalDue);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2017-11-06 09:31:02 +01:00
|
|
|
entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.5m), new Key()), Accounted = true });
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
accounting = paymentMethod.Calculate();
|
2017-10-27 10:53:04 +02:00
|
|
|
//Since we need to spend one more txout, it should be 1.1 - 0,5 + 0.1
|
2017-12-21 10:01:26 +01:00
|
|
|
Assert.Equal(Money.Coins(0.7m), accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.2m), accounting.TotalDue);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2017-11-06 09:31:02 +01:00
|
|
|
entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.2m), new Key()), Accounted = true });
|
2017-12-21 10:01:26 +01:00
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
accounting = paymentMethod.Calculate();
|
2017-12-21 10:01:26 +01:00
|
|
|
Assert.Equal(Money.Coins(0.6m), accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.3m), accounting.TotalDue);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2017-11-06 09:31:02 +01:00
|
|
|
entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.6m), new Key()), Accounted = true });
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
accounting = paymentMethod.Calculate();
|
2017-12-21 10:01:26 +01:00
|
|
|
Assert.Equal(Money.Zero, accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.3m), accounting.TotalDue);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2017-11-06 09:31:02 +01:00
|
|
|
entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.2m), new Key()), Accounted = true });
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
accounting = paymentMethod.Calculate();
|
2017-12-21 10:01:26 +01:00
|
|
|
Assert.Equal(Money.Zero, accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.3m), accounting.TotalDue);
|
2018-01-09 02:54:19 +01:00
|
|
|
|
|
|
|
entity = new InvoiceEntity();
|
|
|
|
entity.ProductInformation = new ProductInformation() { Price = 5000 };
|
2018-02-19 07:09:05 +01:00
|
|
|
PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary();
|
|
|
|
paymentMethods.Add(new PaymentMethod()
|
2018-02-18 18:38:03 +01:00
|
|
|
{
|
|
|
|
CryptoCode = "BTC",
|
|
|
|
Rate = 1000,
|
|
|
|
TxFee = Money.Coins(0.1m)
|
|
|
|
});
|
2018-02-19 07:09:05 +01:00
|
|
|
paymentMethods.Add(new PaymentMethod()
|
2018-02-18 18:38:03 +01:00
|
|
|
{
|
|
|
|
CryptoCode = "LTC",
|
|
|
|
Rate = 500,
|
|
|
|
TxFee = Money.Coins(0.01m)
|
|
|
|
});
|
2018-02-19 07:09:05 +01:00
|
|
|
entity.SetPaymentMethods(paymentMethods);
|
2018-01-09 02:54:19 +01:00
|
|
|
entity.Payments = new List<PaymentEntity>();
|
2018-02-19 07:09:05 +01:00
|
|
|
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
|
|
|
|
accounting = paymentMethod.Calculate();
|
2018-01-09 02:54:19 +01:00
|
|
|
Assert.Equal(Money.Coins(5.1m), accounting.Due);
|
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("LTC", PaymentTypes.BTCLike), null);
|
|
|
|
accounting = paymentMethod.Calculate();
|
2018-01-09 02:54:19 +01:00
|
|
|
Assert.Equal(Money.Coins(10.01m), accounting.TotalDue);
|
|
|
|
|
|
|
|
entity.Payments.Add(new PaymentEntity() { CryptoCode = "BTC", Output = new TxOut(Money.Coins(1.0m), new Key()), Accounted = true });
|
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
|
|
|
|
accounting = paymentMethod.Calculate();
|
2018-01-09 02:54:19 +01:00
|
|
|
Assert.Equal(Money.Coins(4.2m), accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.0m), accounting.CryptoPaid);
|
|
|
|
Assert.Equal(Money.Coins(1.0m), accounting.Paid);
|
|
|
|
Assert.Equal(Money.Coins(5.2m), accounting.TotalDue);
|
2018-02-25 16:48:12 +01:00
|
|
|
Assert.Equal(2, accounting.TxRequired);
|
2018-01-09 02:54:19 +01:00
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("LTC", PaymentTypes.BTCLike), null);
|
|
|
|
accounting = paymentMethod.Calculate();
|
2018-01-09 02:54:19 +01:00
|
|
|
Assert.Equal(Money.Coins(10.01m + 0.1m * 2 - 2.0m /* 8.21m */), accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(0.0m), accounting.CryptoPaid);
|
|
|
|
Assert.Equal(Money.Coins(2.0m), accounting.Paid);
|
|
|
|
Assert.Equal(Money.Coins(10.01m + 0.1m * 2), accounting.TotalDue);
|
|
|
|
|
|
|
|
entity.Payments.Add(new PaymentEntity() { CryptoCode = "LTC", Output = new TxOut(Money.Coins(1.0m), new Key()), Accounted = true });
|
|
|
|
|
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
|
|
|
|
accounting = paymentMethod.Calculate();
|
2018-01-09 02:54:19 +01:00
|
|
|
Assert.Equal(Money.Coins(4.2m - 0.5m + 0.01m / 2), accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.0m), accounting.CryptoPaid);
|
|
|
|
Assert.Equal(Money.Coins(1.5m), accounting.Paid);
|
|
|
|
Assert.Equal(Money.Coins(5.2m + 0.01m / 2), accounting.TotalDue); // The fee for LTC added
|
2018-02-25 16:48:12 +01:00
|
|
|
Assert.Equal(2, accounting.TxRequired);
|
2018-01-09 02:54:19 +01:00
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("LTC", PaymentTypes.BTCLike), null);
|
|
|
|
accounting = paymentMethod.Calculate();
|
2018-01-09 02:54:19 +01:00
|
|
|
Assert.Equal(Money.Coins(8.21m - 1.0m + 0.01m), accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.0m), accounting.CryptoPaid);
|
|
|
|
Assert.Equal(Money.Coins(3.0m), accounting.Paid);
|
|
|
|
Assert.Equal(Money.Coins(10.01m + 0.1m * 2 + 0.01m), accounting.TotalDue);
|
2018-02-25 16:48:12 +01:00
|
|
|
Assert.Equal(2, accounting.TxRequired);
|
2018-01-09 02:54:19 +01:00
|
|
|
|
|
|
|
var remaining = Money.Coins(4.2m - 0.5m + 0.01m / 2);
|
|
|
|
entity.Payments.Add(new PaymentEntity() { CryptoCode = "BTC", Output = new TxOut(remaining, new Key()), Accounted = true });
|
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
|
|
|
|
accounting = paymentMethod.Calculate();
|
2018-01-09 02:54:19 +01:00
|
|
|
Assert.Equal(Money.Zero, accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.0m) + remaining, accounting.CryptoPaid);
|
|
|
|
Assert.Equal(Money.Coins(1.5m) + remaining, accounting.Paid);
|
|
|
|
Assert.Equal(Money.Coins(5.2m + 0.01m / 2), accounting.TotalDue);
|
|
|
|
Assert.Equal(accounting.Paid, accounting.TotalDue);
|
2018-02-25 16:48:12 +01:00
|
|
|
Assert.Equal(2, accounting.TxRequired);
|
2018-01-09 02:54:19 +01:00
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("LTC", PaymentTypes.BTCLike), null);
|
|
|
|
accounting = paymentMethod.Calculate();
|
2018-01-09 02:54:19 +01:00
|
|
|
Assert.Equal(Money.Zero, accounting.Due);
|
|
|
|
Assert.Equal(Money.Coins(1.0m), accounting.CryptoPaid);
|
|
|
|
Assert.Equal(Money.Coins(3.0m) + remaining * 2, accounting.Paid);
|
|
|
|
// Paying 2 BTC fee, LTC fee removed because fully paid
|
|
|
|
Assert.Equal(Money.Coins(10.01m + 0.1m * 2 + 0.1m * 2 /* + 0.01m no need to pay this fee anymore */), accounting.TotalDue);
|
2018-02-25 16:48:12 +01:00
|
|
|
Assert.Equal(1, accounting.TxRequired);
|
2018-01-09 02:54:19 +01:00
|
|
|
Assert.Equal(accounting.Paid, accounting.TotalDue);
|
2017-12-21 07:52:04 +01:00
|
|
|
#pragma warning restore CS0618
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CanPayUsingBIP70()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
2018-02-23 07:21:42 +01:00
|
|
|
user.RegisterDerivationScheme("BTC");
|
2017-10-27 10:53:04 +02:00
|
|
|
var invoice = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Buyer = new Buyer() { email = "test@fwf.com" },
|
|
|
|
Price = 5000.0,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
//RedirectURL = redirect + "redirect",
|
|
|
|
//NotificationURL = CallbackUri + "/notification",
|
|
|
|
ItemDesc = "Some description",
|
|
|
|
FullNotifications = true
|
|
|
|
}, Facade.Merchant);
|
|
|
|
|
|
|
|
Assert.False(invoice.Refundable);
|
|
|
|
|
|
|
|
var url = new BitcoinUrlBuilder(invoice.PaymentUrls.BIP72);
|
|
|
|
var request = url.GetPaymentRequest();
|
|
|
|
var payment = request.CreatePayment();
|
|
|
|
|
|
|
|
Transaction tx = new Transaction();
|
|
|
|
tx.Outputs.AddRange(request.Details.Outputs.Select(o => new TxOut(o.Amount, o.Script)));
|
|
|
|
var cashCow = tester.ExplorerNode;
|
|
|
|
tx = cashCow.FundRawTransaction(tx).Transaction;
|
|
|
|
tx = cashCow.SignRawTransaction(tx);
|
|
|
|
|
|
|
|
payment.Transactions.Add(tx);
|
|
|
|
|
|
|
|
payment.RefundTo.Add(new PaymentOutput(Money.Coins(1.0m), new Key().ScriptPubKey));
|
|
|
|
var ack = payment.SubmitPayment();
|
|
|
|
Assert.NotNull(ack);
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal("paid", localInvoice.Status);
|
|
|
|
Assert.True(localInvoice.Refundable);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 07:49:19 +01:00
|
|
|
[Fact]
|
|
|
|
public void CanUseLightMoney()
|
|
|
|
{
|
|
|
|
var light = LightMoney.MilliSatoshis(1);
|
|
|
|
Assert.Equal("0.00000000001", light.ToString());
|
2018-02-25 16:48:12 +01:00
|
|
|
|
|
|
|
light = LightMoney.MilliSatoshis(200000);
|
|
|
|
Assert.Equal(200m, light.ToDecimal(LightMoneyUnit.Satoshi));
|
|
|
|
Assert.Equal(0.00000001m * 200m, light.ToDecimal(LightMoneyUnit.BTC));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CanSetLightningServer()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
var storeController = tester.PayTester.GetController<StoresController>(user.UserId);
|
|
|
|
Assert.IsType<ViewResult>(storeController.UpdateStore(user.StoreId).GetAwaiter().GetResult());
|
2018-03-20 18:48:11 +01:00
|
|
|
Assert.IsType<ViewResult>(storeController.AddLightningNode(user.StoreId, "BTC").GetAwaiter().GetResult());
|
2018-02-25 16:48:12 +01:00
|
|
|
|
|
|
|
var testResult = storeController.AddLightningNode(user.StoreId, new LightningNodeViewModel()
|
|
|
|
{
|
|
|
|
Url = tester.MerchantCharge.Client.Uri.AbsoluteUri
|
2018-03-20 18:48:11 +01:00
|
|
|
}, "test", "BTC").GetAwaiter().GetResult();
|
2018-02-25 16:48:12 +01:00
|
|
|
Assert.DoesNotContain("Error", ((LightningNodeViewModel)Assert.IsType<ViewResult>(testResult).Model).StatusMessage, StringComparison.OrdinalIgnoreCase);
|
|
|
|
Assert.True(storeController.ModelState.IsValid);
|
|
|
|
|
|
|
|
Assert.IsType<RedirectToActionResult>(storeController.AddLightningNode(user.StoreId, new LightningNodeViewModel()
|
|
|
|
{
|
|
|
|
Url = tester.MerchantCharge.Client.Uri.AbsoluteUri
|
2018-03-20 18:48:11 +01:00
|
|
|
}, "save", "BTC").GetAwaiter().GetResult());
|
2018-02-25 16:48:12 +01:00
|
|
|
|
|
|
|
var storeVm = Assert.IsType<Models.StoreViewModels.StoreViewModel>(Assert.IsType<ViewResult>(storeController.UpdateStore(user.StoreId).GetAwaiter().GetResult()).Model);
|
2018-03-20 18:48:11 +01:00
|
|
|
Assert.Single(storeVm.LightningNodes.Where(l => !string.IsNullOrEmpty(l.Address)));
|
2018-02-25 16:48:12 +01:00
|
|
|
}
|
2017-12-13 07:49:19 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 16:31:19 +01:00
|
|
|
[Fact]
|
|
|
|
public void CanParseLightningURL()
|
|
|
|
{
|
|
|
|
LightningConnectionString conn = null;
|
|
|
|
Assert.True(LightningConnectionString.TryParse("/test/a", out conn));
|
|
|
|
Assert.Equal("unix://test/a", conn.ToString());
|
|
|
|
Assert.Equal("unix://test/a", conn.ToUri(true).AbsoluteUri);
|
|
|
|
Assert.Equal("unix://test/a", conn.ToUri(false).AbsoluteUri);
|
|
|
|
Assert.Equal(LightningConnectionType.CLightning, conn.ConnectionType);
|
|
|
|
|
|
|
|
Assert.True(LightningConnectionString.TryParse("unix://test/a", out conn));
|
|
|
|
Assert.Equal("unix://test/a", conn.ToString());
|
|
|
|
Assert.Equal("unix://test/a", conn.ToUri(true).AbsoluteUri);
|
|
|
|
Assert.Equal("unix://test/a", conn.ToUri(false).AbsoluteUri);
|
|
|
|
Assert.Equal(LightningConnectionType.CLightning, conn.ConnectionType);
|
|
|
|
|
|
|
|
Assert.True(LightningConnectionString.TryParse("unix://test/a", out conn));
|
|
|
|
Assert.Equal("unix://test/a", conn.ToString());
|
|
|
|
Assert.Equal("unix://test/a", conn.ToUri(true).AbsoluteUri);
|
|
|
|
Assert.Equal("unix://test/a", conn.ToUri(false).AbsoluteUri);
|
|
|
|
Assert.Equal(LightningConnectionType.CLightning, conn.ConnectionType);
|
|
|
|
|
|
|
|
Assert.True(LightningConnectionString.TryParse("tcp://test/a", out conn));
|
|
|
|
Assert.Equal("tcp://test/a", conn.ToString());
|
|
|
|
Assert.Equal("tcp://test/a", conn.ToUri(true).AbsoluteUri);
|
|
|
|
Assert.Equal("tcp://test/a", conn.ToUri(false).AbsoluteUri);
|
|
|
|
Assert.Equal(LightningConnectionType.CLightning, conn.ConnectionType);
|
|
|
|
|
|
|
|
Assert.True(LightningConnectionString.TryParse("http://aaa:bbb@test/a", out conn));
|
|
|
|
Assert.Equal("http://aaa:bbb@test/a", conn.ToString());
|
|
|
|
Assert.Equal("http://aaa:bbb@test/a", conn.ToUri(true).AbsoluteUri);
|
|
|
|
Assert.Equal("http://test/a", conn.ToUri(false).AbsoluteUri);
|
|
|
|
Assert.Equal(LightningConnectionType.Charge, conn.ConnectionType);
|
|
|
|
Assert.Equal("aaa", conn.Username);
|
|
|
|
Assert.Equal("bbb", conn.Password);
|
|
|
|
|
|
|
|
Assert.False(LightningConnectionString.TryParse("lol://aaa:bbb@test/a", out conn));
|
|
|
|
Assert.False(LightningConnectionString.TryParse("https://test/a", out conn));
|
|
|
|
Assert.False(LightningConnectionString.TryParse("unix://dwewoi:dwdwqd@test/a", out conn));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CanSendLightningPayment2()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
tester.PrepareLightning();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
user.RegisterLightningNode("BTC", LightningConnectionType.CLightning);
|
|
|
|
user.RegisterDerivationScheme("BTC");
|
|
|
|
|
|
|
|
var invoice = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 0.01,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
ItemDesc = "Some description"
|
|
|
|
});
|
|
|
|
|
|
|
|
tester.SendLightningPayment(invoice);
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.Equal("complete", localInvoice.Status);
|
|
|
|
Assert.Equal("False", localInvoice.ExceptionStatus.ToString());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 07:21:42 +01:00
|
|
|
[Fact]
|
|
|
|
public void CanSendLightningPayment()
|
|
|
|
{
|
|
|
|
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
tester.PrepareLightning();
|
2018-02-25 16:48:12 +01:00
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
2018-03-20 16:31:19 +01:00
|
|
|
user.RegisterLightningNode("BTC", LightningConnectionType.Charge);
|
2018-02-25 16:48:12 +01:00
|
|
|
user.RegisterDerivationScheme("BTC");
|
2017-12-21 10:06:21 +01:00
|
|
|
|
2018-02-25 16:48:12 +01:00
|
|
|
var invoice = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 0.01,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
ItemDesc = "Some description"
|
|
|
|
});
|
|
|
|
|
|
|
|
tester.SendLightningPayment(invoice);
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.Equal("complete", localInvoice.Status);
|
|
|
|
Assert.Equal("False", localInvoice.ExceptionStatus.ToString());
|
|
|
|
});
|
2018-02-26 05:29:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(Enumerable.Range(0, 5)
|
|
|
|
.Select(_ => CanSendLightningPaymentCore(tester, user))
|
|
|
|
.ToArray());
|
2018-02-23 07:21:42 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-13 07:49:19 +01:00
|
|
|
|
2018-02-26 05:29:23 +01:00
|
|
|
async Task CanSendLightningPaymentCore(ServerTester tester, TestAccount user)
|
|
|
|
{
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(RandomUtils.GetUInt32() % 5));
|
|
|
|
var invoice = await user.BitPay.CreateInvoiceAsync(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 0.01,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
ItemDesc = "Some description"
|
|
|
|
});
|
|
|
|
await tester.SendLightningPaymentAsync(invoice);
|
|
|
|
await EventuallyAsync(async () =>
|
|
|
|
{
|
|
|
|
var localInvoice = await user.BitPay.GetInvoiceAsync(invoice.Id);
|
|
|
|
Assert.Equal("complete", localInvoice.Status);
|
|
|
|
Assert.Equal("False", localInvoice.ExceptionStatus.ToString());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
[Fact]
|
|
|
|
public void CanUseServerInitiatedPairingCode()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var acc = tester.NewAccount();
|
|
|
|
acc.Register();
|
|
|
|
acc.CreateStore();
|
|
|
|
|
|
|
|
var controller = tester.PayTester.GetController<StoresController>(acc.UserId);
|
|
|
|
var token = (RedirectToActionResult)controller.CreateToken(acc.StoreId, new Models.StoreViewModels.CreateTokenViewModel()
|
|
|
|
{
|
|
|
|
Facade = Facade.Merchant.ToString(),
|
|
|
|
Label = "bla",
|
|
|
|
PublicKey = null
|
|
|
|
}).GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
var pairingCode = (string)token.RouteValues["pairingCode"];
|
|
|
|
|
|
|
|
acc.BitPay.AuthorizeClient(new PairingCode(pairingCode)).GetAwaiter().GetResult();
|
|
|
|
Assert.True(acc.BitPay.TestAccess(Facade.Merchant));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CanSendIPN()
|
|
|
|
{
|
|
|
|
using (var callbackServer = new CustomServer())
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var acc = tester.NewAccount();
|
|
|
|
acc.GrantAccess();
|
2018-02-23 07:21:42 +01:00
|
|
|
acc.RegisterDerivationScheme("BTC");
|
2017-10-27 10:53:04 +02:00
|
|
|
var invoice = acc.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 5.0,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
NotificationURL = callbackServer.GetUri().AbsoluteUri,
|
|
|
|
ItemDesc = "Some description",
|
2018-01-18 12:56:55 +01:00
|
|
|
FullNotifications = true,
|
|
|
|
ExtendedNotifications = true
|
2017-10-27 10:53:04 +02:00
|
|
|
});
|
|
|
|
BitcoinUrlBuilder url = new BitcoinUrlBuilder(invoice.PaymentUrls.BIP21);
|
|
|
|
tester.ExplorerNode.SendToAddress(url.Address, url.Amount);
|
|
|
|
Thread.Sleep(5000);
|
|
|
|
callbackServer.ProcessNextRequest((ctx) =>
|
|
|
|
{
|
|
|
|
var ipn = new StreamReader(ctx.Request.Body).ReadToEnd();
|
|
|
|
JsonConvert.DeserializeObject<InvoicePaymentNotification>(ipn); //can deserialize
|
|
|
|
});
|
|
|
|
var invoice2 = acc.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.NotNull(invoice2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CantPairTwiceWithSamePubkey()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var acc = tester.NewAccount();
|
|
|
|
acc.Register();
|
|
|
|
var store = acc.CreateStore();
|
|
|
|
var pairingCode = acc.BitPay.RequestClientAuthorization("test", Facade.Merchant);
|
|
|
|
Assert.IsType<RedirectToActionResult>(store.Pair(pairingCode.ToString(), acc.StoreId).GetAwaiter().GetResult());
|
|
|
|
|
|
|
|
pairingCode = acc.BitPay.RequestClientAuthorization("test1", Facade.Merchant);
|
|
|
|
var store2 = acc.CreateStore();
|
|
|
|
store2.Pair(pairingCode.ToString(), store2.CreatedStoreId).GetAwaiter().GetResult();
|
2018-02-17 05:18:16 +01:00
|
|
|
Assert.Contains(nameof(PairingResult.ReusedKey), store2.StatusMessage, StringComparison.CurrentCultureIgnoreCase);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 09:31:02 +01:00
|
|
|
[Fact]
|
|
|
|
public void CanRBFPayment()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
2018-02-23 07:21:42 +01:00
|
|
|
user.RegisterDerivationScheme("BTC");
|
2017-11-06 09:31:02 +01:00
|
|
|
var invoice = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 5000.0,
|
|
|
|
Currency = "USD"
|
|
|
|
}, Facade.Merchant);
|
2018-02-19 07:09:05 +01:00
|
|
|
var payment1 = invoice.BtcDue + Money.Coins(0.0001m);
|
|
|
|
var payment2 = invoice.BtcDue;
|
2017-11-06 09:31:02 +01:00
|
|
|
var tx1 = new uint256(tester.ExplorerNode.SendCommand("sendtoaddress", new object[]
|
|
|
|
{
|
2018-02-17 05:18:16 +01:00
|
|
|
invoice.BitcoinAddress,
|
2017-11-06 09:31:02 +01:00
|
|
|
payment1.ToString(),
|
|
|
|
null, //comment
|
|
|
|
null, //comment_to
|
|
|
|
false, //subtractfeefromamount
|
|
|
|
true, //replaceable
|
|
|
|
}).ResultString);
|
2018-01-11 14:52:28 +01:00
|
|
|
var invoiceAddress = BitcoinAddress.Create(invoice.BitcoinAddress, user.SupportedNetwork.NBitcoinNetwork);
|
2017-11-06 09:31:02 +01:00
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
invoice = user.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.Equal(payment1, invoice.BtcPaid);
|
2018-02-19 07:09:05 +01:00
|
|
|
Assert.Equal("paid", invoice.Status);
|
|
|
|
Assert.Equal("paidOver", invoice.ExceptionStatus.ToString());
|
2018-01-11 14:52:28 +01:00
|
|
|
invoiceAddress = BitcoinAddress.Create(invoice.BitcoinAddress, user.SupportedNetwork.NBitcoinNetwork);
|
2017-11-06 09:31:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var tx = tester.ExplorerNode.GetRawTransaction(new uint256(tx1));
|
|
|
|
foreach (var input in tx.Inputs)
|
|
|
|
{
|
|
|
|
input.ScriptSig = Script.Empty; //Strip signatures
|
|
|
|
}
|
|
|
|
var output = tx.Outputs.First(o => o.Value == payment1);
|
|
|
|
output.Value = payment2;
|
|
|
|
output.ScriptPubKey = invoiceAddress.ScriptPubKey;
|
|
|
|
var replaced = tester.ExplorerNode.SignRawTransaction(tx);
|
|
|
|
tester.ExplorerNode.SendRawTransaction(replaced);
|
2018-01-17 07:02:53 +01:00
|
|
|
var test = tester.ExplorerClient.GetUTXOs(user.DerivationScheme, null);
|
2017-11-06 09:31:02 +01:00
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
invoice = user.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.Equal(payment2, invoice.BtcPaid);
|
2018-02-19 07:09:05 +01:00
|
|
|
Assert.Equal("False", invoice.ExceptionStatus.ToString());
|
2017-11-06 09:31:02 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 15:35:52 +01:00
|
|
|
[Fact]
|
|
|
|
public void CanParseFilter()
|
|
|
|
{
|
|
|
|
var filter = "storeid:abc status:abed blabhbalh ";
|
|
|
|
var search = new SearchString(filter);
|
|
|
|
Assert.Equal("storeid:abc status:abed blabhbalh", search.ToString());
|
|
|
|
Assert.Equal("blabhbalh", search.TextSearch);
|
|
|
|
Assert.Equal("abc", search.Filters["storeid"]);
|
|
|
|
Assert.Equal("abed", search.Filters["status"]);
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
[Fact]
|
2018-01-07 18:36:41 +01:00
|
|
|
public void TestAccessBitpayAPI()
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
Assert.False(user.BitPay.TestAccess(Facade.Merchant));
|
|
|
|
user.GrantAccess();
|
2018-02-23 07:21:42 +01:00
|
|
|
user.RegisterDerivationScheme("BTC");
|
2017-10-27 10:53:04 +02:00
|
|
|
Assert.True(user.BitPay.TestAccess(Facade.Merchant));
|
2018-01-07 18:36:41 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-13 14:01:09 +01:00
|
|
|
|
2018-01-17 07:59:31 +01:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CanTweakRate()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
2018-02-23 07:21:42 +01:00
|
|
|
user.RegisterDerivationScheme("BTC");
|
2018-01-17 07:59:31 +01:00
|
|
|
|
|
|
|
// First we try payment with a merchant having only BTC
|
|
|
|
var invoice1 = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 5000.0,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
ItemDesc = "Some description",
|
|
|
|
FullNotifications = true
|
|
|
|
}, Facade.Merchant);
|
|
|
|
|
|
|
|
|
|
|
|
var storeController = tester.PayTester.GetController<StoresController>(user.UserId);
|
|
|
|
var vm = (StoreViewModel)((ViewResult)storeController.UpdateStore(user.StoreId).Result).Model;
|
|
|
|
Assert.Equal(1.0, vm.RateMultiplier);
|
|
|
|
vm.RateMultiplier = 0.5;
|
|
|
|
storeController.UpdateStore(user.StoreId, vm).Wait();
|
|
|
|
|
|
|
|
|
|
|
|
var invoice2 = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 5000.0,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
ItemDesc = "Some description",
|
|
|
|
FullNotifications = true
|
|
|
|
}, Facade.Merchant);
|
|
|
|
|
|
|
|
Assert.True(invoice2.BtcPrice.Almost(invoice1.BtcPrice * 2, 0.00001m));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 14:01:09 +01:00
|
|
|
[Fact]
|
|
|
|
public void CanHaveLTCOnlyStore()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
2018-02-23 07:21:42 +01:00
|
|
|
user.RegisterDerivationScheme("LTC");
|
2018-01-13 14:01:09 +01:00
|
|
|
|
|
|
|
// First we try payment with a merchant having only BTC
|
|
|
|
var invoice = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 500,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
ItemDesc = "Some description",
|
|
|
|
FullNotifications = true
|
|
|
|
}, Facade.Merchant);
|
|
|
|
|
|
|
|
Assert.Single(invoice.CryptoInfo);
|
|
|
|
Assert.Equal("LTC", invoice.CryptoInfo[0].CryptoCode);
|
|
|
|
var cashCow = tester.LTCExplorerNode;
|
|
|
|
var invoiceAddress = BitcoinAddress.Create(invoice.CryptoInfo[0].Address, cashCow.Network);
|
|
|
|
var firstPayment = Money.Coins(0.1m);
|
|
|
|
cashCow.SendToAddress(invoiceAddress, firstPayment);
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
invoice = user.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.Equal(firstPayment, invoice.CryptoInfo[0].Paid);
|
|
|
|
});
|
|
|
|
|
|
|
|
Assert.Single(invoice.CryptoInfo); // Only BTC should be presented
|
|
|
|
|
|
|
|
var controller = tester.PayTester.GetController<InvoiceController>(null);
|
|
|
|
var checkout = (Models.InvoicingModels.PaymentModel)((JsonResult)controller.GetStatus(invoice.Id, null).GetAwaiter().GetResult()).Value;
|
|
|
|
Assert.Single(checkout.AvailableCryptos);
|
|
|
|
Assert.Equal("LTC", checkout.CryptoCode);
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
|
|
|
|
// Despite it is called BitcoinAddress it should be LTC because BTC is not available
|
|
|
|
Assert.Null(invoice.BitcoinAddress);
|
2018-01-14 07:26:14 +01:00
|
|
|
Assert.NotEqual(1.0, invoice.Rate);
|
2018-01-13 14:01:09 +01:00
|
|
|
Assert.NotEqual(invoice.BtcDue, invoice.CryptoInfo[0].Due); // Should be BTC rate
|
|
|
|
cashCow.SendToAddress(invoiceAddress, invoice.CryptoInfo[0].Due);
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
invoice = user.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.Equal("paid", invoice.Status);
|
|
|
|
checkout = (Models.InvoicingModels.PaymentModel)((JsonResult)controller.GetStatus(invoice.Id, null).GetAwaiter().GetResult()).Value;
|
|
|
|
Assert.Equal("paid", checkout.Status);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 18:36:41 +01:00
|
|
|
|
2018-01-11 09:29:48 +01:00
|
|
|
[Fact]
|
|
|
|
public void CanPayWithTwoCurrencies()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
2018-02-23 07:21:42 +01:00
|
|
|
user.RegisterDerivationScheme("BTC");
|
2018-01-11 09:29:48 +01:00
|
|
|
// First we try payment with a merchant having only BTC
|
|
|
|
var invoice = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 5000.0,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
ItemDesc = "Some description",
|
|
|
|
FullNotifications = true
|
|
|
|
}, Facade.Merchant);
|
|
|
|
|
|
|
|
var cashCow = tester.ExplorerNode;
|
2018-01-20 06:09:57 +01:00
|
|
|
cashCow.Generate(2); // get some money in case
|
2018-01-11 09:29:48 +01:00
|
|
|
var invoiceAddress = BitcoinAddress.Create(invoice.BitcoinAddress, cashCow.Network);
|
|
|
|
var firstPayment = Money.Coins(0.04m);
|
|
|
|
cashCow.SendToAddress(invoiceAddress, firstPayment);
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
invoice = user.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.True(invoice.BtcPaid == firstPayment);
|
|
|
|
});
|
|
|
|
|
|
|
|
Assert.Single(invoice.CryptoInfo); // Only BTC should be presented
|
|
|
|
|
|
|
|
var controller = tester.PayTester.GetController<InvoiceController>(null);
|
|
|
|
var checkout = (Models.InvoicingModels.PaymentModel)((JsonResult)controller.GetStatus(invoice.Id, null).GetAwaiter().GetResult()).Value;
|
|
|
|
Assert.Single(checkout.AvailableCryptos);
|
|
|
|
Assert.Equal("BTC", checkout.CryptoCode);
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
|
|
|
|
// Retry now with LTC enabled
|
|
|
|
user.RegisterDerivationScheme("LTC");
|
|
|
|
invoice = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 5000.0,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
ItemDesc = "Some description",
|
|
|
|
FullNotifications = true
|
|
|
|
}, Facade.Merchant);
|
|
|
|
|
|
|
|
cashCow = tester.ExplorerNode;
|
|
|
|
invoiceAddress = BitcoinAddress.Create(invoice.BitcoinAddress, cashCow.Network);
|
|
|
|
firstPayment = Money.Coins(0.04m);
|
|
|
|
cashCow.SendToAddress(invoiceAddress, firstPayment);
|
2018-01-20 06:09:57 +01:00
|
|
|
Logs.Tester.LogInformation("First payment sent to " + invoiceAddress);
|
2018-01-11 09:29:48 +01:00
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
invoice = user.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.True(invoice.BtcPaid == firstPayment);
|
|
|
|
});
|
|
|
|
|
|
|
|
cashCow = tester.LTCExplorerNode;
|
|
|
|
var ltcCryptoInfo = invoice.CryptoInfo.FirstOrDefault(c => c.CryptoCode == "LTC");
|
|
|
|
Assert.NotNull(ltcCryptoInfo);
|
|
|
|
invoiceAddress = BitcoinAddress.Create(ltcCryptoInfo.Address, cashCow.Network);
|
2018-02-17 05:18:16 +01:00
|
|
|
var secondPayment = Money.Coins(decimal.Parse(ltcCryptoInfo.Due, CultureInfo.InvariantCulture));
|
2018-01-11 09:29:48 +01:00
|
|
|
cashCow.Generate(2); // LTC is not worth a lot, so just to make sure we have money...
|
|
|
|
cashCow.SendToAddress(invoiceAddress, secondPayment);
|
2018-01-20 06:09:57 +01:00
|
|
|
Logs.Tester.LogInformation("Second payment sent to " + invoiceAddress);
|
2018-01-11 09:29:48 +01:00
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
invoice = user.BitPay.GetInvoice(invoice.Id);
|
|
|
|
Assert.Equal(Money.Zero, invoice.BtcDue);
|
|
|
|
var ltcPaid = invoice.CryptoInfo.First(c => c.CryptoCode == "LTC");
|
|
|
|
Assert.Equal(Money.Zero, ltcPaid.Due);
|
|
|
|
Assert.Equal(secondPayment, ltcPaid.CryptoPaid);
|
|
|
|
Assert.Equal("paid", invoice.Status);
|
|
|
|
Assert.False((bool)((JValue)invoice.ExceptionStatus).Value);
|
|
|
|
});
|
|
|
|
|
|
|
|
controller = tester.PayTester.GetController<InvoiceController>(null);
|
|
|
|
checkout = (Models.InvoicingModels.PaymentModel)((JsonResult)controller.GetStatus(invoice.Id, "LTC").GetAwaiter().GetResult()).Value;
|
|
|
|
Assert.Equal(2, checkout.AvailableCryptos.Count);
|
|
|
|
Assert.Equal("LTC", checkout.CryptoCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 18:57:44 +02:00
|
|
|
[Fact]
|
|
|
|
public void CanParseCurrencyValue()
|
|
|
|
{
|
|
|
|
Assert.True(CurrencyValue.TryParse("1.50USD", out var result));
|
|
|
|
Assert.Equal("1.50 USD", result.ToString());
|
|
|
|
Assert.True(CurrencyValue.TryParse("1.50 USD", out result));
|
|
|
|
Assert.Equal("1.50 USD", result.ToString());
|
|
|
|
Assert.True(CurrencyValue.TryParse("1.50 usd", out result));
|
|
|
|
Assert.Equal("1.50 USD", result.ToString());
|
|
|
|
Assert.True(CurrencyValue.TryParse("1 usd", out result));
|
|
|
|
Assert.Equal("1 USD", result.ToString());
|
|
|
|
Assert.True(CurrencyValue.TryParse("1usd", out result));
|
|
|
|
Assert.Equal("1 USD", result.ToString());
|
|
|
|
Assert.True(CurrencyValue.TryParse("1.501 usd", out result));
|
|
|
|
Assert.Equal("1.50 USD", result.ToString());
|
|
|
|
Assert.False(CurrencyValue.TryParse("1.501 WTFF", out result));
|
|
|
|
Assert.False(CurrencyValue.TryParse("1,501 usd", out result));
|
|
|
|
Assert.False(CurrencyValue.TryParse("1.501", out result));
|
|
|
|
}
|
|
|
|
|
2018-03-24 12:40:26 +01:00
|
|
|
[Fact]
|
|
|
|
public void CanParseDerivationScheme()
|
|
|
|
{
|
|
|
|
var parser = new DerivationSchemeParser(Network.TestNet, NBXplorer.ChainType.Test);
|
|
|
|
NBXplorer.DerivationStrategy.DerivationStrategyBase result;
|
|
|
|
// Passing electrum stuff
|
|
|
|
// Native
|
|
|
|
result = parser.Parse("zpub6nL6PUGurpU3DfPDSZaRS6WshpbNc9ctCFFzrCn54cssnheM31SZJZUcFHKtjJJNhAueMbh6ptFMfy1aeiMQJr3RJ4DDt1hAPx7sMTKV48t");
|
|
|
|
Assert.Equal("tpubD93CJNkmGjLXnsBqE2zGDqfEh1Q8iJ8wueordy3SeWt1RngbbuxXCsqASuVWFywmfoCwUE1rSfNJbaH4cBNcbp8WcyZgPiiRSTazLGL8U9w", result.ToString());
|
|
|
|
// P2SH
|
|
|
|
result = parser.Parse("ypub6QqdH2c5z79681jUgdxjGJzGW9zpL4ryPCuhtZE4GpvrJoZqM823XQN6iSQeVbbbp2uCRQ9UgpeMcwiyV6qjvxTWVcxDn2XEAnioMUwsrQ5");
|
|
|
|
Assert.Equal("tpubD6NzVbkrYhZ4YWjDJUACG9E8fJx2NqNY1iynTiPKEjJrzzRKAgha3nNnwGXr2BtvCJKJHW4nmG7rRqc2AGGy2AECgt16seMyV2FZivUmaJg-[p2sh]", result.ToString());
|
|
|
|
result = parser.Parse("xpub661MyMwAqRbcGeVGU5e5KBcau1HHEUGf9Wr7k4FyLa8yRPNQrrVa7Ndrgg8Afbe2UYXMSL6tJBFd2JewwWASsePPLjkcJFL1tTVEs3UQ23X");
|
|
|
|
Assert.Equal("tpubD6NzVbkrYhZ4YSg7vGdAX6wxE8NwDrmih9SR6cK7gUtsAg37w5LfFpJgviCxC6bGGT4G3uckqH5fiV9ZLN1gm5qgQLVuymzFUR5ed7U7ksu-[legacy]", result.ToString());
|
|
|
|
////////////////
|
2018-03-26 06:52:14 +02:00
|
|
|
|
|
|
|
var tpub = "tpubD6NzVbkrYhZ4Wc65tjhmcKdWFauAo7bGLRTxvggygkNyp6SMGutJp7iociwsinU33jyNBp1J9j2hJH5yQsayfiS3LEU2ZqXodAcnaygra8o";
|
|
|
|
|
|
|
|
result = parser.Parse(tpub);
|
|
|
|
Assert.Equal(tpub, result.ToString());
|
2018-03-24 12:40:26 +01:00
|
|
|
parser.HintScriptPubKey = BitcoinAddress.Create("tb1q4s33amqm8l7a07zdxcunqnn3gcsjcfz3xc573l", parser.Network).ScriptPubKey;
|
2018-03-26 06:52:14 +02:00
|
|
|
result = parser.Parse(tpub);
|
|
|
|
Assert.Equal(tpub, result.ToString());
|
2018-03-24 12:40:26 +01:00
|
|
|
|
|
|
|
parser.HintScriptPubKey = BitcoinAddress.Create("2N2humNio3YTApSfY6VztQ9hQwDnhDvaqFQ", parser.Network).ScriptPubKey;
|
2018-03-26 06:52:14 +02:00
|
|
|
result = parser.Parse(tpub);
|
|
|
|
Assert.Equal($"{tpub}-[p2sh]", result.ToString());
|
2018-03-24 12:40:26 +01:00
|
|
|
|
|
|
|
parser.HintScriptPubKey = BitcoinAddress.Create("mwD8bHS65cdgUf6rZUUSoVhi3wNQFu1Nfi", parser.Network).ScriptPubKey;
|
2018-03-26 06:52:14 +02:00
|
|
|
result = parser.Parse(tpub);
|
|
|
|
Assert.Equal($"{tpub}-[legacy]", result.ToString());
|
2018-03-24 12:40:26 +01:00
|
|
|
|
|
|
|
parser.HintScriptPubKey = BitcoinAddress.Create("2N2humNio3YTApSfY6VztQ9hQwDnhDvaqFQ", parser.Network).ScriptPubKey;
|
2018-03-26 06:52:14 +02:00
|
|
|
result = parser.Parse($"{tpub}-[legacy]");
|
|
|
|
Assert.Equal($"{tpub}-[p2sh]", result.ToString());
|
2018-03-24 12:40:26 +01:00
|
|
|
|
2018-03-26 06:52:14 +02:00
|
|
|
result = parser.Parse(tpub);
|
|
|
|
Assert.Equal($"{tpub}-[p2sh]", result.ToString());
|
2018-03-24 12:40:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-07 18:36:41 +01:00
|
|
|
[Fact]
|
|
|
|
public void InvoiceFlowThroughDifferentStatesCorrectly()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
tester.Start();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
2018-02-23 07:21:42 +01:00
|
|
|
user.RegisterDerivationScheme("BTC");
|
2017-10-27 10:53:04 +02:00
|
|
|
var invoice = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 5000.0,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
ItemDesc = "Some description",
|
|
|
|
FullNotifications = true
|
|
|
|
}, Facade.Merchant);
|
|
|
|
var repo = tester.PayTester.GetService<InvoiceRepository>();
|
|
|
|
var ctx = tester.PayTester.GetService<ApplicationDbContextFactory>().CreateContext();
|
2018-02-25 16:48:12 +01:00
|
|
|
Assert.Equal(0, invoice.CryptoInfo[0].TxCount);
|
2017-11-12 15:03:33 +01:00
|
|
|
Eventually(() =>
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2017-12-16 17:04:20 +01:00
|
|
|
var textSearchResult = tester.PayTester.InvoiceRepository.GetInvoices(new InvoiceQuery()
|
2017-11-12 15:03:33 +01:00
|
|
|
{
|
|
|
|
StoreId = user.StoreId,
|
|
|
|
TextSearch = invoice.OrderId
|
|
|
|
}).GetAwaiter().GetResult();
|
2017-12-21 07:52:04 +01:00
|
|
|
Assert.Single(textSearchResult);
|
2017-12-16 17:04:20 +01:00
|
|
|
textSearchResult = tester.PayTester.InvoiceRepository.GetInvoices(new InvoiceQuery()
|
2017-11-12 15:03:33 +01:00
|
|
|
{
|
|
|
|
StoreId = user.StoreId,
|
|
|
|
TextSearch = invoice.Id
|
|
|
|
}).GetAwaiter().GetResult();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
Assert.Single(textSearchResult);
|
2017-11-12 15:03:33 +01:00
|
|
|
});
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
invoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal(Money.Coins(0), invoice.BtcPaid);
|
|
|
|
Assert.Equal("new", invoice.Status);
|
2017-12-21 07:52:04 +01:00
|
|
|
Assert.False((bool)((JValue)invoice.ExceptionStatus).Value);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
2018-02-16 17:34:40 +01:00
|
|
|
Assert.Single(user.BitPay.GetInvoices(invoice.InvoiceTime.UtcDateTime));
|
|
|
|
Assert.Empty(user.BitPay.GetInvoices(invoice.InvoiceTime.UtcDateTime + TimeSpan.FromDays(2)));
|
|
|
|
Assert.Single(user.BitPay.GetInvoices(invoice.InvoiceTime.UtcDateTime - TimeSpan.FromDays(5)));
|
|
|
|
Assert.Single(user.BitPay.GetInvoices(invoice.InvoiceTime.UtcDateTime - TimeSpan.FromDays(5), invoice.InvoiceTime.DateTime + TimeSpan.FromDays(1.0)));
|
|
|
|
Assert.Empty(user.BitPay.GetInvoices(invoice.InvoiceTime.UtcDateTime - TimeSpan.FromDays(5), invoice.InvoiceTime.DateTime - TimeSpan.FromDays(1)));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
var firstPayment = Money.Coins(0.04m);
|
|
|
|
|
|
|
|
var txFee = Money.Zero;
|
|
|
|
|
|
|
|
var rate = user.BitPay.GetRates();
|
|
|
|
|
|
|
|
var cashCow = tester.ExplorerNode;
|
|
|
|
|
|
|
|
var invoiceAddress = BitcoinAddress.Create(invoice.BitcoinAddress, cashCow.Network);
|
|
|
|
var iii = ctx.AddressInvoices.ToArray();
|
|
|
|
Assert.True(IsMapped(invoice, ctx));
|
|
|
|
cashCow.SendToAddress(invoiceAddress, firstPayment);
|
|
|
|
|
|
|
|
var invoiceEntity = repo.GetInvoice(null, invoice.Id, true).GetAwaiter().GetResult();
|
2017-12-21 07:52:04 +01:00
|
|
|
Assert.Single(invoiceEntity.HistoricalAddresses);
|
2017-10-27 10:53:04 +02:00
|
|
|
Assert.Null(invoiceEntity.HistoricalAddresses[0].UnAssigned);
|
|
|
|
|
|
|
|
Money secondPayment = Money.Zero;
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal("new", localInvoice.Status);
|
|
|
|
Assert.Equal(firstPayment, localInvoice.BtcPaid);
|
|
|
|
txFee = localInvoice.BtcDue - invoice.BtcDue;
|
2017-12-21 07:52:04 +01:00
|
|
|
Assert.Equal("paidPartial", localInvoice.ExceptionStatus.ToString());
|
2018-02-25 16:48:12 +01:00
|
|
|
Assert.Equal(1, localInvoice.CryptoInfo[0].TxCount);
|
2017-10-27 10:53:04 +02:00
|
|
|
Assert.NotEqual(localInvoice.BitcoinAddress, invoice.BitcoinAddress); //New address
|
|
|
|
Assert.True(IsMapped(invoice, ctx));
|
|
|
|
Assert.True(IsMapped(localInvoice, ctx));
|
|
|
|
|
|
|
|
invoiceEntity = repo.GetInvoice(null, invoice.Id, true).GetAwaiter().GetResult();
|
2018-02-17 05:18:16 +01:00
|
|
|
var historical1 = invoiceEntity.HistoricalAddresses.FirstOrDefault(h => h.GetAddress() == invoice.BitcoinAddress);
|
2017-10-27 10:53:04 +02:00
|
|
|
Assert.NotNull(historical1.UnAssigned);
|
2018-02-17 05:18:16 +01:00
|
|
|
var historical2 = invoiceEntity.HistoricalAddresses.FirstOrDefault(h => h.GetAddress() == localInvoice.BitcoinAddress);
|
2017-10-27 10:53:04 +02:00
|
|
|
Assert.Null(historical2.UnAssigned);
|
|
|
|
invoiceAddress = BitcoinAddress.Create(localInvoice.BitcoinAddress, cashCow.Network);
|
|
|
|
secondPayment = localInvoice.BtcDue;
|
|
|
|
});
|
|
|
|
|
|
|
|
cashCow.SendToAddress(invoiceAddress, secondPayment);
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal("paid", localInvoice.Status);
|
2018-02-25 16:48:12 +01:00
|
|
|
Assert.Equal(2, localInvoice.CryptoInfo[0].TxCount);
|
2017-10-27 10:53:04 +02:00
|
|
|
Assert.Equal(firstPayment + secondPayment, localInvoice.BtcPaid);
|
|
|
|
Assert.Equal(Money.Zero, localInvoice.BtcDue);
|
|
|
|
Assert.Equal(localInvoice.BitcoinAddress, invoiceAddress.ToString()); //no new address generated
|
|
|
|
Assert.True(IsMapped(localInvoice, ctx));
|
2017-12-18 00:56:27 +01:00
|
|
|
Assert.False((bool)((JValue)localInvoice.ExceptionStatus).Value);
|
2017-10-27 10:53:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
cashCow.Generate(1); //The user has medium speed settings, so 1 conf is enough to be confirmed
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal("confirmed", localInvoice.Status);
|
|
|
|
});
|
|
|
|
|
|
|
|
cashCow.Generate(5); //Now should be complete
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal("complete", localInvoice.Status);
|
2017-12-18 00:56:27 +01:00
|
|
|
Assert.NotEqual(0.0, localInvoice.Rate);
|
2017-10-27 10:53:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
invoice = user.BitPay.CreateInvoice(new Invoice()
|
|
|
|
{
|
|
|
|
Price = 5000.0,
|
|
|
|
Currency = "USD",
|
|
|
|
PosData = "posData",
|
|
|
|
OrderId = "orderId",
|
|
|
|
//RedirectURL = redirect + "redirect",
|
|
|
|
//NotificationURL = CallbackUri + "/notification",
|
|
|
|
ItemDesc = "Some description",
|
|
|
|
FullNotifications = true
|
|
|
|
}, Facade.Merchant);
|
|
|
|
invoiceAddress = BitcoinAddress.Create(invoice.BitcoinAddress, cashCow.Network);
|
|
|
|
|
|
|
|
cashCow.SendToAddress(invoiceAddress, invoice.BtcDue + Money.Coins(1));
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal("paid", localInvoice.Status);
|
|
|
|
Assert.Equal(Money.Zero, localInvoice.BtcDue);
|
|
|
|
Assert.Equal("paidOver", (string)((JValue)localInvoice.ExceptionStatus).Value);
|
|
|
|
});
|
|
|
|
|
|
|
|
cashCow.Generate(1);
|
|
|
|
|
|
|
|
Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal("confirmed", localInvoice.Status);
|
|
|
|
Assert.Equal(Money.Zero, localInvoice.BtcDue);
|
|
|
|
Assert.Equal("paidOver", (string)((JValue)localInvoice.ExceptionStatus).Value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CheckRatesProvider()
|
|
|
|
{
|
2018-01-08 18:57:06 +01:00
|
|
|
var coinAverage = new CoinAverageRateProvider("BTC");
|
2017-10-27 10:53:04 +02:00
|
|
|
var jpy = coinAverage.GetRateAsync("JPY").GetAwaiter().GetResult();
|
|
|
|
var jpy2 = new BitpayRateProvider(new Bitpay(new Key(), new Uri("https://bitpay.com/"))).GetRateAsync("JPY").GetAwaiter().GetResult();
|
|
|
|
|
2018-01-08 18:57:06 +01:00
|
|
|
var cached = new CachedRateProvider("BTC", coinAverage, new MemoryCache(new MemoryCacheOptions() { ExpirationScanFrequency = TimeSpan.FromSeconds(1.0) }));
|
2017-10-27 10:53:04 +02:00
|
|
|
cached.CacheSpan = TimeSpan.FromSeconds(10);
|
|
|
|
var a = cached.GetRateAsync("JPY").GetAwaiter().GetResult();
|
|
|
|
var b = cached.GetRateAsync("JPY").GetAwaiter().GetResult();
|
|
|
|
//Manually check that cache get hit after 10 sec
|
|
|
|
var c = cached.GetRateAsync("JPY").GetAwaiter().GetResult();
|
2018-01-19 08:00:20 +01:00
|
|
|
|
|
|
|
var bitstamp = new CoinAverageRateProvider("BTC") { Exchange = "bitstamp" };
|
|
|
|
var bitstampRate = bitstamp.GetRateAsync("USD").GetAwaiter().GetResult();
|
|
|
|
Assert.Throws<RateUnavailableException>(() => bitstamp.GetRateAsync("XXXXX").GetAwaiter().GetResult());
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsMapped(Invoice invoice, ApplicationDbContext ctx)
|
|
|
|
{
|
2018-02-19 03:31:34 +01:00
|
|
|
var h = BitcoinAddress.Create(invoice.BitcoinAddress).ScriptPubKey.Hash.ToString();
|
|
|
|
return ctx.AddressInvoices.FirstOrDefault(i => i.InvoiceDataId == invoice.Id && i.GetAddress() == h) != null;
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Eventually(Action act)
|
|
|
|
{
|
2018-02-26 05:29:23 +01:00
|
|
|
CancellationTokenSource cts = new CancellationTokenSource(20000);
|
2017-10-27 10:53:04 +02:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
act();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (XunitException) when (!cts.Token.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
cts.Token.WaitHandle.WaitOne(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 05:29:23 +01:00
|
|
|
|
|
|
|
private async Task EventuallyAsync(Func<Task> act)
|
|
|
|
{
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource(20000);
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await act();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (XunitException) when (!cts.Token.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
await Task.Delay(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
}
|