refactoring

This commit is contained in:
Felix
2024-06-12 13:01:31 +00:00
parent 811c2231e9
commit 0ad4b5d761
4 changed files with 73 additions and 53 deletions

View File

@ -16,8 +16,9 @@ pub struct OrderRequest {
#[derive(Serialize)] #[derive(Serialize)]
pub struct BondSubmissionRequest { pub struct BondSubmissionRequest {
pub serialized_bond_tx_hex: String,
pub robohash_base91: String, pub robohash_base91: String,
pub payout_address: String, pub signed_bond_base91: String,
pub frost_public_nonce_hex: String, pub payout_address: String, // does this make sense here?
pub musig_pub_nonce_base91: String,
pub musig_pubkey_base91: String,
} }

View File

@ -0,0 +1,43 @@
use crate::cli::TraderSettings;
use crate::communication::api::OfferCreationResponse;
use crate::wallet::{
bond::Bond,
musig2::{MuSigData, MusigNonce},
TradingWallet,
};
use anyhow::Result;
use bdk::database::MemoryDatabase;
// use bdk::{
// bitcoin::block,
// blockchain::{Blockchain, ElectrumBlockchain},
// electrum_client::Client,
// wallet::AddressIndex::LastUnused,
// Wallet,
// };
pub struct ActiveOffer {}
impl ActiveOffer {
pub fn create(
trading_wallet: &TradingWallet,
maker_config: &TraderSettings,
) -> Result<ActiveOffer> {
let offer_conditions = OfferCreationResponse::fetch(maker_config)?;
let trading_wallet = &trading_wallet.wallet;
let offer_conditions = OfferCreationResponse {
// hardcoded for testing, locking_address is owned by .env xprv
locking_amount: 90000,
bond_address: "tb1pfdvgfzwp8vhmelpv8w9kezz7nsmxw68jz6yehgze6mzx0t6r9t2qv9ynmm"
.to_string(),
};
let bond = Bond::assemble(trading_wallet, &offer_conditions, maker_config)?;
let payout_pubkey = trading_wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?;
let musig_data = MuSigData::create(&maker_config.wallet_xprv, trading_wallet.secp_ctx())?;
Ok(ActiveOffer {})
}
}

View File

@ -1,43 +1,19 @@
mod maker_utils;
use self::maker_utils::ActiveOffer;
use crate::cli::TraderSettings; use crate::cli::TraderSettings;
use crate::communication::api::OfferCreationResponse; use crate::wallet::TradingWallet;
use crate::wallet::{
bond::Bond,
load_wallet,
musig2::{MuSigData, MusigNonce},
};
use anyhow::Result; use anyhow::Result;
use bdk::{
bitcoin::block,
blockchain::{Blockchain, ElectrumBlockchain},
electrum_client::Client,
wallet::AddressIndex::LastUnused,
};
pub fn run_maker(maker_config: &TraderSettings) -> Result<()> { pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
let blockchain = ElectrumBlockchain::from(Client::new(&maker_config.electrum_endpoint)?); let wallet = TradingWallet::load_wallet(maker_config)?; // initialize the wallet with xprv
// let offer_conditions = OfferCreationResponse::fetch(maker_config)?;
let offer_conditions = OfferCreationResponse {
// hardcoded for testing, locking_address is owned by .env xprv
locking_amount: 90000,
bond_address: "tb1pfdvgfzwp8vhmelpv8w9kezz7nsmxw68jz6yehgze6mzx0t6r9t2qv9ynmm".to_string(),
};
let wallet = load_wallet(maker_config, &blockchain)?; // initialize the wallet with xprv
let bond = Bond::assemble(&wallet, &offer_conditions, maker_config)?; // assemble the Bond transaction for offer creation
// blockchain.broadcast(&bond.extract_tx())?; // publish bond to be mined for testing
let payout_pubkey = wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?;
let musig = MuSigData::create(&maker_config.wallet_xprv, wallet.secp_ctx())?;
let offer = ActiveOffer::create(&wallet, maker_config)?;
// dbg!(&bond.extract_tx().txid()); // dbg!(&bond.extract_tx().txid());
Ok(()) Ok(())
} }
pub fn run_taker(taker_config: &TraderSettings) -> Result<()> { pub fn run_taker(taker_config: &TraderSettings) -> Result<()> {
let blockchain = ElectrumBlockchain::from(Client::new(&taker_config.electrum_endpoint)?);
// panic!("Taker to be implemented!"); // panic!("Taker to be implemented!");
Ok(()) Ok(())

View File

@ -18,10 +18,10 @@ use bdk::{
use std::str::FromStr; use std::str::FromStr;
use wallet_utils::get_seed; use wallet_utils::get_seed;
// pub struct WalletDescriptors { pub struct TradingWallet {
// pub descriptor: Bip86<ExtendedPrivKey>, pub wallet: Wallet<MemoryDatabase>,
// pub change_descriptor: Option<Bip86<ExtendedPrivKey>>, pub backend: ElectrumBlockchain,
// } }
pub fn get_wallet_xprv(xprv_input: Option<String>) -> Result<ExtendedPrivKey> { pub fn get_wallet_xprv(xprv_input: Option<String>) -> Result<ExtendedPrivKey> {
let xprv: ExtendedPrivKey; let xprv: ExtendedPrivKey;
@ -37,21 +37,21 @@ pub fn get_wallet_xprv(xprv_input: Option<String>) -> Result<ExtendedPrivKey> {
Ok(xprv) Ok(xprv)
} }
pub fn load_wallet( impl TradingWallet {
trader_config: &TraderSettings, pub fn load_wallet(trader_config: &TraderSettings) -> Result<TradingWallet> {
blockchain: &ElectrumBlockchain, let backend = ElectrumBlockchain::from(Client::new(&trader_config.electrum_endpoint)?);
) -> Result<Wallet<MemoryDatabase>> { let wallet = Wallet::new(
let wallet = Wallet::new( Bip86(trader_config.wallet_xprv.clone(), KeychainKind::External),
Bip86(trader_config.wallet_xprv.clone(), KeychainKind::External), Some(Bip86(
Some(Bip86( trader_config.wallet_xprv.clone(),
trader_config.wallet_xprv.clone(), KeychainKind::Internal,
KeychainKind::Internal, )),
)), bitcoin::Network::Testnet,
bitcoin::Network::Testnet, MemoryDatabase::default(), // non-permanent storage
MemoryDatabase::default(), // non-permanent storage )?;
)?;
wallet.sync(blockchain, SyncOptions::default())?; wallet.sync(&backend, SyncOptions::default())?;
println!("Balance: {} SAT", wallet.get_balance()?); dbg!("Balance: {} SAT", wallet.get_balance()?);
Ok(wallet) Ok(TradingWallet { wallet, backend })
}
} }