Browse Source

Minor change

Cesar Rodas 1 year ago
parent
commit
f2d85ab5b3
2 changed files with 19 additions and 23 deletions
  1. 3 3
      client.js
  2. 16 20
      utxo/src/amount.rs

+ 3 - 3
client.js

@@ -23,7 +23,7 @@ async function deposit(account, amount, asset) {
     body: JSON.stringify({
       memo: "deposit",
       account,
-      amount: amount.toString(),
+      cents: amount.toString(),
       asset,
       status: 'pending',
     })
@@ -90,10 +90,10 @@ async function change_status(id, s_status) {
 }
 
 async function test() {
-  let d = (await deposit(addr1, 1.5, "BTC/8"));
+  let d = (await deposit(addr1, 1512312, "BTC/8"));
   dbg(d);
   dbg(await change_status(d._id, 'settled'));
-  d = (await deposit(addr2, 1000000, "USD/4"));
+  d = (await deposit(addr2, 1001234, "USD/4"));
   dbg(await change_status(d._id, 'settled'));
 
   const t = await trade(1, "BTC/8", addr1, 26751.11, "USD/4", addr2);

+ 16 - 20
utxo/src/amount.rs

@@ -1,5 +1,5 @@
 use crate::Asset;
-use serde::{de, Deserialize, Serialize, Serializer};
+use serde::{de, ser::SerializeStruct, Deserialize, Serialize, Serializer};
 
 /// The raw storage for cents, the more the better
 pub type AmountCents = i128;
@@ -66,24 +66,27 @@ impl TryInto<Amount> for AnyAmount {
 /// The `cents` and `Asset` must be used to store amounts in the storage
 /// layer. Float or string representations should be used to display
 #[derive(
-    Clone,
-    Debug,
-    Eq,
-    PartialEq,
-    Serialize,
-    Deserialize,
-    borsh::BorshSerialize,
-    borsh::BorshDeserialize,
+    Clone, Debug, Eq, PartialEq, Deserialize, borsh::BorshSerialize, borsh::BorshDeserialize,
 )]
 pub struct Amount {
     asset: Asset,
-    #[serde(
-        deserialize_with = "deserialize_string_to_amount",
-        serialize_with = "serialize_amount_as_string"
-    )]
+    #[serde(deserialize_with = "deserialize_string_to_amount")]
     cents: AmountCents,
 }
 
+impl Serialize for Amount {
+    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+    where
+        S: Serializer,
+    {
+        let mut state = serializer.serialize_struct("amount", 3)?;
+        state.serialize_field("asset", &self.asset)?;
+        state.serialize_field("cents", &self.cents.to_string())?;
+        state.serialize_field("human", &self.to_string())?;
+        state.end()
+    }
+}
+
 fn deserialize_string_to_amount<'de, D>(deserializer: D) -> Result<AmountCents, D::Error>
 where
     D: de::Deserializer<'de>,
@@ -92,13 +95,6 @@ where
     s.parse::<AmountCents>().map_err(serde::de::Error::custom)
 }
 
-fn serialize_amount_as_string<S>(value: &AmountCents, serializer: S) -> Result<S::Ok, S::Error>
-where
-    S: Serializer,
-{
-    serializer.serialize_str(&value.to_string())
-}
-
 impl Amount {
     /// Creates a new amount from an asset and cents
     pub(crate) fn new(asset: Asset, cents: AmountCents) -> Self {