From 0578ae1b06f531e0e9239fabb606099eb1a3143e Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Mon, 11 Dec 2023 13:47:51 +0100 Subject: [PATCH] chore: add example for receiving tokens --- Cargo.lock | 4 ++-- flutter/native/src/api.rs | 3 +-- integrationtests/Cargo.toml | 2 +- integrationtests/tests/tests.rs | 2 -- moksha-cli/src/main.rs | 3 +-- moksha-fedimint/Cargo.toml | 2 +- moksha-wallet/Cargo.toml | 4 ++-- moksha-wallet/examples/receive_tokens.rs | 25 ++++++++++++++++++++++++ moksha-wallet/src/localstore/memory.rs | 2 -- moksha-wallet/src/localstore/mod.rs | 2 -- moksha-wallet/src/localstore/rexie.rs | 2 -- moksha-wallet/src/localstore/sqlite.rs | 25 +++++++++++------------- moksha-wallet/src/wallet.rs | 2 -- moksha-wallet/tests/tests.rs | 1 - 14 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 moksha-wallet/examples/receive_tokens.rs diff --git a/Cargo.lock b/Cargo.lock index 2c2aa11d..0100406a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4334,9 +4334,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.34.0" +version = "1.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9" +checksum = "841d45b238a16291a4e1584e61820b8ae57d696cc5015c459c229ccc6990cc1c" dependencies = [ "backtrace", "bytes", diff --git a/flutter/native/src/api.rs b/flutter/native/src/api.rs index 22245b68..41a3f99d 100644 --- a/flutter/native/src/api.rs +++ b/flutter/native/src/api.rs @@ -65,12 +65,11 @@ pub fn init_cashu() -> anyhow::Result { // FIXME let db_path = moksha_wallet::config_path::db_path(); - let new_localstore = + let _new_localstore = moksha_wallet::localstore::sqlite::SqliteLocalStore::with_path(db_path.clone()) .await .map_err(anyhow::Error::from) .unwrap(); - new_localstore.migrate().await; db_path }); drop(rt); diff --git a/integrationtests/Cargo.toml b/integrationtests/Cargo.toml index 21edcca9..0bfe1ca3 100644 --- a/integrationtests/Cargo.toml +++ b/integrationtests/Cargo.toml @@ -32,7 +32,7 @@ secp256k1 = { version = "0.24.0", default-features = false, features = ["recover tracing = "0.1.40" [target.'cfg(not(target_family="wasm"))'.dependencies] -tokio = { version = "1.34.0", features = ["sync", "rt"] } +tokio = { version = "1.35.0", features = ["sync", "rt"] } [dev-dependencies] anyhow = { version = "1.0.75", features = ["backtrace"] } diff --git a/integrationtests/tests/tests.rs b/integrationtests/tests/tests.rs index ad389c24..f5af26e1 100644 --- a/integrationtests/tests/tests.rs +++ b/integrationtests/tests/tests.rs @@ -1,7 +1,6 @@ use moksha_wallet::client::reqwest::HttpClient; use moksha_wallet::client::Client; use moksha_wallet::localstore::sqlite::SqliteLocalStore; -use moksha_wallet::localstore::LocalStore; use moksha_wallet::wallet::WalletBuilder; use mokshamint::lightning::{LightningType, LnbitsLightningSettings}; use mokshamint::mint::Mint; @@ -73,7 +72,6 @@ pub fn test_integration() -> anyhow::Result<()> { let localstore = SqliteLocalStore::with_path(format!("{tmp_dir}/test_wallet.db")) .await .expect("Could not create localstore"); - localstore.migrate().await; let wallet = WalletBuilder::default() .with_client(client) diff --git a/moksha-cli/src/main.rs b/moksha-cli/src/main.rs index 9149b811..a339e726 100644 --- a/moksha-cli/src/main.rs +++ b/moksha-cli/src/main.rs @@ -46,7 +46,7 @@ enum Command { #[cfg(not(target_arch = "wasm32"))] #[tokio::main] async fn main() -> anyhow::Result<()> { - use moksha_wallet::localstore::{sqlite::SqliteLocalStore, LocalStore}; + use moksha_wallet::localstore::sqlite::SqliteLocalStore; let cli = Opts::parse(); @@ -60,7 +60,6 @@ async fn main() -> anyhow::Result<()> { }; let localstore = SqliteLocalStore::with_path(db_path.clone()).await?; - localstore.migrate().await; let client = moksha_wallet::client::reqwest::HttpClient::new(); diff --git a/moksha-fedimint/Cargo.toml b/moksha-fedimint/Cargo.toml index c553e9c9..a9b94788 100644 --- a/moksha-fedimint/Cargo.toml +++ b/moksha-fedimint/Cargo.toml @@ -26,7 +26,7 @@ fedimint-wallet-client = { git = "https://github.com/fedimint/fedimint", tag = " futures = "0.3.29" [target.'cfg(not(target_family="wasm"))'.dependencies] -tokio = { version = "1.34.0", features = ["rt"] } +tokio = { version = "1.35.0", features = ["rt"] } fedimint-rocksdb = { git = "https://github.com/fedimint/fedimint", tag = "v0.1.5" } [target.'cfg(target_family = "wasm")'.dependencies] diff --git a/moksha-wallet/Cargo.toml b/moksha-wallet/Cargo.toml index 62c04a27..97d3e6b5 100644 --- a/moksha-wallet/Cargo.toml +++ b/moksha-wallet/Cargo.toml @@ -38,8 +38,8 @@ tokio = { version = "1.34.0", features = ["rt", "sync"] } [target.'cfg(not(target_family="wasm"))'.dependencies] reqwest = { version = "0.11.22", features = ["serde_json", "json", "rustls-tls"], default-features = false } -tokio = { version = "1.34.0", features = ["rt", "rt-multi-thread", "macros"] } -sqlx = { version = "0.7.2", default-features = false, features = ["sqlite", "runtime-tokio-rustls", "migrate", "macros"] } +tokio = { version = "1.35.0", features = ["rt", "rt-multi-thread", "macros"] } +sqlx = { version = "0.7.3", default-features = false, features = ["sqlite", "runtime-tokio-rustls", "migrate", "macros"] } [dev-dependencies] tempfile = "3.8.1" diff --git a/moksha-wallet/examples/receive_tokens.rs b/moksha-wallet/examples/receive_tokens.rs new file mode 100644 index 00000000..50c66505 --- /dev/null +++ b/moksha-wallet/examples/receive_tokens.rs @@ -0,0 +1,25 @@ +use std::env::temp_dir; + +use moksha_core::token::TokenV3; +use moksha_wallet::{ + client::reqwest::HttpClient, localstore::sqlite::SqliteLocalStore, wallet::WalletBuilder, +}; +use url::Url; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let db_path = temp_dir().join("wallet.db").to_str().unwrap().to_string(); + let localstore = SqliteLocalStore::with_path(db_path).await?; + let client = HttpClient::new(); + let wallet = WalletBuilder::default() + .with_client(client) + .with_localstore(localstore) + .with_mint_url(Url::parse("https://mutinynet.moksha.cash:3338")?) + .build() + .await?; + let tokens = TokenV3::deserialize("cashuAeyJ0b2tlbiI6IFt7InByb29mcyI6IFt7ImlkIjogIjAwOTkxZjRmMjc3MzMzOGMiLCAiYW1vdW50IjogMiwgInNlY3JldCI6ICI5ZmFjZWE0Y2QzN2I3ZWRlOGE4NmQzYWY1ZWIxZTczNzIxMDNmZDE2YTQ1M2E5NDQ5YjE0MDFkZDhhMzAzMWJiIiwgIkMiOiAiMDM2ZTVhOWJhOWE1ZjYxZmQ5MTk3YzM2OTgzZjc1YzAzYTUyYzc0YTJmZmM2NTBmNzg5MjJlMDcyZWY1MTI0YjZlIn1dLCAibWludCI6ICJodHRwczovL21pbnQubXV0aW55bmV0Lm1va3NoYS5jYXNoOjMzMzgifV19")?; + wallet.receive_tokens(&tokens).await?; + let balance = wallet.get_balance().await?; + println!("New balance: {} sats", balance); + Ok(()) +} diff --git a/moksha-wallet/src/localstore/memory.rs b/moksha-wallet/src/localstore/memory.rs index 68d85c5f..9bccb403 100644 --- a/moksha-wallet/src/localstore/memory.rs +++ b/moksha-wallet/src/localstore/memory.rs @@ -15,8 +15,6 @@ pub struct MemoryLocalStore { #[async_trait(?Send)] impl LocalStore for MemoryLocalStore { - async fn migrate(&self) {} - async fn add_proofs(&self, proofs: &Proofs) -> Result<(), MokshaWalletError> { for proof in proofs.proofs() { self.proofs.lock().await.push(proof.clone()); diff --git a/moksha-wallet/src/localstore/mod.rs b/moksha-wallet/src/localstore/mod.rs index 08e79cb7..e7549bc6 100644 --- a/moksha-wallet/src/localstore/mod.rs +++ b/moksha-wallet/src/localstore/mod.rs @@ -25,6 +25,4 @@ pub trait LocalStore { async fn get_keysets(&self) -> Result, MokshaWalletError>; async fn add_keyset(&self, keyset: &WalletKeyset) -> Result<(), MokshaWalletError>; - - async fn migrate(&self); } diff --git a/moksha-wallet/src/localstore/rexie.rs b/moksha-wallet/src/localstore/rexie.rs index 688c8376..8bd8d983 100644 --- a/moksha-wallet/src/localstore/rexie.rs +++ b/moksha-wallet/src/localstore/rexie.rs @@ -37,8 +37,6 @@ impl RexieLocalStore { #[async_trait(?Send)] impl LocalStore for RexieLocalStore { - async fn migrate(&self) {} - async fn add_proofs(&self, proofs: &Proofs) -> std::result::Result<(), MokshaWalletError> { let db = Self::get_rexie().await; diff --git a/moksha-wallet/src/localstore/sqlite.rs b/moksha-wallet/src/localstore/sqlite.rs index adb3d554..35dae909 100644 --- a/moksha-wallet/src/localstore/sqlite.rs +++ b/moksha-wallet/src/localstore/sqlite.rs @@ -4,7 +4,7 @@ use moksha_core::proof::{Proof, Proofs}; use crate::error::MokshaWalletError; use crate::localstore::{LocalStore, WalletKeyset}; -use sqlx::{sqlite::SqliteError, SqlitePool}; +use sqlx::sqlite::SqliteError; use sqlx::Row; @@ -15,13 +15,6 @@ pub struct SqliteLocalStore { #[async_trait(?Send)] impl LocalStore for SqliteLocalStore { - async fn migrate(&self) { - sqlx::migrate!("./migrations") - .run(&self.pool) - .await - .expect("Could not run migrations"); - } - async fn delete_proofs(&self, proofs: &Proofs) -> Result<(), MokshaWalletError> { let proof_secrets = proofs .proofs() @@ -109,14 +102,19 @@ impl LocalStore for SqliteLocalStore { } impl SqliteLocalStore { - pub fn new(pool: SqlitePool) -> Self { - Self { pool } - } - pub async fn with_path(absolute_path: String) -> Result { let pool = sqlx::SqlitePool::connect(format!("sqlite://{absolute_path}?mode=rwc").as_str()) .await?; - Ok(Self { pool }) + let store = Self { pool }; + store.migrate().await; + Ok(store) + } + + async fn migrate(&self) { + sqlx::migrate!("./migrations") + .run(&self.pool) + .await + .expect("Could not run migrations"); } pub async fn start_transaction( @@ -171,7 +169,6 @@ mod tests { let localstore: Arc = Arc::new(SqliteLocalStore::with_path(format!("{tmp_dir}/test_wallet.db")).await?); - localstore.migrate().await; let tokens: TokenV3 = read_fixture("token_60.cashu")? .trim() diff --git a/moksha-wallet/src/wallet.rs b/moksha-wallet/src/wallet.rs index 0349f973..6d8f2eb0 100644 --- a/moksha-wallet/src/wallet.rs +++ b/moksha-wallet/src/wallet.rs @@ -484,8 +484,6 @@ mod tests { #[async_trait(?Send)] impl LocalStore for MockLocalStore { - async fn migrate(&self) {} - async fn add_proofs(&self, _: &Proofs) -> Result<(), crate::error::MokshaWalletError> { Ok(()) } diff --git a/moksha-wallet/tests/tests.rs b/moksha-wallet/tests/tests.rs index 19c7f0cc..d2446f5c 100644 --- a/moksha-wallet/tests/tests.rs +++ b/moksha-wallet/tests/tests.rs @@ -120,7 +120,6 @@ async fn test_pay_invoice_can_not_melt() -> anyhow::Result<()> { let localstore = SqliteLocalStore::with_path(format!("{tmp_dir}/test_wallet.db")) .await .expect("Could not create localstore"); - localstore.migrate().await; localstore.add_proofs(&tokens.proofs()).await?; assert_eq!(64, localstore.get_proofs().await?.total_amount());