Skip to content

Commit

Permalink
BIP21: Use bitcoin::Amount instead of f64 to store amount
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Aug 27, 2024
1 parent 5fe27fa commit 994b199
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/swaps/magic_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn find_magic_routing_hint(invoice: &str) -> Result<Option<RouteHintHop>, Er
}

/// Parse a BIP21 String and get the network, address, asset_id if present
pub fn parse_bip21(uri: &str) -> Result<(String, String, f64, Option<String>), Error> {
pub fn parse_bip21(uri: &str) -> Result<(String, String, bitcoin::Amount, Option<String>), Error> {
let parts: Vec<&str> = uri.split('?').collect();

let (network_address, params) = (parts[0], parts[1]);
Expand All @@ -58,14 +58,14 @@ pub fn parse_bip21(uri: &str) -> Result<(String, String, f64, Option<String>), E

// Parse URI parameters
let params: Vec<&str> = params.split('&').collect();
let mut amount = 0f64;
let mut amount = bitcoin::Amount::from_sat(0);
let mut assetid = None::<String>;

for param in params {
let pair: Vec<&str> = param.split('=').collect();
match pair[0] {
"amount" => {
amount = match f64::from_str(pair[1]) {
amount = match bitcoin::Amount::from_str_in(pair[1], bitcoin::Denomination::Bitcoin) {
Ok(r) => r,
Err(e) => {
return Err(Error::Generic(
Expand All @@ -88,7 +88,7 @@ pub fn check_for_mrh(
boltz_api_v2: &BoltzApiClientV2,
invoice: &str,
network: Chain,
) -> Result<Option<(String, f64)>, Error> {
) -> Result<Option<(String, bitcoin::Amount)>, Error> {
if let Some(route_hint) = find_magic_routing_hint(&invoice)? {
let mrh_resp = boltz_api_v2.get_mrh_bip21(&invoice)?;

Expand Down Expand Up @@ -144,7 +144,7 @@ fn test_bip21_parsing() {

assert_eq!(network, "liquidtestnet");
assert_eq!(address, "tlq1qqt3sgky7zert7237tred5rqmmx0eargp625zkyhr2ldw6yqdvh5fusnm5xk0qfjpejvgm37q7mqtv5epfksv78jweytmqgpd8");
assert_eq!(amount, 0.00005122);
assert_eq!(amount.to_btc(), 0.00005122);
assert_eq!(
assetid,
Some("144c654344aa716d6f3abcc1ca90e5641e4e2a7f633bc09fe3baf64585819a4".to_string())
Expand All @@ -170,8 +170,7 @@ fn test_bip21_parsing_with_rounding_edge_cases() {
let uri = format!("liquidtestnet:{liquid_address}?amount={amount_btc}&assetid={asset_id}");
let (_network, _address, bip21_amount, _assetid) = parse_bip21(&uri).unwrap();

let parsed_amount_btc = bip21_amount;
let parsed_amount_sat = (parsed_amount_btc * 100_000_000.0) as u64;
let parsed_amount_sat = bip21_amount.to_sat();

assert_eq!(parsed_amount_sat, amount_sat);
}
Expand Down

0 comments on commit 994b199

Please sign in to comment.