diff --git a/binding.h b/binding.h index 6611a9d..0dc1735 100644 --- a/binding.h +++ b/binding.h @@ -86,12 +86,6 @@ struct CResult_____c_char new_diversified_address(void); struct CResult_u32 get_latest_height(void); -struct CResult_____c_char send_multi_payment(uint8_t coin, - uint32_t account, - char *recipients_json, - uint32_t anchor_offset, - int64_t port); - void skip_to_last_height(uint8_t coin); struct CResult_u32 rewind_to(uint32_t height); diff --git a/src/api/contact.rs b/src/api/contact.rs index b3e6eee..6ce2362 100644 --- a/src/api/contact.rs +++ b/src/api/contact.rs @@ -1,6 +1,6 @@ //! Contact Address book -use crate::api::payment_v2::build_sign_send_multi_payment; +use crate::api::payment_v2::{build_tx_plan, sign_and_broadcast}; use crate::api::recipient::RecipientMemo; use crate::api::sync::get_latest_height; use crate::coinconfig::CoinConfig; @@ -50,14 +50,14 @@ async fn save_contacts_tx(memos: &[Memo], anchor_offset: u32) -> anyhow::Result< }) .collect(); - let tx_id = build_sign_send_multi_payment( + let tx_plan = build_tx_plan( c.coin, c.id_account, last_height, &recipients, anchor_offset, - Box::new(|_| {}), ) .await?; + let tx_id = sign_and_broadcast(c.coin, c.id_account, &tx_plan).await?; Ok(tx_id) } diff --git a/src/api/dart_ffi.rs b/src/api/dart_ffi.rs index 75f8509..aef0f81 100644 --- a/src/api/dart_ffi.rs +++ b/src/api/dart_ffi.rs @@ -99,7 +99,7 @@ pub struct CResult { pub unsafe extern "C" fn init_wallet(db_path: *mut c_char) { try_init_logger(); from_c_str!(db_path); - let _ = init_coin(0, &format!("{}/zec.db", &db_path)); + let _ = init_coin(0, &format!("{}/zec-test.db", &db_path)); let _ = init_coin(1, &format!("{}/yec.db", &db_path)); let _ = init_coin(2, &format!("{}/arrr.db", &db_path)); } @@ -295,34 +295,34 @@ fn report_progress(progress: Progress, port: i64) { } } -#[tokio::main] -#[no_mangle] -pub async unsafe extern "C" fn send_multi_payment( - coin: u8, - account: u32, - recipients_json: *mut c_char, - anchor_offset: u32, - port: i64, -) -> CResult<*mut c_char> { - from_c_str!(recipients_json); - let res = async move { - let height = crate::api::sync::get_latest_height().await?; - let recipients = crate::api::recipient::parse_recipients(&recipients_json)?; - let res = crate::api::payment_v2::build_sign_send_multi_payment( - coin, - account, - height, - &recipients, - anchor_offset, - Box::new(move |progress| { - report_progress(progress, port); - }), - ) - .await?; - Ok(res) - }; - to_cresult_str(res.await) -} +// #[tokio::main] +// #[no_mangle] +// pub async unsafe extern "C" fn send_multi_payment( +// coin: u8, +// account: u32, +// recipients_json: *mut c_char, +// anchor_offset: u32, +// port: i64, +// ) -> CResult<*mut c_char> { +// from_c_str!(recipients_json); +// let res = async move { +// let height = crate::api::sync::get_latest_height().await?; +// let recipients = crate::api::recipient::parse_recipients(&recipients_json)?; +// let res = crate::api::payment_v2::build_sign_send_multi_payment( +// coin, +// account, +// height, +// &recipients, +// anchor_offset, +// Box::new(move |progress| { +// report_progress(progress, port); +// }), +// ) +// .await?; +// Ok(res) +// }; +// to_cresult_str(res.await) +// } #[tokio::main] #[no_mangle] diff --git a/src/api/payment_v2.rs b/src/api/payment_v2.rs index f667447..ec4ef50 100644 --- a/src/api/payment_v2.rs +++ b/src/api/payment_v2.rs @@ -87,20 +87,15 @@ pub async fn sign_and_broadcast( ) -> anyhow::Result { let tx = sign_plan(coin, account, tx_plan)?; let txid = broadcast_tx(&tx).await?; + let id_notes: Vec<_> = tx_plan + .spends + .iter() + .filter_map(|n| if n.id != 0 { Some(n.id) } else { None }) + .collect(); + mark_spent(coin, &id_notes)?; Ok(txid) } -pub async fn build_sign_send_multi_payment( - coin: u8, - account: u32, - last_height: u32, - recipients: &[RecipientMemo], - confirmations: u32, - progress_callback: PaymentProgressCallback, -) -> anyhow::Result { - todo!() -} - /// Make a transaction that shields the transparent balance pub async fn shield_taddr(coin: u8, account: u32) -> anyhow::Result { // let last_height = get_latest_height().await?; @@ -108,3 +103,10 @@ pub async fn shield_taddr(coin: u8, account: u32) -> anyhow::Result { // Ok(tx_id) todo!() } + +fn mark_spent(coin: u8, ids: &[u32]) -> anyhow::Result<()> { + let c = CoinConfig::get(coin); + let mut db = c.db()?; + db.tx_mark_spend(ids)?; + Ok(()) +} diff --git a/src/db.rs b/src/db.rs index 0e4dbd0..af9e6a9 100644 --- a/src/db.rs +++ b/src/db.rs @@ -577,6 +577,7 @@ impl DbAdapter { witness, }; Ok(UTXO { + id: id_note, source, amount: amount as u64, }) @@ -606,6 +607,7 @@ impl DbAdapter { witness, }; Ok(UTXO { + id: id_note, source, amount: amount as u64, }) diff --git a/src/note_selection.rs b/src/note_selection.rs index be6772d..fda297a 100644 --- a/src/note_selection.rs +++ b/src/note_selection.rs @@ -4,7 +4,7 @@ pub use crate::note_selection::types::{ }; pub use crate::note_selection::TransactionBuilderError::TxTooComplex; pub use builder::{build_tx, get_secret_keys, TxBuilderContext}; -pub use fee::{FeeCalculator, FeeZIP327}; +pub use fee::{FeeCalculator, FeeFlat, FeeZIP327}; pub use optimize::build_tx_plan; use std::str::FromStr; pub use utxo::fetch_utxos; diff --git a/src/note_selection/types.rs b/src/note_selection/types.rs index 6d23b04..fcd7610 100644 --- a/src/note_selection/types.rs +++ b/src/note_selection/types.rs @@ -94,6 +94,7 @@ pub struct PoolAllocation(pub [u64; 3]); #[derive(Clone, Serialize, Deserialize, Debug)] pub struct UTXO { + pub id: u32, pub source: Source, pub amount: u64, } diff --git a/src/note_selection/utxo.rs b/src/note_selection/utxo.rs index 05914da..47ea4ed 100644 --- a/src/note_selection/utxo.rs +++ b/src/note_selection/utxo.rs @@ -39,6 +39,7 @@ async fn get_transparent_utxos(coin: u8, account: u32) -> anyhow::Result( progress.trial_decryptions += synchronizer.process(&blocks.0)? as u64; } - // Orchard - log::info!("Orchard"); - { - let decrypter = OrchardDecrypter::new(network); - let warper = WarpProcessor::new(OrchardHasher::new()); - let mut synchronizer = OrchardSynchronizer::new( - decrypter, - warper, - orchard_vks.clone(), - db_builder.clone(), - "orchard".to_string(), - ); - synchronizer.initialize(height)?; - progress.trial_decryptions += synchronizer.process(&blocks.0)? as u64; + if c.chain.has_unified() { + // Orchard + log::info!("Orchard"); + { + let decrypter = OrchardDecrypter::new(network); + let warper = WarpProcessor::new(OrchardHasher::new()); + let mut synchronizer = OrchardSynchronizer::new( + decrypter, + warper, + orchard_vks.clone(), + db_builder.clone(), + "orchard".to_string(), + ); + synchronizer.initialize(height)?; + progress.trial_decryptions += synchronizer.process(&blocks.0)? as u64; + } } let db = db_builder.build()?;