From 3427384d9f0737e666db4ca4393c30e5f379a261 Mon Sep 17 00:00:00 2001 From: deuszx Date: Fri, 13 Oct 2023 18:22:39 +0200 Subject: [PATCH 1/4] RM ensure_running macro --- farm/contracts/lib.rs | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/farm/contracts/lib.rs b/farm/contracts/lib.rs index adb910c6..2db79c3a 100644 --- a/farm/contracts/lib.rs +++ b/farm/contracts/lib.rs @@ -220,8 +220,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(true)?; let mut running = self.get_state()?; // We allow owner of the farm to stop it prematurely @@ -251,8 +251,9 @@ mod farm { /// Deposits the given amount of tokens into the farm. #[ink(message)] - #[modifiers(ensure_running(true), non_zero_amount(amount))] + #[modifiers(non_zero_amount(amount))] pub fn deposit(&mut self, amount: u128) -> Result<(), FarmError> { + self.ensure_running(true)?; self.update_reward_index()?; self.add_shares(amount) } @@ -260,8 +261,8 @@ mod farm { /// 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(true)?; self.update_reward_index()?; let token_balance = safe_balance_of(&self.pool.into(), self.env().caller()); self.add_shares(token_balance) @@ -446,6 +447,13 @@ mod farm { fn get_state(&self) -> Result { self.state.get().ok_or(FarmError::StateMissing) } + + fn ensure_running(&self, should_be_running: bool) -> Result<(), FarmError> { + if !should_be_running && self.is_running { + return Err(FarmError::StillRunning) + } + Ok(()) + } } type TokenId = AccountId; @@ -572,21 +580,6 @@ mod farm { use openbrush::modifier_definition; - #[modifier_definition] - pub fn ensure_running( - instance: &mut Farm, - body: F, - should_be_running: bool, - ) -> Result - where - F: FnOnce(&mut Farm) -> Result, - { - if !should_be_running && instance.is_running { - return Err(FarmError::StillRunning) - } - body(instance) - } - #[modifier_definition] pub fn non_zero_amount(instance: &mut Farm, body: F, amount: u128) -> Result where From 9764969aea42a1077ba4126ad9df992c0de52290 Mon Sep 17 00:00:00 2001 From: deuszx Date: Fri, 13 Oct 2023 18:25:03 +0200 Subject: [PATCH 2/4] RM non_zero_amount modifier --- farm/contracts/lib.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/farm/contracts/lib.rs b/farm/contracts/lib.rs index 2db79c3a..075eec24 100644 --- a/farm/contracts/lib.rs +++ b/farm/contracts/lib.rs @@ -251,8 +251,8 @@ mod farm { /// Deposits the given amount of tokens into the farm. #[ink(message)] - #[modifiers(non_zero_amount(amount))] pub fn deposit(&mut self, amount: u128) -> Result<(), FarmError> { + Self::assert_non_zero_amount(amount)?; self.ensure_running(true)?; self.update_reward_index()?; self.add_shares(amount) @@ -265,13 +265,14 @@ mod farm { self.ensure_running(true)?; 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(); @@ -454,6 +455,13 @@ mod farm { } Ok(()) } + + fn assert_non_zero_amount(amount: u128) -> Result<(), FarmError> { + if amount == 0 { + return Err(FarmError::InvalidAmountArgument) + } + Ok(()) + } } type TokenId = AccountId; @@ -578,19 +586,6 @@ mod farm { } } - use openbrush::modifier_definition; - - #[modifier_definition] - pub fn non_zero_amount(instance: &mut Farm, body: F, amount: u128) -> Result - where - F: FnOnce(&mut Farm) -> Result, - { - 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 From ddc86764ebcc5a38d81fa6279aabe332967f58a9 Mon Sep 17 00:00:00 2001 From: deuszx Date: Fri, 13 Oct 2023 18:26:08 +0200 Subject: [PATCH 3/4] RM openbrush dep from farm --- Cargo.lock | 1 - farm/contracts/Cargo.toml | 2 -- farm/contracts/lib.rs | 2 -- 3 files changed, 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 784e9583..e4e92587 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,7 +42,6 @@ version = "0.1.0" dependencies = [ "amm-helpers", "ink", - "openbrush", "parity-scale-codec", "primitive-types 0.12.1", "psp22-traits", diff --git a/farm/contracts/Cargo.toml b/farm/contracts/Cargo.toml index 3cd09b70..788e32ea 100644 --- a/farm/contracts/Cargo.toml +++ b/farm/contracts/Cargo.toml @@ -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", ] } @@ -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", diff --git a/farm/contracts/lib.rs b/farm/contracts/lib.rs index 075eec24..e869ac57 100644 --- a/farm/contracts/lib.rs +++ b/farm/contracts/lib.rs @@ -28,8 +28,6 @@ mod farm { rewards_earned, }; - use openbrush::modifiers; - use primitive_types::U256; use psp22_traits::PSP22; From 18a0dd90ba8c9825b78745394103c4d643897f85 Mon Sep 17 00:00:00 2001 From: deuszx Date: Sat, 14 Oct 2023 00:10:57 +0200 Subject: [PATCH 4/4] Simplify ensure_running check --- farm/contracts/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/farm/contracts/lib.rs b/farm/contracts/lib.rs index e869ac57..05027f3a 100644 --- a/farm/contracts/lib.rs +++ b/farm/contracts/lib.rs @@ -219,7 +219,7 @@ mod farm { /// 2. Farm's `end` timestamp is still in the future. #[ink(message)] pub fn stop(&mut self) -> Result<(), FarmError> { - self.ensure_running(true)?; + self.ensure_running()?; let mut running = self.get_state()?; // We allow owner of the farm to stop it prematurely @@ -251,7 +251,7 @@ mod farm { #[ink(message)] pub fn deposit(&mut self, amount: u128) -> Result<(), FarmError> { Self::assert_non_zero_amount(amount)?; - self.ensure_running(true)?; + self.ensure_running()?; self.update_reward_index()?; self.add_shares(amount) } @@ -260,7 +260,7 @@ mod farm { /// NOTE: Requires that the caller has approved the farm to spend their tokens. #[ink(message)] pub fn deposit_all(&mut self) -> Result<(), FarmError> { - self.ensure_running(true)?; + 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)?; @@ -447,9 +447,9 @@ mod farm { self.state.get().ok_or(FarmError::StateMissing) } - fn ensure_running(&self, should_be_running: bool) -> Result<(), FarmError> { - if !should_be_running && self.is_running { - return Err(FarmError::StillRunning) + fn ensure_running(&self) -> Result<(), FarmError> { + if !self.is_running { + return Err(FarmError::NotRunning) } Ok(()) }