Example #1
0
        /// <summary>
        /// Connect a customer LN node to the merchant LN node
        /// </summary>
        /// <returns></returns>
        private async Task PrepareLightningAsync(ILightningInvoiceClient client)
        {
            bool awaitingLocking = false;

            while (true)
            {
                var merchantInfo = await WaitLNSynched(client, CustomerLightningD, MerchantLightningD);

                var peers = await CustomerLightningD.ListPeersAsync();

                var filteringToTargetedPeers = peers.Where(a => a.Id == merchantInfo.NodeId);
                var channel = filteringToTargetedPeers
                              .SelectMany(p => p.Channels)
                              .Where(c => !SKIPPED_STATES.Contains(c.State ?? ""))
                              .FirstOrDefault();

                switch (channel?.State)
                {
                case null:
                    var address = await CustomerLightningD.NewAddressAsync();

                    await ExplorerNode.SendToAddressAsync(address, Money.Coins(0.5m));

                    ExplorerNode.Generate(1);
                    await WaitLNSynched(client, CustomerLightningD, MerchantLightningD);

                    await Task.Delay(1000);

                    var merchantNodeInfo = new NodeInfo(merchantInfo.NodeId, merchantInfo.Address, merchantInfo.P2PPort);
                    await CustomerLightningD.ConnectAsync(merchantNodeInfo);

                    await CustomerLightningD.FundChannelAsync(merchantNodeInfo, Money.Satoshis(16777215));

                    break;

                case "CHANNELD_AWAITING_LOCKIN":
                    ExplorerNode.Generate(awaitingLocking ? 1 : 10);
                    await WaitLNSynched(client, CustomerLightningD, MerchantLightningD);

                    awaitingLocking = true;
                    break;

                case "CHANNELD_NORMAL":
                    return;

                default:
                    throw new NotSupportedException(channel?.State ?? "");
                }
            }
        }
Example #2
0
        /// <summary>
        /// Connect a customer LN node to the merchant LN node
        /// </summary>
        /// <returns></returns>
        public async Task PrepareLightningAsync()
        {
            while (true)
            {
                var skippedStates = new[] { "ONCHAIN", "CHANNELD_SHUTTING_DOWN", "CLOSINGD_SIGEXCHANGE", "CLOSINGD_COMPLETE", "FUNDING_SPEND_SEEN" };
                var channel       = (await CustomerLightningD.ListPeersAsync())
                                    .SelectMany(p => p.Channels)
                                    .Where(c => !skippedStates.Contains(c.State ?? ""))
                                    .FirstOrDefault();
                switch (channel?.State)
                {
                case null:
                    var merchantInfo = await WaitLNSynched();

                    var clightning = new NodeInfo(merchantInfo.Id, MerchantCharge.P2PHost, merchantInfo.Port);
                    await CustomerLightningD.ConnectAsync(clightning);

                    var address = await CustomerLightningD.NewAddressAsync();

                    await ExplorerNode.SendToAddressAsync(address, Money.Coins(0.2m));

                    ExplorerNode.Generate(1);
                    await WaitLNSynched();

                    await Task.Delay(1000);

                    await CustomerLightningD.FundChannelAsync(clightning, Money.Satoshis(16777215));

                    break;

                case "CHANNELD_AWAITING_LOCKIN":
                    ExplorerNode.Generate(1);
                    await WaitLNSynched();

                    break;

                case "CHANNELD_NORMAL":
                    return;

                default:
                    throw new NotSupportedException(channel?.State ?? "");
                }
            }
        }