Skip to content

Commit 876e47f

Browse files
committed
Adds offers for an account to cli
Updates the account command to have an offers command for listing the offers associated with that account. Also, adds `Clone` to the `Offer` struct so that it works with `Iter`. closes #161
1 parent 6ed8c40 commit 876e47f

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

cli/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
- Initial CLI is built and usable in it's most basic form. Listed endpoints can be scrolled.
1212
- Available data:
1313
- Account effects can be seen
14+
- Account offers can be seen
1415
- All effects can be seen
1516
- All ledgers can be seen
1617
- All operations can be seen

cli/src/account.rs

+21
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,24 @@ pub fn effects(client: &Client, matches: &ArgMatches) -> Result<()> {
5555
let _ = fmt.stop();
5656
res
5757
}
58+
59+
pub fn offers(client: &Client, matches: &ArgMatches) -> Result<()> {
60+
let pager = Pager::from_arg(&matches);
61+
62+
let id = matches.value_of("ID").expect("ID is required");
63+
let endpoint = account::Offers::new(id);
64+
let endpoint = pager.assign(endpoint);
65+
let endpoint = cursor::assign_from_arg(matches, endpoint);
66+
let endpoint = ordering::assign_from_arg(matches, endpoint);
67+
68+
let iter = sync::Iter::new(&client, endpoint);
69+
70+
let mut res = Ok(());
71+
let mut fmt = Formatter::start_stdout(Simple);
72+
pager.paginate(iter, |result| match result {
73+
Ok(offer) => fmt.render(&offer),
74+
Err(err) => res = Err(err.into()),
75+
});
76+
let _ = fmt.stop();
77+
res
78+
}

cli/src/fmt/simple/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod account;
1212
mod asset;
1313
mod effect;
1414
mod ledger;
15+
mod offer;
1516
mod orderbook;
1617
mod payment;
1718
mod trade_aggregation;

cli/src/fmt/simple/offer.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use fmt::Render;
2+
use super::Simple;
3+
use stellar_client::resources::{AssetIdentifier, Offer};
4+
5+
fn render_asset(id: &AssetIdentifier) -> String {
6+
if id.is_native() {
7+
id.code().to_string()
8+
} else {
9+
format!("{}-{}", id.code(), id.issuer())
10+
}
11+
}
12+
13+
impl Render<Offer> for Simple {
14+
fn render(&self, offer: &Offer) -> Option<String> {
15+
let mut buf = String::new();
16+
append_to_buffer!(buf, "ID: {}", offer.id());
17+
append_to_buffer!(buf, "Seller: {}", offer.seller());
18+
append_to_buffer!(buf, "Selling: {}", render_asset(offer.selling()));
19+
append_to_buffer!(buf, "Buying: {}", render_asset(offer.buying()));
20+
append_to_buffer!(buf, "Amount: {}", offer.amount());
21+
append_to_buffer!(buf, "Price: {}", offer.price());
22+
Some(buf)
23+
}
24+
}

cli/src/main.rs

+13
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ fn build_app<'a, 'b>() -> App<'a, 'b> {
9797
)
9898
),
9999

100+
)
101+
.subcommand(
102+
listable!(
103+
SubCommand::with_name("offers")
104+
.about("Fetch all open offers for an account")
105+
.arg(
106+
Arg::with_name("ID")
107+
.required(true)
108+
.help("The identifier of the account to look up"),
109+
)
110+
),
111+
100112
),
101113
)
102114
.subcommand(
@@ -394,6 +406,7 @@ fn main() {
394406
("details", Some(sub_m)) => account::details(&client, sub_m),
395407
("transactions", Some(sub_m)) => account::transactions(&client, sub_m),
396408
("effects", Some(sub_m)) => account::effects(&client, sub_m),
409+
("offers", Some(sub_m)) => account::offers(&client, sub_m),
397410
_ => return print_help_and_exit(),
398411
},
399412
("assets", Some(sub_m)) => match sub_m.subcommand() {

client/src/resources/offer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ mod offer_summary_tests {
8181
}
8282

8383
/// An offer being made for particular assets at a particular exchange rate.
84-
#[derive(Deserialize, Debug)]
84+
#[derive(Deserialize, Debug, Clone)]
8585
pub struct Offer {
8686
id: i64,
8787
paging_token: String,

0 commit comments

Comments
 (0)