Files
palladum-lightning/plugins/bkpr/channel_event.c
Rusty Russell 2a191479a3 bkpr: take, don't steal in new_channel_event.
Cleaner (I'm about to hand it a sha256 on the stack).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30

59 lines
1.7 KiB
C

#include "config.h"
#include <bitcoin/chainparams.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/tal/str/str.h>
#include <common/amount.h>
#include <common/json_stream.h>
#include <plugins/bkpr/channel_event.h>
#include <plugins/bkpr/descriptions.h>
#include <plugins/bkpr/rebalances.h>
struct channel_event *new_channel_event(const tal_t *ctx,
const char *tag,
struct amount_msat credit,
struct amount_msat debit,
struct amount_msat fees,
const struct sha256 *payment_id TAKES,
u32 part_id,
u64 timestamp)
{
struct channel_event *ev = tal(ctx, struct channel_event);
ev->tag = tal_strdup(ev, tag);
ev->credit = credit;
ev->debit = debit;
ev->fees = fees;
ev->payment_id = tal_dup_or_null(ev, struct sha256, payment_id);
ev->part_id = part_id;
ev->timestamp = timestamp;
return ev;
}
void json_add_channel_event(struct json_stream *out,
const struct bkpr *bkpr,
struct channel_event *ev)
{
json_object_start(out, NULL);
json_add_string(out, "account", ev->acct_name);
json_add_string(out, "type", "channel");
json_add_string(out, "tag", ev->tag);
json_add_amount_msat(out, "credit_msat", ev->credit);
json_add_amount_msat(out, "debit_msat", ev->debit);
if (!amount_msat_is_zero(ev->fees))
json_add_amount_msat(out, "fees_msat", ev->fees);
json_add_string(out, "currency", chainparams->lightning_hrp);
if (ev->payment_id) {
const char *desc = channel_event_description(bkpr, ev);
json_add_sha256(out, "payment_id", ev->payment_id);
json_add_u32(out, "part_id", ev->part_id);
if (desc)
json_add_string(out, "description", desc);
}
json_add_u64(out, "timestamp", ev->timestamp);
json_add_bool(out, "is_rebalance",
find_rebalance(bkpr, ev->db_id) != NULL);
json_object_end(out);
}