Skip to content

Commit b27f905

Browse files
committed
refactor(wallet): use an Option around fee_type for neater code
1 parent a76d1ea commit b27f905

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

wallet/src/actors/app/handlers/create_data_req.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use witnet_data_structures::{
1010
use crate::{
1111
actors::{app, worker},
1212
types::{
13-
self, from_generic_type, into_generic_type, number_from_string, u32_to_string,
13+
self, fee_compat, from_generic_type, into_generic_type, number_from_string, u32_to_string,
1414
DataRequestOutputHelper, FeeType, TransactionHelper,
1515
},
1616
};
@@ -26,8 +26,7 @@ pub struct CreateDataReqRequest {
2626
request: DataRequestOutput,
2727
#[serde(deserialize_with = "deserialize_fee_backwards_compatible")]
2828
fee: Fee,
29-
#[serde(default)]
30-
fee_type: FeeType,
29+
fee_type: Option<FeeType>,
3130
}
3231

3332
#[derive(Debug, Serialize, Deserialize)]
@@ -60,7 +59,7 @@ impl Handler<CreateDataReqRequest> for app::App {
6059

6160
// For the sake of backwards compatibility, if the `fee_type` argument was provided, then we
6261
// treat the `fee` argument as such type, regardless of how it was originally deserialized.
63-
let fee = msg.fee_type.fee_compat(msg.fee);
62+
let fee = fee_compat(msg.fee, msg.fee_type);
6463

6564
let f = fut::result(validated).and_then(move |request, slf: &mut Self, _ctx| {
6665
let params = types::DataReqParams { request, fee };

wallet/src/actors/app/handlers/create_vtt.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use witnet_data_structures::{
1313
use crate::{
1414
actors::{app, worker},
1515
types::{
16-
self, from_generic_type, from_generic_type_vec, into_generic_type, into_generic_type_vec,
17-
number_from_string, u32_to_string, FeeType, TransactionHelper, VttOutputParamsHelper,
16+
self, fee_compat, from_generic_type, from_generic_type_vec, into_generic_type,
17+
into_generic_type_vec, number_from_string, u32_to_string, FeeType, TransactionHelper,
18+
VttOutputParamsHelper,
1819
},
1920
};
2021

@@ -29,8 +30,7 @@ pub struct VttOutputParams {
2930
pub struct CreateVttRequest {
3031
#[serde(deserialize_with = "deserialize_fee_backwards_compatible")]
3132
fee: Fee,
32-
#[serde(default)]
33-
fee_type: FeeType,
33+
fee_type: Option<FeeType>,
3434
label: Option<String>,
3535
#[serde(
3636
serialize_with = "into_generic_type_vec::<_, VttOutputParamsHelper, _>",
@@ -89,7 +89,7 @@ impl Handler<CreateVttRequest> for app::App {
8989

9090
// For the sake of backwards compatibility, if the `fee_type` argument was provided, then we
9191
// treat the `fee` argument as such type, regardless of how it was originally deserialized.
92-
let fee = msg.fee_type.fee_compat(msg.fee);
92+
let fee = fee_compat(msg.fee, msg.fee_type);
9393

9494
let f = fut::result(validated).and_then(move |outputs, act: &mut Self, _ctx| {
9595
let params = types::VttParams {

wallet/src/types.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -277,27 +277,24 @@ pub struct SuperBlockNotification {
277277
pub consolidated_block_hashes: Vec<String>,
278278
}
279279

280-
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
280+
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
281281
#[serde(rename_all = "camelCase")]
282282
pub enum FeeType {
283-
#[default]
284283
Absolute,
285284
Relative,
286285
}
287286

288-
impl FeeType {
289-
/// For the sake of backwards compatibility, turn a `Fee::Absolute` into `Fee::Relative` if the
290-
/// `FeeType` is `Relative`, and a `Fee::Relative` into `Fee::Absolute` if the `FeeType` is
291-
/// `Absolute`.
292-
#[allow(clippy::cast_precision_loss)]
293-
pub fn fee_compat(&self, fee: Fee) -> Fee {
294-
match (self, fee) {
295-
(&FeeType::Absolute, Fee::Relative(relative)) => Fee::from(relative.into_absolute(1)),
296-
(&FeeType::Relative, Fee::Absolute(absolute)) => {
297-
Fee::relative_from_float(absolute.as_nanowits() as f64)
298-
}
299-
_ => fee,
287+
/// For the sake of backwards compatibility, turn a `Fee::Absolute` into `Fee::Relative` if the
288+
/// `FeeType` is `Relative`, and a `Fee::Relative` into `Fee::Absolute` if the `FeeType` is
289+
/// `Absolute`.
290+
#[allow(clippy::cast_precision_loss)]
291+
pub fn fee_compat(fee: Fee, fee_type: Option<FeeType>) -> Fee {
292+
match (fee, fee_type) {
293+
(Fee::Relative(relative), Some(FeeType::Absolute)) => Fee::from(relative.into_absolute(1)),
294+
(Fee::Absolute(absolute), Some(FeeType::Relative)) => {
295+
Fee::relative_from_float(absolute.as_nanowits() as f64)
300296
}
297+
_ => fee,
301298
}
302299
}
303300

@@ -738,16 +735,22 @@ mod tests {
738735

739736
#[test]
740737
fn test_fee_type_backwards_compatibility() {
741-
let fee = FeeType::Absolute.fee_compat(Fee::absolute_from_nanowits(123456));
738+
let fee = fee_compat(Fee::absolute_from_nanowits(123456), None);
739+
assert_eq!(fee, Fee::absolute_from_nanowits(123456));
740+
741+
let fee = fee_compat(Fee::relative_from_float(123.456), None);
742+
assert_eq!(fee, Fee::relative_from_float(123.456));
743+
744+
let fee = fee_compat(Fee::absolute_from_nanowits(123456), Some(FeeType::Absolute));
742745
assert_eq!(fee, Fee::absolute_from_nanowits(123456));
743746

744-
let fee = FeeType::Absolute.fee_compat(Fee::relative_from_float(123.456));
747+
let fee = fee_compat(Fee::relative_from_float(123.456), Some(FeeType::Absolute));
745748
assert_eq!(fee, Fee::absolute_from_nanowits(123));
746749

747-
let fee = FeeType::Relative.fee_compat(Fee::absolute_from_nanowits(123456));
750+
let fee = fee_compat(Fee::absolute_from_nanowits(123456), Some(FeeType::Relative));
748751
assert_eq!(fee, Fee::relative_from_float(123456.0));
749752

750-
let fee = FeeType::Relative.fee_compat(Fee::relative_from_float(123.456));
753+
let fee = fee_compat(Fee::relative_from_float(123.456), Some(FeeType::Relative));
751754
assert_eq!(fee, Fee::relative_from_float(123.456));
752755
}
753756
}

0 commit comments

Comments
 (0)