mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-08-09 03:20:03 +00:00
fetch taker psbt
This commit is contained in:
@ -50,6 +50,7 @@ pub struct OfferTakenResponse {
|
|||||||
|
|
||||||
// Taker structures //
|
// Taker structures //
|
||||||
|
|
||||||
|
// request all fitting offers from the coordinator
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct OffersRequest {
|
pub struct OffersRequest {
|
||||||
pub buy_offers: bool, // true if looking for buy offers, false if looking for sell offers
|
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,
|
pub amount_max_sat: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
// response of the coordinator, containing all fitting offers to the OffersRequest request
|
||||||
pub struct PublicOffer {
|
|
||||||
pub amount_sat: u64,
|
|
||||||
pub offer_id_hex: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct PublicOffers {
|
pub struct PublicOffers {
|
||||||
pub offers: Option<Vec<PublicOffer>>, // don't include offers var in return json if no offers are available
|
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)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct RequestOfferPsbt {
|
pub struct OfferPsbtRequest {
|
||||||
pub offer: PublicOffer,
|
pub offer: PublicOffer,
|
||||||
pub trade_data: BondSubmissionRequest,
|
pub trade_data: BondSubmissionRequest,
|
||||||
}
|
}
|
||||||
|
@ -42,40 +42,44 @@ impl PublicOffers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PublicOffer { tbd
|
impl PublicOffer {
|
||||||
// pub fn take(&self, taker_config: &TraderSettings) -> Result<BondRequirementResponse> {
|
pub fn request_bond(&self, taker_config: &TraderSettings) -> Result<BondRequirementResponse> {
|
||||||
// let client = reqwest::blocking::Client::new();
|
let client = reqwest::blocking::Client::new();
|
||||||
// let res = client
|
let res = client
|
||||||
// .post(format!(
|
.post(format!(
|
||||||
// "{}{}",
|
"{}{}",
|
||||||
// taker_config.coordinator_endpoint, "/take-offer"
|
taker_config.coordinator_endpoint, "/request-taker-bond"
|
||||||
// ))
|
))
|
||||||
// .json(self)
|
.json(self)
|
||||||
// .send()?
|
.send()?
|
||||||
// .json::<BondRequirementResponse>()?;
|
.json::<BondRequirementResponse>()?;
|
||||||
// Ok(res)
|
Ok(res)
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OfferTakenRequest { // tbd
|
impl OfferPsbtRequest {
|
||||||
// pub fn taker_request(
|
pub fn taker_request(
|
||||||
// bond: &Bond,
|
offer: &PublicOffer,
|
||||||
// mut musig_data: &MuSigData,
|
trade_data: BondSubmissionRequest,
|
||||||
// taker_config: &TraderSettings,
|
taker_config: &TraderSettings,
|
||||||
// ) -> Result<PartiallySignedTransaction> {
|
) -> Result<PartiallySignedTransaction> {
|
||||||
// let request = RequestOfferPsbt {
|
let request = OfferPsbtRequest {
|
||||||
// offer:
|
offer: offer.clone(),
|
||||||
// };
|
trade_data,
|
||||||
|
};
|
||||||
|
|
||||||
// let client = reqwest::blocking::Client::new();
|
let client = reqwest::blocking::Client::new();
|
||||||
// let res = client
|
let res = client
|
||||||
// .post(format!(
|
.post(format!(
|
||||||
// "{}{}",
|
"{}{}",
|
||||||
// taker_config.coordinator_endpoint, "/submit-taker-bond"
|
taker_config.coordinator_endpoint, "/submit-taker-bond"
|
||||||
// ))
|
))
|
||||||
// .json(self)
|
.json(&request)
|
||||||
// .send()?
|
.send()?
|
||||||
// .json::<OfferTakenResponse>()?;
|
.json::<OfferTakenResponse>()?;
|
||||||
// Ok(res)
|
|
||||||
// }
|
let psbt_bytes = hex::decode(res.trade_psbt_hex_to_sign)?;
|
||||||
|
let psbt = PartiallySignedTransaction::deserialize(&psbt_bytes)?;
|
||||||
|
Ok(psbt)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,33 @@
|
|||||||
|
use bdk::electrum_client::Request;
|
||||||
|
|
||||||
|
use crate::communication::api::{OfferPsbtRequest, RequestOfferPsbt};
|
||||||
|
|
||||||
use super::utils::*;
|
use super::utils::*;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
impl ActiveOffer {
|
impl ActiveOffer {
|
||||||
// pub fn take( tbd
|
pub fn take(
|
||||||
// trading_wallet: &TradingWallet,
|
trading_wallet: &TradingWallet,
|
||||||
// taker_config: &TraderSettings,
|
taker_config: &TraderSettings,
|
||||||
// offer: &PublicOffer,
|
offer: &PublicOffer,
|
||||||
// ) -> Result<ActiveOffer> {
|
) -> Result<ActiveOffer> {
|
||||||
// let bond_conditions: BondRequirementResponse = offer.take(taker_config)?;
|
// fetching the bond requirements for the requested Offer (amount, locking address)
|
||||||
// let (bond, mut musig_data, payout_address) =
|
let bond_conditions: BondRequirementResponse = offer.request_bond(taker_config)?;
|
||||||
// trading_wallet.trade_onchain_assembly(&bond_conditions, taker_config)?;
|
|
||||||
// let trading_psbt =
|
// 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<()> {
|
pub fn wait_on_maker(&self) -> Result<()> {
|
||||||
|
@ -57,7 +57,3 @@ impl Bond {
|
|||||||
Ok(psbt)
|
Ok(psbt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl BranchAndBoundCoinSelection
|
|
||||||
// pub fn new(size_of_change: u64) -> Self
|
|
||||||
// Create new instance with target size for change output
|
|
||||||
|
@ -63,12 +63,11 @@ impl TradingWallet {
|
|||||||
offer_conditions: &BondRequirementResponse,
|
offer_conditions: &BondRequirementResponse,
|
||||||
trader_config: &TraderSettings,
|
trader_config: &TraderSettings,
|
||||||
) -> Result<(PartiallySignedTransaction, MuSigData, AddressInfo)> {
|
) -> 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 bond = Bond::assemble(&self.wallet, &offer_conditions, trader_config)?;
|
||||||
let payout_address: AddressInfo =
|
let payout_address: AddressInfo =
|
||||||
trading_wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?;
|
trading_wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?;
|
||||||
let mut musig_data =
|
let musig_data = MuSigData::create(&trader_config.wallet_xprv, trading_wallet.secp_ctx())?;
|
||||||
MuSigData::create(&trader_config.wallet_xprv, trading_wallet.secp_ctx())?;
|
|
||||||
|
|
||||||
Ok((bond, musig_data, payout_address))
|
Ok((bond, musig_data, payout_address))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user