From 228809853f59ab9bb97fe7389ed22121bae9ae79 Mon Sep 17 00:00:00 2001 From: Felix <51097237+f321x@users.noreply.github.com> Date: Mon, 17 Jun 2024 11:47:07 +0000 Subject: [PATCH] fetch taker psbt --- .../trader/src/communication/api.rs | 18 +++-- .../src/communication/taker_requests.rs | 70 ++++++++++--------- .../trader/src/trading/taker_utils.rs | 35 +++++++--- taptrade-cli-demo/trader/src/wallet/bond.rs | 4 -- taptrade-cli-demo/trader/src/wallet/mod.rs | 5 +- 5 files changed, 76 insertions(+), 56 deletions(-) diff --git a/taptrade-cli-demo/trader/src/communication/api.rs b/taptrade-cli-demo/trader/src/communication/api.rs index 9f9caea..4739519 100644 --- a/taptrade-cli-demo/trader/src/communication/api.rs +++ b/taptrade-cli-demo/trader/src/communication/api.rs @@ -50,6 +50,7 @@ pub struct OfferTakenResponse { // Taker structures // +// request all fitting offers from the coordinator #[derive(Debug, Serialize)] pub struct OffersRequest { pub buy_offers: bool, // true if looking for buy offers, false if looking for sell offers @@ -57,19 +58,22 @@ pub struct OffersRequest { pub amount_max_sat: u64, } -#[derive(Debug, Deserialize, Serialize)] -pub struct PublicOffer { - pub amount_sat: u64, - pub offer_id_hex: String, -} - +// response of the coordinator, containing all fitting offers to the OffersRequest request #[derive(Debug, Deserialize)] pub struct PublicOffers { pub offers: Option>, // don't include offers var in return json if no offers are available } +// Offer information of each offer returned by the previous response +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct PublicOffer { + pub amount_sat: u64, + pub offer_id_hex: String, +} + +// request to receive the escrow psbt to sign for the specified offer to take it #[derive(Debug, Serialize)] -pub struct RequestOfferPsbt { +pub struct OfferPsbtRequest { pub offer: PublicOffer, pub trade_data: BondSubmissionRequest, } diff --git a/taptrade-cli-demo/trader/src/communication/taker_requests.rs b/taptrade-cli-demo/trader/src/communication/taker_requests.rs index a6771da..0e903ba 100644 --- a/taptrade-cli-demo/trader/src/communication/taker_requests.rs +++ b/taptrade-cli-demo/trader/src/communication/taker_requests.rs @@ -42,40 +42,44 @@ impl PublicOffers { } } -impl PublicOffer { tbd - // pub fn take(&self, taker_config: &TraderSettings) -> Result { - // let client = reqwest::blocking::Client::new(); - // let res = client - // .post(format!( - // "{}{}", - // taker_config.coordinator_endpoint, "/take-offer" - // )) - // .json(self) - // .send()? - // .json::()?; - // Ok(res) - // } +impl PublicOffer { + pub fn request_bond(&self, taker_config: &TraderSettings) -> Result { + let client = reqwest::blocking::Client::new(); + let res = client + .post(format!( + "{}{}", + taker_config.coordinator_endpoint, "/request-taker-bond" + )) + .json(self) + .send()? + .json::()?; + Ok(res) + } } -impl OfferTakenRequest { // tbd - // pub fn taker_request( - // bond: &Bond, - // mut musig_data: &MuSigData, - // taker_config: &TraderSettings, - // ) -> Result { - // let request = RequestOfferPsbt { - // offer: - // }; +impl OfferPsbtRequest { + pub fn taker_request( + offer: &PublicOffer, + trade_data: BondSubmissionRequest, + taker_config: &TraderSettings, + ) -> Result { + let request = OfferPsbtRequest { + offer: offer.clone(), + trade_data, + }; - // let client = reqwest::blocking::Client::new(); - // let res = client - // .post(format!( - // "{}{}", - // taker_config.coordinator_endpoint, "/submit-taker-bond" - // )) - // .json(self) - // .send()? - // .json::()?; - // Ok(res) - // } + let client = reqwest::blocking::Client::new(); + let res = client + .post(format!( + "{}{}", + taker_config.coordinator_endpoint, "/submit-taker-bond" + )) + .json(&request) + .send()? + .json::()?; + + let psbt_bytes = hex::decode(res.trade_psbt_hex_to_sign)?; + let psbt = PartiallySignedTransaction::deserialize(&psbt_bytes)?; + Ok(psbt) + } } diff --git a/taptrade-cli-demo/trader/src/trading/taker_utils.rs b/taptrade-cli-demo/trader/src/trading/taker_utils.rs index 876ece3..592cecc 100644 --- a/taptrade-cli-demo/trader/src/trading/taker_utils.rs +++ b/taptrade-cli-demo/trader/src/trading/taker_utils.rs @@ -1,16 +1,33 @@ +use bdk::electrum_client::Request; + +use crate::communication::api::{OfferPsbtRequest, RequestOfferPsbt}; + use super::utils::*; use super::*; impl ActiveOffer { - // pub fn take( tbd - // trading_wallet: &TradingWallet, - // taker_config: &TraderSettings, - // offer: &PublicOffer, - // ) -> Result { - // let bond_conditions: BondRequirementResponse = offer.take(taker_config)?; - // let (bond, mut musig_data, payout_address) = - // trading_wallet.trade_onchain_assembly(&bond_conditions, taker_config)?; - // let trading_psbt = + pub fn take( + trading_wallet: &TradingWallet, + taker_config: &TraderSettings, + offer: &PublicOffer, + ) -> Result { + // fetching the bond requirements for the requested Offer (amount, locking address) + let bond_conditions: BondRequirementResponse = offer.request_bond(taker_config)?; + + // assembly of the Bond transaction and generation of MuSig data and payout address + let (bond, mut musig_data, payout_address) = + trading_wallet.trade_onchain_assembly(&bond_conditions, taker_config)?; + + // now we submit the signed bond transaction to the coordinator and receive the escrow PSBT we have to sign + // in exchange + let bond_submission_request = BondSubmissionRequest::prepare_bond_request( + &bond, + &payout_address, + &mut musig_data, + taker_config, + )?; + let escrow_contract_psbt = + OfferPsbtRequest::taker_request(offer, bond_submission_request, taker_config)?; } pub fn wait_on_maker(&self) -> Result<()> { diff --git a/taptrade-cli-demo/trader/src/wallet/bond.rs b/taptrade-cli-demo/trader/src/wallet/bond.rs index 4952199..bee743c 100644 --- a/taptrade-cli-demo/trader/src/wallet/bond.rs +++ b/taptrade-cli-demo/trader/src/wallet/bond.rs @@ -57,7 +57,3 @@ impl Bond { Ok(psbt) } } - -// impl BranchAndBoundCoinSelection -// pub fn new(size_of_change: u64) -> Self -// Create new instance with target size for change output diff --git a/taptrade-cli-demo/trader/src/wallet/mod.rs b/taptrade-cli-demo/trader/src/wallet/mod.rs index 68c1e7b..3d67c6f 100644 --- a/taptrade-cli-demo/trader/src/wallet/mod.rs +++ b/taptrade-cli-demo/trader/src/wallet/mod.rs @@ -63,12 +63,11 @@ impl TradingWallet { offer_conditions: &BondRequirementResponse, trader_config: &TraderSettings, ) -> Result<(PartiallySignedTransaction, MuSigData, AddressInfo)> { - let trading_wallet = self.wallet; + let trading_wallet = &self.wallet; let bond = Bond::assemble(&self.wallet, &offer_conditions, trader_config)?; let payout_address: AddressInfo = trading_wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?; - let mut musig_data = - MuSigData::create(&trader_config.wallet_xprv, trading_wallet.secp_ctx())?; + let musig_data = MuSigData::create(&trader_config.wallet_xprv, trading_wallet.secp_ctx())?; Ok((bond, musig_data, payout_address)) }