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

Wallet multi mint support #264

Merged
merged 6 commits into from
Apr 2, 2024
Merged
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
24 changes: 16 additions & 8 deletions integrationtests/tests/tests_lnbitsmock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,28 @@ pub async fn test_bolt11_lnbitsmock() -> anyhow::Result<()> {
let wallet = WalletBuilder::default()
.with_client(client)
.with_localstore(localstore)
.with_mint_url(mint_url)
.build()
.await?;
let wallet_keysets = wallet.add_mint_keysets(&mint_url).await?;
let wallet_keyset = wallet_keysets.first().unwrap(); // FIXME

// get initial balance
let balance = wallet.get_balance().await?;
assert_eq!(0, balance, "Initial balance should be 0");

// mint some tokens
let mint_amount = 6_000;
let mint_quote = wallet.create_quote_bolt11(mint_amount).await?;
let mint_quote = wallet.create_quote_bolt11(&mint_url, mint_amount).await?;
let hash = mint_quote.clone().quote;

sleep_until(Instant::now() + Duration::from_millis(1_000)).await;
let mint_result = wallet
.mint_tokens(&PaymentMethod::Bolt11, mint_amount.into(), hash.clone())
.mint_tokens(
wallet_keyset,
&PaymentMethod::Bolt11,
mint_amount.into(),
hash.clone(),
)
.await?;
assert_eq!(6_000, mint_result.total_amount());

Expand All @@ -78,9 +84,11 @@ pub async fn test_bolt11_lnbitsmock() -> anyhow::Result<()> {
// pay ln-invoice
let invoice_1000 = read_fixture("invoice_1000.txt")?;
let quote = wallet
.get_melt_quote_bolt11(invoice_1000.clone(), CurrencyUnit::Sat)
.get_melt_quote_bolt11(&mint_url, invoice_1000.clone(), CurrencyUnit::Sat)
.await?;
let result_pay_invoice = wallet.pay_invoice(&quote, invoice_1000).await;
let result_pay_invoice = wallet
.pay_invoice(wallet_keyset, &quote, invoice_1000)
.await;
if result_pay_invoice.is_err() {
println!("error in pay_invoice{:?}", result_pay_invoice);
}
Expand All @@ -90,20 +98,20 @@ pub async fn test_bolt11_lnbitsmock() -> anyhow::Result<()> {

// receive 10 sats
let token_10: moksha_core::token::TokenV3 = read_fixture("token_10.cashu")?.try_into()?;
let result_receive = wallet.receive_tokens(&token_10).await;
let result_receive = wallet.receive_tokens(wallet_keyset, &token_10).await;
assert!(result_receive.is_ok());
let balance = wallet.get_balance().await?;
assert_eq!(5_010, balance);

// send 10 tokens
let result_send = wallet.send_tokens(10).await;
let result_send = wallet.send_tokens(wallet_keyset, 10).await;
assert!(result_send.is_ok());
assert_eq!(10, result_send.unwrap().total_amount());
let balance = wallet.get_balance().await?;
assert_eq!(5_000, balance);

// get info
let info = wallet.get_mint_info().await?;
let info = wallet.get_mint_info(&mint_url).await?;
assert!(!info.nuts.nut4.disabled);
Ok(())
}
118 changes: 107 additions & 11 deletions integrationtests/tests/tests_lnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,18 @@ async fn test_btc_onchain_mint_melt() -> anyhow::Result<()> {
let wallet = WalletBuilder::default()
.with_client(client)
.with_localstore(localstore)
.with_mint_url(mint_url)
.build()
.await?;
let wallet_keysets = wallet.add_mint_keysets(&mint_url).await?;
let wallet_keyset = wallet_keysets.first().unwrap(); // FIXME

// get initial balance
let balance = wallet.get_balance().await?;
assert_eq!(0, balance, "Initial balance should be 0");

// mint 6_000 sats bitcoin onchain
let mint_amount = 60_000;
let mint_quote = wallet.create_quote_onchain(mint_amount).await?;
let mint_quote = wallet.create_quote_onchain(&mint_url, mint_amount).await?;

let btc_client = BitcoinClient::new_local().await?;
btc_client
Expand All @@ -92,6 +93,7 @@ async fn test_btc_onchain_mint_melt() -> anyhow::Result<()> {

let _mint_response = wallet
.mint_tokens(
wallet_keyset,
&PaymentMethod::BtcOnchain,
Amount(mint_amount),
mint_quote.quote,
Expand All @@ -104,15 +106,15 @@ async fn test_btc_onchain_mint_melt() -> anyhow::Result<()> {

let melt_amount = 21_000;
let melt_quotes = wallet
.get_melt_quote_btconchain(btc_address.clone(), melt_amount)
.get_melt_quote_btconchain(&mint_url, btc_address.clone(), melt_amount)
.await?;

let first_quote = melt_quotes.first().unwrap();
let result = wallet.pay_onchain(first_quote).await?;
let result = wallet.pay_onchain(wallet_keyset, first_quote).await?;
assert!(!result.paid);
btc_client.mine_blocks(1).await?;

let is_tx_paid = wallet.is_onchain_tx_paid(result.txid).await?;
let is_tx_paid = wallet.is_onchain_tx_paid(&mint_url, result.txid).await?;
assert!(is_tx_paid);

Ok(())
Expand Down Expand Up @@ -155,26 +157,34 @@ async fn test_bolt11_mint() -> anyhow::Result<()> {
assert!(keysets.is_ok());

// create wallet
let localstore = SqliteLocalStore::with_in_memory().await?;
let tmp_dir = tempfile::tempdir()?;
let wallet_file = tmp_dir.path().join("wallet.db");
let localstore = SqliteLocalStore::with_path(wallet_file.to_str().unwrap().to_owned()).await?;
let wallet = WalletBuilder::default()
.with_client(client)
.with_localstore(localstore)
.with_mint_url(mint_url)
.build()
.await?;
let wallet_keysets = wallet.add_mint_keysets(&mint_url).await?;
let wallet_keyset = wallet_keysets.first().unwrap(); // FIXME

// get initial balance
let balance = wallet.get_balance().await?;
assert_eq!(0, balance, "Initial balance should be 0");

// mint some tokens
let mint_amount = 6_000;
let mint_quote = wallet.create_quote_bolt11(mint_amount).await?;
let mint_quote = wallet.create_quote_bolt11(&mint_url, mint_amount).await?;
let hash = mint_quote.clone().quote;

sleep_until(Instant::now() + Duration::from_millis(1_000)).await;
let mint_result = wallet
.mint_tokens(&PaymentMethod::Bolt11, mint_amount.into(), hash.clone())
.mint_tokens(
wallet_keyset,
&PaymentMethod::Bolt11,
mint_amount.into(),
hash.clone(),
)
.await?;
assert_eq!(6_000, mint_result.total_amount());

Expand All @@ -185,15 +195,101 @@ async fn test_bolt11_mint() -> anyhow::Result<()> {
let wallet_lnd = LndClient::new_wallet_lnd().await?;
let invoice_1000 = wallet_lnd.create_invoice(1_000).await?;
let quote = wallet
.get_melt_quote_bolt11(invoice_1000.clone(), CurrencyUnit::Sat)
.get_melt_quote_bolt11(&mint_url, invoice_1000.clone(), CurrencyUnit::Sat)
.await?;
let result_pay_invoice = wallet.pay_invoice(&quote, invoice_1000).await;
let result_pay_invoice = wallet
.pay_invoice(wallet_keyset, &quote, invoice_1000)
.await;
if result_pay_invoice.is_err() {
println!("error in pay_invoice{:?}", result_pay_invoice);
}
assert!(result_pay_invoice.is_ok());
let balance = wallet.get_balance().await?;
assert_eq!(5_000, balance);

// send tokens
let exported_tokens = wallet.send_tokens(wallet_keyset, 100).await?;
assert_eq!(100, exported_tokens.total_amount());
let balance = wallet.get_balance().await?;
assert_eq!(4_900, balance);
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_bolt11_send() -> anyhow::Result<()> {
// create postgres container that will be destroyed after the test is done
let docker = clients::Cli::default();
let node = Postgres::default().with_host_auth();
let img = RunnableImage::from(node).with_tag("16.2-alpine");
let node = docker.run(img);
let host_port = node.get_host_port_ipv4(5432);

fund_mint_lnd(2_000_000).await?;
open_channel_with_wallet(500_000).await?;

// start mint server
tokio::spawn(async move {
let lnd_settings = LndLightningSettings::new(
lnd_client::LND_MINT_ADDRESS.parse().expect("invalid url"),
"../data/lnd-mint/tls.cert".into(),
"../data/lnd-mint/data/chain/bitcoin/regtest/admin.macaroon".into(),
);

start_mint(host_port, LightningType::Lnd(lnd_settings.clone()), None)
.await
.expect("Could not start mint server");
});

// Wait for the server to start
tokio::time::sleep(Duration::from_millis(800)).await;

let client = CrossPlatformHttpClient::new();
let mint_url = Url::parse("http://127.0.0.1:8686")?;
let keys = client.get_keys(&mint_url).await;
assert!(keys.is_ok());

let keysets = client.get_keysets(&mint_url).await;
assert!(keysets.is_ok());

// create wallet
let tmp_dir = tempfile::tempdir()?;
let wallet_file = tmp_dir.path().join("wallet.db");
let localstore = SqliteLocalStore::with_path(wallet_file.to_str().unwrap().to_owned()).await?;
let wallet = WalletBuilder::default()
.with_client(client)
.with_localstore(localstore)
.build()
.await?;
let wallet_keysets = wallet.add_mint_keysets(&mint_url).await?;
let wallet_keyset = wallet_keysets.first().unwrap(); // FIXME

// get initial balance
let balance = wallet.get_balance().await?;
assert_eq!(0, balance, "Initial balance should be 0");

// mint some tokens
let mint_amount = 2_000;
let mint_quote = wallet.create_quote_bolt11(&mint_url, mint_amount).await?;
let hash = mint_quote.clone().quote;

sleep_until(Instant::now() + Duration::from_millis(1_000)).await;
let mint_result = wallet
.mint_tokens(
wallet_keyset,
&PaymentMethod::Bolt11,
mint_amount.into(),
hash.clone(),
)
.await?;
assert_eq!(2_000, mint_result.total_amount());

let balance = wallet.get_balance().await?;
assert_eq!(2_000, balance);

// send tokens
let exported_tokens = wallet.send_tokens(wallet_keyset, 100).await?;
assert_eq!(100, exported_tokens.total_amount());
let balance = wallet.get_balance().await?;
assert_eq!(1_900, balance);
Ok(())
}
3 changes: 1 addition & 2 deletions integrationtests/tests/tests_moksha_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ async fn test_cli() -> anyhow::Result<()> {

// compile the moksha-cli binary and run it
let mut cmd = Command::cargo_bin("moksha-cli")?;
cmd.arg("-m").arg("http://127.0.0.1:8686").arg("info");
cmd.arg("info");
let output = cmd.unwrap();
assert!(output.status.success());
println!("output: {:?}", output.status);
Ok(())
}
20 changes: 14 additions & 6 deletions integrationtests/tests/tests_nutshell_compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ pub async fn test_nutshell_compatibility() -> anyhow::Result<()> {
let wallet = WalletBuilder::default()
.with_client(client)
.with_localstore(localstore)
.with_mint_url(mint_url)
.build()
.await?;
let wallet_keysets = wallet.add_mint_keysets(&mint_url).await?;
let wallet_keyset = wallet_keysets.first().unwrap(); // FIXME

// check if mint info is correct
let mint_info = wallet.get_mint_info().await?;
let mint_info = wallet.get_mint_info(&mint_url).await?;
assert_eq!(Some("nutshell".to_owned()), mint_info.name);

// get initial balance
Expand All @@ -38,12 +39,17 @@ pub async fn test_nutshell_compatibility() -> anyhow::Result<()> {

// mint some tokens
let mint_amount = 6_000;
let mint_quote = wallet.create_quote_bolt11(mint_amount).await?;
let mint_quote = wallet.create_quote_bolt11(&mint_url, mint_amount).await?;
let hash = mint_quote.clone().quote;

sleep_until(Instant::now() + Duration::from_millis(1_000)).await;
let mint_result = wallet
.mint_tokens(&PaymentMethod::Bolt11, mint_amount.into(), hash.clone())
.mint_tokens(
wallet_keyset,
&PaymentMethod::Bolt11,
mint_amount.into(),
hash.clone(),
)
.await?;
assert_eq!(6_000, mint_result.total_amount());

Expand All @@ -53,10 +59,12 @@ pub async fn test_nutshell_compatibility() -> anyhow::Result<()> {
// pay ln-invoice (10_000 invoice + 10 sats fee_reserve / 9 sats get returned)
let invoice_1000 = read_fixture("invoice_1000.txt")?;
let quote = wallet
.get_melt_quote_bolt11(invoice_1000.clone(), CurrencyUnit::Sat)
.get_melt_quote_bolt11(&mint_url, invoice_1000.clone(), CurrencyUnit::Sat)
.await?;
assert_eq!(10, quote.fee_reserve);
let result_pay_invoice = wallet.pay_invoice(&quote, invoice_1000).await;
let result_pay_invoice = wallet
.pay_invoice(wallet_keyset, &quote, invoice_1000)
.await;

if result_pay_invoice.is_err() {
println!("error in pay_invoice{:?}", result_pay_invoice);
Expand Down
3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ run-mint *ARGS:

# run cli-wallet with the given args
run-cli *ARGS:
RUST_BACKTRACE=1 cargo run --bin moksha-cli -- -m http://127.0.0.1:3338 -d ./data/wallet {{ARGS}}
RUST_BACKTRACE=1 cargo run --bin moksha-cli -- --db-dir ./data/wallet {{ARGS}}


# runs all tests
run-tests:
Expand Down
Loading
Loading