SubscribeChannelEvents
SubscribeChannelEvents creates a uni-directional stream from the server to the client in which any updates relevant to the state of the channels are sent over. Events include new active channels, inactive channels, and closed channels.
Source: lightning.proto
gRPC
info
This is a server-streaming RPC
rpc SubscribeChannelEvents (ChannelEventSubscription) returns (stream ChannelEventUpdate);
REST
HTTP Method | Path |
---|---|
GET | /v1/channels/subscribe |
Code Samples
- gRPC
- REST
- Shell
- Javascript
- Python
const fs = require('fs');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const GRPC_HOST = 'localhost:10009'
const MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
const TLS_PATH = 'LND_DIR/tls.cert'
const loaderOptions = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
const packageDefinition = protoLoader.loadSync('lightning.proto', loaderOptions);
const lnrpc = grpc.loadPackageDefinition(packageDefinition).lnrpc;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
const tlsCert = fs.readFileSync(TLS_PATH);
const sslCreds = grpc.credentials.createSsl(tlsCert);
const macaroon = fs.readFileSync(MACAROON_PATH).toString('hex');
const macaroonCreds = grpc.credentials.createFromMetadataGenerator(function(args, callback) {
let metadata = new grpc.Metadata();
metadata.add('macaroon', macaroon);
callback(null, metadata);
});
let creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
let client = new lnrpc.Lightning(GRPC_HOST, creds);
let request = {};
let call = client.subscribeChannelEvents(request);
call.on('data', function(response) {
// A response was received from the server.
console.log(response);
});
call.on('status', function(status) {
// The current status of the stream.
});
call.on('end', function() {
// The server has closed the stream.
});
// Console output:
// {
// "open_channel": <Channel>,
// "closed_channel": <ChannelCloseSummary>,
// "active_channel": <ChannelPoint>,
// "inactive_channel": <ChannelPoint>,
// "pending_open_channel": <PendingUpdate>,
// "fully_resolved_channel": <ChannelPoint>,
// "type": <UpdateType>,
// }
import codecs, grpc, os
# Generate the following 2 modules by compiling the lightning.proto with the grpcio-tools.
# See https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/python.md for instructions.
import lightning_pb2 as lnrpc, lightning_pb2_grpc as lightningstub
GRPC_HOST = 'localhost:10009'
MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
TLS_PATH = 'LND_DIR/tls.cert'
# create macaroon credentials
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
def metadata_callback(context, callback):
callback([('macaroon', macaroon)], None)
auth_creds = grpc.metadata_call_credentials(metadata_callback)
# create SSL credentials
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
cert = open(TLS_PATH, 'rb').read()
ssl_creds = grpc.ssl_channel_credentials(cert)
# combine macaroon and SSL credentials
combined_creds = grpc.composite_channel_credentials(ssl_creds, auth_creds)
# make the request
channel = grpc.secure_channel(GRPC_HOST, combined_creds)
stub = lightningstub.LightningStub(channel)
request = lnrpc.ChannelEventSubscription()
for response in stub.SubscribeChannelEvents(request):
print(response)
# {
# "open_channel": <Channel>,
# "closed_channel": <ChannelCloseSummary>,
# "active_channel": <ChannelPoint>,
# "inactive_channel": <ChannelPoint>,
# "pending_open_channel": <PendingUpdate>,
# "fully_resolved_channel": <ChannelPoint>,
# "type": <UpdateType>,
# }
- Javascript
- Python
const fs = require('fs');
const request = require('request');
const REST_HOST = 'localhost:8080'
const MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
let options = {
url: `https://${REST_HOST}/v1/channels/subscribe`,
// Work-around for self-signed certificates.
rejectUnauthorized: false,
json: true,
headers: {
'Grpc-Metadata-macaroon': fs.readFileSync(MACAROON_PATH).toString('hex'),
},
}
request.get(options, function(error, response, body) {
console.log(body);
});
// Console output:
// {
// "open_channel": <object>, // <Channel>
// "closed_channel": <object>, // <ChannelCloseSummary>
// "active_channel": <object>, // <ChannelPoint>
// "inactive_channel": <object>, // <ChannelPoint>
// "pending_open_channel": <object>, // <PendingUpdate>
// "fully_resolved_channel": <object>, // <ChannelPoint>
// "type": <string>, // <UpdateType>
// }
// --------------------------
// Example with websockets:
// --------------------------
const WebSocket = require('ws');
const fs = require('fs');
const REST_HOST = 'localhost:8080'
const MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
let ws = new WebSocket(`wss://${REST_HOST}/v1/channels/subscribe?method=GET`, {
// Work-around for self-signed certificates.
rejectUnauthorized: false,
headers: {
'Grpc-Metadata-Macaroon': fs.readFileSync(MACAROON_PATH).toString('hex'),
},
});
let requestBody = {
};
ws.on('open', function() {
ws.send(JSON.stringify(requestBody));
});
ws.on('error', function(err) {
console.log('Error: ' + err);
});
ws.on('message', function(body) {
console.log(body);
});
// Console output:
// {
// "open_channel": <object>, // <Channel>
// "closed_channel": <object>, // <ChannelCloseSummary>
// "active_channel": <object>, // <ChannelPoint>
// "inactive_channel": <object>, // <ChannelPoint>
// "pending_open_channel": <object>, // <PendingUpdate>
// "fully_resolved_channel": <object>, // <ChannelPoint>
// "type": <string>, // <UpdateType>
// }
import base64, codecs, json, requests
REST_HOST = 'localhost:8080'
MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
TLS_PATH = 'LND_DIR/tls.cert'
url = f'https://{REST_HOST}/v1/channels/subscribe'
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
headers = {'Grpc-Metadata-macaroon': macaroon}
r = requests.get(url, headers=headers, stream=True, verify=TLS_PATH)
for raw_response in r.iter_lines():
json_response = json.loads(raw_response)
print(json_response)
# {
# "open_channel": <Channel>,
# "closed_channel": <ChannelCloseSummary>,
# "active_channel": <ChannelPoint>,
# "inactive_channel": <ChannelPoint>,
# "pending_open_channel": <PendingUpdate>,
# "fully_resolved_channel": <ChannelPoint>,
# "type": <UpdateType>,
# }
# There is no CLI command for this RPC
Messages
lnrpc.ChannelEventSubscription
Source: lightning.proto
note
This request has no parameters.
lnrpc.ChannelEventUpdate
Source: lightning.proto
Field | gRPC Type | REST Type |
---|---|---|
open_channel | Channel | object |
closed_channel | ChannelCloseSummary | object |
active_channel | ChannelPoint | object |
inactive_channel | ChannelPoint | object |
pending_open_channel | PendingUpdate | object |
fully_resolved_channel | ChannelPoint | object |
type | UpdateType | string |
Nested Messages
lnrpc.Channel
Field | gRPC Type | REST Type |
---|---|---|
active | bool | boolean |
remote_pubkey | string | string |
channel_point | string | string |
chan_id | uint64 | string |
capacity | int64 | string |
local_balance | int64 | string |
remote_balance | int64 | string |
commit_fee | int64 | string |
commit_weight | int64 | string |
fee_per_kw | int64 | string |
unsettled_balance | int64 | string |
total_satoshis_sent | int64 | string |
total_satoshis_received | int64 | string |
num_updates | uint64 | string |
pending_htlcs | HTLC[] | array |
csv_delay deprecated | uint32 | integer |
private | bool | boolean |
initiator | bool | boolean |
chan_status_flags | string | string |
local_chan_reserve_sat deprecated | int64 | string |
remote_chan_reserve_sat deprecated | int64 | string |
static_remote_key deprecated | bool | boolean |
commitment_type | CommitmentType | string |
lifetime | int64 | string |
uptime | int64 | string |
close_address | string | string |
push_amount_sat | uint64 | string |
thaw_height | uint32 | integer |
local_constraints | ChannelConstraints | object |
remote_constraints | ChannelConstraints | object |
alias_scids | uint64[] | array |
zero_conf | bool | boolean |
zero_conf_confirmed_scid | uint64 | string |
peer_alias | string | string |
peer_scid_alias | uint64 | string |
memo | string | string |
custom_channel_data | bytes | string |
lnrpc.ChannelCloseSummary
Field | gRPC Type | REST Type |
---|---|---|
channel_point | string | string |
chan_id | uint64 | string |
chain_hash | string | string |
closing_tx_hash | string | string |
remote_pubkey | string | string |
capacity | int64 | string |
close_height | uint32 | integer |
settled_balance | int64 | string |
time_locked_balance | int64 | string |
close_type | ClosureType | string |
open_initiator | Initiator | string |
close_initiator | Initiator | string |
resolutions | Resolution[] | array |
alias_scids | uint64[] | array |
zero_conf_confirmed_scid | uint64 | string |
lnrpc.ChannelConstraints
Field | gRPC Type | REST Type |
---|---|---|
csv_delay | uint32 | integer |
chan_reserve_sat | uint64 | string |
dust_limit_sat | uint64 | string |
max_pending_amt_msat | uint64 | string |
min_htlc_msat | uint64 | string |
max_accepted_htlcs | uint32 | integer |
lnrpc.ChannelPoint
Field | gRPC Type | REST Type |
---|---|---|
funding_txid_bytes | bytes | string |
funding_txid_str | string | string |
output_index | uint32 | integer |
lnrpc.HTLC
Field | gRPC Type | REST Type |
---|---|---|
incoming | bool | boolean |
amount | int64 | string |
hash_lock | bytes | string |
expiration_height | uint32 | integer |
htlc_index | uint64 | string |
forwarding_channel | uint64 | string |
forwarding_htlc_index | uint64 | string |
lnrpc.OutPoint
Field | gRPC Type | REST Type |
---|---|---|
txid_bytes | bytes | string |
txid_str | string | string |
output_index | uint32 | integer |
lnrpc.PendingUpdate
Field | gRPC Type | REST Type |
---|---|---|
txid | bytes | string |
output_index | uint32 | integer |
lnrpc.Resolution
Field | gRPC Type | REST Type |
---|---|---|
resolution_type | ResolutionType | string |
outcome | ResolutionOutcome | string |
outpoint | OutPoint | object |
amount_sat | uint64 | string |
sweep_txid | string | string |
Enums
lnrpc.ChannelCloseSummary.ClosureType
Name | Number |
---|---|
COOPERATIVE_CLOSE | 0 |
LOCAL_FORCE_CLOSE | 1 |
REMOTE_FORCE_CLOSE | 2 |
BREACH_CLOSE | 3 |
FUNDING_CANCELED | 4 |
ABANDONED | 5 |
lnrpc.ChannelEventUpdate.UpdateType
Name | Number |
---|---|
OPEN_CHANNEL | 0 |
CLOSED_CHANNEL | 1 |
ACTIVE_CHANNEL | 2 |
INACTIVE_CHANNEL | 3 |
PENDING_OPEN_CHANNEL | 4 |
FULLY_RESOLVED_CHANNEL | 5 |
lnrpc.CommitmentType
Name | Number |
---|---|
UNKNOWN_COMMITMENT_TYPE | 0 |
LEGACY | 1 |
STATIC_REMOTE_KEY | 2 |
ANCHORS | 3 |
SCRIPT_ENFORCED_LEASE | 4 |
SIMPLE_TAPROOT | 5 |
SIMPLE_TAPROOT_OVERLAY | 6 |
lnrpc.Initiator
Name | Number |
---|---|
INITIATOR_UNKNOWN | 0 |
INITIATOR_LOCAL | 1 |
INITIATOR_REMOTE | 2 |
INITIATOR_BOTH | 3 |
lnrpc.ResolutionOutcome
Name | Number |
---|---|
OUTCOME_UNKNOWN | 0 |
CLAIMED | 1 |
UNCLAIMED | 2 |
ABANDONED | 3 |
FIRST_STAGE | 4 |
TIMEOUT | 5 |
lnrpc.ResolutionType
Name | Number |
---|---|
TYPE_UNKNOWN | 0 |
ANCHOR | 1 |
INCOMING_HTLC | 2 |
OUTGOING_HTLC | 3 |
COMMIT | 4 |