fetch taker psbt

This commit is contained in:
Felix
2024-06-17 11:47:07 +00:00
parent eb438388db
commit 228809853f
5 changed files with 76 additions and 56 deletions

View File

@ -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<Vec<PublicOffer>>, // 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,
}

View File

@ -42,40 +42,44 @@ impl PublicOffers {
}
}
impl PublicOffer { tbd
// pub fn take(&self, taker_config: &TraderSettings) -> Result<BondRequirementResponse> {
// let client = reqwest::blocking::Client::new();
// let res = client
// .post(format!(
// "{}{}",
// taker_config.coordinator_endpoint, "/take-offer"
// ))
// .json(self)
// .send()?
// .json::<BondRequirementResponse>()?;
// Ok(res)
// }
impl PublicOffer {
pub fn request_bond(&self, taker_config: &TraderSettings) -> Result<BondRequirementResponse> {
let client = reqwest::blocking::Client::new();
let res = client
.post(format!(
"{}{}",
taker_config.coordinator_endpoint, "/request-taker-bond"
))
.json(self)
.send()?
.json::<BondRequirementResponse>()?;
Ok(res)
}
}
impl OfferTakenRequest { // tbd
// pub fn taker_request(
// bond: &Bond,
// mut musig_data: &MuSigData,
// taker_config: &TraderSettings,
// ) -> Result<PartiallySignedTransaction> {
// let request = RequestOfferPsbt {
// offer:
// };
impl OfferPsbtRequest {
pub fn taker_request(
offer: &PublicOffer,
trade_data: BondSubmissionRequest,
taker_config: &TraderSettings,
) -> Result<PartiallySignedTransaction> {
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::<OfferTakenResponse>()?;
// Ok(res)
// }
let client = reqwest::blocking::Client::new();
let res = client
.post(format!(
"{}{}",
taker_config.coordinator_endpoint, "/submit-taker-bond"
))
.json(&request)
.send()?
.json::<OfferTakenResponse>()?;
let psbt_bytes = hex::decode(res.trade_psbt_hex_to_sign)?;
let psbt = PartiallySignedTransaction::deserialize(&psbt_bytes)?;
Ok(psbt)
}
}

View File

@ -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<ActiveOffer> {
// 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<ActiveOffer> {
// 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<()> {

View File

@ -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

View File

@ -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))
}