Skip to content

Commit 4917a32

Browse files
committed
feat(wallet): enable backwards compatibility with fee_type argument
1 parent cbf1b0c commit 4917a32

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

data_structures/src/fee.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ impl fmt::Display for Fee {
145145
}
146146
}
147147

148+
impl From<AbsoluteFee> for Fee {
149+
fn from(absolute: AbsoluteFee) -> Self {
150+
Self::Absolute(absolute)
151+
}
152+
}
153+
154+
impl From<RelativeFee> for Fee {
155+
fn from(relative: RelativeFee) -> Self {
156+
Self::Relative(relative)
157+
}
158+
}
159+
148160
/// Allow backwards compatibility with old Wallet API clients that may provide fee values without
149161
/// tagging whether they are absolute or relative.
150162
///

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
actors::{app, worker},
1212
types::{
1313
self, from_generic_type, into_generic_type, number_from_string, u32_to_string,
14-
DataRequestOutputHelper, TransactionHelper,
14+
DataRequestOutputHelper, FeeType, TransactionHelper,
1515
},
1616
};
1717

@@ -26,6 +26,8 @@ pub struct CreateDataReqRequest {
2626
request: DataRequestOutput,
2727
#[serde(deserialize_with = "deserialize_fee_backwards_compatible")]
2828
fee: Fee,
29+
#[serde(default)]
30+
fee_type: FeeType,
2931
}
3032

3133
#[derive(Debug, Serialize, Deserialize)]
@@ -56,11 +58,12 @@ impl Handler<CreateDataReqRequest> for app::App {
5658
fn handle(&mut self, msg: CreateDataReqRequest, _ctx: &mut Self::Context) -> Self::Result {
5759
let validated = validate(msg.request.clone()).map_err(app::validation_error);
5860

61+
// For the sake of backwards compatibility, if the `fee_type` argument was provided, then we
62+
// treat the `fee` argument as such type, regardless of how it was originally deserialized.
63+
let fee = msg.fee_type.fee_compat(msg.fee);
64+
5965
let f = fut::result(validated).and_then(move |request, slf: &mut Self, _ctx| {
60-
let params = types::DataReqParams {
61-
request,
62-
fee: msg.fee,
63-
};
66+
let params = types::DataReqParams { request, fee };
6467

6568
slf.create_data_req(&msg.session_id, &msg.wallet_id, params)
6669
.map_ok(

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
actors::{app, worker},
1515
types::{
1616
self, from_generic_type, from_generic_type_vec, into_generic_type, into_generic_type_vec,
17-
number_from_string, u32_to_string, TransactionHelper, VttOutputParamsHelper,
17+
number_from_string, u32_to_string, FeeType, TransactionHelper, VttOutputParamsHelper,
1818
},
1919
};
2020

@@ -29,6 +29,8 @@ pub struct VttOutputParams {
2929
pub struct CreateVttRequest {
3030
#[serde(deserialize_with = "deserialize_fee_backwards_compatible")]
3131
fee: Fee,
32+
#[serde(default)]
33+
fee_type: FeeType,
3234
label: Option<String>,
3335
#[serde(
3436
serialize_with = "into_generic_type_vec::<_, VttOutputParamsHelper, _>",
@@ -85,9 +87,13 @@ impl Handler<CreateVttRequest> for app::App {
8587
let validated =
8688
validate_output_addresses(testnet, &msg.outputs).map_err(app::validation_error);
8789

90+
// For the sake of backwards compatibility, if the `fee_type` argument was provided, then we
91+
// treat the `fee` argument as such type, regardless of how it was originally deserialized.
92+
let fee = msg.fee_type.fee_compat(msg.fee);
93+
8894
let f = fut::result(validated).and_then(move |outputs, act: &mut Self, _ctx| {
8995
let params = types::VttParams {
90-
fee: msg.fee,
96+
fee,
9197
outputs,
9298
utxo_strategy: msg.utxo_strategy.clone(),
9399
selected_utxos: msg.selected_utxos.iter().map(|x| x.into()).collect(),

wallet/src/types.rs

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

280+
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
281+
#[serde(rename_all = "camelCase")]
282+
pub enum FeeType {
283+
#[default]
284+
Absolute,
285+
Relative,
286+
}
287+
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,
300+
}
301+
}
302+
}
303+
280304
// Serialization helper
281305

282306
/// Transaction data structure

0 commit comments

Comments
 (0)