Skip to content

Commit d4c484a

Browse files
authored
add sat conversion for bill payment link and add timestamps to light … (#396)
* add sat conversion for bill payment link and add timestamps to light bill * fix lint and add to full bill * remove check from payment
1 parent 7ba1b32 commit d4c484a

File tree

7 files changed

+54
-26
lines changed

7 files changed

+54
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ lettre = { version = "0.11.10", features = [
6060
"tokio1-native-tls",
6161
"file-transport",
6262
] }
63+
rust_decimal = { version = "1.35.0", default-features = false, features = ["std"] }
6364
nostr-sdk = { version = "0.38.0", features = ["nip59"] }
6465
uuid = { version = "1.11.0", features = ["v4"] }
6566
infer = { version = "0.16", default-features = false }

src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ pub const PAYMENT_DEADLINE_SECONDS: u64 = 86400 * 2; // 2 days
1616
pub const ACCEPT_DEADLINE_SECONDS: u64 = 86400 * 2; // 2 days
1717
pub const RECOURSE_DEADLINE_SECONDS: u64 = 86400 * 2; // 2 days
1818

19+
// Currency
20+
pub const SAT_TO_BTC_RATE: i64 = 100_000_000;
21+
1922
// Validation
2023
pub const MAX_FILE_SIZE_BYTES: usize = 1_000_000; // ~1 MB
2124
pub const MAX_FILE_NAME_CHARACTERS: usize = 200;

src/external/bitcoin.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::CONFIG;
1+
use crate::{util, CONFIG};
22
use async_trait::async_trait;
33
use bitcoin::{secp256k1::Scalar, Network};
44
use serde::Deserialize;
@@ -167,7 +167,8 @@ impl BitcoinClientApi for BitcoinClient {
167167
}
168168

169169
fn generate_link_to_pay(&self, address: &str, sum: u64, message: &str) -> String {
170-
let link = format!("bitcoin:{}?amount={}&message={}", address, sum, message);
170+
let btc_sum = util::currency::sat_to_btc(sum);
171+
let link = format!("bitcoin:{}?amount={}&message={}", address, btc_sum, message);
171172
link
172173
}
173174

src/service/bill_service.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,8 @@ impl BillService {
10761076
Ok(BitcreditBillToReturn {
10771077
id: bill.id,
10781078
time_of_drawing,
1079+
time_of_maturity: util::date::date_string_to_i64_timestamp(&bill.maturity_date, None)
1080+
.unwrap_or(0) as u64,
10791081
country_of_issuing: bill.country_of_issuing,
10801082
city_of_issuing: bill.city_of_issuing,
10811083
drawee: bill.drawee,
@@ -1121,29 +1123,20 @@ impl BillService {
11211123
.get_last_version_bill(&chain, &bill_keys, identity)
11221124
.await?;
11231125

1124-
// We only check payment, if the maturity date hasn't expired
1125-
if let Some(maturity_date_timestamp) =
1126-
util::date::date_string_to_i64_timestamp(&bill.maturity_date, None)
1126+
let holder_public_key = match bill.endorsee {
1127+
None => &bill.payee.node_id,
1128+
Some(ref endorsee) => &endorsee.node_id,
1129+
};
1130+
let address_to_pay = self
1131+
.bitcoin_client
1132+
.get_address_to_pay(&bill_keys.public_key, holder_public_key)?;
1133+
if let Ok((paid, sum)) = self
1134+
.bitcoin_client
1135+
.check_if_paid(&address_to_pay, bill.sum)
1136+
.await
11271137
{
1128-
if maturity_date_timestamp
1129-
> (util::date::now().timestamp() - PAYMENT_DEADLINE_SECONDS as i64)
1130-
{
1131-
let holder_public_key = match bill.endorsee {
1132-
None => &bill.payee.node_id,
1133-
Some(ref endorsee) => &endorsee.node_id,
1134-
};
1135-
let address_to_pay = self
1136-
.bitcoin_client
1137-
.get_address_to_pay(&bill_keys.public_key, holder_public_key)?;
1138-
if let Ok((paid, sum)) = self
1139-
.bitcoin_client
1140-
.check_if_paid(&address_to_pay, bill.sum)
1141-
.await
1142-
{
1143-
if paid && sum > 0 {
1144-
self.store.set_to_paid(bill_id, &address_to_pay).await?;
1145-
}
1146-
}
1138+
if paid && sum > 0 {
1139+
self.store.set_to_paid(bill_id, &address_to_pay).await?;
11471140
}
11481141
}
11491142
Ok(())
@@ -3221,6 +3214,8 @@ pub struct LightBitcreditBillToReturn {
32213214
pub sum: String,
32223215
pub currency: String,
32233216
pub issue_date: String,
3217+
pub time_of_drawing: u64,
3218+
pub time_of_maturity: u64,
32243219
}
32253220

32263221
impl From<BitcreditBillToReturn> for LightBitcreditBillToReturn {
@@ -3235,6 +3230,9 @@ impl From<BitcreditBillToReturn> for LightBitcreditBillToReturn {
32353230
sum: value.sum,
32363231
currency: value.currency,
32373232
issue_date: value.issue_date,
3233+
time_of_drawing: value.time_of_drawing,
3234+
time_of_maturity: util::date::date_string_to_i64_timestamp(&value.maturity_date, None)
3235+
.unwrap_or(0) as u64,
32383236
}
32393237
}
32403238
}
@@ -3243,6 +3241,7 @@ impl From<BitcreditBillToReturn> for LightBitcreditBillToReturn {
32433241
pub struct BitcreditBillToReturn {
32443242
pub id: String,
32453243
pub time_of_drawing: u64,
3244+
pub time_of_maturity: u64,
32463245
pub country_of_issuing: String,
32473246
pub city_of_issuing: String,
32483247
/// The party obliged to pay a Bill

src/util/currency.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::service::{Error, Result};
1+
use crate::{
2+
constants::SAT_TO_BTC_RATE,
3+
service::{Error, Result},
4+
};
5+
use rust_decimal::Decimal;
26

37
pub fn parse_sum(sum: &str) -> Result<u64> {
48
match sum.parse::<u64>() {
@@ -10,3 +14,22 @@ pub fn parse_sum(sum: &str) -> Result<u64> {
1014
pub fn sum_to_string(sum: u64) -> String {
1115
sum.to_string()
1216
}
17+
18+
pub fn sat_to_btc(val: u64) -> String {
19+
let conversion_factor = Decimal::new(1, 0) / Decimal::new(SAT_TO_BTC_RATE, 0);
20+
let sat_dec = Decimal::from(val);
21+
let btc_dec = sat_dec * conversion_factor;
22+
btc_dec.to_string()
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn sat_to_btc_test() {
31+
assert_eq!(sat_to_btc(1000), String::from("0.00001000"));
32+
assert_eq!(sat_to_btc(10000), String::from("0.00010000"));
33+
assert_eq!(sat_to_btc(1), String::from("0.00000001"));
34+
}
35+
}

src/web/handlers/quotes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub async fn return_quote(
2424
let bill_id_hex = hex::encode(bill_id_u8);
2525
let copy_id_hex = bill_id_hex.clone();
2626

27-
let local_node_id = get_current_identity_node_id(&state).await;
27+
let local_node_id = get_current_identity_node_id(state).await;
2828
if !quote.bill_id.is_empty() && quote.quote_id.is_empty() {
2929
// Usage of thread::spawn is necessary here, because we spawn a new tokio runtime in the
3030
// thread, but this logic will be replaced soon

0 commit comments

Comments
 (0)