Skip to content

Commit

Permalink
Merge pull request #71 from ElrondNetwork/view-functions
Browse files Browse the repository at this point in the history
View functions
  • Loading branch information
catalinnnn authored May 27, 2021
2 parents c371a8d + 5b3f530 commit 635e9ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
22 changes: 21 additions & 1 deletion dex/elrond_dex_farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(&(&current + amount));
}

fn decrease_farming_token_reserve(&self, amount: &Self::BigUint) -> SCResult<()> {
let current = self.farming_token_reserve().get();
require!(&current >= amount, "Not enough farming reserve");
self.farming_token_reserve().set(&(&current - amount));
Ok(())
}

#[view(getFarmingTokenReserve)]
#[storage_mapper("farming_token_reserve")]
fn farming_token_reserve(&self) -> SingleValueMapper<Self::Storage, Self::BigUint>;
}
3 changes: 2 additions & 1 deletion dex/mandos/farm_with_egld_token.scen.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit 635e9ad

Please sign in to comment.