diff --git a/crates/tests-e2e/src/fund.rs b/crates/tests-e2e/src/fund.rs index d2ab53c8b..52e453f5e 100644 --- a/crates/tests-e2e/src/fund.rs +++ b/crates/tests-e2e/src/fund.rs @@ -1,21 +1,25 @@ +use crate::app::AppHandle; +use crate::wait_until; use anyhow::bail; use anyhow::Result; use native::api; +use native::api::PaymentFlow; +use native::api::Status; +use native::api::WalletHistoryItem; use reqwest::Client; use serde::Deserialize; use tokio::task::spawn_blocking; -// TODO: Fetch these from the app -pub const FUNDING_TRANSACTION_FEES: u64 = 153; - -/// Pay a lightning invoice using an LND faucet -/// -/// Returns the funded amount (in satoshis) -pub async fn fund_app_with_faucet(client: &Client, funding_amount: u64) -> Result { - let invoice = spawn_blocking(move || { - api::create_invoice_with_amount(funding_amount).expect("to succeed") - }) - .await?; +/// Instruct the LND faucet to pay an invoice generated with the purpose of opening a JIT channel +/// between the coordinator and an app. +pub async fn fund_app_with_faucet( + app: &AppHandle, + client: &Client, + fund_amount: u64, +) -> Result<()> { + let invoice = + spawn_blocking(move || api::create_invoice_with_amount(fund_amount).expect("to succeed")) + .await?; api::decode_invoice(invoice.clone()).expect("to decode invoice we created"); pay_with_faucet(client, invoice).await?; @@ -23,7 +27,51 @@ pub async fn fund_app_with_faucet(client: &Client, funding_amount: u64) -> Resul // Ensure we sync the wallet info after funding spawn_blocking(move || api::refresh_wallet_info().expect("to succeed")).await?; - Ok(funding_amount - FUNDING_TRANSACTION_FEES) + // Wait until the app has an outbound payment which should correspond to the channel-opening fee + wait_until!(app + .rx + .wallet_info() + .expect("to have wallet info") + .history + .iter() + .any(|item| matches!( + item, + WalletHistoryItem { + flow: PaymentFlow::Outbound, + status: Status::Confirmed, + .. + } + ))); + + let order_matching_fee = app + .rx + .wallet_info() + .expect("to have wallet info") + .history + .iter() + .find_map(|item| match item { + WalletHistoryItem { + flow: PaymentFlow::Outbound, + status: Status::Confirmed, + amount_sats, + .. + } => Some(amount_sats), + _ => None, + }) + .copied() + .expect("to have an order-matching fee"); + + tracing::info!(%fund_amount, %order_matching_fee, "Successfully funded app with faucet"); + assert_eq!( + app.rx + .wallet_info() + .expect("to have wallet info") + .balances + .lightning, + fund_amount - order_matching_fee + ); + + Ok(()) } async fn pay_with_faucet(client: &Client, invoice: String) -> Result<()> { diff --git a/crates/tests-e2e/src/setup.rs b/crates/tests-e2e/src/setup.rs index 0c8033ce9..4cea7d3c3 100644 --- a/crates/tests-e2e/src/setup.rs +++ b/crates/tests-e2e/src/setup.rs @@ -61,7 +61,8 @@ impl TestSetup { "App should start with empty wallet" ); - let funded_amount = fund_app_with_faucet(&client, 50_000) + let fund_amount = 50_000; + fund_app_with_faucet(&app, &client, fund_amount) .await .expect("to be able to fund"); @@ -71,11 +72,7 @@ impl TestSetup { .expect("to have wallet info") .balances .lightning; - tracing::info!(%funded_amount, %ln_balance, "Successfully funded app with faucet"); - - if funded_amount != ln_balance { - tracing::warn!("Expected funded amount does not match ln balance!"); - } + tracing::info!(%fund_amount, %ln_balance, "Successfully funded app with faucet"); assert!(ln_balance > 0, "App wallet should be funded"); diff --git a/crates/tests-e2e/tests/basic.rs b/crates/tests-e2e/tests/basic.rs index 3e3e55005..e5191cc37 100644 --- a/crates/tests-e2e/tests/basic.rs +++ b/crates/tests-e2e/tests/basic.rs @@ -34,17 +34,10 @@ async fn app_can_be_funded_with_lnd_faucet() -> Result<()> { let app = run_app().await; // Unfunded wallet should be empty - assert_eq!(app.rx.wallet_info().unwrap().balances.on_chain, 0); assert_eq!(app.rx.wallet_info().unwrap().balances.lightning, 0); - let funded_amount = fund_app_with_faucet(&client, 50_000).await?; + let fund_amount = 50_000; + fund_app_with_faucet(&app, &client, fund_amount).await?; - assert_eq!(app.rx.wallet_info().unwrap().balances.on_chain, 0); - - tracing::info!(%funded_amount, "Successfully funded app with faucet"); - assert_eq!( - app.rx.wallet_info().unwrap().balances.lightning, - funded_amount - ); Ok(()) }