pub fn swap_income_message<'a, L, C, K>(
	wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
	keychain_mask: Option<&SecretKey>,
	swap_message: &str,
	swap_lock: Option<Arc<Mutex<()>>>,
) -> Result<Option<Message>, Error>
where
	L: WalletLCProvider<'a, C, K>,
	C: NodeClient + 'a,
	K: Keychain + 'a,
{
	let message = Message::from_json(swap_message)?;
	let swap_id = message.id.to_string();

	debug!("Get swap message {:?}", message);

	wallet_lock!(wallet_inst, w);
	let node_client = w.w2n_client().clone();
	let keychain = w.keychain(keychain_mask)?;
	let skey = get_swap_storage_key(&keychain)?;

	let (lock, need_to_lock) = match swap_lock {
		Some(lock) => (lock.clone(), false),
		None => (trades::get_swap_lock(&swap_id), true),
	};

	let _guard = if need_to_lock {
		Some(lock.lock())
	} else {
		None
	};

	let ack_msg = match &message.inner {
		Update::None => {
			return Err(
				ErrorKind::Generic("Get empty message, nothing to process".to_string()).into(),
			)
		}
		Update::Offer(offer_update) => {
			// We get an offer
			if trades::get_swap_trade(swap_id.as_str(), &skey, &*lock).is_ok() {
				return Err( ErrorKind::Generic(format!("trade with SwapID {} already exist. Probably you already processed this message", swap_id)).into());
			}

			let mut swap_api = crate::swap::api::create_instance(
				&offer_update.secondary_currency,
				node_client.clone(),
			)?;

			// Creating Buyer context
			let context = create_context(
				&mut **w,
				keychain_mask,
				&mut swap_api,
				&keychain,
				offer_update.secondary_currency,
				false,
				None,
				0,
			)?;

			let (id, offer, secondary_update) = message.unwrap_offer()?;
			let swap = BuyApi::accept_swap_offer(
				&keychain,
				&context,
				id,
				offer,
				secondary_update,
				&node_client,
			)?;

			trades::store_swap_trade(&context, &swap, &skey, &*lock)?;
			println!(
				"You get an offer to swap {} to MWC. SwapID is {}",
				swap.secondary_currency, swap.id
			);
			Some(Message::new(
				id,
				Update::MessageAcknowledge(1),
				SecondaryUpdate::Empty,
			))
		}
		Update::MessageAcknowledge(msg_id) => {
			let (context, mut swap) = trades::get_swap_trade(swap_id.as_str(), &skey, &*lock)?;
			match msg_id {
				1 => {
					if swap.is_seller() {
						swap.ack_msg1();
					}
				}
				2 => {
					if !swap.is_seller() {
						swap.ack_msg1();
					}
				}
				_ => {
					return Err(ErrorKind::Generic(format!(
						"Get unknown message group {} at 'MessageAcknowledge'",
						msg_id
					))
					.into())
				}
			}
			trades::store_swap_trade(&context, &swap, &skey, &*lock)?;
			None
		}
		_ => {
			let (context, mut swap) = trades::get_swap_trade(swap_id.as_str(), &skey, &*lock)?;

			let swap_api =
				crate::swap::api::create_instance(&swap.secondary_currency, node_client)?;
			let tx_conf = swap_api.request_tx_confirmations(&keychain, &swap)?;
			let mut fsm = swap_api.get_fsm(&keychain, &swap);
			let msg_gr = match &&message.inner {
				Update::Offer(_) | Update::AcceptOffer(_) => 1,
				_ => 2,
			};

			fsm.process(Input::IncomeMessage(message), &mut swap, &context, &tx_conf)?;
			trades::store_swap_trade(&context, &swap, &skey, &*lock)?;
			println!("Processed income message for SwapId {}", swap.id);

			Some(Message::new(
				swap.id.clone(),
				Update::MessageAcknowledge(msg_gr),
				SecondaryUpdate::Empty,
			))
		}
	};
	Ok(ack_msg)
}
/// Processing swap income message. Note result of that can be a new offer of modification of the current one
/// We only notify user about that, no permission will be ask.
/// Reason: Nothing will be done with the funds until user will go forward manually
/// Return: option ack message
Processing swap income message. Note result of that can be a new offer of modification of the current one
We only notify user about that, no permission will be ask.
Reason: Nothing will be done with the funds until user will go forward manually
Return: option ack message
Processing swap income message . Note result of that can be a new offer of modification of the current one We only notify user about that no permission will be ask . Reason : Nothing will be done with the funds until user will go forward manually Return : option ack message
pub fn swap_income_message<'a, L, C, K>(
	wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
	keychain_mask: Option<&SecretKey>,
	swap_message: &str,
	swap_lock: Option<Arc<Mutex<()>>>,
) -> Result<Option<Message>, Error>
where
	L: WalletLCProvider<'a, C, K>,
	C: NodeClient + 'a,
	K: Keychain + 'a,
{
	let message = Message::from_json(swap_message)?;
	let swap_id = message.id.to_string();
	debug!("Get swap message {:?}", message);
	wallet_lock!(wallet_inst, w);
	let node_client = w.w2n_client().clone();
	let keychain = w.keychain(keychain_mask)?;
	let skey = get_swap_storage_key(&keychain)?;
	let (lock, need_to_lock) = match swap_lock {
		Some(lock) => (lock.clone(), false),
		None => (trades::get_swap_lock(&swap_id), true),
	};
	let _guard = if need_to_lock {
		Some(lock.lock())
	} else {
		None
	};
	let ack_msg = match &message.inner {
		Update::None => {
			return Err(
				ErrorKind::Generic("Get empty message, nothing to process".to_string()).into(),
			)
		}
		Update::Offer(offer_update) => {
			// We get an offer
			if trades::get_swap_trade(swap_id.as_str(), &skey, &*lock).is_ok() {
				return Err( ErrorKind::Generic(format!("trade with SwapID {} already exist. Probably you already processed this message", swap_id)).into());
			}
			let mut swap_api = crate::swap::api::create_instance(
				&offer_update.secondary_currency,
				node_client.clone(),
			)?;
			// Creating Buyer context
			let context = create_context(
				&mut **w,
				keychain_mask,
				&mut swap_api,
				&keychain,
				offer_update.secondary_currency,
				false,
				None,
				0,
			)?;
			let (id, offer, secondary_update) = message.unwrap_offer()?;
			let swap = BuyApi::accept_swap_offer(
				&keychain,
				&context,
				id,
				offer,
				secondary_update,
				&node_client,
			)?;
			trades::store_swap_trade(&context, &swap, &skey, &*lock)?;
			println!(
				"You get an offer to swap {} to MWC. SwapID is {}",
				swap.secondary_currency, swap.id
			);
			Some(Message::new(
				id,
				Update::MessageAcknowledge(1),
				SecondaryUpdate::Empty,
			))
		}
		Update::MessageAcknowledge(msg_id) => {
			let (context, mut swap) = trades::get_swap_trade(swap_id.as_str(), &skey, &*lock)?;
			match msg_id {
				1 => {
					if swap.is_seller() {
						swap.ack_msg1();
					}
				}
				2 => {
					if !swap.is_seller() {
						swap.ack_msg1();
					}
				}
				_ => {
					return Err(ErrorKind::Generic(format!(
						"Get unknown message group {} at 'MessageAcknowledge'",
						msg_id
					))
					.into())
				}
			}
			trades::store_swap_trade(&context, &swap, &skey, &*lock)?;
			None
		}
		_ => {
			let (context, mut swap) = trades::get_swap_trade(swap_id.as_str(), &skey, &*lock)?;
			let swap_api =
				crate::swap::api::create_instance(&swap.secondary_currency, node_client)?;
			let tx_conf = swap_api.request_tx_confirmations(&keychain, &swap)?;
			let mut fsm = swap_api.get_fsm(&keychain, &swap);
			let msg_gr = match &&message.inner {
				Update::Offer(_) | Update::AcceptOffer(_) => 1,
				_ => 2,
			};
			fsm.process(Input::IncomeMessage(message), &mut swap, &context, &tx_conf)?;
			trades::store_swap_trade(&context, &swap, &skey, &*lock)?;
			println!("Processed income message for SwapId {}", swap.id);
			Some(Message::new(
				swap.id.clone(),
				Update::MessageAcknowledge(msg_gr),
				SecondaryUpdate::Empty,
			))
		}
	};
	Ok(ack_msg)
}
pub fn swap_income_message < ' a , L , C , K > ( wallet_inst : Arc < Mutex < Box < dyn WalletInst < ' a , L , C , K > > > > , keychain_mask : Option < & SecretKey > , swap_message : & str , swap_lock : Option < Arc < Mutex < ( ) > > > , ) -> Result < Option < Message > , Error > where L : WalletLCProvider < ' a , C , K > , C : NodeClient + ' a , K : Keychain + ' a , { let message = Message :: from_json ( swap_message ) ? ; let swap_id = message . id . to_string ( ) ; debug ! ( " " message ) ; wallet_lock ! ( wallet_inst w ) ; let node_client = w . w2n_client ( ) . clone ( ) ; let keychain = w . keychain ( keychain_mask ) ? ; let skey = get_swap_storage_key ( & keychain ) ? ; let ( lock , need_to_lock ) = match swap_lock { Some ( lock ) => ( lock . clone ( ) , false ) , None => ( trades :: get_swap_lock ( & swap_id ) , true ) , } ; let _guard = if need_to_lock { Some ( lock . lock ( ) ) } else { None } ; let ack_msg = match & message . inner { Update :: None => { return Err ( ErrorKind :: Generic ( " " . to_string ( ) ) . into ( ) , ) } Update :: Offer ( offer_update ) => { // We get an offer if trades :: get_swap_trade ( swap_id . as_str ( ) , & skey , & * lock ) . is_ok ( ) { return Err ( ErrorKind :: Generic ( format ! ( " " swap_id ) ) . into ( ) ) ; } let mut swap_api = crate :: swap :: api :: create_instance ( & offer_update . secondary_currency , node_client . clone ( ) , ) ? ; // Creating Buyer context let context = create_context ( & mut * * w , keychain_mask , & mut swap_api , & keychain , offer_update . secondary_currency , false , None , 0 , ) ? ; let ( id , offer , secondary_update ) = message . unwrap_offer ( ) ? ; let swap = BuyApi :: accept_swap_offer ( & keychain , & context , id , offer , secondary_update , & node_client , ) ? ; trades :: store_swap_trade ( & context , & swap , & skey , & * lock ) ? ; println ! ( " " swap secondary_currency swap id ) ; Some ( Message :: new ( id , Update :: MessageAcknowledge ( 1 ) , SecondaryUpdate :: Empty , ) ) } Update :: MessageAcknowledge ( msg_id ) => { let ( context , mut swap ) = trades :: get_swap_trade ( swap_id . as_str ( ) , & skey , & * lock ) ? ; match msg_id { 1 => { if swap . is_seller ( ) { swap . ack_msg1 ( ) ; } } 2 => { if ! swap . is_seller ( ) { swap . ack_msg1 ( ) ; } } _ => { return Err ( ErrorKind :: Generic ( format ! ( " " msg_id ) ) . into ( ) ) } } trades :: store_swap_trade ( & context , & swap , & skey , & * lock ) ? ; None } _ => { let ( context , mut swap ) = trades :: get_swap_trade ( swap_id . as_str ( ) , & skey , & * lock ) ? ; let swap_api = crate :: swap :: api :: create_instance ( & swap . secondary_currency , node_client ) ? ; let tx_conf = swap_api . request_tx_confirmations ( & keychain , & swap ) ? ; let mut fsm = swap_api . get_fsm ( & keychain , & swap ) ; let msg_gr = match & & message . inner { Update :: Offer ( _ ) | Update :: AcceptOffer ( _ ) => 1 , _ => 2 , } ; fsm . process ( Input :: IncomeMessage ( message ) , & mut swap , & context , & tx_conf ) ? ; trades :: store_swap_trade ( & context , & swap , & skey , & * lock ) ? ; println ! ( " " swap id ) ; Some ( Message :: new ( swap . id . clone ( ) , Update :: MessageAcknowledge ( msg_gr ) , SecondaryUpdate :: Empty , ) ) } } ; Ok ( ack_msg ) }
Processing swap income message.
Processing swap income message .
wallet_inst
Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>
keychain_mask
Option<&SecretKey>
swap_message
&str
swap_lock
Option<Arc<Mutex<()>>>
wallet_inst
Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>
keychain_mask
Option<&SecretKey>
swap_message
&str
swap_lock
Option<Arc<Mutex<()>>>