Skip to content

Commit

Permalink
fix(pay): update convertible object up to spec
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 27, 2023
1 parent 69063b3 commit ccadc2a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
18 changes: 15 additions & 3 deletions src/core/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ pub struct Currency {
pub symbol: String,
pub decimals: u8,
pub multiplier: f64,
pub convertible: bool,
pub convertible: Option<CurrencyConvertible>,
}

#[derive(Clone, Debug)]
pub struct CurrencyConvertible {
pub min: u64,
pub max: u64,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -66,8 +72,14 @@ mod serde {
pub symbol: &'a str,
pub decimals: u8,
pub multiplier: f64,
#[serde(default)]
pub convertible: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub convertible: Option<CurrencyConvertible>,
}

#[derive(Deserialize, Serialize)]
pub struct CurrencyConvertible {
pub min: u64,
pub max: u64,
}

#[derive(Deserialize, Serialize)]
Expand Down
17 changes: 13 additions & 4 deletions src/core/pay/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ impl TryFrom<&[u8]> for Entrypoint {
symbol: String::from(c.symbol),
decimals: c.decimals,
multiplier: c.multiplier,
convertible: c.convertible,
convertible: c.convertible.map(|c| super::CurrencyConvertible {
min: c.min,
max: c.max,
}),
})
.collect()
});
Expand Down Expand Up @@ -404,7 +407,10 @@ mod tests {
"symbol": "R$",
"multiplier": 314.15,
"decimals": 2,
"convertible": true
"convertible": {
"min": 100,
"max": 999
}
},
{
"code": "USD",
Expand All @@ -424,14 +430,17 @@ mod tests {
assert_eq!(currencies[0].symbol, "R$");
assert_eq!(currencies[0].decimals, 2);
assert!((currencies[0].multiplier - 314.15).abs() < f64::EPSILON);
assert!(currencies[0].convertible);

let convertible = currencies[0].convertible.as_ref().unwrap();
assert_eq!(convertible.min, 100);
assert_eq!(convertible.max, 999);

assert_eq!(currencies[1].code, "USD");
assert_eq!(currencies[1].name, "Dólar");
assert_eq!(currencies[1].symbol, "$");
assert_eq!(currencies[1].decimals, 6);
assert!((currencies[1].multiplier - 14.5).abs() < f64::EPSILON);
assert!(!currencies[1].convertible);
assert!(currencies[1].convertible.is_none());
}

#[test]
Expand Down
13 changes: 9 additions & 4 deletions src/core/pay/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ impl TryFrom<Entrypoint> for Vec<u8> {
symbol: &c.symbol,
decimals: c.decimals,
multiplier: c.multiplier,
convertible: c.convertible,
convertible: c.convertible.as_ref().map(|c| {
super::serde::CurrencyConvertible {
min: c.min,
max: c.max,
}
}),
})
.collect()
}),
Expand Down Expand Up @@ -395,23 +400,23 @@ mod tests {
symbol: String::from("R$"),
decimals: 2,
multiplier: 314.15,
convertible: true,
convertible: Some(super::super::CurrencyConvertible { min: 100, max: 999 }),
},
super::super::Currency {
code: String::from("USD"),
name: String::from("Dolar"),
symbol: String::from("$"),
decimals: 6,
multiplier: 123.321,
convertible: false,
convertible: None,
},
]),
payer: None,
};

assert_eq!(
Vec::<u8>::try_from(query).unwrap(),
br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":0,"currencies":[{"code":"BRL","name":"Reais","symbol":"R$","decimals":2,"multiplier":314.15,"convertible":true},{"code":"USD","name":"Dolar","symbol":"$","decimals":6,"multiplier":123.321,"convertible":false}]}"#
br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":0,"currencies":[{"code":"BRL","name":"Reais","symbol":"R$","decimals":2,"multiplier":314.15,"convertible":{"min":100,"max":999}},{"code":"USD","name":"Dolar","symbol":"$","decimals":6,"multiplier":123.321}]}"#
);
}

Expand Down

0 comments on commit ccadc2a

Please sign in to comment.