Skip to content

Commit

Permalink
Merge pull request #730 from getlipa/feature/change-topup-currency-to…
Browse files Browse the repository at this point in the history
…-string

Change topup currency type to string
  • Loading branch information
danielgranhao authored Nov 6, 2023
2 parents a2d1210 + f15cb56 commit 922cc5a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 45 deletions.
9 changes: 2 additions & 7 deletions examples/node/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::collections::HashSet;
use std::path::Path;
use uniffi_lipalightninglib::{
Amount, DecodedData, ExchangeRate, FiatValue, InvoiceDetails, LightningNode, LiquidityLimit,
LnUrlPayDetails, MaxRoutingFeeMode, OfferKind, PaymentState, TopupCurrency, TzConfig,
LnUrlPayDetails, MaxRoutingFeeMode, OfferKind, PaymentState, TzConfig,
};

pub(crate) fn poll_for_user_input(node: &LightningNode, log_file_path: &str) {
Expand Down Expand Up @@ -673,15 +673,10 @@ fn register_topup(node: &LightningNode, words: &mut dyn Iterator<Item = &str>) -
let iban = words.next().ok_or(anyhow!("IBAN is required"))?;

let currency = words.next().ok_or(anyhow!("Currency is required"))?;
let currency = match currency {
"eur" => TopupCurrency::EUR,
"chf" => TopupCurrency::CHF,
_ => bail!("Invalid currency"),
};

let email = words.next().map(String::from);

let topup_info = node.register_fiat_topup(email, iban.to_string(), currency)?;
let topup_info = node.register_fiat_topup(email, iban.to_string(), currency.to_string())?;
println!("{topup_info:?}");

Ok(())
Expand Down
18 changes: 3 additions & 15 deletions src/fiat_topup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;

#[derive(Debug)]
pub enum TopupCurrency {
EUR,
CHF,
GBP,
}

/// Information about a fiat top-up registration
#[derive(Debug)]
pub struct FiatTopupInfo {
Expand Down Expand Up @@ -153,7 +146,7 @@ impl PocketClient {
pub fn register_pocket_fiat_topup(
&self,
user_iban: &str,
user_currency: TopupCurrency,
user_currency: String,
) -> Result<FiatTopupInfo> {
let challenge_response = self.request_challenge()?;

Expand Down Expand Up @@ -196,7 +189,7 @@ impl PocketClient {
&self,
challenge_response: ChallengeResponse,
user_iban: &str,
user_currency: TopupCurrency,
user_currency: String,
) -> Result<CreateOrderResponse> {
let message = format!(
"I confirm my bitcoin wallet. [{}]",
Expand All @@ -213,16 +206,11 @@ impl PocketClient {
})?
.id;

let user_currency = match user_currency {
TopupCurrency::EUR => "eur",
TopupCurrency::CHF => "chf",
TopupCurrency::GBP => "gbp",
};
let create_order_request = CreateOrderRequest {
active: true,
affiliate_id: None,
payment_method: PaymentMethodRequest {
currency: user_currency.to_string(),
currency: user_currency.to_lowercase(),
debitor_iban: user_iban.to_string(),
},
payout_method: PayoutMethod {
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub use crate::errors::{DecodeInvoiceError, MnemonicError, PayError, PayErrorCod
pub use crate::errors::{Error as LnError, Result, RuntimeErrorCode};
pub use crate::exchange_rate_provider::ExchangeRate;
use crate::exchange_rate_provider::ExchangeRateProviderImpl;
pub use crate::fiat_topup::TopupCurrency;
use crate::fiat_topup::{FiatTopupInfo, PocketClient};
pub use crate::invoice_details::InvoiceDetails;
pub use crate::limits::{LiquidityLimit, PaymentAmountLimits};
Expand Down Expand Up @@ -1043,12 +1042,14 @@ impl LightningNode {
/// Parameters:
/// * `email` - this email will be used to send status information about different topups
/// * `user_iban` - the user will send fiat from this iban
/// * `user_currency` - the fiat currency that will be sent for exchange
/// * `user_currency` - the fiat currency (ISO 4217 currency code) that will be sent for
/// exchange. Not all are supported. A consumer of this library should find out about available
/// ones using other sources.
pub fn register_fiat_topup(
&self,
email: Option<String>,
user_iban: String,
user_currency: TopupCurrency,
user_currency: String,
) -> Result<FiatTopupInfo> {
trace!("register_fiat_topup() - called with - email: {email:?} - user_iban: {user_iban} - user_currency: {user_currency:?}");
user_iban
Expand Down
8 changes: 1 addition & 7 deletions src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface LightningNode {
void accept_pocket_terms_and_conditions();

[Throws=LnError]
FiatTopupInfo register_fiat_topup(string? email, string user_iban, TopupCurrency user_currency);
FiatTopupInfo register_fiat_topup(string? email, string user_iban, string user_currency);

[Throws=LnError]
sequence<OfferInfo> query_uncompleted_offers();
Expand Down Expand Up @@ -269,12 +269,6 @@ dictionary Amount {
FiatValue? fiat;
};

enum TopupCurrency {
"EUR",
"CHF",
"GBP",
};

dictionary FiatTopupInfo {
string order_id;
string debitor_iban;
Expand Down
22 changes: 9 additions & 13 deletions tests/topup_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,48 @@ use perro::Error::InvalidInput;
use std::time::Duration;

use serial_test::file_serial;
use uniffi_lipalightninglib::{OfferInfo, OfferKind, OfferStatus, TopupCurrency};
use uniffi_lipalightninglib::{OfferInfo, OfferKind, OfferStatus};

#[test]
#[file_serial(key, "/tmp/3l-int-tests-lock")]
fn test_topup() {
let node = start_alice().unwrap();

node.register_fiat_topup(
None,
"CH8689144834469929874".to_string(),
TopupCurrency::CHF,
)
.expect("Couldn't register topup without email");
node.register_fiat_topup(None, "CH8689144834469929874".to_string(), "CHF".to_string())
.expect("Couldn't register topup without email");

node.register_fiat_topup(
Some("alice-topup@integration.lipa.swiss".to_string()),
"CH0389144436836555818".to_string(),
TopupCurrency::CHF,
"chf".to_string(),
)
.expect("Couldn't register topup with email");

node.register_fiat_topup(
Some("alice-topup@integration.lipa.swiss".to_string()),
"CH9289144414389576442".to_string(),
TopupCurrency::CHF,
"chf".to_string(),
)
.expect("Couldn't register second topup with used email");

node.register_fiat_topup(
Some("alice-topup2@integration.lipa.swiss".to_string()),
"CH9289144414389576442".to_string(),
TopupCurrency::CHF,
"chf".to_string(),
)
.expect("Couldn't re-register topup with different email");

let result = node.register_fiat_topup(
Some("alice-topup@integration.lipa.swiss".to_string()),
"INVALID_IBAN".to_string(),
TopupCurrency::CHF,
"chf".to_string(),
);
assert!(matches!(result, Err(InvalidInput { .. })));

let expected_offer_count = node.query_uncompleted_offers().unwrap().len() + 1;

// `DK1125112511251125` triggers a new topup ready to be collected
node.register_fiat_topup(None, "DK1125112511251125".to_string(), TopupCurrency::EUR)
node.register_fiat_topup(None, "DK1125112511251125".to_string(), "eur".to_string())
.unwrap();

wait_for_condition!(
Expand All @@ -78,7 +74,7 @@ fn test_topup() {
let expected_offer_count = uncompleted_offers.len() + 1;

// `DK2225222522252225` triggers a new topup that is directly refunded
node.register_fiat_topup(None, "DK2225222522252225".to_string(), TopupCurrency::EUR)
node.register_fiat_topup(None, "DK2225222522252225".to_string(), "eur".to_string())
.unwrap();

wait_for_condition!(
Expand Down

0 comments on commit 922cc5a

Please sign in to comment.