Skip to content

Commit

Permalink
added permissions hub module
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Nov 20, 2024
1 parent e233615 commit 6b1fbc1
Show file tree
Hide file tree
Showing 23 changed files with 114 additions and 104 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

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>;
}
3 changes: 3 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,9 @@ path = "../../common/modules/utils"
[dependencies.permissions_module]
path = "../../common/modules/permissions_module"

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

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

Expand Down
26 changes: 1 addition & 25 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,7 @@ pub trait ExternalInteractionsModule:
+ farm_token::FarmTokenModule
+ pausable::PausableModule
+ permissions_module::PermissionsModule
+ permissions_hub_module::PermissionsHubModule
+ sc_whitelist_module::SCWhitelistModule
+ events::EventsModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
Expand Down Expand Up @@ -105,29 +106,4 @@ pub trait ExternalInteractionsModule:

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

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>;
}
1 change: 1 addition & 0 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub trait Farm:
+ utils::UtilsModule
+ pausable::PausableModule
+ permissions_module::PermissionsModule
+ permissions_hub_module::PermissionsHubModule
+ sc_whitelist_module::SCWhitelistModule
+ events::EventsModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
Expand Down
10 changes: 10 additions & 0 deletions dex/farm-with-locked-rewards/wasm/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 dex/farm-with-locked-rewards/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ multiversx_sc_wasm_adapter::endpoints! {
removeAdmin => remove_admin_endpoint
updateOwnerOrAdmin => update_owner_or_admin_endpoint
getPermissions => permissions
setPermissionsHubAddress => set_permissions_hub_address
addSCAddressToWhitelist => add_sc_address_to_whitelist
removeSCAddressFromWhitelist => remove_sc_address_from_whitelist
isSCAddressWhitelisted => is_sc_address_whitelisted
Expand All @@ -68,7 +69,6 @@ multiversx_sc_wasm_adapter::endpoints! {
getPairContractManagedAddress => pair_contract_address
enterFarmOnBehalf => enter_farm_on_behalf
claimRewardsOnBehalf => claim_rewards_on_behalf
setPermissionsHubAddress => set_permissions_hub_address
collectUndistributedBoostedRewards => collect_undistributed_boosted_rewards
getBoostedYieldsRewardsPercentage => boosted_yields_rewards_percentage
getAccumulatedRewardsForWeek => accumulated_rewards_for_week
Expand Down
3 changes: 3 additions & 0 deletions dex/farm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ path = "../../common/modules/pausable"
[dependencies.permissions_module]
path = "../../common/modules/permissions_module"

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

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

Expand Down
26 changes: 1 addition & 25 deletions dex/farm/src/external_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub trait ExternalInteractionsModule:
+ farm_token::FarmTokenModule
+ pausable::PausableModule
+ permissions_module::PermissionsModule
+ permissions_hub_module::PermissionsHubModule
+ sc_whitelist_module::SCWhitelistModule
+ events::EventsModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
Expand Down Expand Up @@ -85,29 +86,4 @@ pub trait ExternalInteractionsModule:

claim_rewards_result.into()
}

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>;
}
1 change: 1 addition & 0 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub trait Farm:
+ farm_token::FarmTokenModule
+ pausable::PausableModule
+ permissions_module::PermissionsModule
+ permissions_hub_module::PermissionsHubModule
+ sc_whitelist_module::SCWhitelistModule
+ events::EventsModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
Expand Down
9 changes: 9 additions & 0 deletions dex/farm/wasm/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 dex/farm/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ multiversx_sc_wasm_adapter::endpoints! {
removeAdmin => remove_admin_endpoint
updateOwnerOrAdmin => update_owner_or_admin_endpoint
getPermissions => permissions
setPermissionsHubAddress => set_permissions_hub_address
addSCAddressToWhitelist => add_sc_address_to_whitelist
removeSCAddressFromWhitelist => remove_sc_address_from_whitelist
isSCAddressWhitelisted => is_sc_address_whitelisted
Expand All @@ -65,7 +66,6 @@ multiversx_sc_wasm_adapter::endpoints! {
getPairContractManagedAddress => pair_contract_address
enterFarmOnBehalf => enter_farm_on_behalf
claimRewardsOnBehalf => claim_rewards_on_behalf
setPermissionsHubAddress => set_permissions_hub_address
collectUndistributedBoostedRewards => collect_undistributed_boosted_rewards
getBoostedYieldsRewardsPercentage => boosted_yields_rewards_percentage
getAccumulatedRewardsForWeek => accumulated_rewards_for_week
Expand Down
3 changes: 3 additions & 0 deletions farm-staking/farm-staking-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ path = "../../energy-integration/common-modules/energy-query"
[dependencies.permissions-hub]
path = "../../dex/permissions-hub"

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

[dev-dependencies]
num-bigint = "0.4.2"

Expand Down
1 change: 1 addition & 0 deletions farm-staking/farm-staking-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub trait FarmStakingProxy:
+ external_contracts_interactions::ExternalContractsInteractionsModule
+ lp_farm_token::LpFarmTokenModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ permissions_hub_module::PermissionsHubModule
+ utils::UtilsModule
+ token_send::TokenSendModule
+ energy_query::EnergyQueryModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub trait ProxyExternalInteractionsModule:
+ crate::proxy_actions::stake::ProxyStakeModule
+ crate::proxy_actions::claim::ProxyClaimModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ permissions_hub_module::PermissionsHubModule
+ utils::UtilsModule
+ token_send::TokenSendModule
+ energy_query::EnergyQueryModule
Expand Down Expand Up @@ -115,29 +116,4 @@ pub trait ProxyExternalInteractionsModule:

attributes.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>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use farm_staking_proxy::proxy_actions::stake::ProxyStakeModule;
use farm_staking_proxy::proxy_actions::unstake::ProxyUnstakeModule;

use permissions_hub::PermissionsHub;
use permissions_hub_module::PermissionsHubModule;
use sc_whitelist_module::SCWhitelistModule;

use crate::{
Expand Down
3 changes: 3 additions & 0 deletions farm-staking/farm-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ path = "../../common/modules/pausable"
[dependencies.permissions_module]
path = "../../common/modules/permissions_module"

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

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

Expand Down
Loading

0 comments on commit 6b1fbc1

Please sign in to comment.