From 01b432e3d2ac2313a90d55d06b3fa855c0b71330 Mon Sep 17 00:00:00 2001 From: Janito Vaqueiro Ferreira Filho Date: Mon, 18 Apr 2022 22:55:31 +0000 Subject: [PATCH] Add `Amount::serialize_as_string` helper method A helper method that makes it easier to serialize an `Amount` as a string. This is needed for the response type of the `getaccountbalance` RPC. --- zebra-chain/src/amount.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/zebra-chain/src/amount.rs b/zebra-chain/src/amount.rs index 7ca036acc..87e2d3b71 100644 --- a/zebra-chain/src/amount.rs +++ b/zebra-chain/src/amount.rs @@ -15,6 +15,7 @@ use std::{ use crate::serialization::{ZcashDeserialize, ZcashSerialize}; use byteorder::{ByteOrder, LittleEndian, ReadBytesExt, WriteBytesExt}; +use serde::Serializer; #[cfg(any(test, feature = "proptest-impl"))] pub mod arbitrary; @@ -82,6 +83,16 @@ impl Amount { { 0.try_into().expect("an amount of 0 is always valid") } + + /// Serialize this [`Amount`] as a string instead of an integer. + /// + /// The string contains the amount of Zatoshis in the amount. + pub fn serialize_as_string(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&self.0.to_string()) + } } impl std::ops::Add> for Amount