diff --git a/dex/elrond_dex_farm/src/lib.rs b/dex/elrond_dex_farm/src/lib.rs index 7377b55eb..94da8f9b6 100644 --- a/dex/elrond_dex_farm/src/lib.rs +++ b/dex/elrond_dex_farm/src/lib.rs @@ -126,6 +126,7 @@ pub trait Farm: rewards::RewardsModule + config::ConfigModule { let farming_token_id = self.farming_token_id().get(); require!(token_in == farming_token_id, "Bad input token"); require!(enter_amount > 0, "Cannot farm with amount of 0"); + self.increase_farming_token_reserve(&enter_amount); let (farm_contribution, apr_multiplier) = self.get_farm_contribution(&enter_amount, with_locked_rewards); @@ -200,7 +201,9 @@ pub trait Farm: rewards::RewardsModule + config::ConfigModule { &self.reward_per_share().get(), &farm_attributes.reward_per_share, ); - self.decrease_reward_reserve(&reward)?; + if reward > 0 { + self.decrease_reward_reserve(&reward)?; + } let farming_token_id = self.farming_token_id().get(); let mut farming_token_amount = amount.clone(); @@ -342,6 +345,7 @@ pub trait Farm: rewards::RewardsModule + config::ConfigModule { "Cannot send back farming tokens with amount 0" ); } + self.decrease_farming_token_reserve(&farming_amount)?; self.send_fft_tokens( farming_token_id, farming_amount, @@ -693,4 +697,20 @@ pub trait Farm: rewards::RewardsModule + config::ConfigModule { amount * &Self::BigUint::from(self.penalty_percent().get() as u64) / Self::BigUint::from(100u64) } + + fn increase_farming_token_reserve(&self, amount: &Self::BigUint) { + let current = self.farming_token_reserve().get(); + self.farming_token_reserve().set(&(¤t + amount)); + } + + fn decrease_farming_token_reserve(&self, amount: &Self::BigUint) -> SCResult<()> { + let current = self.farming_token_reserve().get(); + require!(¤t >= amount, "Not enough farming reserve"); + self.farming_token_reserve().set(&(¤t - amount)); + Ok(()) + } + + #[view(getFarmingTokenReserve)] + #[storage_mapper("farming_token_reserve")] + fn farming_token_reserve(&self) -> SingleValueMapper; } diff --git a/dex/mandos/farm_with_egld_token.scen.json b/dex/mandos/farm_with_egld_token.scen.json index dd7f86ec8..fffaa4168 100644 --- a/dex/mandos/farm_with_egld_token.scen.json +++ b/dex/mandos/farm_with_egld_token.scen.json @@ -115,7 +115,8 @@ "str:farming_token_id": "str:WEGLD-abcdef", "str:reward_token_id": "str:WEGLD-abcdef", "str:farm_token_supply": "100,000,000", - "str:create_farm_tokens_gas_limit": "5000000" + "str:create_farm_tokens_gas_limit": "5000000", + "str:farming_token_reserve": "100,000,000" }, "code": "file:../elrond_dex_farm/output/elrond_dex_farm.wasm" },