Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat: payment processor #597

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ jobs:
-p cdk-axum,
-p cdk-cln,
-p cdk-lnd,
-p cdk-phoenixd,
-p cdk-strike,
-p cdk-lnbits,
-p cdk-fake-wallet,
-p cdk-payment-processor,
--bin cdk-cli,
--bin cdk-mintd,
--bin cdk-mintd --no-default-features --features swagger,
Expand Down Expand Up @@ -210,12 +209,11 @@ jobs:
-p cdk --no-default-features --features "mint mint",
-p cdk-axum,
-p cdk-axum --no-default-features --features redis,
-p cdk-strike,
-p cdk-lnbits,
-p cdk-phoenixd,
-p cdk-fake-wallet,
-p cdk-cln,
-p cdk-mint-rpc,
-p cdk-payment-processor,
]
steps:
- name: checkout
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ impl Cln {
impl MintLightning for Cln {
type Err = cdk_lightning::Error;

fn get_settings(&self) -> Settings {
Settings {
async fn get_settings(&self) -> Result<Settings, Self::Err> {
Ok(Settings {
mpp: true,
unit: CurrencyUnit::Msat,
invoice_description: true,
}
})
}

/// Is wait invoice active
Expand Down
8 changes: 7 additions & 1 deletion crates/cdk-common/src/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ pub enum Error {
/// Amount Error
#[error(transparent)]
Amount(#[from] crate::amount::Error),
/// NUT04 Error
#[error(transparent)]
NUT04(#[from] crate::nuts::nut04::Error),
/// NUT05 Error
#[error(transparent)]
NUT05(#[from] crate::nuts::nut05::Error),
/// Custom
#[error("`{0}`")]
Custom(String),
}

/// MintLighting Trait
Expand All @@ -53,7 +59,7 @@ pub trait MintLightning {
type Err: Into<Error> + From<Error>;

/// Base Unit
fn get_settings(&self) -> Settings;
async fn get_settings(&self) -> Result<Settings, Self::Err>;

/// Create a new invoice
async fn create_invoice(
Expand Down
1 change: 1 addition & 0 deletions crates/cdk-fake-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ description = "CDK fake ln backend"

[dependencies]
async-trait = "0.1.74"
anyhow = "1"
bitcoin = { version = "0.32.2", default-features = false }
cdk = { path = "../cdk", version = "0.7.1", default-features = false, features = ["mint"] }
futures = { version = "0.3.28", default-features = false }
Expand Down
10 changes: 5 additions & 5 deletions crates/cdk-fake-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct FakeWallet {
}

impl FakeWallet {
/// Creat new [`FakeWallet`]
/// Create new [`FakeWallet`]
pub fn new(
fee_reserve: FeeReserve,
payment_states: HashMap<String, MeltQuoteState>,
Expand Down Expand Up @@ -99,12 +99,12 @@ impl Default for FakeInvoiceDescription {
impl MintLightning for FakeWallet {
type Err = cdk_lightning::Error;

fn get_settings(&self) -> Settings {
Settings {
async fn get_settings(&self) -> Result<Settings, Self::Err> {
Ok(Settings {
mpp: true,
unit: CurrencyUnit::Msat,
invoice_description: true,
}
})
}

fn is_wait_invoice_active(&self) -> bool {
Expand Down Expand Up @@ -266,7 +266,7 @@ impl MintLightning for FakeWallet {
payment_lookup_id: request_lookup_id.to_string(),
status,
total_spent: Amount::ZERO,
unit: self.get_settings().unit,
unit: self.get_settings().await?.unit,
})
}
}
Expand Down
28 changes: 16 additions & 12 deletions crates/cdk-integration-tests/src/init_fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ where
let localstore = Arc::new(database);
mint_builder = mint_builder.with_localstore(localstore.clone());

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 5_000),
Arc::new(fake_wallet),
);
mint_builder = mint_builder
.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 5_000),
Arc::new(fake_wallet),
)
.await?;

let fee_reserve = FeeReserve {
min_fee_reserve: 1.into(),
Expand All @@ -55,12 +57,14 @@ where

let fake_wallet = FakeWallet::new(fee_reserve, HashMap::default(), HashSet::default(), 0);

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Usd,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 5_000),
Arc::new(fake_wallet),
);
mint_builder = mint_builder
.add_ln_backend(
CurrencyUnit::Usd,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 5_000),
Arc::new(fake_wallet),
)
.await?;

let mnemonic = Mnemonic::generate(12)?;

Expand Down
18 changes: 10 additions & 8 deletions crates/cdk-integration-tests/src/init_pure_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,22 @@ pub async fn create_and_start_test_mint() -> anyhow::Result<Arc<Mint>> {
percent_fee_reserve: 1.0,
};

let ln_fake_backend = Arc::new(FakeWallet::new(
let ln_fake_backend = FakeWallet::new(
fee_reserve.clone(),
HashMap::default(),
HashSet::default(),
0,
));

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 1_000),
ln_fake_backend,
);

mint_builder = mint_builder
.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 1_000),
Arc::new(ln_fake_backend),
)
.await?;

let mnemonic = Mnemonic::generate(12)?;

mint_builder = mint_builder
Expand Down
14 changes: 8 additions & 6 deletions crates/cdk-integration-tests/src/init_regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,14 @@ where
let localstore = Arc::new(database);
mint_builder = mint_builder.with_localstore(localstore.clone());

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 5_000),
Arc::new(lighting),
);
mint_builder = mint_builder
.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 5_000),
Arc::new(lighting),
)
.await?;

let mnemonic = Mnemonic::generate(12)?;

Expand Down
14 changes: 8 additions & 6 deletions crates/cdk-integration-tests/tests/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,14 @@ async fn test_correct_keyset() -> Result<()> {
let localstore = Arc::new(database);
mint_builder = mint_builder.with_localstore(localstore.clone());

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 5_000),
Arc::new(fake_wallet),
);
mint_builder = mint_builder
.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::new(1, 5_000),
Arc::new(fake_wallet),
)
.await?;

mint_builder = mint_builder
.with_name("regtest mint".to_string())
Expand Down
8 changes: 4 additions & 4 deletions crates/cdk-lnbits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ impl LNbits {
impl MintLightning for LNbits {
type Err = cdk_lightning::Error;

fn get_settings(&self) -> Settings {
Settings {
async fn get_settings(&self) -> Result<Settings, Self::Err> {
Ok(Settings {
mpp: false,
unit: CurrencyUnit::Sat,
invoice_description: true,
}
})
}

fn is_wait_invoice_active(&self) -> bool {
Expand Down Expand Up @@ -312,7 +312,7 @@ impl MintLightning for LNbits {
payment.details.amount.unsigned_abs()
+ payment.details.fee.unsigned_abs() / MSAT_IN_SAT,
),
unit: self.get_settings().unit,
unit: self.get_settings().await?.unit,
};

Ok(pay_response)
Expand Down
12 changes: 6 additions & 6 deletions crates/cdk-lnd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ impl MintLightning for Lnd {
type Err = cdk_lightning::Error;

#[instrument(skip_all)]
fn get_settings(&self) -> Settings {
Settings {
async fn get_settings(&self) -> Result<Settings, Self::Err> {
Ok(Settings {
mpp: true,
unit: CurrencyUnit::Msat,
invoice_description: true,
}
})
}

#[instrument(skip_all)]
Expand Down Expand Up @@ -477,7 +477,7 @@ impl MintLightning for Lnd {
payment_preimage: None,
status: MeltQuoteState::Unknown,
total_spent: Amount::ZERO,
unit: self.get_settings().unit,
unit: self.get_settings().await?.unit,
});
} else {
return Err(cdk_lightning::Error::UnknownPaymentState);
Expand All @@ -496,7 +496,7 @@ impl MintLightning for Lnd {
payment_preimage: Some(update.payment_preimage),
status: MeltQuoteState::Unknown,
total_spent: Amount::ZERO,
unit: self.get_settings().unit,
unit: self.get_settings().await?.unit,
},
PaymentStatus::InFlight => {
// Continue waiting for the next update
Expand All @@ -520,7 +520,7 @@ impl MintLightning for Lnd {
payment_preimage: Some(update.payment_preimage),
status: MeltQuoteState::Failed,
total_spent: Amount::ZERO,
unit: self.get_settings().unit,
unit: self.get_settings().await?.unit,
},
};

Expand Down
3 changes: 1 addition & 2 deletions crates/cdk-mintd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ cdk-sqlite = { path = "../cdk-sqlite", version = "0.7.1", default-features = fal
] }
cdk-cln = { path = "../cdk-cln", version = "0.7.1", default-features = false }
cdk-lnbits = { path = "../cdk-lnbits", version = "0.7.1", default-features = false }
cdk-phoenixd = { path = "../cdk-phoenixd", version = "0.7.1", default-features = false }
cdk-lnd = { path = "../cdk-lnd", version = "0.7.1", default-features = false }
cdk-fake-wallet = { path = "../cdk-fake-wallet", version = "0.7.1", default-features = false }
cdk-strike = { path = "../cdk-strike", version = "0.7.1" }
cdk-axum = { path = "../cdk-axum", version = "0.7.1", default-features = false }
cdk-mint-rpc = { path = "../cdk-mint-rpc", version = "0.7.1", default-features = false, optional = true }
cdk-payment-processor = { path = "../cdk-payment-processor", version = "0.7.1", default-features = false }
config = { version = "0.13.3", features = ["toml"] }
clap = { version = "~4.0.32", features = ["derive"] }
bitcoin = { version = "0.32.2", features = [
Expand Down
29 changes: 0 additions & 29 deletions crates/cdk-mintd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ pub enum LnBackend {
#[default]
None,
Cln,
Strike,
LNbits,
FakeWallet,
Phoenixd,
Lnd,
}

Expand All @@ -60,10 +58,8 @@ impl std::str::FromStr for LnBackend {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"cln" => Ok(LnBackend::Cln),
"strike" => Ok(LnBackend::Strike),
"lnbits" => Ok(LnBackend::LNbits),
"fakewallet" => Ok(LnBackend::FakeWallet),
"phoenixd" => Ok(LnBackend::Phoenixd),
"lnd" => Ok(LnBackend::Lnd),
_ => Err(format!("Unknown Lightning backend: {}", s)),
}
Expand Down Expand Up @@ -93,12 +89,6 @@ impl Default for Ln {
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Strike {
pub api_key: String,
pub supported_units: Option<Vec<CurrencyUnit>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct LNbits {
pub admin_api_key: String,
Expand Down Expand Up @@ -126,15 +116,6 @@ pub struct Lnd {
pub reserve_fee_min: Amount,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Phoenixd {
pub api_password: String,
pub api_url: String,
pub bolt12: bool,
pub fee_percent: f32,
pub reserve_fee_min: Amount,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FakeWallet {
pub supported_units: Vec<CurrencyUnit>,
Expand Down Expand Up @@ -199,9 +180,7 @@ pub struct Settings {
pub mint_info: MintInfo,
pub ln: Ln,
pub cln: Option<Cln>,
pub strike: Option<Strike>,
pub lnbits: Option<LNbits>,
pub phoenixd: Option<Phoenixd>,
pub lnd: Option<Lnd>,
pub fake_wallet: Option<FakeWallet>,
pub database: Database,
Expand Down Expand Up @@ -291,18 +270,10 @@ impl Settings {
settings.cln.is_some(),
"CLN backend requires a valid config."
),
LnBackend::Strike => assert!(
settings.strike.is_some(),
"Strike backend requires a valid config."
),
LnBackend::LNbits => assert!(
settings.lnbits.is_some(),
"LNbits backend requires a valid config"
),
LnBackend::Phoenixd => assert!(
settings.phoenixd.is_some(),
"Phoenixd backend requires a valid config"
),
LnBackend::Lnd => {
assert!(
settings.lnd.is_some(),
Expand Down
Loading
Loading