Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
claddyy committed Oct 5, 2024
1 parent 7e1e64c commit 1eae69c
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions src/util/lnurl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::max;
use crate::error::Error;
use lightning_invoice::Bolt11Invoice;
use lnurl::lightning_address::LightningAddress;
Expand Down Expand Up @@ -54,7 +55,7 @@ pub fn fetch_invoice(address: &str, amount_msats: u64) -> Result<String, Error>
}
}

pub fn create_withdraw_response(voucher: &str) -> Result<WithdrawalResponse, Error> {
pub fn create_withdraw_response(voucher: &str) -> Result<(WithdrawalResponse, u64), Error> {
let lnurl =
LnUrl::from_str(voucher).map_err(|_| Error::Generic("Invalid LNURL".to_string()))?;

Expand All @@ -67,7 +68,10 @@ pub fn create_withdraw_response(voucher: &str) -> Result<WithdrawalResponse, Err
.map_err(|e| Error::HTTP(e.to_string()))?;

match res {
LnUrlResponse::LnUrlWithdrawResponse(withdraw) => Ok(withdraw),
LnUrlResponse::LnUrlWithdrawResponse(withdraw) => {
let max_withdrawable = withdraw.max_withdrawable;
Ok((withdraw, max_withdrawable))
},
_ => Err(Error::Generic("Unexpected response type".to_string())),
}
}
Expand Down Expand Up @@ -121,24 +125,39 @@ mod tests {
}

#[test]
fn test_process_withdrawal() {
let invoice = "lnbc50u1pnsqfxhpp55c04g7ku3d9k89z286twfh6z9zym43cskms842zkxmgn5dq3ls5qcqpjsp5yw4taatpjkmq3as42pfhq47e7zhs3mr6u5jgsx5ttvdcxamx34hs9q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgqdqqmqz9gxqyjw5qrzjqwryaup9lh50kkranzgcdnn2fgvx390wgj5jd07rwr3vxeje0glcllm8u4a8gvusysqqqqlgqqqqqeqqjqjmdpw5m89ce7gtycnw5d8557gduyjzeqetzecekv54spjtxfqzk4hfhzjec3w7ur6zvy4v3yxrfpeeccsz5npwxhfy77tjfr3mkmeacq0suj0n";
fn test_fetch_withdrawable_info(){
let voucher = "LNURL1DP68GURN8GHJ7ER9D4HJUMRWVF5HGUEWVDHK6TMHD96XSERJV9MJ7CTSDYHHVVF0D3H82UNV9ARRY5JTDP49XUJPXDHH2VJHG4ZY6K3NV9PHSTMP29ZH2JMFXFVKW4ZDXVEH57RX2P5NYNTWGGVNVP88";
assert!(validate_lnurl(voucher));
let withdraw_response = match create_withdraw_response(voucher) {
Ok(response) => response,

match create_withdraw_response(voucher) {
Ok((withdraw_response, max_withdrawable)) => {
println!("Max withdrawable : {} sats", max_withdrawable);
println!("Withdrawal response : {:?}", withdraw_response);
},
Err(e) => {
println!("Failed to create withdraw response: {:?}", e);
return;
println!("Error creating withdraw response: {:?}", e);
panic!("Failed to create withdraw response");
}
};

println!("Successfully created withdraw response");
let result = process_withdrawal(&withdraw_response, invoice);

assert!(result.is_ok(), "Withdrawal failed: {:?}", result.err());
assert_eq!(result.unwrap(), "Withdrawal successful");

println!("Withdrawal test passed successfully");
}
}
}
// fn test_process_withdrawal() {
// let invoice = "lnbc1pnsp6tapp54x7jmlaes8ry3yvjrc70alnt8juel64t0tyxws6j0hrkzerac0wqcqpjsp587x6qpkztycvkzwa3369hkp0e32zv2lqz5e2wunjck4n85x5ze4s9q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgqdqqmqz9gxqyjw5qrzjqwryaup9lh50kkranzgcdnn2fgvx390wgj5jd07rwr3vxeje0glcllm8u4a8gvusysqqqqlgqqqqqeqqjqk77qukrmmpu7j3ct55xc5qgml4w06sdcf07nrc5xlajz8wum7z9zvhgq8a5mpdx2t4jx5xv437m2jyd76w5y3dlzwg4vaefmsvlmvugqpt7xrk";
// let voucher = "LNURL1DP68GURN8GHJ7ER9D4HJUMRWVF5HGUEWVDHK6TMHD96XSERJV9MJ7CTSDYHHVVF0D3H82UNV9ARRY5JTDP49XUJPXDHH2VJHG4ZY6K3NV9PHSTMP29ZH2JMFXFVKW4ZDXVEH57RX2P5NYNTWGGVNVP88";
// assert!(validate_lnurl(voucher));
// let (withdraw_response, max_withdrawable) = match create_withdraw_response(voucher) {
// Ok(response) => response,
// Err(e) => {
// println!("Failed to create withdraw response: {:?}", e);
// return;
// }
// };
//
// println!("Successfully created withdraw response:{}", max_withdrawable);
// let result = process_withdrawal(&withdraw_response, invoice);
//
// assert!(result.is_ok(), "Withdrawal failed: {:?}", result.err());
// assert!(result.unwrap().starts_with("Withdrawal successful"));
//
// println!("Withdrawal test passed successfully");
// }
}

0 comments on commit 1eae69c

Please sign in to comment.