pub fn swap_adjust<'a, L, C, K>(
	wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
	keychain_mask: Option<&SecretKey>,
	swap_id: &str,
	adjust_cmd: &str,
	method: Option<String>,
	destination: Option<String>,
	secondary_address: Option<String>, // secondary address to adjust
	secondary_fee: Option<f32>,
) -> Result<(StateId, Action), Error>
where
	L: WalletLCProvider<'a, C, K>,
	C: NodeClient + 'a,
	K: Keychain + 'a,
{
	wallet_lock!(wallet_inst, w);
	let keychain = w.keychain(keychain_mask)?;
	let skey = get_swap_storage_key(&keychain)?;
	let node_client = w.w2n_client();

	let swap_lock = trades::get_swap_lock(&swap_id.to_string());
	let _l = swap_lock.lock();
	let (context, mut swap) = trades::get_swap_trade(swap_id, &skey, &*swap_lock)?;

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

	match adjust_cmd {
		"cancel" => {
			if !fsm.is_cancellable(&swap)? {
				return Err(ErrorKind::Generic(
					"Swap Trade is not cancellable at current stage".to_string(),
				)
				.into());
			}

			// Cancelling the trade
			let tx_conf = swap_api.request_tx_confirmations(&keychain, &swap)?;
			let resp = fsm.process(Input::Cancel, &mut swap, &context, &tx_conf)?;
			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;

			return Ok((swap.state.clone(), resp.action.unwrap_or(Action::None)));
		}
		"destination" => {
			if method.is_none() || destination.is_none() {
				return Err(ErrorKind::Generic(
					"Please define both '--method' and '--dest' values".to_string(),
				)
				.into());
			}
			let method = method.unwrap();

			swap.communication_method = method;
			swap.communication_address = destination.unwrap();
			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;
			return Ok((swap.state.clone(), Action::None));
		}
		"secondary_address" => {
			if secondary_address.is_none() {
				return Err(ErrorKind::Generic(
					"Please define '--buyer_refund_address' or '--secondary_address' values"
						.to_string(),
				)
				.into());
			}

			let secondary_address = secondary_address.unwrap();
			swap.secondary_currency
				.validate_address(&secondary_address)?;

			match &mut swap.role {
				Role::Buyer(address) => {
					address.replace(secondary_address);
				}
				Role::Seller(address, _) => {
					*address = secondary_address;
				}
			}

			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;
			return Ok((swap.state.clone(), Action::None));
		}
		"secondary_fee" => {
			if secondary_fee.is_none() {
				return Err(ErrorKind::Generic(
					"Please define '--secondary_fee' values".to_string(),
				)
				.into());
			}

			let secondary_fee = secondary_fee.unwrap();
			if secondary_fee <= 0.0 {
				return Err(ErrorKind::Generic(
					"Please define positive '--secondary_fee' value".to_string(),
				)
				.into());
			}

			swap.secondary_fee = secondary_fee;
			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;
			return Ok((swap.state.clone(), Action::None));
		}
		adjusted_state => {
			let state = StateId::from_cmd_str(adjusted_state)?;
			if !fsm.has_state(&state) {
				return Err(ErrorKind::Generic(format!(
					"State {} is invalid for this trade",
					adjusted_state
				))
				.into());
			}
			swap.state = state;

			let tx_conf = swap_api.request_tx_confirmations(&keychain, &swap)?;
			let resp = fsm.process(Input::Check, &mut swap, &context, &tx_conf)?;
			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;

			return Ok((swap.state.clone(), resp.action.unwrap_or(Action::None)));
		}
	}
}
/// Update the state of Swap trade. Returns the new state
/// method & destination required for adjust_cmd='destination'
Update the state of Swap trade. Returns the new state
method & destination required for adjust_cmd='destination'
Update the state of Swap trade . Returns the new state method & destination required for adjust_cmd = ' destination '
pub fn swap_adjust<'a, L, C, K>(
	wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
	keychain_mask: Option<&SecretKey>,
	swap_id: &str,
	adjust_cmd: &str,
	method: Option<String>,
	destination: Option<String>,
	secondary_address: Option<String>, // secondary address to adjust
	secondary_fee: Option<f32>,
) -> Result<(StateId, Action), Error>
where
	L: WalletLCProvider<'a, C, K>,
	C: NodeClient + 'a,
	K: Keychain + 'a,
{
	wallet_lock!(wallet_inst, w);
	let keychain = w.keychain(keychain_mask)?;
	let skey = get_swap_storage_key(&keychain)?;
	let node_client = w.w2n_client();
	let swap_lock = trades::get_swap_lock(&swap_id.to_string());
	let _l = swap_lock.lock();
	let (context, mut swap) = trades::get_swap_trade(swap_id, &skey, &*swap_lock)?;
	let swap_api =
		crate::swap::api::create_instance(&swap.secondary_currency, node_client.clone())?;
	let mut fsm = swap_api.get_fsm(&keychain, &swap);
	match adjust_cmd {
		"cancel" => {
			if !fsm.is_cancellable(&swap)? {
				return Err(ErrorKind::Generic(
					"Swap Trade is not cancellable at current stage".to_string(),
				)
				.into());
			}
			// Cancelling the trade
			let tx_conf = swap_api.request_tx_confirmations(&keychain, &swap)?;
			let resp = fsm.process(Input::Cancel, &mut swap, &context, &tx_conf)?;
			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;
			return Ok((swap.state.clone(), resp.action.unwrap_or(Action::None)));
		}
		"destination" => {
			if method.is_none() || destination.is_none() {
				return Err(ErrorKind::Generic(
					"Please define both '--method' and '--dest' values".to_string(),
				)
				.into());
			}
			let method = method.unwrap();
			swap.communication_method = method;
			swap.communication_address = destination.unwrap();
			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;
			return Ok((swap.state.clone(), Action::None));
		}
		"secondary_address" => {
			if secondary_address.is_none() {
				return Err(ErrorKind::Generic(
					"Please define '--buyer_refund_address' or '--secondary_address' values"
						.to_string(),
				)
				.into());
			}
			let secondary_address = secondary_address.unwrap();
			swap.secondary_currency
				.validate_address(&secondary_address)?;
			match &mut swap.role {
				Role::Buyer(address) => {
					address.replace(secondary_address);
				}
				Role::Seller(address, _) => {
					*address = secondary_address;
				}
			}
			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;
			return Ok((swap.state.clone(), Action::None));
		}
		"secondary_fee" => {
			if secondary_fee.is_none() {
				return Err(ErrorKind::Generic(
					"Please define '--secondary_fee' values".to_string(),
				)
				.into());
			}
			let secondary_fee = secondary_fee.unwrap();
			if secondary_fee <= 0.0 {
				return Err(ErrorKind::Generic(
					"Please define positive '--secondary_fee' value".to_string(),
				)
				.into());
			}
			swap.secondary_fee = secondary_fee;
			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;
			return Ok((swap.state.clone(), Action::None));
		}
		adjusted_state => {
			let state = StateId::from_cmd_str(adjusted_state)?;
			if !fsm.has_state(&state) {
				return Err(ErrorKind::Generic(format!(
					"State {} is invalid for this trade",
					adjusted_state
				))
				.into());
			}
			swap.state = state;
			let tx_conf = swap_api.request_tx_confirmations(&keychain, &swap)?;
			let resp = fsm.process(Input::Check, &mut swap, &context, &tx_conf)?;
			trades::store_swap_trade(&context, &swap, &skey, &*swap_lock)?;
			return Ok((swap.state.clone(), resp.action.unwrap_or(Action::None)));
		}
	}
}
pub fn swap_adjust < ' a , L , C , K > ( wallet_inst : Arc < Mutex < Box < dyn WalletInst < ' a , L , C , K > > > > , keychain_mask : Option < & SecretKey > , swap_id : & str , adjust_cmd : & str , method : Option < String > , destination : Option < String > , secondary_address : Option < String > , // secondary address to adjust secondary_fee : Option < f32 > , ) -> Result < ( StateId , Action ) , Error > where L : WalletLCProvider < ' a , C , K > , C : NodeClient + ' a , K : Keychain + ' a , { wallet_lock ! ( wallet_inst w ) ; let keychain = w . keychain ( keychain_mask ) ? ; let skey = get_swap_storage_key ( & keychain ) ? ; let node_client = w . w2n_client ( ) ; let swap_lock = trades :: get_swap_lock ( & swap_id . to_string ( ) ) ; let _l = swap_lock . lock ( ) ; let ( context , mut swap ) = trades :: get_swap_trade ( swap_id , & skey , & * swap_lock ) ? ; let swap_api = crate :: swap :: api :: create_instance ( & swap . secondary_currency , node_client . clone ( ) ) ? ; let mut fsm = swap_api . get_fsm ( & keychain , & swap ) ; match adjust_cmd { " " => { if ! fsm . is_cancellable ( & swap ) ? { return Err ( ErrorKind :: Generic ( " " . to_string ( ) , ) . into ( ) ) ; } // Cancelling the trade let tx_conf = swap_api . request_tx_confirmations ( & keychain , & swap ) ? ; let resp = fsm . process ( Input :: Cancel , & mut swap , & context , & tx_conf ) ? ; trades :: store_swap_trade ( & context , & swap , & skey , & * swap_lock ) ? ; return Ok ( ( swap . state . clone ( ) , resp . action . unwrap_or ( Action :: None ) ) ) ; } " " => { if method . is_none ( ) || destination . is_none ( ) { return Err ( ErrorKind :: Generic ( " " . to_string ( ) , ) . into ( ) ) ; } let method = method . unwrap ( ) ; swap . communication_method = method ; swap . communication_address = destination . unwrap ( ) ; trades :: store_swap_trade ( & context , & swap , & skey , & * swap_lock ) ? ; return Ok ( ( swap . state . clone ( ) , Action :: None ) ) ; } " " => { if secondary_address . is_none ( ) { return Err ( ErrorKind :: Generic ( " " . to_string ( ) , ) . into ( ) ) ; } let secondary_address = secondary_address . unwrap ( ) ; swap . secondary_currency . validate_address ( & secondary_address ) ? ; match & mut swap . role { Role :: Buyer ( address ) => { address . replace ( secondary_address ) ; } Role :: Seller ( address , _ ) => { * address = secondary_address ; } } trades :: store_swap_trade ( & context , & swap , & skey , & * swap_lock ) ? ; return Ok ( ( swap . state . clone ( ) , Action :: None ) ) ; } " " => { if secondary_fee . is_none ( ) { return Err ( ErrorKind :: Generic ( " " . to_string ( ) , ) . into ( ) ) ; } let secondary_fee = secondary_fee . unwrap ( ) ; if secondary_fee <= 0.0 { return Err ( ErrorKind :: Generic ( " " . to_string ( ) , ) . into ( ) ) ; } swap . secondary_fee = secondary_fee ; trades :: store_swap_trade ( & context , & swap , & skey , & * swap_lock ) ? ; return Ok ( ( swap . state . clone ( ) , Action :: None ) ) ; } adjusted_state => { let state = StateId :: from_cmd_str ( adjusted_state ) ? ; if ! fsm . has_state ( & state ) { return Err ( ErrorKind :: Generic ( format ! ( " " adjusted_state ) ) . into ( ) ) ; } swap . state = state ; let tx_conf = swap_api . request_tx_confirmations ( & keychain , & swap ) ? ; let resp = fsm . process ( Input :: Check , & mut swap , & context , & tx_conf ) ? ; trades :: store_swap_trade ( & context , & swap , & skey , & * swap_lock ) ? ; return Ok ( ( swap . state . clone ( ) , resp . action . unwrap_or ( Action :: None ) ) ) ; } } }
Update the state of Swap trade.
Update the state of Swap trade .
wallet_inst
Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>
keychain_mask
Option<&SecretKey>
swap_id
&str
adjust_cmd
&str
method
Option<String>
destination
Option<String>
secondary_address
Option<String>
secondary_fee
Option<f32>
wallet_inst
Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>
keychain_mask
Option<&SecretKey>
swap_id
&str
adjust_cmd
&str
method
Option<String>
destination
Option<String>
secondary_address
Option<String>
secondary_fee
Option<f32>