diff --git a/taptrade-cli-demo/trader/src/cli/mod.rs b/taptrade-cli-demo/trader/src/cli/mod.rs index e0e706e..91ae4a2 100755 --- a/taptrade-cli-demo/trader/src/cli/mod.rs +++ b/taptrade-cli-demo/trader/src/cli/mod.rs @@ -50,6 +50,12 @@ impl OfferType { OfferType::Sell(value) => *value, } } + pub fn is_buy_order(&self) -> bool { + match self { + OfferType::Buy(_) => true, + OfferType::Sell(_) => false, + } + } } impl CliSettings { diff --git a/taptrade-cli-demo/trader/src/communication/api.rs b/taptrade-cli-demo/trader/src/communication/api.rs index d89ffca..e7d7c4e 100644 --- a/taptrade-cli-demo/trader/src/communication/api.rs +++ b/taptrade-cli-demo/trader/src/communication/api.rs @@ -1,4 +1,4 @@ -use serde::{Deserialize, Serialize}; +use super::*; // maker step 1 // requesting to create an offer on the orderbook (POST request) @@ -47,3 +47,23 @@ pub struct OfferTakenRequest { pub struct OfferTakenResponse { pub trade_psbt_hex_to_sign: String, } + +// Taker structures + +#[derive(Debug, Serialize)] +pub struct OffersRequest { + pub buy_offers: bool, // true if looking for buy offers, false if looking for sell offers + pub amount_min_sat: u64, + pub amount_max_sat: u64, +} + +#[derive(Debug, Deserialize)] +pub struct PublicOffer { + pub amount_sat: u64, + pub offer_id_hex: String, +} + +#[derive(Debug, Deserialize)] +pub struct PublicOffers { + pub offers: Option>, // don't include offers var in return json if no offers are available +} diff --git a/taptrade-cli-demo/trader/src/communication/mod.rs b/taptrade-cli-demo/trader/src/communication/mod.rs index b1ac985..5bb9464 100644 --- a/taptrade-cli-demo/trader/src/communication/mod.rs +++ b/taptrade-cli-demo/trader/src/communication/mod.rs @@ -1,4 +1,5 @@ pub mod api; +pub mod taker_requests; use crate::{ cli::{OfferType, TraderSettings}, diff --git a/taptrade-cli-demo/trader/src/communication/taker_requests.rs b/taptrade-cli-demo/trader/src/communication/taker_requests.rs new file mode 100644 index 0000000..c77391b --- /dev/null +++ b/taptrade-cli-demo/trader/src/communication/taker_requests.rs @@ -0,0 +1,41 @@ +use anyhow::Context; + +use super::{api::*, *}; + +impl PublicOffers { + pub fn fetch(taker_config: &TraderSettings) -> Result { + let amount = taker_config.trade_type.value(); + let request = OffersRequest { + buy_offers: taker_config.trade_type.is_buy_order(), + amount_min_sat: (amount as f64 * 0.9).round() as u64, // range can be made variable in production + amount_max_sat: (amount as f64 * 1.1).round() as u64, + }; + + let client = reqwest::blocking::Client::new(); + let res = client + .post(format!( + "{}{}", + taker_config.coordinator_endpoint, "/fetch-available-offers" + )) + .json(&request) + .send()? + .json::()?; + Ok(res) + } + + pub fn ask_user_to_select(&self) -> Result<&PublicOffer> { + for (index, offer) in self.offers.as_ref().unwrap().iter().enumerate() { + println!( + "Offer Index: {} | Amount: {} | ID: {}", + index, offer.amount_sat, offer.offer_id_hex + ); + } + + println!("Enter index of the offer you want to accept: "); + let mut input = String::new(); + std::io::stdin().read_line(&mut input)?; + let index: usize = input.trim().parse().context("Wrong index entered")?; + + Ok(&self.offers.as_ref().unwrap()[index]) + } +} diff --git a/taptrade-cli-demo/trader/src/trading/mod.rs b/taptrade-cli-demo/trader/src/trading/mod.rs index 7b928df..fd35512 100644 --- a/taptrade-cli-demo/trader/src/trading/mod.rs +++ b/taptrade-cli-demo/trader/src/trading/mod.rs @@ -1,9 +1,9 @@ pub mod maker_utils; use self::maker_utils::ActiveOffer; -use crate::cli::TraderSettings; -use crate::wallet::TradingWallet; +use crate::{cli::TraderSettings, communication::api::PublicOffers, wallet::TradingWallet}; use anyhow::Result; +use std::{thread, time::Duration}; pub fn run_maker(maker_config: &TraderSettings) -> Result<()> { let wallet = TradingWallet::load_wallet(maker_config)?; // initialize the wallet with xprv @@ -16,7 +16,15 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> { } pub fn run_taker(taker_config: &TraderSettings) -> Result<()> { - // panic!("Taker to be implemented!"); + let wallet = TradingWallet::load_wallet(maker_config)?; + let mut available_offers = PublicOffers::fetch(taker_config)?; + + while let None = available_offers.offers { + println!("No offers available, trying again in 10 sec."); + thread::sleep(Duration::from_secs(10)); + available_offers = PublicOffers::fetch(taker_config)?; + } + let selected_offer = available_offers.ask_user_to_select()?; Ok(()) }