Skip to main content

cli_core/
account.rs

1//! Account commands over the client-form natives.
2
3use forms::{ACCOUNT_MERGE, FormValues, write_payload};
4use nomiscript::Value;
5use rpc::Session;
6
7use crate::CommandError;
8use crate::eval::{escape_str, plist_field};
9use crate::forms::map_references;
10use crate::transaction::{eval_text, submit_call};
11
12/// Merge the `from` account of `values` into its `into` account; the id of
13/// the account that remains.
14///
15/// # Errors
16/// [`CommandError::Argument`] for an unknown or ambiguous pick, or the
17/// server's refusal.
18pub async fn merge(session: &mut Session, values: &FormValues) -> Result<String, CommandError> {
19    let mapped = map_references(session, &ACCOUNT_MERGE, values).await?;
20    eval_text(session, &submit_call(&ACCOUNT_MERGE, &mapped)).await
21}
22
23/// The `(merge-preview …)` call for the merge `values` describe.
24#[must_use]
25pub fn merge_preview_call(values: &FormValues) -> String {
26    format!(
27        "(merge-preview {})",
28        escape_str(&write_payload(&ACCOUNT_MERGE, values))
29    )
30}
31
32/// The count of splits a `merge-preview` reply says would move.
33#[must_use]
34pub fn merge_preview_splits(preview: &Value) -> Option<u64> {
35    plist_field(preview, ":splits")?.parse().ok()
36}
37
38#[cfg(test)]
39mod tests;