Skip to content

Commit

Permalink
Split fee (#118)
Browse files Browse the repository at this point in the history
* Split fee logic

* Change status on Dispute

* Fix from_utf8() bug

* Updated sqlx data

generated by: cargo sqlx prepare
  • Loading branch information
grunch authored Sep 26, 2023
1 parent 1606446 commit 626a675
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
36 changes: 18 additions & 18 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@
},
"query": "\n UPDATE orders\n SET\n master_seller_pubkey = ?1\n WHERE id = ?2\n "
},
"1a4f24e4d6c6447e7f6b9c7207bd12bc23e87f37d2c8676bffd8f4eb86c31405": {
"301fbda63c61bf75d60bc330d5b1aacae80248c253a2fe9e97f5a74a7acafe75": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
}
},
"query": "\n UPDATE orders\n SET\n seller_dispute = ?1\n WHERE id = ?2\n "
"query": "\n UPDATE orders\n SET\n buyer_sent_rate = ?1\n WHERE id = ?2\n "
},
"301fbda63c61bf75d60bc330d5b1aacae80248c253a2fe9e97f5a74a7acafe75": {
"4465f33fba7d31a7154b9710438d9319d02018e873a54d720cda2f7eb07aece6": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
"Right": 6
}
},
"query": "\n UPDATE orders\n SET\n buyer_sent_rate = ?1\n WHERE id = ?2\n "
"query": "\n UPDATE orders\n SET\n buyer_pubkey = ?1,\n seller_pubkey = ?2,\n status = ?3,\n preimage = ?4,\n hash = ?5\n WHERE id = ?6\n "
},
"4465f33fba7d31a7154b9710438d9319d02018e873a54d720cda2f7eb07aece6": {
"45bc1577516315db37be5481594323e87e6bd406f2248b5b44015eaf3ff34a14": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 6
"Right": 2
}
},
"query": "\n UPDATE orders\n SET\n buyer_pubkey = ?1,\n seller_pubkey = ?2,\n status = ?3,\n preimage = ?4,\n hash = ?5\n WHERE id = ?6\n "
"query": "\n UPDATE orders\n SET\n buyer_dispute = ?1,\n status = 'Dispute'\n WHERE id = ?2\n "
},
"567a4d6906624fb39ff9719c0f15626b42d215d6648b6274bda052cfc9f48102": {
"describe": {
Expand All @@ -50,45 +50,45 @@
},
"query": "\n UPDATE orders\n SET\n seller_sent_rate = ?1\n WHERE id = ?2\n "
},
"77ea98f6af16fa6e5a7d604965593700c563f88d88cb10b348bdc4200c87ad1d": {
"6850cc0e71b2fc6d8632e96513f661de41349ee483172db98ddea8e5999d5329": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
}
},
"query": "\n UPDATE orders\n SET\n buyer_pubkey = ?1\n WHERE id = ?2\n "
"query": "\n UPDATE orders\n SET\n seller_dispute = ?1,\n status = 'Dispute'\n WHERE id = ?2\n "
},
"878c89a9ccf7cf33910f32a9dd2c09f45364dd744dc9e5749a318825343be542": {
"77ea98f6af16fa6e5a7d604965593700c563f88d88cb10b348bdc4200c87ad1d": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 4
"Right": 2
}
},
"query": "\n UPDATE orders\n SET\n cancel_initiator_pubkey = ?1,\n buyer_cooperativecancel = ?2,\n seller_cooperativecancel = ?3\n WHERE id = ?4\n "
"query": "\n UPDATE orders\n SET\n buyer_pubkey = ?1\n WHERE id = ?2\n "
},
"89253158dce7eb8ed1c4fce2120418909fde010398815c5e673fb7d406dc5b4f": {
"878c89a9ccf7cf33910f32a9dd2c09f45364dd744dc9e5749a318825343be542": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
"Right": 4
}
},
"query": "\n UPDATE orders\n SET\n buyer_invoice = ?1\n WHERE id = ?2\n "
"query": "\n UPDATE orders\n SET\n cancel_initiator_pubkey = ?1,\n buyer_cooperativecancel = ?2,\n seller_cooperativecancel = ?3\n WHERE id = ?4\n "
},
"900597cf1e8ed114cb383856d398ffb23b1d15e48c1708fc2b40cfcc649df9fc": {
"89253158dce7eb8ed1c4fce2120418909fde010398815c5e673fb7d406dc5b4f": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
}
},
"query": "\n UPDATE orders\n SET\n buyer_dispute = ?1\n WHERE id = ?2\n "
"query": "\n UPDATE orders\n SET\n buyer_invoice = ?1\n WHERE id = ?2\n "
},
"97414183485e04346f420960beed77912ebe76089f58d30f0f526e232ce4be25": {
"describe": {
Expand Down
8 changes: 5 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub async fn update_order_event_id_status(
let mostro_settings = Settings::get_mostro();
let status = status.to_string();
// We calculate the bot fee
let fee = mostro_settings.fee / 2.0;
let fee = mostro_settings.fee;
let fee = fee * amount as f64;
let fee = fee.round() as i64;

Expand Down Expand Up @@ -484,7 +484,8 @@ pub async fn update_order_buyer_dispute(
r#"
UPDATE orders
SET
buyer_dispute = ?1
buyer_dispute = ?1,
status = 'Dispute'
WHERE id = ?2
"#,
buyer_dispute,
Expand All @@ -507,7 +508,8 @@ pub async fn update_order_seller_dispute(
r#"
UPDATE orders
SET
seller_dispute = ?1
seller_dispute = ?1,
status = 'Dispute'
WHERE id = ?2
"#,
seller_dispute,
Expand Down
15 changes: 11 additions & 4 deletions src/lightning/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub fn decode_invoice(payment_request: &str) -> Result<Invoice, MostroError> {
Ok(invoice)
}

/// Verify if a buyer invoice is valid
/// Verify if a buyer invoice is valid,
/// if the invoice have amount we check if the amount minus fee is the same
pub fn is_valid_invoice(
payment_request: &str,
amount: Option<u64>,
Expand All @@ -23,20 +24,26 @@ pub fn is_valid_invoice(
let mostro_settings = Settings::get_mostro();
let ln_settings = Settings::get_ln();

let amount_msat = invoice.amount_milli_satoshis().unwrap_or(0) / 1000;
let amount_sat = invoice.amount_milli_satoshis().unwrap_or(0) / 1000;
let fee = fee.unwrap_or(0);
// We receive the total order fee, we divide by 2 because we split the fee
let fee = fee as f64 / 2.0;
let fee = fee.round() as u64;

if let Some(amt) = amount {
if amount_msat > 0 && amount_msat != (amt - fee) {
if amount_sat > 0 && amount_sat != (amt - fee) {
return Err(MostroError::WrongAmountError);
}
}
if amount_msat > 0 && amount_msat < mostro_settings.min_payment_amount as u64 {

if amount_sat > 0 && amount_sat < mostro_settings.min_payment_amount as u64 {
return Err(MostroError::MinAmountError);
}

if invoice.is_expired() {
return Err(MostroError::InvoiceExpiredError);
}

let parsed = payment_request.parse::<SignedRawInvoice>()?;

let (parsed_invoice, _, _) = parsed.into_parts();
Expand Down
6 changes: 5 additions & 1 deletion src/lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ impl LndConnector {
let invoice = decode_invoice(payment_request).unwrap();
let payment_hash = invoice.payment_hash();
let payment_hash = payment_hash.to_vec();
let hash = String::from_utf8(payment_hash.clone()).unwrap();
let hash: String = payment_hash
.clone()
.iter()
.map(|b| format!("{:02x}", b))
.collect();
let mostro_settings = Settings::get_mostro();

// We need to set a max fee amount
Expand Down

0 comments on commit 626a675

Please sign in to comment.