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

Function amount_msat_sub_fee

common/amount.c:656–684  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

654}
655
656struct amount_msat amount_msat_sub_fee(struct amount_msat in,
657 u32 fee_base_msat,
658 u32 fee_proportional_millionths)
659{
660 struct amount_msat out, out_plus_one;
661
662 /* out = in - base - (out * prop / 1000000)
663 * Thus: out * (1 + prop / 1000000) = in - base
664 * out = (in - base) / (1 + prop / 1000000)
665 * out = 1000000 * (in - base) / (1000000 + prop)
666 *
667 * Since we round the fee down, out can be a bit bigger than
668 * expected, so we iterate upwards.
669 */
670 if (!amount_msat_sub(&out, in, amount_msat(fee_base_msat)))
671 return AMOUNT_MSAT(0);
672 if (!amount_msat_mul_div(&out, out, 1000000,
673 1000000 + fee_proportional_millionths))
674 return AMOUNT_MSAT(0);
675
676 /* If we calc reverse, it must work! */
677 assert(within_fee(in, out, fee_base_msat, fee_proportional_millionths));
678
679 /* We can be out-by-one */
680 if (amount_msat_add(&out_plus_one, out, AMOUNT_MSAT(1))
681 && within_fee(in, out_plus_one, fee_base_msat, fee_proportional_millionths))
682 return out_plus_one;
683 return out;
684}
685
686bool amount_msat_add_fee(struct amount_msat *amt,
687 u32 fee_base_msat,

Callers 5

runFunction · 0.85
test_amount_sub_feeFunction · 0.85
convert_flows_to_routesFunction · 0.85
flow_max_deliverableFunction · 0.85
flow_min_deliverableFunction · 0.85

Calls 5

amount_msat_subFunction · 0.85
amount_msat_mul_divFunction · 0.85
within_feeFunction · 0.85
amount_msat_addFunction · 0.85
amount_msatFunction · 0.70

Tested by 1

test_amount_sub_feeFunction · 0.68