From 8c47edb3b5eb930284c0bc59feda4a55095a42c0 Mon Sep 17 00:00:00 2001 From: f321x Date: Fri, 2 Aug 2024 17:13:29 +0200 Subject: [PATCH] fix bond submission/api --- .vscode/settings.json | 3 ++- .../coordinator/src/communication/api.rs | 1 - taptrade-cli-demo/coordinator/src/coordinator/mod.rs | 3 --- taptrade-cli-demo/coordinator/src/database/mod.rs | 9 +++------ taptrade-cli-demo/coordinator/src/main.rs | 2 +- taptrade-cli-demo/coordinator/src/wallet/mod.rs | 5 ++--- taptrade-cli-demo/trader/src/communication/api.rs | 3 +-- taptrade-cli-demo/trader/src/communication/mod.rs | 3 ++- taptrade-cli-demo/trader/src/main.rs | 1 + taptrade-cli-demo/trader/src/trading/maker_utils.rs | 11 ++++++++++- taptrade-cli-demo/trader/src/trading/taker_utils.rs | 3 ++- taptrade-cli-demo/trader/src/wallet/bond.rs | 3 ++- 12 files changed, 26 insertions(+), 21 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index cd5ce5d..051dab9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,6 @@ "${workspaceFolder}/taptrade-cli-demo/trader/Cargo.toml", ], "nixEnvSelector.suggestion": true, - "nixEnvSelector.nixFile": "${workspaceFolder}/shell.nix" + "nixEnvSelector.nixFile": "${workspaceFolder}/shell.nix", + "lldb.showDisassembly": "never" } diff --git a/taptrade-cli-demo/coordinator/src/communication/api.rs b/taptrade-cli-demo/coordinator/src/communication/api.rs index a06e14c..f1b4ec6 100644 --- a/taptrade-cli-demo/coordinator/src/communication/api.rs +++ b/taptrade-cli-demo/coordinator/src/communication/api.rs @@ -66,7 +66,6 @@ pub struct PublicOffers { pub struct OfferTakenResponse { pub escrow_psbt_hex: String, pub escrow_output_descriptor: String, - pub escrow_tx_fee_address: String, pub escrow_amount_maker_sat: u64, pub escrow_amount_taker_sat: u64, pub escrow_fee_sat_per_participant: u64, diff --git a/taptrade-cli-demo/coordinator/src/coordinator/mod.rs b/taptrade-cli-demo/coordinator/src/coordinator/mod.rs index 058fea5..d4e399c 100755 --- a/taptrade-cli-demo/coordinator/src/coordinator/mod.rs +++ b/taptrade-cli-demo/coordinator/src/coordinator/mod.rs @@ -150,7 +150,6 @@ pub async fn handle_taker_bond( Ok(OfferTakenResponse { escrow_psbt_hex: escrow_output_data.escrow_psbt_hex, escrow_output_descriptor: escrow_output_data.escrow_output_descriptor, - escrow_tx_fee_address: escrow_output_data.escrow_tx_fee_address, escrow_amount_maker_sat: escrow_output_data.escrow_amount_maker_sat, escrow_amount_taker_sat: escrow_output_data.escrow_amount_taker_sat, escrow_fee_sat_per_participant: escrow_output_data.escrow_fee_sat_per_participant, @@ -165,7 +164,6 @@ pub async fn get_offer_status_maker( let EscrowPsbt { escrow_output_descriptor, - escrow_tx_fee_address, escrow_amount_maker_sat, escrow_amount_taker_sat, escrow_fee_sat_per_participant, @@ -186,7 +184,6 @@ pub async fn get_offer_status_maker( Ok(OfferTakenResponse { escrow_psbt_hex, escrow_output_descriptor, - escrow_tx_fee_address, escrow_amount_maker_sat, escrow_amount_taker_sat, escrow_fee_sat_per_participant, diff --git a/taptrade-cli-demo/coordinator/src/database/mod.rs b/taptrade-cli-demo/coordinator/src/database/mod.rs index 77ecb2c..aa42f5b 100644 --- a/taptrade-cli-demo/coordinator/src/database/mod.rs +++ b/taptrade-cli-demo/coordinator/src/database/mod.rs @@ -375,9 +375,9 @@ impl CoordinatorDB { "INSERT OR REPLACE INTO taken_offers (offer_id, robohash_maker, robohash_taker, is_buy_order, amount_sat, bond_ratio, offer_duration_ts, bond_address_maker, bond_address_taker, bond_amount_sat, bond_tx_hex_maker, bond_tx_hex_taker, payout_address_maker, payout_address_taker, taproot_pubkey_hex_maker, taproot_pubkey_hex_taker, musig_pub_nonce_hex_maker, musig_pubkey_hex_maker, - musig_pub_nonce_hex_taker, musig_pubkey_hex_taker, escrow_psbt_hex, escrow_output_descriptor, escrow_tx_fee_address, escrow_psbt_is_confirmed, escrow_ongoing, + musig_pub_nonce_hex_taker, musig_pubkey_hex_taker, escrow_psbt_hex, escrow_output_descriptor, escrow_psbt_is_confirmed, escrow_ongoing, escrow_taproot_pk_coordinator, escrow_amount_maker_sat, escrow_amount_taker_sat, escrow_fee_per_participant) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ) .bind(public_offer.offer_id) .bind(public_offer.robohash_maker) @@ -401,7 +401,6 @@ impl CoordinatorDB { .bind(trade_and_taker_info.trade_data.musig_pubkey_hex.clone()) .bind(&escrow_tx_data.escrow_output_descriptor) .bind(&escrow_tx_data.escrow_psbt_hex) - .bind(&escrow_tx_data.escrow_tx_fee_address) .bind(0) .bind(0) .bind(&escrow_tx_data.coordinator_xonly_escrow_pk) @@ -419,7 +418,7 @@ impl CoordinatorDB { offer_id_hex: &str, ) -> Result> { let offer = sqlx::query( - "SELECT escrow_output_descriptor, escrow_tx_fee_address, escrow_amount_maker_sat, + "SELECT escrow_output_descriptor, escrow_amount_maker_sat, escrow_amount_taker_sat, escrow_fee_per_participant, escrow_taproot_pk_coordinator FROM taken_offers WHERE offer_id = ?", ) @@ -431,7 +430,6 @@ impl CoordinatorDB { None => return Ok(None), }; let escrow_output_descriptor = offer.try_get::("escrow_output_descriptor")?; - let escrow_tx_fee_address = offer.try_get::("escrow_tx_fee_address")?; let escrow_amount_maker_sat = offer.try_get::("escrow_amount_maker_sat")? as u64; let escrow_amount_taker_sat = offer.try_get::("escrow_amount_taker_sat")? as u64; let escrow_fee_sat_per_participant = @@ -443,7 +441,6 @@ impl CoordinatorDB { Ok(Some(EscrowPsbt { escrow_psbt_hex, escrow_output_descriptor, - escrow_tx_fee_address, coordinator_xonly_escrow_pk, escrow_amount_maker_sat, escrow_amount_taker_sat, diff --git a/taptrade-cli-demo/coordinator/src/main.rs b/taptrade-cli-demo/coordinator/src/main.rs index 4f6e5bc..d24a0ac 100755 --- a/taptrade-cli-demo/coordinator/src/main.rs +++ b/taptrade-cli-demo/coordinator/src/main.rs @@ -32,7 +32,7 @@ pub struct Coordinator { #[tokio::main] async fn main() -> Result<()> { env_logger::builder() - .filter_module("coordinator", log::LevelFilter::Debug) + .filter_module("coordinator", log::LevelFilter::Trace) .filter_level(log::LevelFilter::Info) .init(); dotenv().ok(); diff --git a/taptrade-cli-demo/coordinator/src/wallet/mod.rs b/taptrade-cli-demo/coordinator/src/wallet/mod.rs index 4d7d767..8013bf3 100644 --- a/taptrade-cli-demo/coordinator/src/wallet/mod.rs +++ b/taptrade-cli-demo/coordinator/src/wallet/mod.rs @@ -45,7 +45,6 @@ pub struct CoordinatorWallet { pub struct EscrowPsbt { pub escrow_psbt_hex: String, pub escrow_output_descriptor: String, - pub escrow_tx_fee_address: String, pub coordinator_xonly_escrow_pk: String, pub escrow_amount_maker_sat: u64, pub escrow_amount_taker_sat: u64, @@ -126,7 +125,7 @@ impl CoordinatorWallet { bond_tx_hex: &str, requirements: &BondRequirements, ) -> Result<()> { - debug!("Validating bond in validate_bond_tx_hex()"); + debug!("Validating bond in validate_bond_tx_hex(): {}", bond_tx_hex); let dummy_monitoring_bond = MonitoringBond { bond_tx_hex: bond_tx_hex.to_string(), trade_id_hex: "0".to_string(), @@ -157,6 +156,7 @@ impl CoordinatorWallet { { let wallet = self.wallet.lock().await; for bond in bonds.as_ref().iter() { + trace!("Deserializing bond in validate_bonds()"); let tx: Transaction = deserialize(&hex::decode(&bond.bond_tx_hex)?)?; debug!("Validating bond in validate_bonds()"); // we need to test this with signed and invalid/unsigned transactions @@ -329,7 +329,6 @@ impl CoordinatorWallet { Ok(EscrowPsbt { escrow_psbt_hex: escrow_psbt.to_string(), escrow_output_descriptor, - escrow_tx_fee_address: escrow_coordinator_fee_address.to_string(), coordinator_xonly_escrow_pk: coordinator_escrow_pk.to_string(), escrow_amount_maker_sat, escrow_amount_taker_sat, diff --git a/taptrade-cli-demo/trader/src/communication/api.rs b/taptrade-cli-demo/trader/src/communication/api.rs index ee6d3c9..95b3415 100644 --- a/taptrade-cli-demo/trader/src/communication/api.rs +++ b/taptrade-cli-demo/trader/src/communication/api.rs @@ -43,14 +43,13 @@ pub struct OrderActivatedResponse { #[derive(Debug, Serialize)] pub struct OfferTakenRequest { pub robohash_hex: String, - pub order_id_hex: String, + pub offer_id_hex: String, } #[derive(Debug, Deserialize)] pub struct OfferTakenResponse { pub escrow_psbt_hex: String, pub escrow_output_descriptor: String, - pub escrow_tx_fee_address: String, pub escrow_amount_maker_sat: u64, pub escrow_amount_taker_sat: u64, pub escrow_fee_sat_per_participant: u64, diff --git a/taptrade-cli-demo/trader/src/communication/mod.rs b/taptrade-cli-demo/trader/src/communication/mod.rs index 4e7cc27..e9b88c2 100644 --- a/taptrade-cli-demo/trader/src/communication/mod.rs +++ b/taptrade-cli-demo/trader/src/communication/mod.rs @@ -136,9 +136,10 @@ impl OfferTakenResponse { offer: &ActiveOffer, trader_setup: &TraderSettings, ) -> Result> { + trace!("Polling offer status from coordinator."); let request = OfferTakenRequest { robohash_hex: trader_setup.robosats_robohash_hex.clone(), - order_id_hex: offer.offer_id_hex.clone(), + offer_id_hex: offer.offer_id_hex.clone(), }; let client = reqwest::blocking::Client::new(); let res = client diff --git a/taptrade-cli-demo/trader/src/main.rs b/taptrade-cli-demo/trader/src/main.rs index 298d76b..0abdb03 100644 --- a/taptrade-cli-demo/trader/src/main.rs +++ b/taptrade-cli-demo/trader/src/main.rs @@ -21,6 +21,7 @@ fn start_trade_pipeline(cli_input: &CliSettings) -> Result<()> { fn main() -> Result<()> { env_logger::builder() + .filter_module("trader", log::LevelFilter::Debug) .filter_level(log::LevelFilter::Info) .init(); diff --git a/taptrade-cli-demo/trader/src/trading/maker_utils.rs b/taptrade-cli-demo/trader/src/trading/maker_utils.rs index de57736..1bb789b 100644 --- a/taptrade-cli-demo/trader/src/trading/maker_utils.rs +++ b/taptrade-cli-demo/trader/src/trading/maker_utils.rs @@ -1,3 +1,8 @@ +use bdk::bitcoin::amount::serde::as_btc::opt::serialize; +use bdk::bitcoin::consensus::encode::serialize_hex; +use bdk::bitcoin::Transaction; +use serde::Serialize; + use super::utils::*; use super::*; @@ -14,9 +19,13 @@ impl ActiveOffer { let (psbt_inputs_hex_csv, escrow_change_address) = trading_wallet.get_escrow_psbt_inputs(offer_conditions.locking_amount_sat as i64)?; + debug!( + "Submitting maker bond: {:#?}", + hex::encode(bond.serialize()) + ); let bond_submission_request = BondSubmissionRequest { robohash_hex: maker_config.robosats_robohash_hex.clone(), - signed_bond_hex: bond.to_string(), + signed_bond_hex: serialize_hex(&bond.clone().extract_tx()), payout_address: payout_address.address.to_string(), musig_pub_nonce_hex: hex::encode(musig_data.nonce.get_pub_for_sharing()?.serialize()), musig_pubkey_hex: hex::encode(musig_data.public_key.to_string()), diff --git a/taptrade-cli-demo/trader/src/trading/taker_utils.rs b/taptrade-cli-demo/trader/src/trading/taker_utils.rs index db70b6b..2ca2f47 100644 --- a/taptrade-cli-demo/trader/src/trading/taker_utils.rs +++ b/taptrade-cli-demo/trader/src/trading/taker_utils.rs @@ -1,3 +1,4 @@ +use bdk::bitcoin::consensus::encode::serialize_hex; use bdk::electrum_client::Request; use crate::communication::api::{IsOfferReadyRequest, OfferPsbtRequest, PsbtSubmissionRequest}; @@ -27,7 +28,7 @@ impl ActiveOffer { let bond_submission_request = BondSubmissionRequest { robohash_hex: taker_config.robosats_robohash_hex.clone(), - signed_bond_hex: bond.to_string(), + signed_bond_hex: serialize_hex(&bond.clone().extract_tx()), payout_address: payout_address.address.to_string(), taproot_pubkey_hex: trading_wallet.taproot_pubkey.to_string(), musig_pub_nonce_hex: musig_data.nonce.get_pub_for_sharing()?.to_string(), diff --git a/taptrade-cli-demo/trader/src/wallet/bond.rs b/taptrade-cli-demo/trader/src/wallet/bond.rs index b05aa78..e052cdd 100644 --- a/taptrade-cli-demo/trader/src/wallet/bond.rs +++ b/taptrade-cli-demo/trader/src/wallet/bond.rs @@ -1,8 +1,9 @@ use anyhow::{anyhow, Result}; use bdk::bitcoin::address::{NetworkChecked, NetworkUnchecked}; +use bdk::bitcoin::amount::serde::as_btc::opt::deserialize; use bdk::bitcoin::psbt::PartiallySignedTransaction; -use bdk::bitcoin::ScriptBuf; use bdk::bitcoin::{Address, Network}; +use bdk::bitcoin::{ScriptBuf, Transaction}; use bdk::{ database::MemoryDatabase, wallet::coin_selection::BranchAndBoundCoinSelection, FeeRate, SignOptions, Wallet,