mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-12-01 04:56:41 -05:00
Add tracing span attribute log_reference_id to logs caused by rpc call
This commit is contained in:
parent
fb4265b2ea
commit
1ec16c6bc1
3 changed files with 121 additions and 139 deletions
|
|
@ -9,73 +9,93 @@ use libp2p::core::Multiaddr;
|
|||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use jsonrpsee::types::Params;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
|
||||
let mut module = RpcModule::new(context);
|
||||
|
||||
module
|
||||
.register_async_method("suspend_current_swap", |_, context| async move {
|
||||
execute_request(Method::SuspendCurrentSwap, &context).await
|
||||
.register_async_method("suspend_current_swap", |params, context| async move {
|
||||
execute_request(params, Method::SuspendCurrentSwap, &context).await
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_async_method("get_swap_info", |params, context| async move {
|
||||
let params: HashMap<String, Uuid> = params.parse()?;
|
||||
.register_async_method("get_swap_info", |params_raw, context| async move {
|
||||
let params: HashMap<String, Uuid> = params_raw.parse()?;
|
||||
|
||||
let swap_id = params.get("swap_id").ok_or_else(|| {
|
||||
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
||||
})?;
|
||||
|
||||
get_swap_info(*swap_id, &context).await
|
||||
execute_request(
|
||||
params_raw,
|
||||
Method::GetSwapInfo {
|
||||
swap_id: *swap_id,
|
||||
},
|
||||
&context,
|
||||
).await
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_async_method("get_bitcoin_balance", |_, context| async move {
|
||||
get_bitcoin_balance(&context).await
|
||||
.register_async_method("get_bitcoin_balance", |params, context| async move {
|
||||
execute_request(params, Method::Balance, &context).await
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_async_method("get_history", |_, context| async move {
|
||||
get_history(&context).await
|
||||
.register_async_method("get_history", |params, context| async move {
|
||||
execute_request(params, Method::History, &context).await
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_async_method("get_raw_history", |_, context| async move {
|
||||
get_raw_history(&context).await
|
||||
.register_async_method("get_raw_history", |params, context| async move {
|
||||
execute_request(params, Method::RawHistory, &context).await
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_async_method("resume_swap", |params, context| async move {
|
||||
let params: HashMap<String, Uuid> = params.parse()?;
|
||||
.register_async_method("resume_swap", |params_raw, context| async move {
|
||||
let params: HashMap<String, Uuid> = params_raw.parse()?;
|
||||
|
||||
let swap_id = params.get("swap_id").ok_or_else(|| {
|
||||
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
||||
})?;
|
||||
|
||||
resume_swap(*swap_id, &context).await
|
||||
execute_request(
|
||||
params_raw,
|
||||
Method::Resume {
|
||||
swap_id: *swap_id,
|
||||
},
|
||||
&context,
|
||||
).await
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_async_method("cancel_refund_swap", |params, context| async move {
|
||||
let params: HashMap<String, Uuid> = params.parse()?;
|
||||
.register_async_method("cancel_refund_swap", |params_raw, context| async move {
|
||||
let params: HashMap<String, Uuid> = params_raw.parse()?;
|
||||
|
||||
let swap_id = params.get("swap_id").ok_or_else(|| {
|
||||
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
||||
})?;
|
||||
|
||||
cancel_and_refund_swap(*swap_id, &context).await
|
||||
execute_request(
|
||||
params_raw,
|
||||
Method::CancelAndRefund {
|
||||
swap_id: *swap_id,
|
||||
},
|
||||
&context,
|
||||
).await
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_async_method("withdraw_btc", |params, context| async move {
|
||||
let params: HashMap<String, String> = params.parse()?;
|
||||
.register_async_method("withdraw_btc", |params_raw, context| async move {
|
||||
let params: HashMap<String, String> = params_raw.parse()?;
|
||||
|
||||
let amount = if let Some(amount_str) = params.get("amount") {
|
||||
Some(
|
||||
|
|
@ -96,12 +116,19 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
|
|||
let withdraw_address =
|
||||
bitcoin_address::validate(withdraw_address, context.config.is_testnet)?;
|
||||
|
||||
withdraw_btc(withdraw_address, amount, &context).await
|
||||
execute_request(
|
||||
params_raw,
|
||||
Method::WithdrawBtc {
|
||||
amount,
|
||||
address: withdraw_address,
|
||||
},
|
||||
&context,
|
||||
).await
|
||||
})
|
||||
.expect("Could not register RPC method withdraw_btc");
|
||||
module
|
||||
.register_async_method("buy_xmr", |params, context| async move {
|
||||
let params: HashMap<String, String> = params.parse()?;
|
||||
.register_async_method("buy_xmr", |params_raw, context| async move {
|
||||
let params: HashMap<String, String> = params_raw.parse()?;
|
||||
|
||||
let bitcoin_change_address = bitcoin::Address::from_str(
|
||||
params.get("bitcoin_change_address").ok_or_else(|| {
|
||||
|
|
@ -132,29 +159,38 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
|
|||
})?)
|
||||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
|
||||
|
||||
buy_xmr(
|
||||
bitcoin_change_address,
|
||||
monero_receive_address,
|
||||
seller,
|
||||
execute_request(
|
||||
params_raw,
|
||||
Method::BuyXmr {
|
||||
bitcoin_change_address,
|
||||
monero_receive_address,
|
||||
seller,
|
||||
swap_id: Uuid::new_v4(),
|
||||
},
|
||||
&context,
|
||||
)
|
||||
.await
|
||||
).await
|
||||
})
|
||||
.unwrap();
|
||||
module
|
||||
.register_async_method("list_sellers", |params, context| async move {
|
||||
let params: HashMap<String, Multiaddr> = params.parse()?;
|
||||
.register_async_method("list_sellers", |params_raw, context| async move {
|
||||
let params: HashMap<String, Multiaddr> = params_raw.parse()?;
|
||||
let rendezvous_point = params.get("rendezvous_point").ok_or_else(|| {
|
||||
jsonrpsee_core::Error::Custom("Does not contain rendezvous_point".to_string())
|
||||
})?;
|
||||
|
||||
list_sellers(rendezvous_point.clone(), &context).await
|
||||
execute_request(
|
||||
params_raw,
|
||||
Method::ListSellers {
|
||||
rendezvous_point: rendezvous_point.clone(),
|
||||
},
|
||||
&context,
|
||||
).await
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_async_method("get_current_swap", |_, context| async move {
|
||||
get_current_swap(&context).await
|
||||
.register_async_method("get_current_swap", |params, context| async move {
|
||||
execute_request(params, Method::GetCurrentSwap, &context).await
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
|
|
@ -162,88 +198,19 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
|
|||
}
|
||||
|
||||
async fn execute_request(
|
||||
params: Params<'static>,
|
||||
cmd: Method,
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
let request = Request::new(cmd);
|
||||
let params_parsed = params.parse::<HashMap<String, String>>()
|
||||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
|
||||
|
||||
let reference_id = params_parsed
|
||||
.get("log_reference_id");
|
||||
|
||||
let request = Request::with_id(cmd, reference_id.map(|s| s.clone()));
|
||||
request
|
||||
.call(Arc::clone(context))
|
||||
.await
|
||||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
|
||||
}
|
||||
|
||||
async fn get_current_swap(
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(Method::GetCurrentSwap, context).await
|
||||
}
|
||||
|
||||
async fn get_bitcoin_balance(
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(Method::Balance, context).await
|
||||
}
|
||||
|
||||
async fn get_history(context: &Arc<Context>) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(Method::History, context).await
|
||||
}
|
||||
|
||||
async fn get_raw_history(
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(Method::RawHistory, context).await
|
||||
}
|
||||
|
||||
async fn get_swap_info(
|
||||
swap_id: Uuid,
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(Method::GetSwapInfo { swap_id }, context).await
|
||||
}
|
||||
|
||||
async fn resume_swap(
|
||||
swap_id: Uuid,
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(Method::Resume { swap_id }, context).await
|
||||
}
|
||||
|
||||
async fn cancel_and_refund_swap(
|
||||
swap_id: Uuid,
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(Method::CancelAndRefund { swap_id }, context).await
|
||||
}
|
||||
|
||||
async fn withdraw_btc(
|
||||
address: bitcoin::Address,
|
||||
amount: Option<bitcoin::Amount>,
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(Method::WithdrawBtc { amount, address }, context).await
|
||||
}
|
||||
|
||||
async fn buy_xmr(
|
||||
bitcoin_change_address: bitcoin::Address,
|
||||
monero_receive_address: monero::Address,
|
||||
seller: Multiaddr,
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(
|
||||
Method::BuyXmr {
|
||||
seller,
|
||||
swap_id: Uuid::new_v4(),
|
||||
bitcoin_change_address,
|
||||
monero_receive_address,
|
||||
},
|
||||
context,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn list_sellers(
|
||||
rendezvous_point: Multiaddr,
|
||||
context: &Arc<Context>,
|
||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||
execute_request(Method::ListSellers { rendezvous_point }, context).await
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue