Skip to content

Commit

Permalink
chore: use mint_url and wallet_keyset in wallet api
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Apr 2, 2024
1 parent dd8c8cd commit 96125ee
Show file tree
Hide file tree
Showing 14 changed files with 679 additions and 259 deletions.
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(())
}
46 changes: 31 additions & 15 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 @@ -161,22 +163,28 @@ async fn test_bolt11_mint() -> 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 @@ -187,9 +195,11 @@ 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);
}
Expand All @@ -198,7 +208,7 @@ async fn test_bolt11_mint() -> anyhow::Result<()> {
assert_eq!(5_000, balance);

// send tokens
let exported_tokens = wallet.send_tokens(100).await?;
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);
Expand Down Expand Up @@ -248,30 +258,36 @@ async fn test_bolt11_send() -> 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 = 2_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!(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(100).await?;
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);
Expand Down
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

0 comments on commit 96125ee

Please sign in to comment.