MCPcopy Create free account
hub / github.com/ElementsProject/lightning / do_list_balances

Function do_list_balances

plugins/bkpr/bookkeeper.c:920–986  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

918}
919
920static struct command_result *do_list_balances(struct command *cmd,
921 void *unused)
922{
923 struct json_stream *res;
924 struct account **accts;
925 struct bkpr *bkpr = bkpr_of(cmd->plugin);
926
927 res = jsonrpc_stream_success(cmd);
928 /* List of accts */
929 accts = list_accounts(cmd, bkpr);
930
931 json_array_start(res, "accounts");
932 for (size_t i = 0; i < tal_count(accts); i++) {
933 struct amount_msat credit, debit, balance;
934 bool has_events;
935
936 has_events = account_get_credit_debit(bkpr, cmd,
937 accts[i]->name,
938 &credit, &debit);
939 if (!amount_msat_sub(&balance, credit, debit)) {
940 plugin_err(cmd->plugin,
941 "Account balance underflow for account %s (credit %s, debit %s)",
942 accts[i]->name,
943 fmt_amount_msat(tmpctx, credit),
944 fmt_amount_msat(tmpctx, debit));
945 }
946
947 /* Skip the external acct balance, it's effectively
948 * meaningless */
949 if (is_external_account(accts[i]->name))
950 continue;
951
952 /* Add it to the result data */
953 json_object_start(res, NULL);
954
955 json_add_string(res, "account", accts[i]->name);
956 if (accts[i]->peer_id) {
957 json_add_node_id(res, "peer_id", accts[i]->peer_id);
958 json_add_bool(res, "we_opened", accts[i]->we_opened);
959 json_add_bool(res, "account_closed",
960 !(!accts[i]->closed_event_db_id));
961 json_add_bool(res, "account_resolved",
962 accts[i]->onchain_resolved_block > 0);
963 if (accts[i]->onchain_resolved_block > 0)
964 json_add_u32(res, "resolved_at_block",
965 accts[i]->onchain_resolved_block);
966 }
967
968 /* FIXME: This API is now overkill! */
969 json_array_start(res, "balances");
970 /* We expect no entry if account is not used. */
971 for (size_t j = 0; j < has_events; j++) {
972 json_object_start(res, NULL);
973 json_add_amount_msat(res, "balance_msat",
974 balance);
975 json_add_string(res, "coin_type",
976 chainparams->lightning_hrp);
977 json_object_end(res);

Callers

nothing calls this directly

Calls 15

json_array_startFunction · 0.85
amount_msat_subFunction · 0.85
is_external_accountFunction · 0.85
json_object_startFunction · 0.85
json_add_node_idFunction · 0.85
json_add_boolFunction · 0.85
json_add_u32Function · 0.85
json_add_amount_msatFunction · 0.85
json_object_endFunction · 0.85
json_array_endFunction · 0.85
bkpr_ofFunction · 0.70
list_accountsFunction · 0.70

Tested by

no test coverage detected