Skip to content

Commit

Permalink
refactor: embed migrations, move bin to examples crate, cleanup depen…
Browse files Browse the repository at this point in the history
…dencies
  • Loading branch information
notmandatory committed Oct 9, 2024
1 parent aff27a9 commit 73cb9b5
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
target
Cargo.lock
*.sqlite
.env
32 changes: 9 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
[package]
name = "bdk-sqlx"
name = "bdk_sqlx"
version = "0.1.0"
edition = "2021"

[lib]
name = "bdk_sqlx"
path = "src/lib.rs"

[[bin]]
name = "async_wallet_bdk_sqlx"
path = "src/main.rs"

[dependencies]
bdk_wallet = { version = "1.0.0-beta.5" }
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.125"
sqlx = { version = "0.8.1", default-features = false, features = ["runtime-tokio", "tls-rustls-ring","derive", "postgres", "sqlite", "json", "chrono", "uuid", "sqlx-macros", "migrate"] }
thiserror = "1"
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.125"
better-panic = "0.3.0"
rustls = "0.23.12"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde_json", "json"] }
anyhow = "1.0.86"
rand = "0.8.5"
uuid = "1.10.0"
assert_matches = "1.5.0"
pg-embed = { version = "0.7.1", features = ["default"] }


bdk_wallet = { git = "https://github.com/bitcoindevkit/bdk", tag = "v1.0.0-beta.2", features = ["std"], default-features = false }
bdk_chain = { git = "https://github.com/bitcoindevkit/bdk", tag = "v1.0.0-beta.2" }
bdk_electrum = { git = "https://github.com/bitcoindevkit/bdk", tag = "v1.0.0-beta.2" }
bdk_testenv = { git = "https://github.com/bitcoindevkit/bdk", tag = "v1.0.0-beta.2" }
[dev-dependencies]
assert_matches = "1.5.0"
bdk_electrum = { version = "0.19.0"}
bdk_testenv = { version = "0.10.0" }
better-panic = "0.3.0"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# bdk-sqlx

## Status

This crate is still **EXPERIMENTAL** do not use with mainnet wallets.

## Testing

Expand Down
15 changes: 15 additions & 0 deletions examples/bdk_sqlx_postgres/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "bdk_sqlx_postgres"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.86"
bdk_electrum = { version = "0.19.0"}
bdk_sqlx = { path = "../.." }
bdk_wallet = "1.0.0-beta.5"
better-panic = "0.3.0"
rustls = "0.23.12"
tokio = "1.40.0"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde_json", "json"] }
5 changes: 3 additions & 2 deletions src/main.rs → examples/bdk_sqlx_postgres/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::collections::HashSet;
use std::io::Write;

use bdk_electrum::{electrum_client, BdkElectrumClient};
use bdk_sqlx::sqlx::Postgres;
use bdk_sqlx::Store;
use bdk_wallet::bitcoin::secp256k1::Secp256k1;
use bdk_wallet::bitcoin::Network;
use bdk_wallet::{KeychainKind, PersistedWallet, Wallet};
use better_panic::Settings;
use rustls::crypto::ring::default_provider;
use sqlx::Postgres;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
Expand All @@ -35,6 +35,7 @@ async fn main() -> anyhow::Result<()> {
default_provider()
.install_default()
.expect("Failed to install rustls default crypto provider");

Settings::debug()
.most_recent_first(false)
.lineno_suffix(true)
Expand Down Expand Up @@ -120,7 +121,7 @@ async fn main() -> anyhow::Result<()> {
fn electrum(wallet: &mut PersistedWallet<Store<Postgres>>) -> anyhow::Result<()> {
let client = BdkElectrumClient::new(electrum_client::Client::new(ELECTRUM_URL)?);

// Populate the electrum client's transaction cache so it doesn't redownload transaction we
// Populate the electrum client's transaction cache so it doesn't re-download transaction we
// already have.
client.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));

Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use bdk_chain::miniscript;
use bdk_wallet::bitcoin;
use bdk_wallet::chain as bdk_chain;

pub use sqlx;
use sqlx::Database;
use sqlx::Pool;

Expand All @@ -33,6 +34,9 @@ pub enum BdkSqlxError {
/// sqlx error
#[error("sqlx error: {0}")]
Sqlx(#[from] sqlx::Error),
/// migrate error
#[error("migrate error: {0}")]
Migrate(#[from] sqlx::migrate::MigrateError),
}

/// Manages a pool of database connections.
Expand Down
7 changes: 2 additions & 5 deletions src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use bdk_wallet::descriptor::{Descriptor, DescriptorPublicKey, ExtendedDescriptor
use bdk_wallet::KeychainKind::{External, Internal};
use bdk_wallet::{AsyncWalletPersister, ChangeSet, KeychainKind};
use serde_json::json;
use sqlx::migrate::Migrator;
use sqlx::postgres::PgRow;
use sqlx::sqlx_macros::migrate;
use sqlx::{
postgres::{PgPool, Postgres},
FromRow, Pool, Row, Transaction,
Expand Down Expand Up @@ -96,10 +96,7 @@ impl Store<Postgres> {
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
info!("migrate and read");
if self.migration {
let migrator = Migrator::new(std::path::Path::new("./migrations/postgres/"))
.await
.unwrap();
migrator.run(&self.pool).await.unwrap();
migrate!("migrations/postgres").run(&self.pool).await?;
}

let mut tx = self.pool.begin().await?;
Expand Down
11 changes: 3 additions & 8 deletions src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ use bdk_wallet::KeychainKind::{External, Internal};
use bdk_wallet::{AsyncWalletPersister, ChangeSet, KeychainKind};
use serde_json::json;
use sqlx::sqlite::SqliteRow;
use sqlx::{
migrate::Migrator,
sqlite::{SqlitePool, SqlitePoolOptions},
};
use sqlx::sqlite::{SqlitePool, SqlitePoolOptions};
use sqlx::sqlx_macros::migrate;
use sqlx::{sqlite::Sqlite, FromRow, Pool, Row, Transaction};
use tracing::info;

Expand Down Expand Up @@ -112,10 +110,7 @@ impl Store<Sqlite> {
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
info!("migrate and read");
if self.migration {
let migrator = Migrator::new(std::path::Path::new("./migrations/sqlite/"))
.await
.unwrap();
migrator.run(&self.pool).await.unwrap();
migrate!("./migrations/sqlite/").run(&self.pool).await?;
}

let mut tx = self.pool.begin().await?;
Expand Down
16 changes: 8 additions & 8 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![allow(unused)]
use crate::{BdkSqlxError, FutureResult, Store};
use assert_matches::assert_matches;
use bdk_chain::bitcoin::constants::ChainHash;
use bdk_chain::bitcoin::hashes::Hash;
use bdk_chain::bitcoin::secp256k1::Secp256k1;
use bdk_chain::bitcoin::Network::Signet;
use bdk_chain::bitcoin::{BlockHash, Network};
use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
use bdk_chain::BlockId;
use bdk_electrum::{electrum_client, BdkElectrumClient};
use bdk_testenv::bitcoincore_rpc::RpcApi;
use bdk_testenv::TestEnv;
use bdk_testenv::{anyhow, TestEnv};
use bdk_wallet::bitcoin::constants::ChainHash;
use bdk_wallet::bitcoin::hashes::Hash;
use bdk_wallet::bitcoin::secp256k1::Secp256k1;
use bdk_wallet::bitcoin::Network::Signet;
use bdk_wallet::bitcoin::{BlockHash, Network};
use bdk_wallet::chain::BlockId;
use bdk_wallet::miniscript::{Descriptor, DescriptorPublicKey};
use bdk_wallet::{
descriptor,
descriptor::ExtendedDescriptor,
Expand Down

0 comments on commit 73cb9b5

Please sign in to comment.