public ActionResult CloseChannel(string cryptoCode, [FromBody] CloseChannelRequest req) { var n = _networkProvider.GetByCryptoCode(cryptoCode.ToLowerInvariant()); var peerMan = _peerManagerProvider.GetPeerManager(n); var channels = peerMan.ChannelManager.ListChannels(_pool); var s = channels.FirstOrDefault(x => x.RemoteNetworkId == req.TheirNetworkKey); if (s is null) { return(NotFound($"There is no opened channel against {req.TheirNetworkKey}")); } peerMan.ChannelManager.CloseChannel(s.ChannelId); peerMan.ProcessEvents(); return(Ok()); }
public async Task <JsonResult> GetWalletInfo(string cryptoCode) { var n = _networkProvider.GetByCryptoCode(cryptoCode); var derivationStrategy = await _walletService.GetOurDerivationStrategyAsync(n); var balance = await _walletService.GetBalanceAsync(n); var outboundCaps = _peerManagerProvider.GetPeerManager(n).ChannelManager.ListChannels(_pool) .Select(c => c.OutboundCapacityMSat).ToArray(); var offChainBalance = outboundCaps.Length == 0 ? 0 : outboundCaps.Aggregate((x, acc) => x + acc); var resp = new WalletInfo { DerivationStrategy = derivationStrategy, OnChainBalanceSatoshis = balance, OffChainBalanceMSat = offChainBalance }; return(new JsonResult(resp, _repositoryProvider.GetSerializer(n).Options)); }
public async Task PayInvoice(PaymentRequest invoice, long?amountMSat = null, CancellationToken ct = default) { var amount = invoice.AmountValue?.Value.MilliSatoshi ?? amountMSat; if (amount is null) { throw new NRustLightningException($"You must specify payment amount if it is not included in invoice"); } var n = _networkProvider.TryGetByInvoice(invoice); if (n is null) { throw new NRustLightningException($"Unknown invoice prefix {invoice.PrefixValue}"); } var peerMan = _peerManagerProvider.GetPeerManager(n); var failureTcs = new TaskCompletionSource <Event>(); var successTcs = new TaskCompletionSource <Event>(); _eventAggregator.Subscribe <Event>(e => { if (e is Event.PaymentFailed paymentFailed && paymentFailed.Item.PaymentHash.Equals(invoice.PaymentHash)) { _logger.LogError($"Payment for invoice ({invoice}) failed"); failureTcs.SetResult(paymentFailed); } if (e is Event.PaymentSent paymentSent && paymentSent.Item.Hash.Equals(invoice.PaymentHash)) { _logger.LogInformation($"Payment for invoice ({invoice}) succeed"); successTcs.SetResult(paymentSent); } }); peerMan.SendPayment(invoice.NodeIdValue.Item, invoice.PaymentHash, new List <RouteHint>(), LNMoney.MilliSatoshis(amount.Value), invoice.MinFinalCLTVExpiryDelta); peerMan.ProcessEvents(); var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); cts.CancelAfter(TimeSpan.FromSeconds(_config.Value.PaymentTimeoutSec)); var delay = Task.Delay(TimeSpan.FromMilliseconds(-1), cts.Token); var task = Task.WhenAny(failureTcs.Task, successTcs.Task); await Task.WhenAny(task, delay); if (cts.IsCancellationRequested) { throw new NRustLightningException($"Payment for {invoice} did not finish in {_config.Value.PaymentTimeoutSec} seconds"); } var resultEvent = await await task; if (resultEvent is Event.PaymentFailed paymentFailed) { if (paymentFailed.Item.RejectedByDest) { throw new NRustLightningException($"Failed to pay! rejected by destination"); } throw new NRustLightningException($"Failed to pay!"); } if (resultEvent is Event.PaymentSent paymentSent) { await _invoiceRepository.SetPreimage(paymentSent.Item); } }