Move monero/bitcoin modules to files

This commit is contained in:
Tobin C. Harding 2020-10-21 13:01:10 +11:00
parent aaf1363c05
commit 08ec776daa
5 changed files with 36 additions and 69 deletions

View File

@ -9,7 +9,7 @@ description = "XMR/BTC trustless atomic swaps."
anyhow = "1"
async-trait = "0.1"
atty = "0.2"
bitcoin = "0.25" # TODO: Upgrade other crates in this repo to use this version.
bitcoin = { version = "0.25", features = ["rand", "use-serde"] } # TODO: Upgrade other crates in this repo to use this version.
derivative = "2"
futures = { version = "0.3", default-features = false }
libp2p = { version = "0.28", default-features = false, features = ["tcp-tokio", "yamux", "mplex", "dns", "noise", "request-response"] }

1
swap/src/bitcoin.rs Normal file
View File

@ -0,0 +1 @@
pub use bitcoin::Amount;

View File

@ -2,7 +2,9 @@ use serde::{Deserialize, Serialize};
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;
@ -26,6 +28,7 @@ pub enum Rsp {
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct SwapParams {
/// Amount of BTC to swap.
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount,
/// Amount of XMR to swap.
pub xmr: monero::Amount,
@ -36,71 +39,3 @@ impl Display for SwapParams {
write!(f, "{} for {}", self.btc, self.xmr)
}
}
// FIXME: Amount modules are a quick hack so we can derive serde.
pub mod monero {
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Amount(u64);
impl Amount {
/// Create an [Amount] with piconero precision and the given number of
/// piconeros.
///
/// A piconero (a.k.a atomic unit) is equal to 1e-12 XMR.
pub fn from_piconero(amount: u64) -> Self {
Amount(amount)
}
pub fn as_piconero(&self) -> u64 {
self.0
}
}
impl From<Amount> for u64 {
fn from(from: Amount) -> u64 {
from.0
}
}
impl fmt::Display for Amount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} piconeros", self.0)
}
}
}
pub mod bitcoin {
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Amount(u64);
impl Amount {
/// The zero amount.
pub const ZERO: Amount = Amount(0);
/// Exactly one satoshi.
pub const ONE_SAT: Amount = Amount(1);
/// Exactly one bitcoin.
pub const ONE_BTC: Amount = Amount(100_000_000);
/// Create an [Amount] with satoshi precision and the given number of
/// satoshis.
pub fn from_sat(satoshi: u64) -> Amount {
Amount(satoshi)
}
pub fn as_sat(&self) -> u64 {
self.0
}
}
impl fmt::Display for Amount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} satoshis", self.0)
}
}
}

30
swap/src/monero.rs Normal file
View File

@ -0,0 +1,30 @@
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Amount(u64);
impl Amount {
/// Create an [Amount] with piconero precision and the given number of
/// piconeros.
///
/// A piconero (a.k.a atomic unit) is equal to 1e-12 XMR.
pub fn from_piconero(amount: u64) -> Self {
Amount(amount)
}
pub fn as_piconero(&self) -> u64 {
self.0
}
}
impl From<Amount> for u64 {
fn from(from: Amount) -> u64 {
from.0
}
}
impl fmt::Display for Amount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} piconeros", self.0)
}
}

View File

@ -15,6 +15,7 @@ pub const TIMEOUT: u64 = 3600; // One hour.
/// Messages Bob sends to Alice.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum BobToAlice {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
AmountsFromBtc(bitcoin::Amount),
AmountsFromXmr(monero::Amount),
/* TODO: How are we going to do this when the messages are not Clone?