2022-10-19 23:24:36 -07:00
|
|
|
//! Contact Address book
|
|
|
|
|
2022-12-25 03:16:22 -08:00
|
|
|
use crate::api::payment_v2::build_tx_plan;
|
2022-11-12 17:39:12 -08:00
|
|
|
use crate::api::recipient::RecipientMemo;
|
2022-06-08 05:48:16 -07:00
|
|
|
use crate::api::sync::get_latest_height;
|
|
|
|
use crate::coinconfig::CoinConfig;
|
|
|
|
use crate::contact::{serialize_contacts, Contact};
|
2022-09-04 04:19:49 -07:00
|
|
|
use crate::db::AccountData;
|
2022-12-25 03:16:22 -08:00
|
|
|
use crate::TransactionPlan;
|
2022-12-30 12:34:26 -08:00
|
|
|
use zcash_primitives::memo::Memo;
|
2022-06-08 05:48:16 -07:00
|
|
|
|
2022-10-19 23:24:36 -07:00
|
|
|
/// Store contact in the database
|
|
|
|
/// # Arguments
|
|
|
|
/// * `id`: contact id
|
|
|
|
/// * `name`: contact name
|
|
|
|
/// * `address`: contact address
|
|
|
|
/// * `dirty`: true if the database hasn't been saved to the blockchain yet
|
2022-06-08 05:48:16 -07:00
|
|
|
pub fn store_contact(id: u32, name: &str, address: &str, dirty: bool) -> anyhow::Result<()> {
|
|
|
|
let c = CoinConfig::get_active();
|
|
|
|
let contact = Contact {
|
|
|
|
id,
|
|
|
|
name: name.to_string(),
|
|
|
|
address: address.to_string(),
|
|
|
|
};
|
|
|
|
c.db()?.store_contact(&contact, dirty)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-19 23:24:36 -07:00
|
|
|
/// Save the new/modified contacts to the blockchain
|
|
|
|
/// # Arguments
|
|
|
|
/// * `anchor_offset`: minimum confirmations required for note selection
|
2022-12-25 03:16:22 -08:00
|
|
|
pub async fn commit_unsaved_contacts(anchor_offset: u32) -> anyhow::Result<TransactionPlan> {
|
2022-06-08 05:48:16 -07:00
|
|
|
let c = CoinConfig::get_active();
|
|
|
|
let contacts = c.db()?.get_unsaved_contacts()?;
|
|
|
|
let memos = serialize_contacts(&contacts)?;
|
2022-12-25 03:16:22 -08:00
|
|
|
let tx_plan = save_contacts_tx(&memos, anchor_offset).await?;
|
|
|
|
Ok(tx_plan)
|
2022-06-08 05:48:16 -07:00
|
|
|
}
|
|
|
|
|
2022-12-25 03:16:22 -08:00
|
|
|
async fn save_contacts_tx(memos: &[Memo], anchor_offset: u32) -> anyhow::Result<TransactionPlan> {
|
2022-06-08 05:48:16 -07:00
|
|
|
let c = CoinConfig::get_active();
|
|
|
|
let last_height = get_latest_height().await?;
|
2022-09-04 04:19:49 -07:00
|
|
|
let AccountData { address, .. } = c.db()?.get_account_info(c.id_account)?;
|
2022-06-08 05:48:16 -07:00
|
|
|
let recipients: Vec<_> = memos
|
|
|
|
.iter()
|
|
|
|
.map(|m| RecipientMemo {
|
|
|
|
address: address.clone(),
|
|
|
|
amount: 0,
|
2022-12-10 14:01:10 -08:00
|
|
|
fee_included: false,
|
2022-06-08 05:48:16 -07:00
|
|
|
memo: m.clone(),
|
|
|
|
max_amount_per_note: 0,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2022-11-13 01:00:21 -08:00
|
|
|
let tx_plan = build_tx_plan(
|
2022-11-12 17:39:12 -08:00
|
|
|
c.coin,
|
|
|
|
c.id_account,
|
2022-06-08 05:48:16 -07:00
|
|
|
last_height,
|
|
|
|
&recipients,
|
2022-11-15 19:51:52 -08:00
|
|
|
1,
|
2022-06-08 05:48:16 -07:00
|
|
|
anchor_offset,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-12-25 03:16:22 -08:00
|
|
|
Ok(tx_plan)
|
2022-06-08 05:48:16 -07:00
|
|
|
}
|