Skip to content

Commit

Permalink
Merge pull request #40 from Cardinal-Cryptography/rm-ob-macros
Browse files Browse the repository at this point in the history
  • Loading branch information
deuszx authored Oct 16, 2023
2 parents 6ff0082 + 18a0dd9 commit 579f0b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions farm/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ scale-info = { version = "2.3", default-features = false, features = [
"derive",
], optional = true }

openbrush = { version = "3.2.0", default-features = false }
primitive-types = { version = "0.12.1", default-features = false, features = [
"codec",
] }
Expand All @@ -31,7 +30,6 @@ std = [
"scale/std",
"scale-info/std",
"psp22-traits/std",
"openbrush/std",
"primitive-types/std",
"primitive-types/scale-info",
"sp-arithmetic/std",
Expand Down
54 changes: 20 additions & 34 deletions farm/contracts/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ mod farm {
rewards_earned,
};

use openbrush::modifiers;

use primitive_types::U256;
use psp22_traits::PSP22;

Expand Down Expand Up @@ -220,8 +218,8 @@ mod farm {
/// 1. Farm is not in `Running` state.
/// 2. Farm's `end` timestamp is still in the future.
#[ink(message)]
#[modifiers(ensure_running(true))]
pub fn stop(&mut self) -> Result<(), FarmError> {
self.ensure_running()?;
let mut running = self.get_state()?;

// We allow owner of the farm to stop it prematurely
Expand Down Expand Up @@ -251,26 +249,28 @@ mod farm {

/// Deposits the given amount of tokens into the farm.
#[ink(message)]
#[modifiers(ensure_running(true), non_zero_amount(amount))]
pub fn deposit(&mut self, amount: u128) -> Result<(), FarmError> {
Self::assert_non_zero_amount(amount)?;
self.ensure_running()?;
self.update_reward_index()?;
self.add_shares(amount)
}

/// Deposits all of the LP tokens the caller has.
/// NOTE: Requires that the caller has approved the farm to spend their tokens.
#[ink(message)]
#[modifiers(ensure_running(true))]
pub fn deposit_all(&mut self) -> Result<(), FarmError> {
self.ensure_running()?;
self.update_reward_index()?;
let token_balance = safe_balance_of(&self.pool.into(), self.env().caller());
Self::assert_non_zero_amount(token_balance)?;
self.add_shares(token_balance)
}

/// Withdraws the given amount of shares from the farm.
#[ink(message)]
#[modifiers(non_zero_amount(amount))]
pub fn withdraw(&mut self, amount: u128) -> Result<(), FarmError> {
Self::assert_non_zero_amount(amount)?;
self.update_reward_index()?;
let caller = self.env().caller();

Expand Down Expand Up @@ -446,6 +446,20 @@ mod farm {
fn get_state(&self) -> Result<State, FarmError> {
self.state.get().ok_or(FarmError::StateMissing)
}

fn ensure_running(&self) -> Result<(), FarmError> {
if !self.is_running {
return Err(FarmError::NotRunning)
}
Ok(())
}

fn assert_non_zero_amount(amount: u128) -> Result<(), FarmError> {
if amount == 0 {
return Err(FarmError::InvalidAmountArgument)
}
Ok(())
}
}

type TokenId = AccountId;
Expand Down Expand Up @@ -570,34 +584,6 @@ mod farm {
}
}

use openbrush::modifier_definition;

#[modifier_definition]
pub fn ensure_running<F, T>(
instance: &mut Farm,
body: F,
should_be_running: bool,
) -> Result<T, FarmError>
where
F: FnOnce(&mut Farm) -> Result<T, FarmError>,
{
if !should_be_running && instance.is_running {
return Err(FarmError::StillRunning)
}
body(instance)
}

#[modifier_definition]
pub fn non_zero_amount<F, T>(instance: &mut Farm, body: F, amount: u128) -> Result<T, FarmError>
where
F: FnOnce(&mut Farm) -> Result<T, FarmError>,
{
if amount == 0 {
return Err(FarmError::InvalidAmountArgument)
}
body(instance)
}

use ink::codegen::TraitCallBuilder;

// We're making a concious choice here that we don't want to fail the whole transaction
Expand Down

0 comments on commit 579f0b7

Please sign in to comment.