| 895 | } |
| 896 | |
| 897 | struct amount_sat change_amount(struct amount_sat excess, u32 feerate_perkw, |
| 898 | size_t total_weight) |
| 899 | { |
| 900 | size_t outweight; |
| 901 | struct amount_sat change_fee; |
| 902 | |
| 903 | /* Must be able to pay for its own additional weight */ |
| 904 | outweight = bitcoin_tx_output_weight(BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN); |
| 905 | |
| 906 | /* Rounding can cause off by one errors, so we do this */ |
| 907 | if (!amount_sat_sub(&change_fee, |
| 908 | amount_tx_fee(feerate_perkw, outweight + total_weight), |
| 909 | amount_tx_fee(feerate_perkw, total_weight))) |
| 910 | return AMOUNT_SAT(0); |
| 911 | |
| 912 | if (!amount_sat_sub(&excess, excess, change_fee)) |
| 913 | return AMOUNT_SAT(0); |
| 914 | |
| 915 | /* Must be non-dust */ |
| 916 | if (!amount_sat_greater_eq(excess, chainparams->dust_limit)) |
| 917 | return AMOUNT_SAT(0); |
| 918 | |
| 919 | return excess; |
| 920 | } |
no test coverage detected