Skip to content

Commit ed11ac5

Browse files
authored
Implement and use proper Sum and Currency types (#713)
1 parent 05657d5 commit ed11ac5

File tree

41 files changed

+855
-685
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+855
-685
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Document versioning scheme
44
* Rework WASM API to return `TSResult<T> = { Success: T } | { Error: JsErrorData }` without triggering exceptions
5+
* Rework `sum` and `currency` into a coherent `Sum` type that's ready for multi-currency and exchange rates (breaking DB change)
56

67
# 0.4.12
78

crates/bcr-ebill-api/src/data/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use bcr_ebill_core::mint;
1313
pub use bcr_ebill_core::name;
1414
pub use bcr_ebill_core::nostr_contact;
1515
pub use bcr_ebill_core::notification;
16+
pub use bcr_ebill_core::sum;
1617
pub use bcr_ebill_core::zip;
1718

1819
pub use bcr_ebill_core::File;

crates/bcr-ebill-api/src/external/bitcoin.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use async_trait::async_trait;
33
use bcr_ebill_core::{
44
PublicKey, ServiceTraitBounds,
55
bill::{InMempoolData, PaidData, PaymentState},
6-
util,
6+
sum::Sum,
77
};
88
use bitcoin::{Network, secp256k1::Scalar};
99
use log::debug;
@@ -66,7 +66,7 @@ pub trait BitcoinClientApi: ServiceTraitBounds {
6666
holder_public_key: &PublicKey,
6767
) -> Result<String>;
6868

69-
fn generate_link_to_pay(&self, address: &str, sum: u64, message: &str) -> String;
69+
fn generate_link_to_pay(&self, address: &str, sum: &Sum, message: &str) -> String;
7070

7171
fn get_combined_private_descriptor(
7272
&self,
@@ -204,8 +204,8 @@ impl BitcoinClientApi for BitcoinClient {
204204
Ok(bitcoin::Address::p2wpkh(&pub_key_bill, get_config().bitcoin_network()).to_string())
205205
}
206206

207-
fn generate_link_to_pay(&self, address: &str, sum: u64, message: &str) -> String {
208-
let btc_sum = util::currency::sat_to_btc(sum);
207+
fn generate_link_to_pay(&self, address: &str, sum: &Sum, message: &str) -> String {
208+
let btc_sum = sum.as_btc_string();
209209
let link = format!("bitcoin:{address}?amount={btc_sum}&message={message}");
210210
link
211211
}

crates/bcr-ebill-api/src/service/bill_service/blocks.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,12 @@ impl BillService {
138138
recourse_deadline_timestamp,
139139
) => {
140140
validate_node_id_network(&recoursee.node_id)?;
141-
let (sum, currency, reason) = match *recourse_reason {
142-
RecourseReason::Accept => (
143-
bill.sum,
144-
bill.currency.clone(),
145-
BillRecourseReasonBlockData::Accept,
146-
),
147-
RecourseReason::Pay(sum, ref currency) => {
148-
(sum, currency.to_owned(), BillRecourseReasonBlockData::Pay)
141+
let (sum, reason) = match *recourse_reason {
142+
RecourseReason::Accept => {
143+
(bill.sum.clone(), BillRecourseReasonBlockData::Accept)
144+
}
145+
RecourseReason::Pay(ref sum) => {
146+
(sum.to_owned(), BillRecourseReasonBlockData::Pay)
149147
}
150148
};
151149
let block_data = BillRequestRecourseBlockData {
@@ -157,7 +155,6 @@ impl BillService {
157155
},
158156
recoursee: recoursee.clone().into(),
159157
sum,
160-
currency: currency.to_owned(),
161158
recourse_reason: reason,
162159
signatory: signing_keys.signatory_identity,
163160
signing_timestamp: timestamp,
@@ -176,11 +173,11 @@ impl BillService {
176173
)?
177174
}
178175
// can be anon to recourse
179-
BillAction::Recourse(recoursee, sum, currency, recourse_reason) => {
176+
BillAction::Recourse(recoursee, sum, recourse_reason) => {
180177
validate_node_id_network(&recoursee.node_id)?;
181178
let reason = match *recourse_reason {
182179
RecourseReason::Accept => BillRecourseReasonBlockData::Accept,
183-
RecourseReason::Pay(_, _) => BillRecourseReasonBlockData::Pay,
180+
RecourseReason::Pay(_) => BillRecourseReasonBlockData::Pay,
184181
};
185182
let block_data = BillRecourseBlockData {
186183
recourser: if holder_is_anon {
@@ -190,8 +187,7 @@ impl BillService {
190187
signer_public_data.clone().into()
191188
},
192189
recoursee: recoursee.clone().into(),
193-
sum: *sum,
194-
currency: currency.to_owned(),
190+
sum: sum.clone(),
195191
recourse_reason: reason,
196192
signatory: signing_keys.signatory_identity,
197193
signing_timestamp: timestamp,
@@ -209,7 +205,7 @@ impl BillService {
209205
)?
210206
}
211207
// can be anon to mint
212-
BillAction::Mint(mint, sum, currency) => {
208+
BillAction::Mint(mint, sum) => {
213209
validate_node_id_network(&mint.node_id())?;
214210
let block_data = BillMintBlockData {
215211
endorser: if holder_is_anon {
@@ -219,8 +215,7 @@ impl BillService {
219215
signer_public_data.clone().into()
220216
},
221217
endorsee: mint.clone().into(),
222-
currency: currency.to_owned(),
223-
sum: *sum,
218+
sum: sum.clone(),
224219
signatory: signing_keys.signatory_identity,
225220
signing_timestamp: timestamp,
226221
signing_address: signer_public_data.postal_address(),
@@ -237,7 +232,7 @@ impl BillService {
237232
)?
238233
}
239234
// can be anon to offer to sell
240-
BillAction::OfferToSell(buyer, sum, currency, buying_deadline_timestamp) => {
235+
BillAction::OfferToSell(buyer, sum, buying_deadline_timestamp) => {
241236
validate_node_id_network(&buyer.node_id())?;
242237
let address_to_pay = self.bitcoin_client.get_address_to_pay(
243238
&bill_keys.public_key,
@@ -251,8 +246,7 @@ impl BillService {
251246
signer_public_data.clone().into()
252247
},
253248
buyer: buyer.clone().into(),
254-
currency: currency.to_owned(),
255-
sum: *sum,
249+
sum: sum.clone(),
256250
payment_address: address_to_pay,
257251
signatory: signing_keys.signatory_identity,
258252
signing_timestamp: timestamp,
@@ -271,7 +265,7 @@ impl BillService {
271265
)?
272266
}
273267
// can be anon to sell
274-
BillAction::Sell(buyer, sum, currency, payment_address) => {
268+
BillAction::Sell(buyer, sum, payment_address) => {
275269
validate_node_id_network(&buyer.node_id())?;
276270
let block_data = BillSellBlockData {
277271
seller: if holder_is_anon {
@@ -281,8 +275,7 @@ impl BillService {
281275
signer_public_data.clone().into()
282276
},
283277
buyer: buyer.clone().into(),
284-
currency: currency.to_owned(),
285-
sum: *sum,
278+
sum: sum.clone(),
286279
payment_address: payment_address.to_owned(),
287280
signatory: signing_keys.signatory_identity,
288281
signing_timestamp: timestamp,

crates/bcr-ebill-api/src/service/bill_service/data_fetching.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use bcr_ebill_core::blockchain::Block;
1212
use bcr_ebill_core::contact::{BillParticipant, Contact};
1313
use bcr_ebill_core::date::Date;
1414
use bcr_ebill_core::identity::IdentityType;
15+
use bcr_ebill_core::sum::Sum;
1516
use bcr_ebill_core::{NodeId, Validate, ValidationError};
1617
use bcr_ebill_core::{
1718
bill::{
@@ -29,7 +30,7 @@ use bcr_ebill_core::{
2930
},
3031
contact::ContactType,
3132
identity::{Identity, IdentityWithAll},
32-
util::{BcrKeys, currency},
33+
util::BcrKeys,
3334
};
3435
use log::{debug, error};
3536
use std::collections::HashMap;
@@ -91,7 +92,6 @@ impl BillService {
9192
drawer: drawer_contact,
9293
payee: payee_contact,
9394
endorsee: endorsee_contact,
94-
currency: bill_first_version.currency,
9595
sum: bill_first_version.sum,
9696
maturity_date: bill_first_version.maturity_date,
9797
issue_date: bill_first_version.issue_date,
@@ -356,7 +356,7 @@ impl BillService {
356356

357357
let link_to_pay = self.bitcoin_client.generate_link_to_pay(
358358
&address_to_pay,
359-
payment_info.sum,
359+
&payment_info.sum,
360360
&format!("Payment in relation to a bill {}", &bill.id),
361361
);
362362

@@ -369,8 +369,7 @@ impl BillService {
369369
buyer,
370370
payment_data: BillWaitingStatePaymentData {
371371
time_of_request: last_block.timestamp,
372-
currency: payment_info.currency.clone(),
373-
sum: currency::sum_to_string(payment_info.sum),
372+
sum: payment_info.sum.clone(),
374373
link_to_pay,
375374
address_to_pay,
376375
mempool_link_for_address_to_pay,
@@ -425,7 +424,7 @@ impl BillService {
425424

426425
let link_to_pay = self.bitcoin_client.generate_link_to_pay(
427426
&address_to_pay,
428-
bill.sum,
427+
&bill.sum,
429428
&format!("Payment in relation to a bill {}", bill.id.clone()),
430429
);
431430

@@ -439,8 +438,7 @@ impl BillService {
439438
payee: holder.clone(),
440439
payment_data: BillWaitingStatePaymentData {
441440
time_of_request: last_block.timestamp,
442-
currency: bill.currency.clone(),
443-
sum: currency::sum_to_string(bill.sum),
441+
sum: bill.sum.clone(),
444442
link_to_pay,
445443
address_to_pay,
446444
mempool_link_for_address_to_pay,
@@ -509,7 +507,7 @@ impl BillService {
509507

510508
let link_to_pay = self.bitcoin_client.generate_link_to_pay(
511509
&address_to_pay,
512-
payment_info.sum,
510+
&payment_info.sum,
513511
&format!("Payment in relation to a bill {}", &bill.id),
514512
);
515513

@@ -523,8 +521,7 @@ impl BillService {
523521
recoursee,
524522
payment_data: BillWaitingStatePaymentData {
525523
time_of_request: last_block.timestamp,
526-
currency: payment_info.currency.clone(),
527-
sum: currency::sum_to_string(payment_info.sum),
524+
sum: payment_info.sum.clone(),
528525
link_to_pay,
529526
address_to_pay,
530527
mempool_link_for_address_to_pay,
@@ -603,8 +600,7 @@ impl BillService {
603600
city_of_issuing: bill.city_of_issuing,
604601
country_of_payment: bill.country_of_payment,
605602
city_of_payment: bill.city_of_payment,
606-
currency: bill.currency,
607-
sum: currency::sum_to_string(bill.sum),
603+
sum: bill.sum.clone(),
608604
files: bill.files,
609605
active_notification: None,
610606
};
@@ -960,8 +956,8 @@ fn calculate_possible_bill_actions_for_caller(
960956
for action in BillCallerBillAction::iter() {
961957
let recourse_reason: Option<RecourseReason> = match action {
962958
BillCallerBillAction::RequestRecourseForPayment => {
963-
// These default values are not used
964-
Some(RecourseReason::Pay(u64::default(), String::default()))
959+
// This default values is not used
960+
Some(RecourseReason::Pay(Sum::new_sat(1).expect("is valid sum")))
965961
}
966962
BillCallerBillAction::RequestRecourseForAcceptance => Some(RecourseReason::Accept),
967963
_ => None,

crates/bcr-ebill-api/src/service/bill_service/issue.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl BillService {
5555
validate_node_id_network(&data.drawee)?;
5656
validate_node_id_network(&data.payee)?;
5757
validate_node_id_network(&data.drawer_public_data.node_id())?;
58-
let (sum, bill_type) = validate_bill_issue(&data)?;
58+
let bill_type = validate_bill_issue(&data)?;
5959

6060
let drawer = match data.drawer_public_data {
6161
BillParticipant::Ident(ref drawer_data) => drawer_data,
@@ -182,8 +182,7 @@ impl BillService {
182182
id: bill_id.clone(),
183183
country_of_issuing: data.country_of_issuing,
184184
city_of_issuing: data.city_of_issuing,
185-
currency: data.currency,
186-
sum,
185+
sum: data.sum,
187186
maturity_date: data.maturity_date,
188187
issue_date: data.issue_date,
189188
country_of_payment: data.country_of_payment,

0 commit comments

Comments
 (0)