Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MEX-589] Update deployer sc impl #978

Open
wants to merge 31 commits into
base: rc/update-deployer-sc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
541392d
farm and farm staking refactor
psorinionut Nov 20, 2024
e233615
fixed unit tests
psorinionut Nov 20, 2024
6b1fbc1
added permissions hub module
psorinionut Nov 20, 2024
6d21325
unit tests fix
psorinionut Nov 20, 2024
dcb6407
farm with locked rewards unit tests fix
psorinionut Nov 20, 2024
a2f526f
added OriginalOwnerHelperModule
psorinionut Nov 20, 2024
8fd716c
impl
dorin-iancu Nov 26, 2024
0bedd08
change deployer impl
dorin-iancu Nov 27, 2024
d5881f7
more views, more storage, todo
dorin-iancu Nov 27, 2024
76f1a4d
move test to separate file
dorin-iancu Nov 27, 2024
a250f01
removing options
dorin-iancu Nov 27, 2024
b2291f4
test setup
dorin-iancu Nov 27, 2024
58a1c5c
deploy farm staking test + changes in farms
dorin-iancu Nov 27, 2024
a6d4df3
fix other tests
dorin-iancu Nov 27, 2024
88270c2
additional endpoint
dorin-iancu Nov 27, 2024
6193e89
new test
dorin-iancu Nov 27, 2024
763221c
change some impl
dorin-iancu Nov 28, 2024
79fa39f
new endpoint
dorin-iancu Nov 28, 2024
c79463c
more tests
dorin-iancu Nov 28, 2024
3951ff2
Merge pull request #966 from multiversx/remove-farm-exit-fee
dorin-iancu Dec 2, 2024
7a23523
Merge branch 'rc/v3.3' into move-farm-pos-code
dorin-iancu Dec 2, 2024
ec0d542
merge with base
dorin-iancu Dec 2, 2024
a2202a4
Merge pull request #968 from multiversx/move-farm-pos-code
dorin-iancu Dec 2, 2024
20bf814
merge again
dorin-iancu Dec 2, 2024
22eab17
Merge pull request #982 from multiversx/merge-rc-v3.3-with-farm-on-be…
dorin-iancu Dec 2, 2024
4dc6419
Merge branch 'rc/update-deployer-sc' into update-deployer-sc-impl
dorin-iancu Dec 2, 2024
a415a0b
Merge branch 'rc/v3.3' into update-deployer-sc-impl
dorin-iancu Dec 2, 2024
aaee2cc
evil clippy
dorin-iancu Dec 2, 2024
dd58d66
clippy in tests
dorin-iancu Dec 2, 2024
daa6e2a
update rust and build
dorin-iancu Dec 2, 2024
90f8d81
more clippy
dorin-iancu Dec 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion common/modules/farm/contexts/src/storage_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'a, C: FarmContracTraitBounds> StorageCache<'a, C> {
}
}

impl<'a, C: FarmContracTraitBounds> Drop for StorageCache<'a, C> {
impl<C: FarmContracTraitBounds> Drop for StorageCache<'_, C> {
fn drop(&mut self) {
// commit changes to storage for the mutable fields
self.sc_ref.reward_reserve().set(&self.reward_reserve);
Expand Down
2 changes: 2 additions & 0 deletions common/modules/farm/farm_base_impl/src/base_farm_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ pub trait BaseFarmInitModule:
self.add_permissions(caller, Permissions::OWNER | Permissions::PAUSE);
self.add_permissions_for_all(admins, Permissions::ADMIN);
};

self.pause();
}
}
15 changes: 15 additions & 0 deletions common/modules/original_owner_helper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "original_owner_helper"
version = "0.0.0"
authors = ["MultiversX <[email protected]>"]
edition = "2021"

[lib]
path = "src/lib.rs"

[dependencies.multiversx-sc]
version = "=0.53.2"
features = ["esdt-token-payment-legacy-decode"]

[dependencies.common_structs]
path = "../../common_structs"
62 changes: 62 additions & 0 deletions common/modules/original_owner_helper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#![no_std]

multiversx_sc::imports!();

use common_structs::{FarmToken, PaymentsVec};

#[multiversx_sc::module]
pub trait OriginalOwnerHelperModule {
fn check_and_return_original_owner<T: FarmToken<Self::Api> + TopDecode>(
&self,
payments: &PaymentsVec<Self::Api>,
farm_token_mapper: &NonFungibleTokenMapper,
) -> ManagedAddress {
let mut original_owner = ManagedAddress::zero();
for payment in payments.iter() {
let attributes: T = farm_token_mapper.get_token_attributes(payment.token_nonce);
let payment_original_owner = attributes.get_original_owner();

if original_owner.is_zero() {
original_owner = payment_original_owner;
} else {
require!(
original_owner == payment_original_owner,
"All position must have the same original owner"
);
}
}

require!(
!original_owner.is_zero(),
"Original owner could not be identified"
);

original_owner
}

fn check_additional_payments_original_owner<T: FarmToken<Self::Api> + TopDecode>(
&self,
user: &ManagedAddress,
payments: &PaymentsVec<Self::Api>,
farm_token_mapper: &NonFungibleTokenMapper,
) {
if payments.len() == 1 {
return;
}

let farm_token_id = farm_token_mapper.get_token_id();
for payment in payments.into_iter() {
if payment.token_identifier != farm_token_id {
continue;
}

let attributes: T = farm_token_mapper.get_token_attributes(payment.token_nonce);
let payment_original_owner = attributes.get_original_owner();

require!(
user == &payment_original_owner,
"Provided address is not the same as the original owner"
);
}
}
}
15 changes: 15 additions & 0 deletions common/modules/permissions_hub_module/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "permissions_hub_module"
version = "0.0.0"
authors = ["MultiversX <[email protected]>"]
edition = "2021"

[lib]
path = "src/permissions_hub_module.rs"

[dependencies.permissions-hub]
path = "../../../dex/permissions-hub"

[dependencies.multiversx-sc]
version = "=0.53.2"
features = ["esdt-token-payment-legacy-decode"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![no_std]

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[multiversx_sc::module]
pub trait PermissionsHubModule {
fn require_user_whitelisted(&self, user: &ManagedAddress, authorized_address: &ManagedAddress) {
let permissions_hub_address = self.permissions_hub_address().get();
let is_whitelisted: bool = self
.permissions_hub_proxy(permissions_hub_address)
.is_whitelisted(user, authorized_address)
.execute_on_dest_context();

require!(is_whitelisted, "Caller is not whitelisted by the user");
}

#[only_owner]
#[endpoint(setPermissionsHubAddress)]
fn set_permissions_hub_address(&self, address: ManagedAddress) {
self.permissions_hub_address().set(&address);
}

#[proxy]
fn permissions_hub_proxy(
&self,
sc_address: ManagedAddress,
) -> permissions_hub::Proxy<Self::Api>;

#[storage_mapper("permissionsHubAddress")]
fn permissions_hub_address(&self) -> SingleValueMapper<ManagedAddress>;
}
6 changes: 6 additions & 0 deletions dex/farm-with-locked-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ path = "../../common/modules/utils"
[dependencies.permissions_module]
path = "../../common/modules/permissions_module"

[dependencies.permissions_hub_module]
path = "../../common/modules/permissions_hub_module"

[dependencies.original_owner_helper]
path = "../../common/modules/original_owner_helper"

[dependencies.sc_whitelist_module]
path = "../../common/modules/sc_whitelist_module"

Expand Down
91 changes: 15 additions & 76 deletions dex/farm-with-locked-rewards/src/external_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub trait ExternalInteractionsModule:
+ farm_token::FarmTokenModule
+ pausable::PausableModule
+ permissions_module::PermissionsModule
+ permissions_hub_module::PermissionsHubModule
+ original_owner_helper::OriginalOwnerHelperModule
+ sc_whitelist_module::SCWhitelistModule
+ events::EventsModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
Expand Down Expand Up @@ -45,7 +47,13 @@ pub trait ExternalInteractionsModule:
let caller = self.blockchain().get_caller();
self.require_user_whitelisted(&user, &caller);

self.check_additional_payments_original_owner(&user);
let payments = self.get_non_empty_payments();
let farm_token_mapper = self.farm_token();
self.check_additional_payments_original_owner::<FarmTokenAttributes<Self::Api>>(
&user,
&payments,
&farm_token_mapper,
);

let boosted_rewards = self.claim_only_boosted_payment(&user);
let new_farm_token = self.enter_farm::<NoMintWrapper<Self>>(user.clone());
Expand All @@ -71,8 +79,13 @@ pub trait ExternalInteractionsModule:
#[payable("*")]
#[endpoint(claimRewardsOnBehalf)]
fn claim_rewards_on_behalf(&self) -> ClaimRewardsResultType<Self::Api> {
let user = self.check_and_return_original_owner();
let payments = self.get_non_empty_payments();
let farm_token_mapper = self.farm_token();
let caller = self.blockchain().get_caller();
let user = self.check_and_return_original_owner::<FarmTokenAttributes<Self::Api>>(
&payments,
&farm_token_mapper,
);
self.require_user_whitelisted(&user, &caller);

let claim_rewards_result = self.claim_rewards::<NoMintWrapper<Self>>(user.clone());
Expand All @@ -94,78 +107,4 @@ pub trait ExternalInteractionsModule:

(claim_rewards_result.new_farm_token, locked_rewards_payment).into()
}

fn check_and_return_original_owner(&self) -> ManagedAddress {
let payments = self.call_value().all_esdt_transfers().clone_value();
let farm_token_mapper = self.farm_token();
let mut original_owner = ManagedAddress::zero();
for payment in payments.into_iter() {
let attributes: FarmTokenAttributes<Self::Api> =
farm_token_mapper.get_token_attributes(payment.token_nonce);

if original_owner.is_zero() {
original_owner = attributes.original_owner;
} else {
require!(
original_owner == attributes.original_owner,
"All position must have the same original owner"
);
}
}

require!(
!original_owner.is_zero(),
"Original owner could not be identified"
);

original_owner
}

fn check_additional_payments_original_owner(&self, user: &ManagedAddress) {
let payments = self.call_value().all_esdt_transfers().clone_value();
if payments.len() == 1 {
return;
}

let farm_token_mapper = self.farm_token();
let farm_token_id = farm_token_mapper.get_token_id();
for payment in payments.into_iter() {
if payment.token_identifier != farm_token_id {
continue;
}

let attributes: FarmTokenAttributes<Self::Api> =
farm_token_mapper.get_token_attributes(payment.token_nonce);

require!(
user == &attributes.original_owner,
"Provided address is not the same as the original owner"
);
}
}

fn require_user_whitelisted(&self, user: &ManagedAddress, authorized_address: &ManagedAddress) {
let permissions_hub_address = self.permissions_hub_address().get();
let is_whitelisted: bool = self
.permissions_hub_proxy(permissions_hub_address)
.is_whitelisted(user, authorized_address)
.execute_on_dest_context();

require!(is_whitelisted, "Caller is not whitelisted by the user");
}

#[only_owner]
#[endpoint(setPermissionsHubAddress)]
fn set_permissions_hub_address(&self, address: ManagedAddress) {
self.permissions_hub_address().set(&address);
}

#[proxy]
fn permissions_hub_proxy(
&self,
sc_address: ManagedAddress,
) -> permissions_hub::Proxy<Self::Api>;

#[storage_mapper("permissionsHubAddress")]
fn permissions_hub_address(&self) -> SingleValueMapper<ManagedAddress>;
}
Loading
Loading