Skip to content

Commit

Permalink
bkpr: add dumpincomecsv command
Browse files Browse the repository at this point in the history
Prints out the `listincome` events as a CSV formatted file

Current csv_format options:

- koinly
- cointracker
- harmony (https://github.com/harmony-csv/harmony)
- quickbooks*

*Quickbooks expects values in 'USD', whereas we print values out
in <currency> (will be noted in the Description field). This won't work
how you'd expect -> you might need to do some conversion etc before
uploading it.

All amounts emitted as 'btc' w/ *11* decimals.
  • Loading branch information
niftynei authored and rustyrussell committed Jul 28, 2022
1 parent a7b7ea5 commit a0b3406
Show file tree
Hide file tree
Showing 3 changed files with 506 additions and 1 deletion.
68 changes: 68 additions & 0 deletions plugins/bkpr/bookkeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,64 @@ static struct fee_sum *find_sum_for_txid(struct fee_sum **sums,
return NULL;
}

static struct command_result *param_csv_format(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct csv_fmt **csv_fmt)
{
*csv_fmt = cast_const(struct csv_fmt *,
csv_match_token(buffer, tok));
if (*csv_fmt)
return NULL;

return command_fail_badparam(cmd, name, buffer, tok,
tal_fmt(cmd,
"should be one of: %s",
csv_list_fmts(cmd)));
}

static struct command_result *json_dump_income(struct command *cmd,
const char *buf,
const jsmntok_t *params)
{
struct json_stream *res;
struct income_event **evs;
struct csv_fmt *csv_fmt;
const char *filename;
bool *consolidate_fees;
char *err;
u64 *start_time, *end_time;

if (!param(cmd, buf, params,
p_req("csv_format", param_csv_format, &csv_fmt),
p_opt("csv_file", param_string, &filename),
p_opt_def("consolidate_fees", param_bool,
&consolidate_fees, true),
p_opt_def("start_time", param_u64, &start_time, 0),
p_opt_def("end_time", param_u64, &end_time, SQLITE_MAX_UINT),
NULL))
return command_param_failed();

/* Ok, go find me some income events! */
db_begin_transaction(db);
evs = list_income_events(cmd, db, *start_time, *end_time,
*consolidate_fees);
db_commit_transaction(db);

if (!filename)
filename = csv_filename(cmd, csv_fmt);

err = csv_print_income_events(cmd, csv_fmt, filename, evs);
if (err)
return command_fail(cmd, PLUGIN_ERROR,
"Unable to create csv file: %s",
err);

res = jsonrpc_stream_success(cmd);
json_add_string(res, "csv_file", filename);
json_add_string(res, "csv_format", csv_fmt->fmt_name);
return command_finished(cmd, res);
}

static struct command_result *json_list_income(struct command *cmd,
const char *buf,
const jsmntok_t *params)
Expand Down Expand Up @@ -1310,6 +1368,16 @@ static const struct plugin_command commands[] = {
"List all events for this node that impacted income",
json_list_income
},
{
"dumpincomecsv",
"bookkeeping",
"Print out all the income events to a csv file in "
" {csv_format",
"Dump income statment data to {csv_file} in {csv_format}."
" Optionally, {consolidate_fee}s into single entries"
" (default: true)",
json_dump_income
},
};

static const char *init(struct plugin *p, const char *b, const jsmntok_t *t)
Expand Down
Loading

0 comments on commit a0b3406

Please sign in to comment.