Skip to content

Commit

Permalink
fix: rename and fix structs visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse authored Mar 26, 2024
1 parent a853ac8 commit e866c3b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
42 changes: 20 additions & 22 deletions lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use boltz_client::util::error::S5Error;
use lwk_signer::SwSigner;
use lwk_wollet::{ElectrumUrl, ElementsNetwork};

#[derive(Debug)]
pub enum Network {
Liquid,
LiquidTestnet,
Expand All @@ -16,6 +17,7 @@ impl From<Network> for ElementsNetwork {
}
}

#[derive(Debug)]
pub struct WalletOptions {
pub signer: SwSigner,
pub network: Network,
Expand All @@ -29,28 +31,31 @@ pub struct WalletOptions {
}

#[derive(Debug)]
pub struct SwapLbtcResponse {
pub id: String,
pub invoice: String,
pub struct ReceivePaymentRequest {
pub invoice_amount_sat: Option<u64>,
pub onchain_amount_sat: Option<u64>,
}

pub enum SwapStatus {
Created,
Mempool,
Completed,
#[derive(Debug)]
pub struct ReceivePaymentResponse {
pub id: String,
pub invoice: String,
}

pub struct ReceivePaymentRequest {
pub invoice_amount_sat: Option<u64>,
pub onchain_amount_sat: Option<u64>,
#[derive(Debug)]
pub struct PreparePaymentResponse {
pub id: String,
pub funding_amount: u64,
pub funding_address: String,
}

#[derive(Debug)]
pub struct SendPaymentResponse {
pub txid: String,
}

#[derive(thiserror::Error, Debug)]
pub enum SwapError {
pub enum PaymentError {
#[error("Could not contact Boltz servers: {err}")]
ServersUnreachable { err: String },

Expand Down Expand Up @@ -79,15 +84,15 @@ pub enum SwapError {
BoltzGeneric { err: String },
}

impl From<S5Error> for SwapError {
impl From<S5Error> for PaymentError {
fn from(err: S5Error) -> Self {
match err.kind {
boltz_client::util::error::ErrorKind::Network
| boltz_client::util::error::ErrorKind::BoltzApi => {
SwapError::ServersUnreachable { err: err.message }
PaymentError::ServersUnreachable { err: err.message }
}
boltz_client::util::error::ErrorKind::Input => SwapError::BadResponse,
_ => SwapError::BoltzGeneric { err: err.message },
boltz_client::util::error::ErrorKind::Input => PaymentError::BadResponse,
_ => PaymentError::BoltzGeneric { err: err.message },
}
}
}
Expand Down Expand Up @@ -152,10 +157,3 @@ impl From<OngoingSwap> for Payment {
}
}
}

#[derive(Debug)]
pub struct PreparePaymentResponse {
pub id: String,
pub funding_amount: u64,
pub funding_address: String,
}
42 changes: 21 additions & 21 deletions lib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use lwk_wollet::{
};

use crate::{
persist::Persister, Network, OngoingSwap, Payment, PaymentType, PreparePaymentResponse,
ReceivePaymentRequest, SendPaymentResponse, SwapError, SwapLbtcResponse, WalletInfo,
WalletOptions,
persist::Persister, Network, OngoingSwap, Payment, PaymentError, PaymentType,
PreparePaymentResponse, ReceivePaymentRequest, ReceivePaymentResponse, SendPaymentResponse,
WalletInfo, WalletOptions,
};

// To avoid sendrawtransaction error "min relay fee not met"
Expand Down Expand Up @@ -213,24 +213,24 @@ impl Wallet {
Ok(txid.to_string())
}

pub fn prepare_payment(&self, invoice: &str) -> Result<PreparePaymentResponse, SwapError> {
pub fn prepare_payment(&self, invoice: &str) -> Result<PreparePaymentResponse, PaymentError> {
let client = self.boltz_client();
let invoice = invoice
.trim()
.parse::<Bolt11Invoice>()
.map_err(|_| SwapError::InvalidInvoice)?;
.map_err(|_| PaymentError::InvalidInvoice)?;

let lbtc_pair = client.get_pairs()?.get_lbtc_pair()?;

let amount_sat = invoice
.amount_milli_satoshis()
.ok_or(SwapError::AmountOutOfRange)?
.ok_or(PaymentError::AmountOutOfRange)?
/ 1000;

lbtc_pair
.limits
.within(amount_sat)
.map_err(|_| SwapError::AmountOutOfRange)?;
.map_err(|_| PaymentError::AmountOutOfRange)?;

let swap_response = client.create_swap(CreateSwapRequest::new_lbtc_submarine(
&lbtc_pair.hash,
Expand All @@ -248,7 +248,7 @@ impl Wallet {
amount_sat,
funding_address: funding_address.clone(),
}])
.map_err(|_| SwapError::PersistError)?;
.map_err(|_| PaymentError::PersistError)?;

Ok(PreparePaymentResponse {
id,
Expand All @@ -260,16 +260,16 @@ impl Wallet {
pub fn send_payment(
&self,
res: &PreparePaymentResponse,
) -> Result<SendPaymentResponse, SwapError> {
) -> Result<SendPaymentResponse, PaymentError> {
let signer = AnySigner::Software(self.get_signer());

let txid = self
.sign_and_send(&[signer], None, &res.funding_address, res.funding_amount)
.map_err(|_| SwapError::SendError)?;
.map_err(|_| PaymentError::SendError)?;

self.swap_persister
.resolve_ongoing_swap(&res.id)
.map_err(|_| SwapError::PersistError)?;
.map_err(|_| PaymentError::PersistError)?;

Ok(SendPaymentResponse { txid })
}
Expand All @@ -280,12 +280,12 @@ impl Wallet {
redeem_script: &str,
blinding_key: &str,
absolute_fees: Option<u64>,
) -> Result<String, SwapError> {
) -> Result<String, PaymentError> {
let network_config = &self.get_network_config();
let mut rev_swap_tx = LBtcSwapTx::new_claim(
LBtcSwapScript::reverse_from_str(redeem_script, blinding_key)?,
self.address()
.map_err(|_| SwapError::WalletError)?
.map_err(|_| PaymentError::WalletError)?
.to_string(),
network_config,
)?;
Expand All @@ -309,11 +309,11 @@ impl Wallet {
pub fn receive_payment(
&self,
req: ReceivePaymentRequest,
) -> Result<SwapLbtcResponse, SwapError> {
) -> Result<ReceivePaymentResponse, PaymentError> {
let mut amount_sat = req
.onchain_amount_sat
.or(req.invoice_amount_sat)
.ok_or(SwapError::AmountOutOfRange)?;
.ok_or(PaymentError::AmountOutOfRange)?;

let client = self.boltz_client();

Expand All @@ -322,15 +322,15 @@ impl Wallet {
lbtc_pair
.limits
.within(amount_sat)
.map_err(|_| SwapError::AmountOutOfRange)?;
.map_err(|_| PaymentError::AmountOutOfRange)?;

let mnemonic = self.signer.mnemonic();
let swap_key =
SwapKey::from_reverse_account(&mnemonic.to_string(), "", self.get_chain(), 0)?;
let lsk = LiquidSwapKey::from(swap_key);

let preimage = Preimage::new();
let preimage_str = preimage.to_string().ok_or(SwapError::InvalidPreimage)?;
let preimage_str = preimage.to_string().ok_or(PaymentError::InvalidPreimage)?;
let preimage_hash = preimage.sha256.to_string();

let swap_response = if req.onchain_amount_sat.is_some() {
Expand Down Expand Up @@ -358,12 +358,12 @@ impl Wallet {
// Double check that the generated invoice includes our data
// https://docs.boltz.exchange/v/api/dont-trust-verify#lightning-invoice-verification
if invoice.payment_hash().to_string() != preimage_hash {
return Err(SwapError::InvalidInvoice);
return Err(PaymentError::InvalidInvoice);
};

let invoice_amount_sat = invoice
.amount_milli_satoshis()
.ok_or(SwapError::InvalidInvoice)?
.ok_or(PaymentError::InvalidInvoice)?
/ 1000;

self.swap_persister
Expand All @@ -380,9 +380,9 @@ impl Wallet {
- CLAIM_ABSOLUTE_FEES
),
}]))
.map_err(|_| SwapError::PersistError)?;
.map_err(|_| PaymentError::PersistError)?;

Ok(SwapLbtcResponse {
Ok(ReceivePaymentResponse {
id: swap_id,
invoice: invoice.to_string(),
})
Expand Down

0 comments on commit e866c3b

Please sign in to comment.