Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli: Add endpoint to print information about a passed-in offer #496

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions shell/_swap-cli
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ _arguments "${_arguments_options[@]}" \
'*--verbose[Set verbosity level]' \
&& ret=0
;;
(offer-info)
_arguments "${_arguments_options[@]}" \
'-d+[Data directory path]:DATA_DIR:_files -/' \
'--data-dir=[Data directory path]:DATA_DIR:_files -/' \
'-T+[Use Tor]:TOR_PROXY:_hosts' \
'--tor-proxy=[Use Tor]:TOR_PROXY:_hosts' \
'-m+[ZMQ socket name/address to forward all incoming protocol messages]:MSG_SOCKET:_files' \
'--msg-socket=[ZMQ socket name/address to forward all incoming protocol messages]:MSG_SOCKET:_files' \
'-x+[ZMQ socket name/address for daemon control interface]:CTL_SOCKET:_files' \
'--ctl-socket=[ZMQ socket name/address for daemon control interface]:CTL_SOCKET:_files' \
'-h[Print help information]' \
'--help[Print help information]' \
'*-v[Set verbosity level]' \
'*--verbose[Set verbosity level]' \
':public-offer -- The offer to be canceled:' \
&& ret=0
;;
(list-listens)
_arguments "${_arguments_options[@]}" \
'-d+[Data directory path]:DATA_DIR:_files -/' \
Expand Down Expand Up @@ -257,6 +274,7 @@ _swap-cli_commands() {
'peers:Lists existing peer connections' \
'list-swaps:Lists running swaps' \
'list-offers:Lists public offers created by daemon' \
'offer-info:Gives information on an open offer' \
'list-listens:Lists listeners created by daemon' \
'make:Maker creates offer and start listening for incoming connections. Command used to to print the resulting public offer that shall be shared with Taker. Additionally it spins up the listener awaiting for connection related to this offer' \
'take:Taker accepts offer and connects to maker'\''s daemon to start the trade' \
Expand Down Expand Up @@ -302,6 +320,11 @@ _swap-cli__needs-funding_commands() {
local commands; commands=()
_describe -t commands 'swap-cli needs-funding commands' commands "$@"
}
(( $+functions[_swap-cli__offer-info_commands] )) ||
_swap-cli__offer-info_commands() {
local commands; commands=()
_describe -t commands 'swap-cli offer-info commands' commands "$@"
}
(( $+functions[_swap-cli__peers_commands] )) ||
_swap-cli__peers_commands() {
local commands; commands=()
Expand Down
16 changes: 16 additions & 0 deletions shell/_swap-cli.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Register-ArgumentCompleter -Native -CommandName 'swap-cli' -ScriptBlock {
[CompletionResult]::new('peers', 'peers', [CompletionResultType]::ParameterValue, 'Lists existing peer connections')
[CompletionResult]::new('list-swaps', 'list-swaps', [CompletionResultType]::ParameterValue, 'Lists running swaps')
[CompletionResult]::new('list-offers', 'list-offers', [CompletionResultType]::ParameterValue, 'Lists public offers created by daemon')
[CompletionResult]::new('offer-info', 'offer-info', [CompletionResultType]::ParameterValue, 'Gives information on an open offer')
[CompletionResult]::new('list-listens', 'list-listens', [CompletionResultType]::ParameterValue, 'Lists listeners created by daemon')
[CompletionResult]::new('make', 'make', [CompletionResultType]::ParameterValue, 'Maker creates offer and start listening for incoming connections. Command used to to print the resulting public offer that shall be shared with Taker. Additionally it spins up the listener awaiting for connection related to this offer')
[CompletionResult]::new('take', 'take', [CompletionResultType]::ParameterValue, 'Taker accepts offer and connects to maker''s daemon to start the trade')
Expand Down Expand Up @@ -108,6 +109,21 @@ Register-ArgumentCompleter -Native -CommandName 'swap-cli' -ScriptBlock {
[CompletionResult]::new('--verbose', 'verbose', [CompletionResultType]::ParameterName, 'Set verbosity level')
break
}
'swap-cli;offer-info' {
[CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'Data directory path')
[CompletionResult]::new('--data-dir', 'data-dir', [CompletionResultType]::ParameterName, 'Data directory path')
[CompletionResult]::new('-T', 'T', [CompletionResultType]::ParameterName, 'Use Tor')
[CompletionResult]::new('--tor-proxy', 'tor-proxy', [CompletionResultType]::ParameterName, 'Use Tor')
[CompletionResult]::new('-m', 'm', [CompletionResultType]::ParameterName, 'ZMQ socket name/address to forward all incoming protocol messages')
[CompletionResult]::new('--msg-socket', 'msg-socket', [CompletionResultType]::ParameterName, 'ZMQ socket name/address to forward all incoming protocol messages')
[CompletionResult]::new('-x', 'x', [CompletionResultType]::ParameterName, 'ZMQ socket name/address for daemon control interface')
[CompletionResult]::new('--ctl-socket', 'ctl-socket', [CompletionResultType]::ParameterName, 'ZMQ socket name/address for daemon control interface')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information')
[CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'Set verbosity level')
[CompletionResult]::new('--verbose', 'verbose', [CompletionResultType]::ParameterName, 'Set verbosity level')
break
}
'swap-cli;list-listens' {
[CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'Data directory path')
[CompletionResult]::new('--data-dir', 'data-dir', [CompletionResultType]::ParameterName, 'Data directory path')
Expand Down
51 changes: 50 additions & 1 deletion shell/swap-cli.bash
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ _swap-cli() {
needs-funding)
cmd+="__needs__funding"
;;
offer-info)
cmd+="__offer__info"
;;
peers)
cmd+="__peers"
;;
Expand All @@ -52,7 +55,7 @@ _swap-cli() {

case "${cmd}" in
swap__cli)
opts="-h -V -d -v -T -m -x --help --version --data-dir --verbose --tor-proxy --msg-socket --ctl-socket info peers list-swaps list-offers list-listens make take revoke-offer progress needs-funding help"
opts="-h -V -d -v -T -m -x --help --version --data-dir --verbose --tor-proxy --msg-socket --ctl-socket info peers list-swaps list-offers offer-info list-listens make take revoke-offer progress needs-funding help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -499,6 +502,52 @@ _swap-cli() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
swap__cli__offer__info)
opts="-h -d -v -T -m -x --help --data-dir --verbose --tor-proxy --msg-socket --ctl-socket <PUBLIC_OFFER>"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
--data-dir)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-d)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--tor-proxy)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-T)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--msg-socket)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-m)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--ctl-socket)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-x)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
swap__cli__peers)
opts="-h -d -v -T -m -x --help --data-dir --verbose --tor-proxy --msg-socket --ctl-socket"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
Expand Down
44 changes: 27 additions & 17 deletions src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use farcaster_core::negotiation::Offer;
use farcaster_core::swap::btcxmr::BtcXmr;
use std::{
convert::TryFrom,
io::{self, Read, Write},
Expand Down Expand Up @@ -159,6 +161,13 @@ impl Exec for Command {
runtime.report_response_or_fail()?;
}

Command::OfferInfo {
public_offer: PublicOffer { offer, .. },
} => {
println!("\n Trading {}\n", offer_buy_information(&offer));
println!("{}\n", offer);
}

Command::Take {
public_offer,
bitcoin_address,
Expand All @@ -180,25 +189,9 @@ impl Exec for Command {
peer_address,
} = public_offer.clone();
if !without_validation {
let taker_role = offer.maker_role.other();
let arb_amount = offer.arbitrating_amount;
let acc_amount = offer.accordant_amount;
println!(
"\nWant to buy {}?\n\nCarefully validate offer!\n",
match taker_role {
SwapRole::Alice => format!(
"{} for {} at {} BTC/XMR",
arb_amount,
acc_amount,
arb_amount.as_btc() / acc_amount.as_xmr()
),
SwapRole::Bob => format!(
"{} for {} at {} XMR/BTC",
acc_amount,
arb_amount,
acc_amount.as_xmr() / arb_amount.as_btc()
),
}
offer_buy_information(&offer)
);
println!("Trade counterparty: {}@{}\n", &node_id, peer_address);
println!("{}\n", offer);
Expand Down Expand Up @@ -263,3 +256,20 @@ fn take_offer() -> bool {
_ => take_offer(),
}
}

fn offer_buy_information(offer: &Offer<BtcXmr>) -> String {
match offer.maker_role.other() {
SwapRole::Alice => format!(
"{} for {} at {} BTC/XMR",
offer.arbitrating_amount,
offer.accordant_amount,
offer.arbitrating_amount.as_btc() / offer.accordant_amount.as_xmr()
),
SwapRole::Bob => format!(
"{} for {} at {} XMR/BTC",
offer.accordant_amount,
offer.arbitrating_amount,
offer.accordant_amount.as_xmr() / offer.arbitrating_amount.as_btc()
),
}
}
8 changes: 8 additions & 0 deletions src/cli/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ pub enum Command {
#[clap(aliases = &["lo"])]
ListOffers,

/// Gives information on an open offer
#[clap(aliases = &["oi"])]
#[display("offer-info<{public_offer}>")]
OfferInfo {
/// The offer to be canceled.
public_offer: PublicOffer<BtcXmr>,
},

// /// Lists IDs of public offers created by daemon
// ListOfferIds,
/// Lists listeners created by daemon
Expand Down