add request function for offer request

This commit is contained in:
Felix
2024-06-04 16:44:09 +00:00
parent aa2f23d5b6
commit 55fe8f80e2
7 changed files with 1868 additions and 2 deletions

View File

@ -1,4 +1,3 @@
mod trader;
mod coordinator;
mod cli;
mod communication;

File diff suppressed because it is too large Load Diff

View File

@ -4,3 +4,8 @@ version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.86"
bdk = "0.29.0"
clap = "4.5.4"
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
serde = "1.0.203"

View File

@ -0,0 +1,91 @@
use clap::{command, Arg, Command, ArgMatches};
#[derive(Debug)]
pub struct Coordinator;
#[derive(Debug)]
pub struct TraderSettings {
pub electrum_endpoint: String,
pub coordinator_endpoint: String,
}
#[derive(Debug)]
pub enum CliSettings {
Coordinator(Coordinator),
Taker(TraderSettings),
Maker(TraderSettings)
}
trait ArgMatchesParser {
fn parse_into_enum(&self) -> CliSettings;
}
impl ArgMatchesParser for ArgMatches {
fn parse_into_enum(&self) -> CliSettings {
if let Some(_mode) = self.subcommand_matches("coordinator") {
CliSettings::Coordinator(Coordinator { })
} else if let Some(mode) = self.subcommand_matches("trader") {
let trader_settings = TraderSettings {
coordinator_endpoint: mode.get_one::<String>("coordinator-ep")
.expect("Coordinator endpoint not provided!").clone(),
electrum_endpoint: mode.get_one::<String>("electrum-ep")
.expect("Electrum endpoint not provided").clone()
};
if mode.contains_id("maker") {
CliSettings::Maker( trader_settings )
} else if mode.contains_id("taker") {
CliSettings::Taker( trader_settings )
} else {
panic!("Wrong arguments for Trader mode!")
}
} else {
panic!("Select either coordinator or trader mode!")
}
}
}
pub fn parse_cli_args() -> CliSettings {
command!()
.about("RoboSats taproot onchain trade pipeline CLI demonstrator. Don't use with real funds.")
.subcommand(
Command::new("coordinator")
.about("Run in coordinator mode.")
)
.subcommand(
Command::new("trader")
.about("Two available trader modes: Maker and Taker. Select one and provide Coordinator and Electum endpoint")
.arg(
Arg::new("taker")
.short('t')
.long("taker")
.help("Run program as taker")
.num_args(0)
.conflicts_with("maker")
)
.arg (
Arg::new("maker")
.short('m')
.long("maker")
.num_args(0)
.help("Run program as maker")
.conflicts_with("taker")
)
.arg(
Arg::new("coordinator-ep")
.short('p')
.long("endpoint")
.required(true)
.help("Communication endpoint of the coordinator to connect to")
)
.arg(
Arg::new("electrum-ep")
.short('e')
.long("electrum")
.required(true)
.help("URL of the electrum endpoint")
)
)
.arg_required_else_help(true)
.get_matches()
.parse_into_enum()
}

View File

@ -0,0 +1,14 @@
use reqwest;
use anyhow::Result;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct OfferConditions {
pub locking_address: String,
}
pub fn fetch_offer(coordinator_ep: &String) -> Result<OfferConditions> {
let res = reqwest::blocking::get(format!("{}{}", coordinator_ep, "/create-offer"))?;
let offer_conditions: OfferConditions = res.json()?;
Ok(offer_conditions)
}

View File

@ -1,3 +1,9 @@
mod cli;
mod communication;
use cli::parse_cli_args;
fn main() {
println!("Hello, world!");
let mode = parse_cli_args();
dbg!(mode);
}