Skip to content

Commit

Permalink
rs: Implement a best-effort msat to sat conversion
Browse files Browse the repository at this point in the history
Some methods (`withdraw`) require their parameter to be in satoshis
rather than millisats. In order for us not to have to come up with yet
another triple of sat, sat_or_all, and sat_or_any, we just bolt it
onto the conversion.
  • Loading branch information
cdecker committed Sep 29, 2023
1 parent 045d529 commit d92ebde
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cln-rpc/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,13 @@ impl TryFrom<&str> for Amount {

impl From<Amount> for String {
fn from(a: Amount) -> String {
format!("{}msat", a.msat)
// Best effort msat to sat conversion, for methods that accept
// sats but not msats
if a.msat % 1000 == 0 {
format!("{}sat", a.msat / 1000)
} else {
format!("{}msat", a.msat)
}
}
}

Expand Down Expand Up @@ -559,14 +565,14 @@ mod test {
(
"{\"amount\": \"42sat\"}",
Amount { msat: 42_000 },
"42000msat",
"42sat",
),
(
"{\"amount\": \"31337btc\"}",
Amount {
msat: 3_133_700_000_000_000,
},
"3133700000000000msat",
"3133700000000sat",
),
];

Expand Down

0 comments on commit d92ebde

Please sign in to comment.