From 177cfd9edcfd1ae945fe067f0f1a4cb1b3f955a5 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 21 Feb 2019 11:16:57 +1030 Subject: [PATCH] common: json_to_msat and json_to_sat helpers. Signed-off-by: Rusty Russell --- common/json_helpers.c | 14 ++++++++++++++ common/json_helpers.h | 10 ++++++++++ common/test/run-json.c | 6 ++++++ 3 files changed, 30 insertions(+) diff --git a/common/json_helpers.c b/common/json_helpers.c index df4f5cd09..d9c20527c 100644 --- a/common/json_helpers.c +++ b/common/json_helpers.c @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -38,6 +39,19 @@ bool json_to_pubkey(const char *buffer, const jsmntok_t *tok, tok->end - tok->start, pubkey); } +bool json_to_msat(const char *buffer, const jsmntok_t *tok, + struct amount_msat *msat) +{ + return parse_amount_msat(msat, + buffer + tok->start, tok->end - tok->start); +} + +bool json_to_sat(const char *buffer, const jsmntok_t *tok, + struct amount_sat *sat) +{ + return parse_amount_sat(sat, buffer + tok->start, tok->end - tok->start); +} + bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok, struct short_channel_id *scid, bool may_be_deprecated_form) diff --git a/common/json_helpers.h b/common/json_helpers.h index 5659fe856..5683bcdde 100644 --- a/common/json_helpers.h +++ b/common/json_helpers.h @@ -4,6 +4,8 @@ #include "config.h" #include +struct amount_msat; +struct amount_sat; struct pubkey; struct short_channel_id; @@ -20,4 +22,12 @@ bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok, struct short_channel_id *scid, bool may_be_deprecated_form); +/* Extract a satoshis amount from this */ +bool json_to_sat(const char *buffer, const jsmntok_t *tok, + struct amount_sat *sat); + +/* Extract a millisatoshis amount from this */ +bool json_to_msat(const char *buffer, const jsmntok_t *tok, + struct amount_msat *msat); + #endif /* LIGHTNING_COMMON_JSON_HELPERS_H */ diff --git a/common/test/run-json.c b/common/test/run-json.c index e44e64c9a..42a1e79d4 100644 --- a/common/test/run-json.c +++ b/common/test/run-json.c @@ -5,6 +5,12 @@ #include /* AUTOGENERATED MOCKS START */ +/* Generated stub for parse_amount_msat */ +bool parse_amount_msat(struct amount_msat *msat UNNEEDED, const char *s UNNEEDED, size_t slen UNNEEDED) +{ fprintf(stderr, "parse_amount_msat called!\n"); abort(); } +/* Generated stub for parse_amount_sat */ +bool parse_amount_sat(struct amount_sat *sat UNNEEDED, const char *s UNNEEDED, size_t slen UNNEEDED) +{ fprintf(stderr, "parse_amount_sat called!\n"); abort(); } /* AUTOGENERATED MOCKS END */