Skip to content

Commit

Permalink
feat(pay): add preliminary support for convert on pay callback
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 16, 2023
1 parent abed4a0 commit 1f3806f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ impl Pay<'_> {
&self,
amount: &crate::pay::Amount,
comment: Option<&str>,
convert: Option<&str>,
) -> Result<crate::pay::client::CallbackResponse, &'static str> {
let callback = self.core.invoice(amount, comment);
let callback = self.core.invoice(amount, comment, convert);

let response = self
.client
Expand Down
30 changes: 28 additions & 2 deletions src/core/pay/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ impl Entrypoint {
&'a self,
amount: &'a super::Amount,
comment: Option<&'a str>,
convert: Option<&'a str>,
) -> Callback<'a> {
Callback {
url: &self.callback,
amount,
comment,
convert,
}
}
}
Expand All @@ -124,13 +126,15 @@ pub struct Callback<'a> {
pub url: &'a url::Url,
pub comment: Option<&'a str>,
pub amount: &'a super::Amount,
pub convert: Option<&'a str>,
}

impl std::fmt::Display for Callback<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let query = ser::Callback {
comment: self.comment,
amount: self.amount,
convert: self.convert,
};

let querystr = serde_urlencoded::to_string(query).map_err(|_| std::fmt::Error)?;
Expand Down Expand Up @@ -186,6 +190,7 @@ mod ser {
pub comment: Option<&'a str>,
#[serde(with = "super::super::serde::amount")]
pub amount: &'a super::super::Amount,
pub convert: Option<&'a str>,
}
}

Expand Down Expand Up @@ -376,7 +381,7 @@ mod tests {

assert_eq!(
parsed
.invoice(&super::super::Amount::Millisatoshis(314), None)
.invoice(&super::super::Amount::Millisatoshis(314), None, None)
.to_string(),
"https://yuri/?o=callback&amount=314"
);
Expand All @@ -397,7 +402,8 @@ mod tests {
parsed
.invoice(
&super::super::Amount::Millisatoshis(314),
Some("comentario")
Some("comentario"),
None
)
.to_string(),
"https://yuri/?o=callback&comment=comentario&amount=314"
Expand All @@ -419,13 +425,33 @@ mod tests {
parsed
.invoice(
&super::super::Amount::Currency(String::from("BRL"), 314),
None,
None
)
.to_string(),
"https://yuri/?o=callback&amount=314.BRL"
);
}

#[test]
fn callback_render_convert() {
let input = r#"{
"metadata": "[[\"text/plain\", \"boneco do steve magal\"]]",
"callback": "https://yuri?o=callback",
"maxSendable": 315,
"minSendable": 314
}"#;

let parsed: super::Entrypoint = input.as_bytes().try_into().expect("parse");

assert_eq!(
parsed
.invoice(&super::super::Amount::Millisatoshis(314), None, Some("BRL"))
.to_string(),
"https://yuri/?o=callback&amount=314&convert=BRL"
);
}

#[test]
fn callback_response_parse_base() {
let input = r#"{ "pr": "pierre" }"#;
Expand Down
16 changes: 13 additions & 3 deletions src/core/pay/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl TryFrom<Entrypoint> for Vec<u8> {
pub struct Callback {
pub amount: super::Amount,
pub comment: Option<String>,
pub convert: Option<String>,
}

impl<'a> TryFrom<&'a str> for Callback {
Expand All @@ -77,9 +78,10 @@ impl<'a> TryFrom<&'a str> for Callback {
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
serde_urlencoded::from_str::<de::Callback>(s)
.map_err(|_| "deserialize failed")
.map(|query| Callback {
amount: query.amount,
comment: query.comment.map(String::from),
.map(|cb| Callback {
amount: cb.amount,
comment: cb.comment.map(String::from),
convert: cb.convert.map(String::from),
})
}
}
Expand Down Expand Up @@ -165,6 +167,7 @@ mod de {
pub comment: Option<&'a str>,
#[serde(with = "super::super::serde::amount")]
pub amount: super::super::Amount,
pub convert: Option<&'a str>,
}
}

Expand Down Expand Up @@ -377,6 +380,13 @@ mod tests {
assert!(parsed.comment.is_none());
}

#[test]
fn callback_parse_convert() {
let input = "amount=314&convert=BRL";
let parsed: super::Callback = input.try_into().expect("parse");
assert_eq!(parsed.convert.unwrap(), "BRL");
}

#[test]
fn callback_response_render_base() {
let input = super::CallbackResponse {
Expand Down
6 changes: 5 additions & 1 deletion tests/lud06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ async fn test() {
);

let invoice = pr
.invoice(&lnurlkit::pay::Amount::Millisatoshis(314), Some("comment"))
.invoice(
&lnurlkit::pay::Amount::Millisatoshis(314),
Some("comment"),
None,
)
.await
.expect("callback");

Expand Down
14 changes: 11 additions & 3 deletions tests/lud09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,18 @@ async fn test() {
};

let invoice = pr
.invoice(&lnurlkit::pay::Amount::Millisatoshis(0), None)
.invoice(&lnurlkit::pay::Amount::Millisatoshis(0), None, None)
.await
.expect("callback");

assert!(invoice.success_action.is_none());

let invoice = pr
.invoice(&lnurlkit::pay::Amount::Millisatoshis(1), Some("mensagem"))
.invoice(
&lnurlkit::pay::Amount::Millisatoshis(1),
Some("mensagem"),
None,
)
.await
.expect("callback");

Expand All @@ -88,7 +92,11 @@ async fn test() {
assert_eq!(&m as &str, "mensagem");

let invoice = pr
.invoice(&lnurlkit::pay::Amount::Millisatoshis(2), Some("descricao"))
.invoice(
&lnurlkit::pay::Amount::Millisatoshis(2),
Some("descricao"),
None,
)
.await
.expect("callback");

Expand Down
4 changes: 2 additions & 2 deletions tests/lud11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ async fn test() {
};

let invoice = pr
.invoice(&lnurlkit::pay::Amount::Millisatoshis(314), None)
.invoice(&lnurlkit::pay::Amount::Millisatoshis(314), None, None)
.await
.expect("callback");

assert!(invoice.disposable);

let invoice = pr
.invoice(&lnurlkit::pay::Amount::Millisatoshis(315), None)
.invoice(&lnurlkit::pay::Amount::Millisatoshis(315), None, None)
.await
.expect("callback");
assert!(!invoice.disposable);
Expand Down
3 changes: 2 additions & 1 deletion tests/lud12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async fn test() {
assert_eq!(pr.core.comment_size.unwrap(), 140);

let invoice = pr
.invoice(&lnurlkit::pay::Amount::Millisatoshis(314), None)
.invoice(&lnurlkit::pay::Amount::Millisatoshis(314), None, None)
.await
.expect("callback");

Expand All @@ -70,6 +70,7 @@ async fn test() {
.invoice(
&lnurlkit::pay::Amount::Millisatoshis(314),
Some("comentario"),
None,
)
.await
.expect("callback");
Expand Down

0 comments on commit 1f3806f

Please sign in to comment.