mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-21 04:28:13 -04:00
refactor: remover Arc for start_daemon
This commit is contained in:
parent
630f4c6f23
commit
92034a5be8
5 changed files with 34 additions and 54 deletions
|
@ -159,11 +159,12 @@ impl Default for SwapLock {
|
|||
|
||||
// workaround for warning over monero_rpc_process which we must own but not read
|
||||
#[allow(dead_code)]
|
||||
#[derive(Clone)]
|
||||
pub struct Context {
|
||||
pub db: Arc<dyn Database + Send + Sync>,
|
||||
bitcoin_wallet: Option<Arc<bitcoin::Wallet>>,
|
||||
monero_wallet: Option<Arc<monero::Wallet>>,
|
||||
monero_rpc_process: Option<monero::WalletRpcProcess>,
|
||||
monero_rpc_process: Option<Arc<monero::WalletRpcProcess>>,
|
||||
pub swap_lock: Arc<SwapLock>,
|
||||
pub config: Config,
|
||||
pub tasks: Arc<PendingTaskList>,
|
||||
|
@ -227,7 +228,7 @@ impl Context {
|
|||
db: open_db(data_dir.join("sqlite")).await?,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
monero_rpc_process,
|
||||
monero_rpc_process: monero_rpc_process.map(Arc::new),
|
||||
config: Config {
|
||||
tor_socks5_port,
|
||||
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
|
||||
|
|
|
@ -679,7 +679,7 @@ pub async fn withdraw_btc(
|
|||
#[tracing::instrument(fields(method = "start_daemon"), skip(context))]
|
||||
pub async fn start_daemon(
|
||||
start_daemon: StartDaemonArgs,
|
||||
context: Arc<Context>,
|
||||
context: Context,
|
||||
) -> Result<serde_json::Value> {
|
||||
let StartDaemonArgs { server_address } = start_daemon;
|
||||
// Default to 127.0.0.1:1234
|
||||
|
|
|
@ -156,8 +156,7 @@ where
|
|||
monero,
|
||||
tor,
|
||||
} => {
|
||||
let context = Arc::new(
|
||||
Context::build(
|
||||
let context = Context::build(
|
||||
Some(bitcoin),
|
||||
Some(monero),
|
||||
Some(tor),
|
||||
|
@ -167,8 +166,7 @@ where
|
|||
json,
|
||||
server_address,
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
.await?;
|
||||
|
||||
start_daemon(StartDaemonArgs { server_address }, context).await?;
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ pub enum Error {
|
|||
|
||||
pub async fn run_server(
|
||||
server_address: SocketAddr,
|
||||
context: Arc<Context>,
|
||||
context: Context,
|
||||
) -> anyhow::Result<(SocketAddr, ServerHandle)> {
|
||||
let cors = CorsLayer::permissive();
|
||||
let middleware = tower::ServiceBuilder::new().layer(cors);
|
||||
|
@ -29,12 +29,7 @@ pub async fn run_server(
|
|||
.build(server_address)
|
||||
.await?;
|
||||
|
||||
let mut modules = RpcModule::new(());
|
||||
{
|
||||
modules
|
||||
.merge(methods::register_modules(context)?)
|
||||
.expect("Could not register RPC modules")
|
||||
}
|
||||
let modules = methods::register_modules(context)?;
|
||||
|
||||
let addr = server.local_addr()?;
|
||||
let server_handle = server.start(modules)?;
|
||||
|
|
|
@ -26,13 +26,11 @@ impl<T> ConvertToJsonRpseeError<T> for Result<T, anyhow::Error> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Context>>> {
|
||||
pub fn register_modules(outer_context: Context) -> Result<RpcModule<Context>> {
|
||||
let mut module = RpcModule::new(outer_context);
|
||||
|
||||
module.register_async_method("suspend_current_swap", |params, context| async move {
|
||||
suspend_current_swap(Arc::unwrap_or_clone(context))
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
suspend_current_swap(context).await.to_jsonrpsee_result()
|
||||
})?;
|
||||
|
||||
module.register_async_method("get_swap_info", |params_raw, context| async move {
|
||||
|
@ -45,7 +43,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
|||
let swap_id = as_uuid(swap_id)
|
||||
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
||||
|
||||
get_swap_info(GetSwapInfoArgs { swap_id }, Arc::unwrap_or_clone(context))
|
||||
get_swap_info(GetSwapInfoArgs { swap_id }, context)
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
})?;
|
||||
|
@ -63,21 +61,17 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
|||
jsonrpsee_core::Error::Custom("force_refesh is not a boolean".to_string())
|
||||
})?;
|
||||
|
||||
get_balance(BalanceArgs { force_refresh }, Arc::unwrap_or_clone(context))
|
||||
get_balance(BalanceArgs { force_refresh }, context)
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
})?;
|
||||
|
||||
module.register_async_method("get_history", |params, context| async move {
|
||||
get_history(Arc::unwrap_or_clone(context))
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
get_history(context).await.to_jsonrpsee_result()
|
||||
})?;
|
||||
|
||||
module.register_async_method("get_raw_states", |params, context| async move {
|
||||
get_raw_states(Arc::unwrap_or_clone(context))
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
get_raw_states(context).await.to_jsonrpsee_result()
|
||||
})?;
|
||||
|
||||
module.register_async_method("resume_swap", |params_raw, context| async move {
|
||||
|
@ -90,7 +84,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
|||
let swap_id = as_uuid(swap_id)
|
||||
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
||||
|
||||
resume_swap(ResumeArgs { swap_id }, Arc::unwrap_or_clone(context))
|
||||
resume_swap(ResumeArgs { swap_id }, context)
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
})?;
|
||||
|
@ -105,10 +99,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
|||
let swap_id = as_uuid(swap_id)
|
||||
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
||||
|
||||
cancel_and_refund(
|
||||
CancelAndRefundArgs { swap_id },
|
||||
Arc::unwrap_or_clone(context),
|
||||
)
|
||||
cancel_and_refund(CancelAndRefundArgs { swap_id }, context)
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
})?;
|
||||
|
@ -126,10 +117,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
|||
jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string())
|
||||
})?;
|
||||
|
||||
monero_recovery(
|
||||
MoneroRecoveryArgs { swap_id },
|
||||
Arc::unwrap_or_clone(context),
|
||||
)
|
||||
monero_recovery(MoneroRecoveryArgs { swap_id }, context)
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
},
|
||||
|
@ -162,7 +150,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
|||
amount,
|
||||
address: withdraw_address,
|
||||
},
|
||||
Arc::unwrap_or_clone(context),
|
||||
context,
|
||||
)
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
|
@ -206,7 +194,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
|||
monero_receive_address,
|
||||
swap_id: Uuid::new_v4(),
|
||||
},
|
||||
Arc::unwrap_or_clone(context),
|
||||
context,
|
||||
)
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
|
@ -230,16 +218,14 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
|||
ListSellersArgs {
|
||||
rendezvous_point: rendezvous_point.clone(),
|
||||
},
|
||||
Arc::unwrap_or_clone(context),
|
||||
context,
|
||||
)
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
})?;
|
||||
|
||||
module.register_async_method("get_current_swap", |params, context| async move {
|
||||
get_current_swap(Arc::unwrap_or_clone(context))
|
||||
.await
|
||||
.to_jsonrpsee_result()
|
||||
get_current_swap(context).await.to_jsonrpsee_result()
|
||||
})?;
|
||||
|
||||
Ok(module)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue