common: Add multiplication primitives for amount_msat and amount_sat

The scale variants weren't usable since they use `double` as a scale
factor, which in turn causes imprecisions.
This commit is contained in:
Christian Decker
2022-08-17 09:40:52 +02:00
parent 493a0dfcd4
commit 75fe1c1a66
2 changed files with 19 additions and 0 deletions

View File

@@ -507,6 +507,22 @@ struct amount_sat amount_sat_div(struct amount_sat sat, u64 div)
return sat;
}
bool amount_sat_mul(struct amount_sat *res, struct amount_sat sat, u64 mul)
{
if ( mul_overflows_u64(sat.satoshis, mul))
return false;
res->satoshis = sat.satoshis * mul;
return true;
}
bool amount_msat_mul(struct amount_msat *res, struct amount_msat msat, u64 mul)
{
if ( mul_overflows_u64(msat.millisatoshis, mul))
return false;
res->millisatoshis = msat.millisatoshis * mul;
return true;
}
bool amount_msat_fee(struct amount_msat *fee,
struct amount_msat amt,
u32 fee_base_msat,

View File

@@ -92,6 +92,9 @@ WARN_UNUSED_RESULT bool amount_sat_scale(struct amount_sat *val,
struct amount_msat amount_msat_div(struct amount_msat msat, u64 div);
struct amount_sat amount_sat_div(struct amount_sat sat, u64 div);
bool amount_sat_mul(struct amount_sat *res, struct amount_sat sat, u64 mul);
bool amount_msat_mul(struct amount_msat *res, struct amount_msat msat, u64 mul);
/* Is a == b? */
bool amount_sat_eq(struct amount_sat a, struct amount_sat b);
bool amount_msat_eq(struct amount_msat a, struct amount_msat b);