mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-12-24 05:55:38 +00:00
taker implementation
This commit is contained in:
@ -14,7 +14,7 @@ pub struct OrderRequest {
|
||||
// coordinator answer to maker step 1
|
||||
// direct Json answer to step 1 (same request)
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct OfferCreationResponse {
|
||||
pub struct BondRequirementResponse {
|
||||
pub bond_address: String, // address the bond ha/workspaces/taptrade-core/taptrade-cli-demo/trader/src/communications to be locked to
|
||||
pub locking_amount_sat: u64, // min amount of the bond output in sat
|
||||
}
|
||||
@ -48,7 +48,7 @@ pub struct OfferTakenResponse {
|
||||
pub trade_psbt_hex_to_sign: String,
|
||||
}
|
||||
|
||||
// Taker structures
|
||||
// Taker structures //
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct OffersRequest {
|
||||
@ -57,7 +57,7 @@ pub struct OffersRequest {
|
||||
pub amount_max_sat: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct PublicOffer {
|
||||
pub amount_sat: u64,
|
||||
pub offer_id_hex: String,
|
||||
|
||||
@ -3,12 +3,12 @@ pub mod taker_requests;
|
||||
|
||||
use crate::{
|
||||
cli::{OfferType, TraderSettings},
|
||||
trading::maker_utils::ActiveOffer,
|
||||
trading::utils::ActiveOffer,
|
||||
wallet::{bond::Bond, musig2::MuSigData},
|
||||
};
|
||||
use anyhow::{anyhow, Result};
|
||||
use api::{
|
||||
BondSubmissionRequest, OfferCreationResponse, OfferTakenRequest, OfferTakenResponse,
|
||||
BondRequirementResponse, BondSubmissionRequest, OfferTakenRequest, OfferTakenResponse,
|
||||
OrderActivatedResponse, OrderRequest,
|
||||
};
|
||||
use bdk::bitcoin::consensus::encode::serialize_hex;
|
||||
@ -18,7 +18,7 @@ use bdk::{
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
impl OfferCreationResponse {
|
||||
impl BondRequirementResponse {
|
||||
fn _format_request(trader_setup: &TraderSettings) -> OrderRequest {
|
||||
let amount: u64;
|
||||
let is_buy_order = match &trader_setup.trade_type {
|
||||
@ -41,7 +41,7 @@ impl OfferCreationResponse {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fetch(trader_setup: &TraderSettings) -> Result<OfferCreationResponse> {
|
||||
pub fn fetch(trader_setup: &TraderSettings) -> Result<BondRequirementResponse> {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let res = client
|
||||
.post(format!(
|
||||
@ -50,7 +50,7 @@ impl OfferCreationResponse {
|
||||
))
|
||||
.json(&Self::_format_request(trader_setup))
|
||||
.send()?
|
||||
.json::<OfferCreationResponse>()?;
|
||||
.json::<BondRequirementResponse>()?;
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ use anyhow::Context;
|
||||
use super::{api::*, *};
|
||||
|
||||
impl PublicOffers {
|
||||
// fetch a list of all publicly available offers on the coordinator fitting the requested range and type
|
||||
pub fn fetch(taker_config: &TraderSettings) -> Result<PublicOffers> {
|
||||
let amount = taker_config.trade_type.value();
|
||||
let request = OffersRequest {
|
||||
@ -23,6 +24,7 @@ impl PublicOffers {
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// ask the user to select a offer to take on the CLI
|
||||
pub fn ask_user_to_select(&self) -> Result<&PublicOffer> {
|
||||
for (index, offer) in self.offers.as_ref().unwrap().iter().enumerate() {
|
||||
println!(
|
||||
@ -39,3 +41,18 @@ impl PublicOffers {
|
||||
Ok(&self.offers.as_ref().unwrap()[index])
|
||||
}
|
||||
}
|
||||
|
||||
impl PublicOffer {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,46 +1,21 @@
|
||||
use crate::cli::TraderSettings;
|
||||
use crate::communication::api::{
|
||||
BondSubmissionRequest, OfferCreationResponse, OfferTakenRequest, OfferTakenResponse,
|
||||
};
|
||||
use crate::wallet::{
|
||||
bond::Bond,
|
||||
musig2::{MuSigData, MusigNonce},
|
||||
TradingWallet,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use bdk::bitcoin::amount::serde::as_btc::deserialize;
|
||||
use bdk::{
|
||||
bitcoin::psbt::PartiallySignedTransaction, database::MemoryDatabase, wallet::AddressInfo,
|
||||
};
|
||||
use std::{thread, time::Duration};
|
||||
|
||||
#[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,
|
||||
}
|
||||
use super::utils::*;
|
||||
use super::*;
|
||||
|
||||
impl ActiveOffer {
|
||||
pub fn create(
|
||||
trading_wallet: &TradingWallet,
|
||||
maker_config: &TraderSettings,
|
||||
) -> Result<ActiveOffer> {
|
||||
let trading_wallet = &trading_wallet.wallet;
|
||||
|
||||
let offer_conditions = OfferCreationResponse::fetch(maker_config)?;
|
||||
let offer_conditions = BondRequirementResponse::fetch(maker_config)?;
|
||||
// let offer_conditions = OfferCreationResponse {
|
||||
// // hardcoded for testing, locking_address is owned by .env xprv
|
||||
// locking_amount_sat: 90000,
|
||||
// bond_address: "tb1pfdvgfzwp8vhmelpv8w9kezz7nsmxw68jz6yehgze6mzx0t6r9t2qv9ynmm"
|
||||
// .to_string(),
|
||||
// };
|
||||
let bond = Bond::assemble(trading_wallet, &offer_conditions, maker_config)?;
|
||||
let payout_address = trading_wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?;
|
||||
let mut musig_data =
|
||||
MuSigData::create(&maker_config.wallet_xprv, trading_wallet.secp_ctx())?;
|
||||
|
||||
let (bond, mut musig_data, payout_address) =
|
||||
Self::onchain_assembly(trading_wallet, &offer_conditions, maker_config)?;
|
||||
let submission_result = BondSubmissionRequest::send(
|
||||
&maker_config.robosats_robohash_hex,
|
||||
&bond,
|
||||
|
||||
@ -1,8 +1,26 @@
|
||||
pub mod maker_utils;
|
||||
pub mod taker_utils;
|
||||
pub mod utils;
|
||||
|
||||
use self::maker_utils::ActiveOffer;
|
||||
use crate::{cli::TraderSettings, communication::api::PublicOffers, wallet::TradingWallet};
|
||||
use self::utils::ActiveOffer;
|
||||
use crate::{
|
||||
cli::TraderSettings,
|
||||
communication::api::{
|
||||
BondRequirementResponse, BondSubmissionRequest, OfferTakenRequest, OfferTakenResponse,
|
||||
PublicOffer, PublicOffers,
|
||||
},
|
||||
wallet::{
|
||||
bond::Bond,
|
||||
musig2::{MuSigData, MusigNonce},
|
||||
TradingWallet,
|
||||
},
|
||||
};
|
||||
use anyhow::Result;
|
||||
use bdk::{
|
||||
bitcoin::{amount::serde::as_btc::deserialize, psbt::PartiallySignedTransaction},
|
||||
database::MemoryDatabase,
|
||||
wallet::AddressInfo,
|
||||
};
|
||||
use std::{thread, time::Duration};
|
||||
|
||||
pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
|
||||
@ -16,7 +34,7 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
|
||||
}
|
||||
|
||||
pub fn run_taker(taker_config: &TraderSettings) -> Result<()> {
|
||||
let wallet = TradingWallet::load_wallet(maker_config)?;
|
||||
let wallet = TradingWallet::load_wallet(taker_config)?;
|
||||
let mut available_offers = PublicOffers::fetch(taker_config)?;
|
||||
|
||||
while let None = available_offers.offers {
|
||||
@ -24,7 +42,7 @@ pub fn run_taker(taker_config: &TraderSettings) -> Result<()> {
|
||||
thread::sleep(Duration::from_secs(10));
|
||||
available_offers = PublicOffers::fetch(taker_config)?;
|
||||
}
|
||||
let selected_offer = available_offers.ask_user_to_select()?;
|
||||
let selected_offer: &PublicOffer = available_offers.ask_user_to_select()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
use super::utils::*;
|
||||
use super::*;
|
||||
|
||||
impl ActiveOffer {
|
||||
pub fn take(
|
||||
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) =
|
||||
Self::onchain_assembly(trading_wallet, &bond_conditions, taker_config)?;
|
||||
}
|
||||
}
|
||||
@ -1 +1,28 @@
|
||||
use super::maker_utils::*;
|
||||
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,
|
||||
}
|
||||
|
||||
impl ActiveOffer {
|
||||
pub fn onchain_assembly(
|
||||
trading_wallet: &TradingWallet,
|
||||
offer_conditions: &BondRequirementResponse,
|
||||
trader_config: &TraderSettings,
|
||||
) -> Result<(PartiallySignedTransaction, MuSigData, AddressInfo)> {
|
||||
let trading_wallet = &trading_wallet.wallet;
|
||||
let bond = Bond::assemble(trading_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())?;
|
||||
|
||||
Ok((bond, musig_data, payout_address))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user