add taker wait on fiat confirmation

This commit is contained in:
Felix
2024-06-18 10:23:03 +00:00
parent 3c24424a9f
commit 98d4475a2c
7 changed files with 66 additions and 11 deletions

View File

@ -85,3 +85,11 @@ pub struct PsbtSubmissionRequest {
pub offer_id_hex: String,
pub robohash_hex: String,
}
// request polled to check if the maker has submitted his escrow transaction
// and the escrow transaction is confirmed once this returns 200 the chat can open
#[derive(Debug, Serialize)]
pub struct IsOfferReadyRequest {
pub robohash_hex: String,
pub offer_id_hex: String,
}

View File

@ -17,6 +17,7 @@ use bdk::{
wallet::AddressInfo,
};
use serde::{Deserialize, Serialize};
use std::{thread::sleep, time::Duration};
impl BondRequirementResponse {
fn _format_request(trader_setup: &TraderSettings) -> OrderRequest {
@ -107,7 +108,7 @@ impl OfferTakenResponse {
let request = OfferTakenRequest {
// maybe can be made a bit more efficient (less clone)
robohash_hex: trader_setup.robosats_robohash_hex.clone(),
order_id_hex: offer.order_id_hex.clone(),
order_id_hex: offer.offer_id_hex.clone(),
};
let client = reqwest::blocking::Client::new();
let res = client

View File

@ -112,3 +112,32 @@ impl PsbtSubmissionRequest {
Ok(())
}
}
impl IsOfferReadyRequest {
pub fn poll(taker_config: &TraderSettings, offer: &ActiveOffer) -> Result<()> {
let request = IsOfferReadyRequest {
robohash_hex: taker_config.robosats_robohash_hex.clone(),
offer_id_hex: offer.offer_id_hex.clone(),
};
let client = reqwest::blocking::Client::new();
loop {
let res = client
.post(format!(
"{}{}",
taker_config.coordinator_endpoint, "/poll-offer-status-taker"
))
.json(&request)
.send()?;
if res.status() == 200 {
return Ok(());
} else if res.status() != 201 {
return Err(anyhow!(
"Submitting taker psbt failed. Status: {}",
res.status()
));
}
// Sleep for 10 sec and poll again
sleep(Duration::from_secs(10));
}
}
}

View File

@ -24,11 +24,11 @@ impl ActiveOffer {
maker_config,
)?;
Ok(ActiveOffer {
order_id_hex: submission_result.order_id_hex,
bond_locked_until_timestamp: submission_result.bond_locked_until_timestamp,
offer_id_hex: submission_result.order_id_hex,
used_musig_config: musig_data,
used_bond: bond,
expected_payout_address: payout_address,
escrow_psbt: None,
})
}

View File

@ -43,8 +43,12 @@ pub fn run_taker(taker_config: &TraderSettings) -> Result<()> {
available_offers = PublicOffers::fetch(taker_config)?;
}
let selected_offer: &PublicOffer = available_offers.ask_user_to_select()?;
// take selected offer and wait for maker to sign his input to the ecrow transaction
let accepted_offer =
ActiveOffer::take(&wallet, taker_config, selected_offer)?.wait_on_maker()?;
ActiveOffer::take(&wallet, taker_config, selected_offer)?.wait_on_maker(taker_config)?;
accepted_offer.wait_on_fiat_confirmation()?;
Ok(())
}

View File

@ -1,6 +1,6 @@
use bdk::electrum_client::Request;
use crate::communication::api::{OfferPsbtRequest, PsbtSubmissionRequest};
use crate::communication::api::{IsOfferReadyRequest, OfferPsbtRequest, PsbtSubmissionRequest};
use super::utils::*;
use super::*;
@ -42,16 +42,29 @@ impl ActiveOffer {
)?;
Ok(ActiveOffer {
order_id_hex: offer.offer_id_hex.clone(),
offer_id_hex: offer.offer_id_hex.clone(),
used_musig_config: musig_data,
used_bond: bond,
expected_payout_address: payout_address,
escrow_psbt: escrow_contract_psbt,
escrow_psbt: Some(escrow_contract_psbt),
})
}
pub fn wait_on_maker(self) -> Result<Self> {
// tbd
pub fn wait_on_maker(self, taker_config: &TraderSettings) -> Result<Self> {
IsOfferReadyRequest::poll(taker_config, &self)?;
Ok(self)
}
pub fn wait_on_fiat_confirmation(&self) -> Result<&Self> {
// let user confirm in CLI that the fiat payment has been sent/receivec
loop {
println!("Please confirm that the fiat payment has been sent/received. (y/N)");
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
if input.trim().to_lowercase() == "y" {
break;
}
}
Ok(self)
}
}

View File

@ -3,9 +3,9 @@ use super::*;
#[derive(Debug)]
pub struct ActiveOffer {
pub order_id_hex: String,
pub offer_id_hex: String,
pub used_musig_config: MuSigData,
pub used_bond: PartiallySignedTransaction,
pub expected_payout_address: AddressInfo,
pub escrow_psbt: PartiallySignedTransaction,
pub escrow_psbt: Option<PartiallySignedTransaction>,
}