| 901 | |
| 902 | |
| 903 | bool wallet_has_funds(struct wallet *w, |
| 904 | const struct utxo **excludes, |
| 905 | u32 current_blockheight, |
| 906 | struct amount_sat *needed) |
| 907 | { |
| 908 | struct db_stmt *stmt; |
| 909 | |
| 910 | stmt = db_prepare_v2(w->db, SQL("SELECT" |
| 911 | " prev_out_tx" |
| 912 | ", prev_out_index" |
| 913 | ", value" |
| 914 | ", type" |
| 915 | ", status" |
| 916 | ", keyindex" |
| 917 | ", channel_id" |
| 918 | ", peer_id" |
| 919 | ", commitment_point" |
| 920 | ", option_anchor_outputs" |
| 921 | ", confirmation_height" |
| 922 | ", spend_height" |
| 923 | ", scriptpubkey " |
| 924 | ", reserved_til" |
| 925 | ", csv_lock" |
| 926 | ", is_in_coinbase" |
| 927 | " FROM outputs" |
| 928 | " WHERE status = ?" |
| 929 | " OR (status = ? AND reserved_til <= ?)")); |
| 930 | db_bind_int(stmt, output_status_in_db(OUTPUT_STATE_AVAILABLE)); |
| 931 | db_bind_int(stmt, output_status_in_db(OUTPUT_STATE_RESERVED)); |
| 932 | db_bind_u64(stmt, current_blockheight); |
| 933 | |
| 934 | db_query_prepared(stmt); |
| 935 | while (db_step(stmt)) { |
| 936 | struct utxo *utxo = wallet_stmt2output(tmpctx, stmt); |
| 937 | |
| 938 | if (excluded(excludes, utxo) |
| 939 | || !deep_enough(-1U, utxo, current_blockheight)) { |
| 940 | continue; |
| 941 | } |
| 942 | |
| 943 | /* If we've found enough, answer is yes. */ |
| 944 | if (!amount_sat_sub(needed, *needed, utxo->amount)) { |
| 945 | *needed = AMOUNT_SAT(0); |
| 946 | tal_free(stmt); |
| 947 | return true; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /* Insufficient funds! */ |
| 952 | tal_free(stmt); |
| 953 | return false; |
| 954 | } |
| 955 | |
| 956 | bool wallet_add_onchaind_utxo(struct wallet *w, |
| 957 | const struct bitcoin_outpoint *outpoint, |
no test coverage detected