Skip to main content

cdk_mintd/env_vars/
lnd.rs

1//! LND environment variables
2
3use std::env;
4use std::path::PathBuf;
5
6use anyhow::{bail, Context, Result};
7
8use crate::config::Lnd;
9
10// LND environment variables
11pub const ENV_LND_ADDRESS: &str = "CDK_MINTD_LND_ADDRESS";
12pub const ENV_LND_CERT_FILE: &str = "CDK_MINTD_LND_CERT_FILE";
13pub const ENV_LND_MACAROON_FILE: &str = "CDK_MINTD_LND_MACAROON_FILE";
14pub const ENV_LND_FEE_PERCENT: &str = "CDK_MINTD_LND_FEE_PERCENT";
15pub const ENV_LND_RESERVE_FEE_MIN: &str = "CDK_MINTD_LND_RESERVE_FEE_MIN";
16
17impl Lnd {
18    pub fn from_env(mut self) -> Result<Self> {
19        if let Ok(address) = env::var(ENV_LND_ADDRESS) {
20            self.address = address;
21        }
22
23        if let Ok(cert_path) = env::var(ENV_LND_CERT_FILE) {
24            self.cert_file = PathBuf::from(cert_path);
25        }
26
27        if let Ok(macaroon_path) = env::var(ENV_LND_MACAROON_FILE) {
28            self.macaroon_file = PathBuf::from(macaroon_path);
29        }
30
31        if let Ok(fee_str) = env::var(ENV_LND_FEE_PERCENT) {
32            let fee = fee_str.parse::<f32>().with_context(|| {
33                format!("{ENV_LND_FEE_PERCENT} must be a floating-point number")
34            })?;
35            if !fee.is_finite() || !(0.0..=1.0).contains(&fee) {
36                bail!(
37                    "{ENV_LND_FEE_PERCENT} must be finite and between 0.0 and 1.0 inclusive (0.02 means 2%)"
38                );
39            }
40            self.fee_percent = fee;
41        }
42
43        if let Ok(reserve_fee_str) = env::var(ENV_LND_RESERVE_FEE_MIN) {
44            if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
45                self.reserve_fee_min = reserve_fee.into();
46            }
47        }
48
49        Ok(self)
50    }
51}