mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-12-25 14:19:19 +00:00
add comments, make functions more generic, waiting for confirmation
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
use crate::wallet::get_wallet_xprv;
|
||||
use anyhow::{anyhow, Result};
|
||||
use bdk::bitcoin::bip32::ExtendedPrivKey;
|
||||
use hex;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{
|
||||
env,
|
||||
@ -90,7 +89,7 @@ impl CliSettings {
|
||||
}
|
||||
|
||||
// parses the hours input string and returns the unix timestamp + the trade duration in seconds
|
||||
fn hours_to_ts(hours: &String) -> Result<u64> {
|
||||
fn hours_to_ts(hours: &str) -> Result<u64> {
|
||||
let duration: u64 = hours.parse()?;
|
||||
Ok(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() + duration * 3600)
|
||||
}
|
||||
@ -98,7 +97,7 @@ impl CliSettings {
|
||||
fn get_trader_settings() -> Result<TraderSettings> {
|
||||
let electrum_endpoint = Self::get_user_input("Enter electrum endpoint: ");
|
||||
let coordinator_endpoint = Self::get_user_input("Enter coordinator endpoint: ");
|
||||
let robosats_robohash_hex = hex::encode(&hash256(&Self::get_user_input(
|
||||
let robosats_robohash_hex = hex::encode(hash256(&Self::get_user_input(
|
||||
"Enter your robosats robot key: ", // just for testing purposes, to be improved to the real robohash spec
|
||||
)));
|
||||
let trade_type: OfferType = Self::get_trade_type(None);
|
||||
|
||||
@ -78,7 +78,7 @@ impl BondSubmissionRequest {
|
||||
}
|
||||
|
||||
pub fn send_maker(
|
||||
robohash_hex: &String,
|
||||
robohash_hex: &str,
|
||||
bond: &PartiallySignedTransaction,
|
||||
musig_data: &mut MuSigData,
|
||||
payout_address: &AddressInfo,
|
||||
@ -106,7 +106,6 @@ impl OfferTakenResponse {
|
||||
trader_setup: &TraderSettings,
|
||||
) -> Result<Option<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.offer_id_hex.clone(),
|
||||
};
|
||||
|
||||
@ -103,7 +103,7 @@ impl IsOfferReadyRequest {
|
||||
return Ok(());
|
||||
} else if res.status() != 201 {
|
||||
return Err(anyhow!(
|
||||
"Submitting taker psbt failed. Status: {}",
|
||||
"Requesting offer status when waiting on maker to sign psbt failed: {}",
|
||||
res.status()
|
||||
));
|
||||
}
|
||||
|
||||
@ -6,8 +6,8 @@ use self::utils::ActiveOffer;
|
||||
use crate::{
|
||||
cli::TraderSettings,
|
||||
communication::api::{
|
||||
BondRequirementResponse, BondSubmissionRequest, OfferTakenRequest, OfferTakenResponse,
|
||||
PsbtSubmissionRequest, PublicOffer, PublicOffers,
|
||||
BondRequirementResponse, BondSubmissionRequest, IsOfferReadyRequest, OfferTakenRequest,
|
||||
OfferTakenResponse, PsbtSubmissionRequest, PublicOffer, PublicOffers,
|
||||
},
|
||||
wallet::{
|
||||
bond::Bond,
|
||||
@ -28,8 +28,8 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
|
||||
|
||||
let offer = ActiveOffer::create(&wallet, maker_config)?;
|
||||
dbg!(&offer);
|
||||
let mut escrow_contract_psbt = offer.wait_until_taken(maker_config)?;
|
||||
|
||||
let mut escrow_contract_psbt = offer.wait_until_taken(maker_config)?;
|
||||
wallet
|
||||
.validate_maker_psbt(&escrow_contract_psbt)?
|
||||
.sign_escrow_psbt(&mut escrow_contract_psbt)?;
|
||||
@ -40,7 +40,9 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
|
||||
offer.offer_id_hex.clone(),
|
||||
maker_config,
|
||||
)?;
|
||||
|
||||
// wait for confirmation
|
||||
offer.wait_on_trade_ready_confirmation(maker_config)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -49,18 +51,19 @@ pub fn run_taker(taker_config: &TraderSettings) -> Result<()> {
|
||||
let wallet = TradingWallet::load_wallet(taker_config)?;
|
||||
let mut available_offers = PublicOffers::fetch(taker_config)?;
|
||||
|
||||
while let None = available_offers.offers {
|
||||
println!("No offers available, trying again in 10 sec.");
|
||||
while available_offers.offers.is_none() {
|
||||
println!("No offers available, fetching again in 10 sec.");
|
||||
thread::sleep(Duration::from_secs(10));
|
||||
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(taker_config)?;
|
||||
let accepted_offer = ActiveOffer::take(&wallet, taker_config, selected_offer)?
|
||||
.wait_on_trade_ready_confirmation(taker_config)?
|
||||
.wait_on_fiat_confirmation_cli_input()?;
|
||||
|
||||
accepted_offer.wait_on_fiat_confirmation()?;
|
||||
// .wait_on_maker_confirmation(); // here we wait for the maker to confirm the reciept of the fiat. We could go into escrow here.
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -50,13 +50,9 @@ impl ActiveOffer {
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
pub fn wait_on_fiat_confirmation_cli_input(&self) -> Result<&Self> {
|
||||
// let user confirm in CLI that the fiat payment has been sent/received
|
||||
println!("The escrow is now locked and the fiat exchange can begin safely.");
|
||||
loop {
|
||||
println!("Please confirm that the fiat payment has been sent/received. (y/N)");
|
||||
let mut input = String::new();
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
use super::maker_utils::*;
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -9,3 +8,12 @@ pub struct ActiveOffer {
|
||||
pub expected_payout_address: AddressInfo,
|
||||
pub escrow_psbt: Option<PartiallySignedTransaction>,
|
||||
}
|
||||
|
||||
impl ActiveOffer {
|
||||
// polls till the other party signed the trade transaction and it got confirmed.
|
||||
// once the coordinator signals OfferReady the fiat exchange can begin
|
||||
pub fn wait_on_trade_ready_confirmation(self, trader_config: &TraderSettings) -> Result<Self> {
|
||||
IsOfferReadyRequest::poll(trader_config, &self)?;
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,11 +43,8 @@ impl TradingWallet {
|
||||
pub fn load_wallet(trader_config: &TraderSettings) -> Result<TradingWallet> {
|
||||
let backend = ElectrumBlockchain::from(Client::new(&trader_config.electrum_endpoint)?);
|
||||
let wallet = Wallet::new(
|
||||
Bip86(trader_config.wallet_xprv.clone(), KeychainKind::External),
|
||||
Some(Bip86(
|
||||
trader_config.wallet_xprv.clone(),
|
||||
KeychainKind::Internal,
|
||||
)),
|
||||
Bip86(trader_config.wallet_xprv, KeychainKind::External),
|
||||
Some(Bip86(trader_config.wallet_xprv, KeychainKind::Internal)),
|
||||
bitcoin::Network::Testnet,
|
||||
MemoryDatabase::default(), // non-permanent storage
|
||||
)?;
|
||||
@ -64,7 +61,7 @@ impl TradingWallet {
|
||||
trader_config: &TraderSettings,
|
||||
) -> Result<(PartiallySignedTransaction, MuSigData, AddressInfo)> {
|
||||
let trading_wallet = &self.wallet;
|
||||
let bond = Bond::assemble(&self.wallet, &offer_conditions, trader_config)?;
|
||||
let bond = Bond::assemble(&self.wallet, offer_conditions, trader_config)?;
|
||||
let payout_address: AddressInfo =
|
||||
trading_wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?;
|
||||
let musig_data = MuSigData::create(&trader_config.wallet_xprv, trading_wallet.secp_ctx())?;
|
||||
|
||||
Reference in New Issue
Block a user