Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Channel Management APIs #84

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/BTCPayServer.Lightning.CLightning/CLightningClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,39 @@ public async Task<PeerInfo[]> ListPeersAsync(CancellationToken cancellation = de
return peers;
}

public Task FundChannelAsync(OpenChannelRequest openChannelRequest, CancellationToken cancellation)
public Task<FundChannelResponse> FundChannelAsync(OpenChannelRequest openChannelRequest, CancellationToken cancellation)
{
OpenChannelRequest.AssertIsSane(openChannelRequest);
List<object> parameters = new List<object>();
parameters.Add(openChannelRequest.NodeInfo.NodeId.ToString());
parameters.Add(openChannelRequest.ChannelAmount.Satoshi);
if (openChannelRequest.FeeRate != null)
parameters.Add($"{openChannelRequest.FeeRate.FeePerK.Satoshi * 4}perkw");
return SendCommandAsync<object>("fundchannel", parameters.ToArray(), true, cancellation: cancellation);
else
{
parameters.Add("normal");
}

if (openChannelRequest.Private != null)
{
parameters.Add(openChannelRequest.Private.ToString().ToLowerInvariant());

}
return SendCommandAsync<FundChannelResponse>("fundchannel", parameters.ToArray(), true, cancellation: cancellation);
}

public class FundChannelResponse
{
[JsonProperty("tx")]
public string Transaction { get; set; }
[JsonProperty("txid")]
public string TransactionId { get; set; }
[JsonProperty("outnum")]
public string FundingOutputIndex { get; set; }
[JsonProperty("channel_id")]
public string ChannelId { get; set; }
[JsonProperty("close_to")]
public string CloseToScriptPubKey { get; set; }
}

public Task ConnectAsync(NodeInfo nodeInfo, CancellationToken cancellation = default)
Expand Down Expand Up @@ -398,6 +422,7 @@ async Task<LightningChannel[]> ILightningClient.ListChannels(CancellationToken c
{
channels.Add(new LightningChannel()
{
Id = channel.ShortChannelId.ToString(),
RemoteNode = new PubKey(peer.Id),
IsPublic = !channel.Private,
LocalBalance = channel.ToUs,
Expand Down Expand Up @@ -486,10 +511,11 @@ async Task<LightningNodeBalance> ILightningClient.GetBalance(CancellationToken c
async Task<OpenChannelResponse> ILightningClient.OpenChannel(OpenChannelRequest openChannelRequest, CancellationToken cancellation)
{
retry:
try
{
await FundChannelAsync(openChannelRequest, cancellation);
}
FundChannelResponse response;
try
{
response = await FundChannelAsync(openChannelRequest, cancellation);
}
catch (LightningRPCException ex) when (ex.Code == CLightningErrorCode.STILL_SYNCING_BITCOIN)
{
await Task.Delay(1000, cancellation);
Expand Down Expand Up @@ -522,7 +548,10 @@ async Task<OpenChannelResponse> ILightningClient.OpenChannel(OpenChannelRequest
{
return new OpenChannelResponse(OpenChannelResult.AlreadyExists);
}
return new OpenChannelResponse(OpenChannelResult.Ok);
return new OpenChannelResponse(OpenChannelResult.Ok)
{
ChannelId = response.ChannelId
};
}

async Task<BitcoinAddress> ILightningClient.GetDepositAddress(CancellationToken cancellation)
Expand Down
1 change: 1 addition & 0 deletions src/BTCPayServer.Lightning.Common/LightningChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace BTCPayServer.Lightning
{
public class LightningChannel
{
public string Id { get; set; }
public PubKey RemoteNode { get; set; }
public bool IsPublic { get; set; }
public bool IsActive { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/BTCPayServer.Lightning.Common/OpenChannelRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public FeeRate FeeRate
{
get; set;
}

public bool? Private { get; set; }
public static void AssertIsSane(OpenChannelRequest openChannelRequest)
{
if (openChannelRequest == null)
Expand Down
2 changes: 2 additions & 0 deletions src/BTCPayServer.Lightning.Common/OpenChannelResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public OpenChannelResult Result
{
get; set;
}

public string ChannelId { get; set; }
}
}
10 changes: 6 additions & 4 deletions src/BTCPayServer.Lightning.Eclair/EclairClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,19 @@ public async Task<string> Connect(PubKey nodeId, string host, int? port = null,
}, cts);
}

public async Task<string> Open(PubKey nodeId, long fundingSatoshis, int? pushMsat = null,
long? fundingFeerateSatByte = null, ChannelFlags? channelFlags = null,
public async Task<string> Open(PubKey nodeId, long fundingSatoshis, string channelType = null,int? pushMsat = null,
long? fundingFeerateSatByte = null, bool? announceChannel = null, int? openTimeoutSeconds = null,
CancellationToken cts = default)
{
return await SendCommandAsync<OpenRequest, string>("open", new OpenRequest()
{
NodeId = nodeId.ToString(),
FundingSatoshis = fundingSatoshis,
ChannelFlags = channelFlags,
AnnounceChannel = announceChannel,
ChannelType = channelType,
PushMsat = pushMsat,
FundingFeerateSatByte = fundingFeerateSatByte
FundingFeerateSatByte = fundingFeerateSatByte,
OpenTimeoutSeconds = openTimeoutSeconds
}, cts);

}
Expand Down
17 changes: 11 additions & 6 deletions src/BTCPayServer.Lightning.Eclair/EclairLightningClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,15 @@ public async Task<OpenChannelResponse> OpenChannel(OpenChannelRequest openChanne
try
{
var result = await _eclairClient.Open(openChannelRequest.NodeInfo.NodeId,
openChannelRequest.ChannelAmount.Satoshi
, null,
Convert.ToInt64(openChannelRequest.FeeRate.SatoshiPerByte), null, cancellation);

openChannelRequest.ChannelAmount.Satoshi, null, null,
(openChannelRequest.FeeRate is null
? (long?)null
: Convert.ToInt64(openChannelRequest.FeeRate.SatoshiPerByte)), openChannelRequest.Private, null,
cancellation);

if (result.Contains("created channel", StringComparison.OrdinalIgnoreCase))
{
var channelId = result.Replace("created channel", "").Trim();
string channelId = result.Replace("created channel", "").Trim();
var channel = await _eclairClient.Channel(channelId, cancellation);
switch (channel.State)
{
Expand All @@ -346,7 +348,10 @@ public async Task<OpenChannelResponse> OpenChannel(OpenChannelRequest openChanne
case "WAIT_FOR_FUNDING_SIGNED":
case "WAIT_FOR_FUNDING_LOCKED":
case "WAIT_FOR_FUNDING_CONFIRMED":
return new OpenChannelResponse(OpenChannelResult.NeedMoreConf);
return new OpenChannelResponse(OpenChannelResult.NeedMoreConf)
{
ChannelId = channelId
};
}
}

Expand Down
8 changes: 0 additions & 8 deletions src/BTCPayServer.Lightning.Eclair/Models/ChannelFlags.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/BTCPayServer.Lightning.Eclair/Models/OpenRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class OpenRequest
public long FundingSatoshis { get; set; }
public long? PushMsat { get; set; }
public long? FundingFeerateSatByte { get; set; }
public ChannelFlags? ChannelFlags { get; set; }
public string ChannelType { get; set; }
public bool? AnnounceChannel { get; set; }
public int? OpenTimeoutSeconds { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/BTCPayServer.Lightning.LND/LndClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,13 @@ async Task<OpenChannelResponse> ILightningClient.OpenChannel(OpenChannelRequest
{
Local_funding_amount = openChannelRequest.ChannelAmount.Satoshi.ToString(CultureInfo.InvariantCulture),
Node_pubkey_string = openChannelRequest.NodeInfo.NodeId.ToString(),
Private = openChannelRequest.Private
};
if (openChannelRequest.FeeRate != null)
{
req.Sat_per_byte = ((int)openChannelRequest.FeeRate.SatoshiPerByte).ToString();
}
var result = await this.SwaggerClient.OpenChannelSyncAsync(req, cancellation);
await SwaggerClient.OpenChannelSyncAsync(req, cancellation);
return new OpenChannelResponse(OpenChannelResult.Ok);
}
catch (SwaggerException ex) when
Expand Down