| 918 | } |
| 919 | |
| 920 | static 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); |
nothing calls this directly
no test coverage detected