mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-01-11 15:39:37 -05:00
Move monero serde code to lib
This commit is contained in:
parent
30298bdf1f
commit
eed5e8e9a4
@ -4,7 +4,6 @@ use std::fmt::{self, Display};
|
||||
pub mod alice;
|
||||
pub mod bitcoin;
|
||||
pub mod bob;
|
||||
pub mod monero;
|
||||
pub mod network;
|
||||
|
||||
pub const ONE_BTC: u64 = 100_000_000;
|
||||
@ -34,7 +33,7 @@ pub struct SwapAmounts {
|
||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||
pub btc: ::bitcoin::Amount,
|
||||
/// Amount of XMR to swap.
|
||||
#[serde(with = "crate::monero::amount_serde")]
|
||||
#[serde(with = "xmr_btc::serde::monero_amount")]
|
||||
pub xmr: xmr_btc::monero::Amount,
|
||||
}
|
||||
|
||||
|
@ -1,27 +0,0 @@
|
||||
//! Monero stuff, for now just serde.
|
||||
|
||||
// This has to be in a sub-module to use with serde derive.
|
||||
pub mod amount_serde {
|
||||
use serde::{de::Error, Deserialize, Deserializer, Serializer};
|
||||
use std::str::FromStr;
|
||||
use xmr_btc::monero::Amount;
|
||||
|
||||
pub fn serialize<S>(value: &Amount, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(&value.as_piconero().to_string())
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Amount, <D as Deserializer<'de>>::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value = String::deserialize(deserializer)?;
|
||||
let value =
|
||||
u64::from_str(value.as_str()).map_err(<D as Deserializer<'de>>::Error::custom)?;
|
||||
let amount = Amount::from_piconero(value);
|
||||
|
||||
Ok(amount)
|
||||
}
|
||||
}
|
@ -132,23 +132,44 @@ pub mod monero_private_key {
|
||||
}
|
||||
|
||||
pub mod bitcoin_amount {
|
||||
use bitcoin::Amount;
|
||||
use serde::{Deserialize, Deserializer, Serializer};
|
||||
|
||||
pub fn serialize<S>(value: &bitcoin::Amount, serializer: S) -> Result<S::Ok, S::Error>
|
||||
pub fn serialize<S>(x: &Amount, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_u64(value.as_sat())
|
||||
s.serialize_u64(x.as_sat())
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<bitcoin::Amount, <D as Deserializer<'de>>::Error>
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Amount, <D as Deserializer<'de>>::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value = u64::deserialize(deserializer)?;
|
||||
let amount = bitcoin::Amount::from_sat(value);
|
||||
let sats = u64::deserialize(deserializer)?;
|
||||
let amount = Amount::from_sat(sats);
|
||||
|
||||
Ok(amount)
|
||||
}
|
||||
}
|
||||
|
||||
pub mod monero_amount {
|
||||
use crate::monero::Amount;
|
||||
use serde::{Deserialize, Deserializer, Serializer};
|
||||
|
||||
pub fn serialize<S>(x: &Amount, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
s.serialize_u64(x.as_piconero())
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Amount, <D as Deserializer<'de>>::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let picos = u64::deserialize(deserializer)?;
|
||||
let amount = Amount::from_piconero(picos);
|
||||
|
||||
Ok(amount)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user