transaction-processor 0.0.1

Write transaction processors for Wavelet in Rust.
Documentation
use account::Account;
use types::{ProcessError, ProcessResult};
use utils::IfNotFound;

pub fn place_stake(sender: &Account, amount: u64) -> ProcessResult<()> {
    let new_stake = match sender.get_u64("stake").if_not_found(|| 0u64)?.checked_add(amount) {
        Some(v) => v,
        None => return Err(ProcessError::Overflow),
    };

    // PERL price is 0.2$USD; we require a minimum of 500$USD which is 2500 PERLs.
    if new_stake < 2500 {
        return Err(ProcessError::Custom("stake requires a minimum of 2500 PERLs".into()));
    }

    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())),
            },
    )?;

    sender.set_u64(
        "stake",
        new_stake,
    )?;

    Ok(())
}

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

    sender.set_u64(
        "balance",
        match sender.get_u64("balance").if_not_found(|| 0u64)?.checked_add(amount)
            {
                Some(v) => v,
                None => return Err(ProcessError::Overflow),
            },
    )?;

    Ok(())
}