2019-12-24 08:20:44 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
using BTCPayServer.Controllers;
|
|
|
|
using BTCPayServer.Models.WalletViewModels;
|
2023-11-29 18:51:40 +09:00
|
|
|
using BTCPayServer.Plugins.Altcoins;
|
2019-12-24 08:20:44 +01:00
|
|
|
using BTCPayServer.Services.Wallets;
|
|
|
|
using BTCPayServer.Tests.Logging;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Configuration.Memory;
|
|
|
|
using NBitcoin;
|
2020-03-20 09:31:22 +01:00
|
|
|
using NBitcoin.Payment;
|
2019-12-24 08:20:44 +01:00
|
|
|
using NBitpayClient;
|
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
{
|
2021-11-22 17:16:08 +09:00
|
|
|
public class ElementsTests : UnitTestBase
|
2019-12-24 08:20:44 +01:00
|
|
|
{
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2019-12-24 08:20:44 +01:00
|
|
|
public const int TestTimeout = 60_000;
|
2021-11-22 17:16:08 +09:00
|
|
|
public ElementsTests(ITestOutputHelper helper) : base(helper)
|
2019-12-24 08:20:44 +01:00
|
|
|
{
|
|
|
|
}
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2019-12-24 08:20:44 +01:00
|
|
|
[Fact]
|
2019-12-24 18:11:21 +09:00
|
|
|
[Trait("Altcoins", "Altcoins")]
|
2019-12-24 08:20:44 +01:00
|
|
|
public async Task OnlyShowSupportedWallets()
|
|
|
|
{
|
2021-11-22 17:16:08 +09:00
|
|
|
using (var tester = CreateServerTester())
|
2019-12-24 08:20:44 +01:00
|
|
|
{
|
2019-12-24 18:11:21 +09:00
|
|
|
tester.ActivateLBTC();
|
2019-12-24 08:20:44 +01:00
|
|
|
await tester.StartAsync();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
user.RegisterDerivationScheme("LBTC");
|
|
|
|
user.RegisterDerivationScheme("BTC");
|
|
|
|
user.RegisterDerivationScheme("USDT");
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2022-01-07 12:32:00 +09:00
|
|
|
Assert.Equal(3, Assert.IsType<ListWalletsViewModel>(Assert.IsType<ViewResult>(await user.GetController<UIWalletsController>().ListWallets()).Model).Wallets.Count);
|
2019-12-24 08:20:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2019-12-24 18:11:21 +09:00
|
|
|
[Trait("Altcoins", "Altcoins")]
|
2019-12-24 08:20:44 +01:00
|
|
|
public async Task ElementsAssetsAreHandledCorrectly()
|
|
|
|
{
|
2021-11-22 17:16:08 +09:00
|
|
|
using (var tester = CreateServerTester())
|
2019-12-24 08:20:44 +01:00
|
|
|
{
|
2019-12-24 18:11:21 +09:00
|
|
|
tester.ActivateLBTC();
|
2019-12-24 08:20:44 +01:00
|
|
|
await tester.StartAsync();
|
2023-10-11 14:12:33 +02:00
|
|
|
|
|
|
|
//https://github.com/ElementsProject/elements/issues/956
|
|
|
|
await tester.LBTCExplorerNode.SendCommandAsync("rescanblockchain");
|
2019-12-24 08:20:44 +01:00
|
|
|
var user = tester.NewAccount();
|
2023-10-11 14:12:33 +02:00
|
|
|
await user.GrantAccessAsync();
|
|
|
|
|
2020-03-20 09:31:22 +01:00
|
|
|
await tester.LBTCExplorerNode.GenerateAsync(4);
|
2019-12-24 08:20:44 +01:00
|
|
|
//no tether on our regtest, lets create it and set it
|
|
|
|
var tether = tester.NetworkProvider.GetNetwork<ElementsBTCPayNetwork>("USDT");
|
|
|
|
var lbtc = tester.NetworkProvider.GetNetwork<ElementsBTCPayNetwork>("LBTC");
|
|
|
|
var issueAssetResult = await tester.LBTCExplorerNode.SendCommandAsync("issueasset", 100000, 0);
|
|
|
|
tether.AssetId = uint256.Parse(issueAssetResult.Result["asset"].ToString());
|
|
|
|
((ElementsBTCPayNetwork)tester.PayTester.GetService<BTCPayWalletProvider>().GetWallet("USDT").Network)
|
|
|
|
.AssetId = tether.AssetId;
|
2020-06-28 17:55:27 +09:00
|
|
|
Assert.Equal(tether.AssetId, tester.NetworkProvider.GetNetwork<ElementsBTCPayNetwork>("USDT").AssetId);
|
|
|
|
Assert.Equal(tether.AssetId, ((ElementsBTCPayNetwork)tester.PayTester.GetService<BTCPayWalletProvider>().GetWallet("USDT").Network).AssetId);
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-11 14:12:33 +02:00
|
|
|
user.RegisterDerivationScheme("LBTC");
|
|
|
|
user.RegisterDerivationScheme("USDT");
|
|
|
|
|
2019-12-24 08:20:44 +01:00
|
|
|
//test: register 2 assets on the same elements network and make sure paying an invoice on one does not affect the other in any way
|
|
|
|
var invoice = await user.BitPay.CreateInvoiceAsync(new Invoice(0.1m, "BTC"));
|
2020-03-20 09:31:22 +01:00
|
|
|
Assert.Equal(3, invoice.SupportedTransactionCurrencies.Count);
|
2019-12-24 08:20:44 +01:00
|
|
|
var ci = invoice.CryptoInfo.Single(info => info.CryptoCode.Equals("LBTC"));
|
|
|
|
//1 lbtc = 1 btc
|
|
|
|
Assert.Equal(1, ci.Rate);
|
2020-06-28 17:55:27 +09:00
|
|
|
var star = await tester.LBTCExplorerNode.SendCommandAsync("sendtoaddress", ci.Address, ci.Due, "", "", false, true,
|
2023-10-11 14:12:33 +02:00
|
|
|
1, "UNSET",false, lbtc.AssetId.ToString());
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2019-12-24 08:20:44 +01:00
|
|
|
TestUtils.Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal("paid", localInvoice.Status);
|
|
|
|
Assert.Single(localInvoice.CryptoInfo.Single(info => info.CryptoCode.Equals("LBTC")).Payments);
|
|
|
|
});
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2019-12-24 08:20:44 +01:00
|
|
|
invoice = await user.BitPay.CreateInvoiceAsync(new Invoice(0.1m, "BTC"));
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2019-12-24 08:20:44 +01:00
|
|
|
ci = invoice.CryptoInfo.Single(info => info.CryptoCode.Equals("USDT"));
|
2020-03-20 09:31:22 +01:00
|
|
|
Assert.Equal(3, invoice.SupportedTransactionCurrencies.Count);
|
2023-10-11 14:12:33 +02:00
|
|
|
star = tester.LBTCExplorerNode.SendCommand("sendtoaddress", ci.Address, decimal.Parse(ci.Due), "x", "z", false, true, 1, "unset", false, tether.AssetId.ToString());
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2019-12-24 08:20:44 +01:00
|
|
|
TestUtils.Eventually(() =>
|
|
|
|
{
|
|
|
|
var localInvoice = user.BitPay.GetInvoice(invoice.Id, Facade.Merchant);
|
|
|
|
Assert.Equal("paid", localInvoice.Status);
|
|
|
|
Assert.Single(localInvoice.CryptoInfo.Single(info => info.CryptoCode.Equals("USDT", StringComparison.InvariantCultureIgnoreCase)).Payments);
|
|
|
|
});
|
2024-10-29 15:43:37 +01:00
|
|
|
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2021-10-23 14:47:15 +09:00
|
|
|
var lbtcBip21 = new BitcoinUrlBuilder(invoice.CryptoInfo.Single(info => info.CryptoCode == "LBTC").PaymentUrls.BIP21, lbtc.NBitcoinNetwork);
|
2020-03-20 09:31:22 +01:00
|
|
|
//precision = 8, 0.1 = 0.1
|
2020-06-28 17:55:27 +09:00
|
|
|
Assert.Equal(0.1m, lbtcBip21.Amount.ToDecimal(MoneyUnit.BTC));
|
2019-12-24 08:20:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|