Skip to content

Commit

Permalink
refactor: remove default wallet name; fix README for example
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Oct 19, 2024
1 parent 7388cfe commit 9997ce7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ This crate is still **EXPERIMENTAL** do not use with mainnet wallets.
```
cargo test -- --test-threads=1
```
5. Run example:

## Example

1. Create empty test database:
```
psql postgres
postgres=# create database example_bdk_wallet;
postgres=# \q
```
2. Set DATABASE_URL to test database:
```
export DATABASE_URL=postgresql://localhost/example_bdk_wallet
```
3. Run example:
```
cargo run --example bdk_sqlx_postgres
```
6 changes: 3 additions & 3 deletions examples/bdk_sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> anyhow::Result<()> {
.with(EnvFilter::new(std::env::var("RUST_LOG").unwrap_or_else(
|_| {
"sqlx=warn,\
bdk_sqlx=info"
bdk_sqlx=debug"
.into()
},
)))
Expand All @@ -57,7 +57,7 @@ async fn main() -> anyhow::Result<()> {
&secp,
)?;
let mut store =
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), Some(wallet_name)).await?;
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), wallet_name).await?;

let mut wallet = match Wallet::load().load_wallet_async(&mut store).await? {
Some(wallet) => wallet,
Expand Down Expand Up @@ -85,7 +85,7 @@ async fn main() -> anyhow::Result<()> {
bdk_wallet::wallet_name_from_descriptor(VAULT_DESC, Some(CHANGE_DESC), NETWORK, &secp)?;

let mut store =
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), Some(wallet_name)).await?;
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), wallet_name).await?;

let mut wallet = match Wallet::load().load_wallet_async(&mut store).await? {
Some(wallet) => wallet,
Expand Down
10 changes: 2 additions & 8 deletions src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@ impl Store<Postgres> {
#[tracing::instrument]
pub async fn new(
pool: Pool<Postgres>,
wallet_name: Option<String>,
wallet_name: String,
migration: bool,
) -> Result<Self, BdkSqlxError> {
info!("new store");

let wallet_name = wallet_name.unwrap_or_else(|| "bdk_pg_wallet".to_string());

Ok(Self {
pool,
wallet_name,
Expand All @@ -76,13 +73,10 @@ impl Store<Postgres> {
#[tracing::instrument]
pub async fn new_with_url(
url: String,
wallet_name: Option<String>,
wallet_name: String,
) -> Result<Store<Postgres>, BdkSqlxError> {
info!("new store with url");

let pool = PgPool::connect(url.as_str()).await?;
let wallet_name = wallet_name.unwrap_or_else(|| "bdk_pg_wallet".to_string());

Ok(Self {
pool,
wallet_name,
Expand Down
10 changes: 2 additions & 8 deletions src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,10 @@ impl Store<Sqlite> {
#[tracing::instrument]
pub async fn new(
pool: Pool<Sqlite>,
wallet_name: Option<String>,
wallet_name: String,
migration: bool,
) -> Result<Self, BdkSqlxError> {
info!("new store");

let wallet_name = wallet_name.unwrap_or_else(|| "bdk_sqlites_wallet".to_string());

Ok(Self {
pool,
wallet_name,
Expand All @@ -79,10 +76,9 @@ impl Store<Sqlite> {
#[tracing::instrument]
pub async fn new_with_url(
url: Option<String>,
wallet_name: Option<String>,
wallet_name: String,
) -> Result<Store<Sqlite>, BdkSqlxError> {
info!("new store with url");

let pool = if let Some(url) = url {
SqlitePool::connect(url.as_str()).await?
} else {
Expand All @@ -95,8 +91,6 @@ impl Store<Sqlite> {
.connect(":memory:")
.await?
};
let wallet_name = wallet_name.unwrap_or_else(|| "bdk_sqlite_wallet".to_string());

Ok(Self {
pool,
wallet_name,
Expand Down
5 changes: 2 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ async fn create_test_stores(wallet_name: String) -> anyhow::Result<Vec<TestStore
let pool = Pool::<Postgres>::connect(&url.clone()).await?;
// Drop all before creating new store for testing
pool.drop_all().await?;
let postgres_store =
Store::<Postgres>::new_with_url(url.clone(), Some(wallet_name.clone())).await?;
let postgres_store = Store::<Postgres>::new_with_url(url.clone(), wallet_name.clone()).await?;
stores.push(TestStore::Postgres(postgres_store));

// Setup sqlite in-memory database
let pool = SqlitePool::connect(":memory:").await?;
let sqlite_store = Store::<Sqlite>::new(pool.clone(), Some(wallet_name.clone()), true).await?;
let sqlite_store = Store::<Sqlite>::new(pool.clone(), wallet_name.clone(), true).await?;
stores.push(TestStore::Sqlite(sqlite_store));

Ok(stores)
Expand Down

0 comments on commit 9997ce7

Please sign in to comment.