mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-07-28 21:53:19 +00:00
add taker escrow psbt submission
This commit is contained in:
@ -77,3 +77,11 @@ pub struct OfferPsbtRequest {
|
||||
pub offer: PublicOffer,
|
||||
pub trade_data: BondSubmissionRequest,
|
||||
}
|
||||
|
||||
// submit signed escrow psbt back to coordinator in a Json like this
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct PsbtSubmissionRequest {
|
||||
pub signed_psbt_hex: String,
|
||||
pub offer_id_hex: String,
|
||||
pub robohash_hex: String,
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use crate::{
|
||||
use anyhow::{anyhow, Result};
|
||||
use api::{
|
||||
BondRequirementResponse, BondSubmissionRequest, OfferTakenRequest, OfferTakenResponse,
|
||||
OrderActivatedResponse, OrderRequest,
|
||||
OrderActivatedResponse, OrderRequest, PsbtSubmissionRequest,
|
||||
};
|
||||
use bdk::bitcoin::consensus::encode::serialize_hex;
|
||||
use bdk::{
|
||||
|
@ -83,3 +83,32 @@ impl OfferPsbtRequest {
|
||||
Ok(psbt)
|
||||
}
|
||||
}
|
||||
|
||||
impl PsbtSubmissionRequest {
|
||||
pub fn submit_taker_psbt(
|
||||
psbt: &PartiallySignedTransaction,
|
||||
offer_id_hex: String,
|
||||
taker_config: &TraderSettings,
|
||||
) -> Result<()> {
|
||||
let request = PsbtSubmissionRequest {
|
||||
signed_psbt_hex: psbt.serialize_hex(),
|
||||
offer_id_hex,
|
||||
robohash_hex: taker_config.robosats_robohash_hex.clone(),
|
||||
};
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let res = client
|
||||
.post(format!(
|
||||
"{}{}",
|
||||
taker_config.coordinator_endpoint, "/submit-taker-psbt"
|
||||
))
|
||||
.json(&request)
|
||||
.send()?;
|
||||
if res.status() != 200 {
|
||||
return Err(anyhow!(
|
||||
"Submitting taker psbt failed. Status: {}",
|
||||
res.status()
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -43,9 +43,8 @@ pub fn run_taker(taker_config: &TraderSettings) -> Result<()> {
|
||||
available_offers = PublicOffers::fetch(taker_config)?;
|
||||
}
|
||||
let selected_offer: &PublicOffer = available_offers.ask_user_to_select()?;
|
||||
let accepted_offer = ActiveOffer::take(&wallet, taker_config, selected_offer)?;
|
||||
|
||||
// accepted_offer.wait_on_maker();
|
||||
let accepted_offer =
|
||||
ActiveOffer::take(&wallet, taker_config, selected_offer)?.wait_on_maker()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use bdk::electrum_client::Request;
|
||||
|
||||
use crate::communication::api::OfferPsbtRequest;
|
||||
use crate::communication::api::{OfferPsbtRequest, PsbtSubmissionRequest};
|
||||
|
||||
use super::utils::*;
|
||||
use super::*;
|
||||
@ -10,9 +10,7 @@ impl ActiveOffer {
|
||||
trading_wallet: &TradingWallet,
|
||||
taker_config: &TraderSettings,
|
||||
offer: &PublicOffer,
|
||||
) -> Result<()> {
|
||||
// return Ok(Active offer)
|
||||
|
||||
) -> Result<ActiveOffer> {
|
||||
// fetching the bond requirements for the requested Offer (amount, locking address)
|
||||
let bond_conditions: BondRequirementResponse = offer.request_bond(taker_config)?;
|
||||
|
||||
@ -28,18 +26,32 @@ impl ActiveOffer {
|
||||
&mut musig_data,
|
||||
taker_config,
|
||||
)?;
|
||||
let escrow_contract_psbt =
|
||||
let mut escrow_contract_psbt =
|
||||
OfferPsbtRequest::taker_request(offer, bond_submission_request, taker_config)?;
|
||||
|
||||
// now we have to verify, sign and submit the escrow psbt again
|
||||
if !trading_wallet.validate_taker_psbt(&escrow_contract_psbt) {
|
||||
panic!("taker psbt invalid!");
|
||||
}
|
||||
Ok(())
|
||||
trading_wallet
|
||||
.validate_taker_psbt(&escrow_contract_psbt)?
|
||||
.sign_escrow_psbt(&mut escrow_contract_psbt)?;
|
||||
|
||||
// submit signed escrow psbt back to coordinator
|
||||
PsbtSubmissionRequest::submit_taker_psbt(
|
||||
&escrow_contract_psbt,
|
||||
offer.offer_id_hex.clone(),
|
||||
taker_config,
|
||||
)?;
|
||||
|
||||
Ok(ActiveOffer {
|
||||
order_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,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn wait_on_maker(&self) -> Result<()> {
|
||||
pub fn wait_on_maker(self) -> Result<Self> {
|
||||
// tbd
|
||||
Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,8 @@ use super::*;
|
||||
#[derive(Debug)]
|
||||
pub struct ActiveOffer {
|
||||
pub order_id_hex: String,
|
||||
pub bond_locked_until_timestamp: u128,
|
||||
pub used_musig_config: MuSigData,
|
||||
pub used_bond: PartiallySignedTransaction,
|
||||
pub expected_payout_address: AddressInfo,
|
||||
pub escrow_psbt: PartiallySignedTransaction,
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ pub mod musig2;
|
||||
pub mod wallet_utils;
|
||||
|
||||
use crate::{cli::TraderSettings, communication::api::BondRequirementResponse};
|
||||
use anyhow::Result;
|
||||
use anyhow::{anyhow, Result};
|
||||
use bdk::{
|
||||
bitcoin::{self, bip32::ExtendedPrivKey, psbt::PartiallySignedTransaction, Network},
|
||||
blockchain::ElectrumBlockchain,
|
||||
@ -13,7 +13,7 @@ use bdk::{
|
||||
miniscript::Descriptor,
|
||||
template::{Bip86, DescriptorTemplate},
|
||||
wallet::AddressInfo,
|
||||
KeychainKind, SyncOptions, Wallet,
|
||||
KeychainKind, SignOptions, SyncOptions, Wallet,
|
||||
};
|
||||
use bond::Bond;
|
||||
use musig2::MuSigData;
|
||||
@ -75,7 +75,17 @@ impl TradingWallet {
|
||||
// validate that the taker psbt references the correct inputs and amounts
|
||||
// taker input should be the same as in the previous bond transaction.
|
||||
// input amount should be the bond amount when buying,
|
||||
pub fn validate_taker_psbt(&self, psbt: &PartiallySignedTransaction) -> bool {
|
||||
false
|
||||
pub fn validate_taker_psbt(&self, psbt: &PartiallySignedTransaction) -> Result<&Self> {
|
||||
dbg!("IMPLEMENT TAKER PSBT VALIDATION!");
|
||||
// tbd once the trade psbt is implemented
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn sign_escrow_psbt(&self, escrow_psbt: &mut PartiallySignedTransaction) -> Result<&Self> {
|
||||
let finalized = self.wallet.sign(escrow_psbt, SignOptions::default())?;
|
||||
if !finalized {
|
||||
return Err(anyhow!("Signing of taker escrow psbt failed!"));
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user