From d92ebde5b170873faa9980184218de58f39af707 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 28 Sep 2023 14:01:13 +0200 Subject: [PATCH] rs: Implement a best-effort msat to sat conversion 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. --- cln-rpc/src/primitives.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cln-rpc/src/primitives.rs b/cln-rpc/src/primitives.rs index 4223f95bd7ef..35bbca5519ec 100644 --- a/cln-rpc/src/primitives.rs +++ b/cln-rpc/src/primitives.rs @@ -468,7 +468,13 @@ impl TryFrom<&str> for Amount { impl From 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) + } } } @@ -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", ), ];