Cleanup, formatting, add get_seller, get_swap_start_date RPC endpoints

This commit is contained in:
binarybaron 2022-12-02 21:46:27 +01:00
parent 0df5af16b8
commit e1983d5639
10 changed files with 639 additions and 355 deletions

View file

@ -1,66 +1,164 @@
use jsonrpsee::http_server::{RpcModule};
use crate::api::{Request, Context, Params};
use crate::cli::command::{Command};
use std::str::FromStr;
use crate::api::{Context, Params, Request};
use crate::cli::command::Command;
use crate::rpc::Error;
use crate::{bitcoin, monero};
use jsonrpsee::http_server::RpcModule;
use libp2p::core::Multiaddr;
use serde_json::json;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use uuid::Uuid;
use std::collections::HashMap;
use libp2p::core::Multiaddr;
pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
let mut module = RpcModule::new(context);
module
.register_async_method("get_bitcoin_balance", |_, context| async move {
get_bitcoin_balance(&context).await.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
},
)
get_bitcoin_balance(&context)
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();
module
.register_async_method("get_history", |_, context| async move {
get_history(&context).await.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
},
)
get_history(&context)
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();
module
.register_async_method("raw_get_history", |_, context| async move {
context
.db
.raw_all()
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();
module
.register_async_method("get_seller", |params, context| async move {
let params: HashMap<String, String> = params.parse()?;
let swap_id = Uuid::from_str(params.get("swap_id").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
})?)
.unwrap();
let peerId = context
.db
.get_peer_id(swap_id)
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
.unwrap();
let addresses = context
.db
.get_addresses(peerId)
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
.unwrap();
Ok(json!({
"peerId": peerId.to_base58(),
"addresses": addresses
}))
})
.unwrap();
module
.register_async_method("get_swap_start_date", |params, context| async move {
let params: HashMap<String, String> = params.parse()?;
let swap_id = Uuid::from_str(params.get("swap_id").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
})?)
.unwrap();
let start_date = context
.db
.get_swap_start_date(swap_id)
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
.unwrap();
Ok(json!({
"start_date": start_date,
}))
})
.unwrap();
module
.register_async_method("resume_swap", |params, context| async move {
let swap_id: HashMap<String, String> = params.parse()?;
let swap_id = Uuid::from_str(swap_id.get("swap_id").ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?).unwrap();
resume_swap(swap_id, &context).await.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
},
)
let params: HashMap<String, String> = params.parse()?;
let swap_id = Uuid::from_str(params.get("swap_id").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
})?)
.unwrap();
resume_swap(swap_id, &context)
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();
module
.register_async_method("withdraw_btc", |params, context| async move {
let map_params: HashMap<String, String> = params.parse()?;
let amount = if let Some(amount_str) = map_params.get("amount") {
Some(::bitcoin::Amount::from_str_in(amount_str, ::bitcoin::Denomination::Bitcoin).map_err(|_| jsonrpsee_core::Error::Custom("Unable to parse amount".to_string()))?)
let params: HashMap<String, String> = params.parse()?;
let amount = if let Some(amount_str) = params.get("amount") {
Some(
::bitcoin::Amount::from_str_in(amount_str, ::bitcoin::Denomination::Bitcoin)
.map_err(|_| {
jsonrpsee_core::Error::Custom("Unable to parse amount".to_string())
})?,
)
} else {
None
};
let withdraw_address = bitcoin::Address::from_str(map_params.get("address").ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain address".to_string()))?).unwrap();
withdraw_btc(withdraw_address, amount, &context).await.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
},
)
let withdraw_address =
bitcoin::Address::from_str(params.get("address").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain address".to_string())
})?)
.unwrap();
withdraw_btc(withdraw_address, amount, &context)
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();
module
.register_async_method("buy_xmr", |params, context| async move {
let map_params: HashMap<String, String> = params.parse()?;
let bitcoin_change_address = bitcoin::Address::from_str(map_params.get("bitcoin_change_address").ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain bitcoin_change_address".to_string()))?).unwrap();
let monero_receive_address = monero::Address::from_str(map_params.get("monero_receive_address").ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain monero_receiveaddress".to_string()))?).unwrap();
let seller = Multiaddr::from_str(map_params.get("seller").ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain seller".to_string()))?).unwrap();
buy_xmr(bitcoin_change_address, monero_receive_address, seller, &context).await.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
},
)
let params: HashMap<String, String> = params.parse()?;
let bitcoin_change_address = bitcoin::Address::from_str(
params.get("bitcoin_change_address").ok_or_else(|| {
jsonrpsee_core::Error::Custom(
"Does not contain bitcoin_change_address".to_string(),
)
})?,
)
.unwrap();
let monero_receive_address = monero::Address::from_str(
params.get("monero_receive_address").ok_or_else(|| {
jsonrpsee_core::Error::Custom(
"Does not contain monero_receiveaddress".to_string(),
)
})?,
)
.unwrap();
let seller = Multiaddr::from_str(params.get("seller").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain seller".to_string())
})?)
.unwrap();
buy_xmr(
bitcoin_change_address,
monero_receive_address,
seller,
&context,
)
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();
module
.register_async_method("list_sellers", |params, context| async move {
let map_params: HashMap<String, String> = params.parse()?;
let rendezvous_point = Multiaddr::from_str(map_params.get("rendezvous_point").ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain rendezvous_point".to_string()))?).unwrap();
list_sellers(rendezvous_point, &context).await.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
},
)
let params: HashMap<String, String> = params.parse()?;
let rendezvous_point =
Multiaddr::from_str(params.get("rendezvous_point").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain rendezvous_point".to_string())
})?)
.unwrap();
list_sellers(rendezvous_point, &context)
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();
module
}
@ -83,7 +181,10 @@ async fn get_history(context: &Arc<Context>) -> anyhow::Result<serde_json::Value
Ok(history)
}
async fn resume_swap(swap_id: Uuid, context: &Arc<Context>) -> anyhow::Result<serde_json::Value, Error> {
async fn resume_swap(
swap_id: Uuid,
context: &Arc<Context>,
) -> anyhow::Result<serde_json::Value, Error> {
let request = Request {
params: Params {
swap_id: Some(swap_id),
@ -95,10 +196,14 @@ async fn resume_swap(swap_id: Uuid, context: &Arc<Context>) -> anyhow::Result<se
let result = request.call(Arc::clone(context)).await.unwrap();
Ok(result)
}
async fn withdraw_btc(withdraw_address: bitcoin::Address, amount: Option<bitcoin::Amount>, context: &Arc<Context>) -> anyhow::Result<serde_json::Value, Error> {
async fn withdraw_btc(
withdraw_address: bitcoin::Address,
amount: Option<bitcoin::Amount>,
context: &Arc<Context>,
) -> anyhow::Result<serde_json::Value, Error> {
let request = Request {
params: Params {
amount: amount,
amount,
address: Some(withdraw_address),
..Default::default()
},
@ -108,7 +213,12 @@ async fn withdraw_btc(withdraw_address: bitcoin::Address, amount: Option<bitcoin
Ok(result)
}
async fn buy_xmr(bitcoin_change_address: bitcoin::Address, monero_receive_address: monero::Address, seller: Multiaddr, context: &Arc<Context>) -> anyhow::Result<serde_json::Value, Error> {
async fn buy_xmr(
bitcoin_change_address: bitcoin::Address,
monero_receive_address: monero::Address,
seller: Multiaddr,
context: &Arc<Context>,
) -> anyhow::Result<serde_json::Value, Error> {
let request = Request {
params: Params {
bitcoin_change_address: Some(bitcoin_change_address),
@ -122,7 +232,10 @@ async fn buy_xmr(bitcoin_change_address: bitcoin::Address, monero_receive_addres
Ok(swap)
}
async fn list_sellers(rendezvous_point: Multiaddr, context: &Arc<Context>) -> anyhow::Result<serde_json::Value, Error> {
async fn list_sellers(
rendezvous_point: Multiaddr,
context: &Arc<Context>,
) -> anyhow::Result<serde_json::Value, Error> {
let request = Request {
params: Params {
rendezvous_point: Some(rendezvous_point),