transaction_processor/
transfer.rs

1use account::Account;
2use types::{ProcessError, ProcessResult};
3use utils::IfNotFound;
4
5pub fn transfer(sender: &Account, recipient: &Account, amount: u64) -> ProcessResult<()> {
6    sender.set_u64(
7        "balance",
8        match sender
9            .get_u64("balance")
10            .if_not_found(|| 0u64)?
11            .checked_sub(amount)
12            {
13                Some(v) => v,
14                None => return Err(ProcessError::Custom("not enough balance".into())),
15            },
16    )?;
17    recipient.set_u64(
18        "balance",
19        match recipient
20            .get_u64("balance")
21            .if_not_found(|| 0u64)?
22            .checked_add(amount)
23            {
24                Some(v) => v,
25                None => return Err(ProcessError::Overflow),
26            },
27    )?;
28    Ok(())
29}