Skip to content

Commit

Permalink
chore: add example for receiving tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 11, 2023
1 parent 0d3d3a1 commit 0578ae1
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions flutter/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ pub fn init_cashu() -> anyhow::Result<String> {
// 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);
Expand Down
2 changes: 1 addition & 1 deletion integrationtests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 0 additions & 2 deletions integrationtests/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions moksha-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion moksha-fedimint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions moksha-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
25 changes: 25 additions & 0 deletions moksha-wallet/examples/receive_tokens.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
2 changes: 0 additions & 2 deletions moksha-wallet/src/localstore/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 0 additions & 2 deletions moksha-wallet/src/localstore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ pub trait LocalStore {

async fn get_keysets(&self) -> Result<Vec<WalletKeyset>, MokshaWalletError>;
async fn add_keyset(&self, keyset: &WalletKeyset) -> Result<(), MokshaWalletError>;

async fn migrate(&self);
}
2 changes: 0 additions & 2 deletions moksha-wallet/src/localstore/rexie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 11 additions & 14 deletions moksha-wallet/src/localstore/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand Down Expand Up @@ -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<Self, MokshaWalletError> {
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(
Expand Down Expand Up @@ -171,7 +169,6 @@ mod tests {

let localstore: Arc<dyn LocalStore> =
Arc::new(SqliteLocalStore::with_path(format!("{tmp_dir}/test_wallet.db")).await?);
localstore.migrate().await;

let tokens: TokenV3 = read_fixture("token_60.cashu")?
.trim()
Expand Down
2 changes: 0 additions & 2 deletions moksha-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
1 change: 0 additions & 1 deletion moksha-wallet/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 0578ae1

Please sign in to comment.