diff --git a/.gitignore b/.gitignore index b285d68..0b2ce60 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/target +target Cargo.lock *.sqlite .env diff --git a/Cargo.toml b/Cargo.toml index b4ea6e8..468e0af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/README.md b/README.md index 07934ce..bd9e589 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # bdk-sqlx +## Status + +This crate is still **EXPERIMENTAL** do not use with mainnet wallets. ## Testing diff --git a/examples/bdk_sqlx_postgres/Cargo.toml b/examples/bdk_sqlx_postgres/Cargo.toml new file mode 100644 index 0000000..fa14260 --- /dev/null +++ b/examples/bdk_sqlx_postgres/Cargo.toml @@ -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"] } diff --git a/src/main.rs b/examples/bdk_sqlx_postgres/src/main.rs similarity index 99% rename from src/main.rs rename to examples/bdk_sqlx_postgres/src/main.rs index c8bca23..50ac31b 100644 --- a/src/main.rs +++ b/examples/bdk_sqlx_postgres/src/main.rs @@ -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; @@ -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) @@ -120,7 +121,7 @@ async fn main() -> anyhow::Result<()> { fn electrum(wallet: &mut PersistedWallet>) -> 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)); diff --git a/src/lib.rs b/src/lib.rs index 0ef114a..9e55847 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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. diff --git a/src/postgres.rs b/src/postgres.rs index 6f3ffa6..03f53d6 100644 --- a/src/postgres.rs +++ b/src/postgres.rs @@ -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, @@ -96,10 +96,7 @@ impl Store { pub(crate) async fn migrate_and_read(&self) -> Result { 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?; diff --git a/src/sqlite.rs b/src/sqlite.rs index 1583fab..53eaec1 100644 --- a/src/sqlite.rs +++ b/src/sqlite.rs @@ -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; @@ -112,10 +110,7 @@ impl Store { pub(crate) async fn migrate_and_read(&self) -> Result { 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?; diff --git a/src/test.rs b/src/test.rs index 227480b..3d42009 100644 --- a/src/test.rs +++ b/src/test.rs @@ -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,