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