1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use account::Account;
use types::{ProcessError, ProcessResult};
use utils::IfNotFound;

pub fn transfer(sender: &Account, recipient: &Account, amount: u64) -> ProcessResult<()> {
    sender.set_u64(
        "balance",
        match sender
            .get_u64("balance")
            .if_not_found(|| 0u64)?
            .checked_sub(amount)
            {
                Some(v) => v,
                None => return Err(ProcessError::Custom("not enough balance".into())),
            },
    )?;
    recipient.set_u64(
        "balance",
        match recipient
            .get_u64("balance")
            .if_not_found(|| 0u64)?
            .checked_add(amount)
            {
                Some(v) => v,
                None => return Err(ProcessError::Overflow),
            },
    )?;
    Ok(())
}