common: add msat to sat convert helper.

We don't always want to round down, sometimes we want to fail.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2022-06-19 16:47:11 +09:30
parent 36a2491a89
commit a52bdeee01
2 changed files with 14 additions and 0 deletions

View File

@@ -18,6 +18,16 @@ bool amount_sat_to_msat(struct amount_msat *msat,
return true;
}
bool amount_msat_to_sat(struct amount_sat *sat,
struct amount_msat msat)
{
if (msat.millisatoshis % MSAT_PER_SAT)
return false;
sat->satoshis = msat.millisatoshis / MSAT_PER_SAT;
return true;
}
/* You can always truncate millisatoshis->satoshis. */
struct amount_sat amount_msat_to_sat_round_down(struct amount_msat msat)
{

View File

@@ -53,6 +53,10 @@ struct amount_sat amount_sat(u64 satoshis);
WARN_UNUSED_RESULT bool amount_sat_to_msat(struct amount_msat *msat,
struct amount_sat sat);
/* You may not always be able to convert millisatoshis->satoshis without rounding. */
WARN_UNUSED_RESULT bool amount_msat_to_sat(struct amount_sat *sat,
struct amount_msat msat);
/* You can always truncate millisatoshis->satoshis. */
struct amount_sat amount_msat_to_sat_round_down(struct amount_msat msat);