bkpr: move json_to functions to respective type files

This commit is contained in:
niftynei
2022-07-19 17:04:37 +09:30
committed by Rusty Russell
parent 5f41d9247e
commit 462fa20c17
8 changed files with 78 additions and 51 deletions

View File

@@ -4,8 +4,10 @@ BOOKKEEPER_PLUGIN_SRC := \
plugins/bkpr/account.c \
plugins/bkpr/account_entry.c \
plugins/bkpr/bookkeeper.c \
plugins/bkpr/chain_event.c \
plugins/bkpr/channel_event.c \
plugins/bkpr/db.c \
plugins/bkpr/onchain_fee.c \
plugins/bkpr/recorder.c
BOOKKEEPER_DB_QUERIES := \

View File

@@ -27,57 +27,6 @@ static struct db *db ;
// FIXME: make relative to directory we're loaded into
static char *db_dsn = "sqlite3://accounts.sqlite3";
static void json_add_channel_event(struct json_stream *out,
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_only(out, "credit", ev->credit);
json_add_amount_msat_only(out, "debit", ev->debit);
json_add_string(out, "currency", ev->currency);
if (ev->payment_id)
json_add_sha256(out, "payment_id", ev->payment_id);
json_add_u64(out, "timestamp", ev->timestamp);
json_object_end(out);
}
static void json_add_chain_event(struct json_stream *out,
struct chain_event *ev)
{
json_object_start(out, NULL);
json_add_string(out, "account", ev->acct_name);
json_add_string(out, "type", "chain");
json_add_string(out, "tag", ev->tag);
json_add_amount_msat_only(out, "credit", ev->credit);
json_add_amount_msat_only(out, "debit", ev->debit);
json_add_string(out, "currency", ev->currency);
json_add_outpoint(out, "outpoint", &ev->outpoint);
if (ev->spending_txid)
json_add_txid(out, "txid", ev->spending_txid);
if (ev->payment_id)
json_add_sha256(out, "payment_id", ev->payment_id);
json_add_u64(out, "timestamp", ev->timestamp);
json_add_u32(out, "blockheight", ev->blockheight);
json_object_end(out);
}
static void json_add_onchain_fee(struct json_stream *out,
struct onchain_fee *fee)
{
json_object_start(out, NULL);
json_add_string(out, "account", fee->acct_name);
json_add_string(out, "type", "onchain_fee");
json_add_string(out, "tag", "onchain_fee");
json_add_amount_msat_only(out, "credit", fee->credit);
json_add_amount_msat_only(out, "debit", fee->debit);
json_add_string(out, "currency", fee->currency);
json_add_u64(out, "timestamp", fee->timestamp);
json_add_txid(out, "txid", &fee->txid);
json_object_end(out);
}
static struct fee_sum *find_sum_for_txid(struct fee_sum **sums,
struct bitcoin_txid *txid)
{

View File

@@ -0,0 +1,26 @@
#include "config.h"
#include <common/json_stream.h>
#include <plugins/bkpr/chain_event.h>
void json_add_chain_event(struct json_stream *out, struct chain_event *ev)
{
json_object_start(out, NULL);
json_add_string(out, "account", ev->acct_name);
if (ev->origin_acct)
json_add_string(out, "origin", ev->origin_acct);
json_add_string(out, "type", "chain");
json_add_string(out, "tag", ev->tag);
json_add_amount_msat_only(out, "credit", ev->credit);
json_add_amount_msat_only(out, "debit", ev->debit);
json_add_string(out, "currency", ev->currency);
json_add_outpoint(out, "outpoint", &ev->outpoint);
if (ev->spending_txid)
json_add_txid(out, "txid", ev->spending_txid);
if (ev->payment_id)
json_add_sha256(out, "payment_id", ev->payment_id);
json_add_u64(out, "timestamp", ev->timestamp);
json_add_u32(out, "blockheight", ev->blockheight);
json_object_end(out);
}

View File

@@ -2,11 +2,13 @@
#define LIGHTNING_PLUGINS_BKPR_CHAIN_EVENT_H
#include "config.h"
#include <bitcoin/tx.h>
#include <ccan/short_types/short_types.h>
struct amount_msat;
struct bitcoin_outpoint;
struct bitcoin_txid;
struct json_stream;
struct chain_event {
@@ -53,4 +55,7 @@ struct chain_event {
struct sha256 *payment_id;
};
void json_add_chain_event(struct json_stream *out,
struct chain_event *ev);
#endif /* LIGHTNING_PLUGINS_BKPR_CHAIN_EVENT_H */

View File

@@ -3,6 +3,7 @@
#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>
struct channel_event *new_channel_event(const tal_t *ctx,
@@ -28,3 +29,19 @@ struct channel_event *new_channel_event(const tal_t *ctx,
return ev;
}
void json_add_channel_event(struct json_stream *out,
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_only(out, "credit", ev->credit);
json_add_amount_msat_only(out, "debit", ev->debit);
json_add_string(out, "currency", ev->currency);
if (ev->payment_id)
json_add_sha256(out, "payment_id", ev->payment_id);
json_add_u64(out, "timestamp", ev->timestamp);
json_object_end(out);
}

View File

@@ -6,6 +6,7 @@
#include <common/utils.h>
struct amount_msat;
struct json_stream;
struct sha256;
struct channel_event {
@@ -54,4 +55,7 @@ struct channel_event *new_channel_event(const tal_t *ctx,
u32 part_id,
u64 timestamp);
void json_add_channel_event(struct json_stream *out,
struct channel_event *ev);
#endif /* LIGHTNING_PLUGINS_BKPR_CHANNEL_EVENT_H */

View File

@@ -0,0 +1,20 @@
#include "config.h"
#include <common/json_stream.h>
#include <plugins/bkpr/onchain_fee.h>
void json_add_onchain_fee(struct json_stream *out,
struct onchain_fee *fee)
{
json_object_start(out, NULL);
json_add_string(out, "account", fee->acct_name);
json_add_string(out, "type", "onchain_fee");
json_add_string(out, "tag", "onchain_fee");
json_add_amount_msat_only(out, "credit", fee->credit);
json_add_amount_msat_only(out, "debit", fee->debit);
json_add_string(out, "currency", fee->currency);
json_add_u64(out, "timestamp", fee->timestamp);
json_add_txid(out, "txid", &fee->txid);
json_object_end(out);
}

View File

@@ -2,6 +2,7 @@
#define LIGHTNING_PLUGINS_BKPR_ONCHAIN_FEE_H
#include "config.h"
#include <bitcoin/tx.h>
#include <ccan/short_types/short_types.h>
struct amount_msat;
@@ -32,4 +33,7 @@ struct onchain_fee {
u32 update_count;
};
void json_add_onchain_fee(struct json_stream *out,
struct onchain_fee *fee);
#endif /* LIGHTNING_PLUGINS_BKPR_ONCHAIN_FEE_H */