Skip to content

Commit

Permalink
Add withdraw functionality for metastaking rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
CostinCarabas committed Jan 22, 2024
1 parent 6dd9f44 commit c031013
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
19 changes: 19 additions & 0 deletions farm-staking/farm-staking/src/custom_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ pub trait CustomRewardsModule:
self.reward_capacity().update(|r| *r += payment_amount);
}

#[payable("*")]
#[endpoint(withdrawRewards)]
fn withdraw_rewards(&self, withdraw_amount: BigUint) {
self.require_caller_has_admin_permissions();

self.reward_capacity().update(|rewards| {
require!(
*rewards >= withdraw_amount,
"Not enough rewards to withdraw"
);

*rewards -= withdraw_amount.clone()
});

let caller = self.blockchain().get_caller();
let reward_token_id = self.reward_token_id().get();
self.send_tokens_non_zero(&caller, &reward_token_id, 0, &withdraw_amount);
}

#[endpoint(endProduceRewards)]
fn end_produce_rewards(&self) {
self.require_caller_has_admin_permissions();
Expand Down
2 changes: 1 addition & 1 deletion farm-staking/farm-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub trait FarmStaking:

(merged_farm_token, boosted_rewards_payment).into()
}

#[view(calculateRewardsForGivenPosition)]
fn calculate_rewards_for_given_position(
&self,
Expand Down
25 changes: 25 additions & 0 deletions farm-staking/farm-staking/tests/farm_staking_setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@ where
.assert_ok();
}

pub fn check_rewards_capacity(&mut self, expected_farm_token_supply: u64) {
self.b_mock
.execute_query(&self.farm_wrapper, |sc| {
let actual_farm_supply = sc.reward_capacity().get();
assert_eq!(
managed_biguint!(expected_farm_token_supply),
actual_farm_supply
);
})
.assert_ok();
}

pub fn allow_external_claim_rewards(&mut self, user: &Address) {
self.b_mock
.execute_tx(user, &self.farm_wrapper, &rust_biguint!(0), |sc| {
Expand Down Expand Up @@ -490,4 +502,17 @@ where
)
.assert_ok();
}

pub fn withdraw_rewards(&mut self, withdraw_amount: &RustBigUint) {
self.b_mock
.execute_tx(
&self.owner_address,
&self.farm_wrapper,
&rust_biguint!(0),
|sc| {
sc.withdraw_rewards(withdraw_amount.into());
},
)
.assert_ok();
}
}
22 changes: 20 additions & 2 deletions farm-staking/farm-staking/tests/farm_staking_test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#![allow(deprecated)]

use multiversx_sc_scenario::{rust_biguint, whitebox_legacy::TxTokenTransfer, DebugApi};
use multiversx_sc_scenario::{
managed_address, rust_biguint, whitebox_legacy::TxTokenTransfer, DebugApi,

Check warning on line 4 in farm-staking/farm-staking/tests/farm_staking_test.rs

View workflow job for this annotation

GitHub Actions / Contracts / Rust tests

unused import: `managed_address`

Check warning on line 4 in farm-staking/farm-staking/tests/farm_staking_test.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

unused import: `managed_address`

Check warning on line 4 in farm-staking/farm-staking/tests/farm_staking_test.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] farm-staking/farm-staking/tests/farm_staking_test.rs#L4

warning: unused import: `managed_address` --> farm-staking/farm-staking/tests/farm_staking_test.rs:4:5 | 4 | managed_address, rust_biguint, whitebox_legacy::TxTokenTransfer, DebugApi, | ^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
Raw output
farm-staking/farm-staking/tests/farm_staking_test.rs:4:5:w:warning: unused import: `managed_address`
 --> farm-staking/farm-staking/tests/farm_staking_test.rs:4:5
  |
4 |     managed_address, rust_biguint, whitebox_legacy::TxTokenTransfer, DebugApi,
  |     ^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default


__END__
};

pub mod farm_staking_setup;
use farm_staking::{
custom_rewards::{BLOCKS_IN_YEAR, MAX_PERCENT},
custom_rewards::{CustomRewardsModule, BLOCKS_IN_YEAR, MAX_PERCENT},

Check warning on line 9 in farm-staking/farm-staking/tests/farm_staking_test.rs

View workflow job for this annotation

GitHub Actions / Contracts / Rust tests

unused import: `CustomRewardsModule`

Check warning on line 9 in farm-staking/farm-staking/tests/farm_staking_test.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

unused import: `CustomRewardsModule`

Check warning on line 9 in farm-staking/farm-staking/tests/farm_staking_test.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] farm-staking/farm-staking/tests/farm_staking_test.rs#L9

warning: unused import: `CustomRewardsModule` --> farm-staking/farm-staking/tests/farm_staking_test.rs:9:22 | 9 | custom_rewards::{CustomRewardsModule, BLOCKS_IN_YEAR, MAX_PERCENT}, | ^^^^^^^^^^^^^^^^^^^
Raw output
farm-staking/farm-staking/tests/farm_staking_test.rs:9:22:w:warning: unused import: `CustomRewardsModule`
 --> farm-staking/farm-staking/tests/farm_staking_test.rs:9:22
  |
9 |     custom_rewards::{CustomRewardsModule, BLOCKS_IN_YEAR, MAX_PERCENT},
  |                      ^^^^^^^^^^^^^^^^^^^


__END__
token_attributes::UnbondSftAttributes,
};
use farm_staking_setup::*;
Expand Down Expand Up @@ -231,3 +233,19 @@ fn test_unbond() {
USER_TOTAL_RIDE_TOKENS + expected_rewards,
);
}

#[test]
fn test_withdraw_rewards() {
DebugApi::dummy();
let mut farm_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let initial_rewards_capacity = 1_000_000_000_000u64;
farm_setup.check_rewards_capacity(initial_rewards_capacity);

let withdraw_amount = rust_biguint!(TOTAL_REWARDS_AMOUNT);
farm_setup.withdraw_rewards(&withdraw_amount);

let final_rewards_capacity = 0u64;
farm_setup.check_rewards_capacity(final_rewards_capacity);
}

0 comments on commit c031013

Please sign in to comment.