diff --git a/clightning/clightning.go b/clightning/clightning.go index 8ddfbd9e..82db1901 100644 --- a/clightning/clightning.go +++ b/clightning/clightning.go @@ -131,6 +131,7 @@ func NewClightningClient(ctx context.Context) (*ClightningClient, <-chan interfa cl.Plugin = glightning.NewPlugin(cl.onInit) err := cl.Plugin.RegisterHooks(&glightning.Hooks{ CustomMsgReceived: cl.OnCustomMsg, + RpcCommand: cl.OnRPCCommand, }) if err != nil { return nil, nil, err @@ -150,6 +151,25 @@ func NewClightningClient(ctx context.Context) (*ClightningClient, <-chan interfa return cl, cl.initChan, nil } +func (cl *ClightningClient) OnRPCCommand(event *glightning.RpcCommandEvent) (*jrpc2.RpcCommandResponse, error) { + if cl.gbitcoin != nil { + ok, err := cl.gbitcoin.Ping() + if err != nil || !ok { + log.Infof("trying to send command %s, but failed to connect: %v", event.Cmd.MethodName, err) + return event.ReturnError("bitcoin_unavailable", -1) + } + } + + if cl.liquidWallet != nil { + ok, err := cl.liquidWallet.Ping() + if err != nil || !ok { + log.Infof("trying to send command %s, but failed to connect: %v", event.Cmd.MethodName, err) + return event.ReturnError("liquid_unavailable", -1) + } + } + return event.Continue(), nil +} + // CanSpend checks if an `amtMsat` can be spend. It returns an error if the // amount is larger than the `ClnMaxPaymentSizeMsat` if the option // `--large-channels` is missing or set to false.