Skip to content

Commit

Permalink
added OriginalOwnerHelperModule
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Nov 20, 2024
1 parent dcb6407 commit a2f526f
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 55 deletions.
11 changes: 11 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/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"
);
}
}
}
56 changes: 1 addition & 55 deletions common/modules/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

multiversx_sc::imports!();

use common_structs::{FarmToken, PaymentAttributesPair, PaymentsVec};
use common_structs::{PaymentAttributesPair, PaymentsVec};
use fixed_supply_token::FixedSupplyToken;
use mergeable::Mergeable;

Expand Down Expand Up @@ -116,60 +116,6 @@ pub trait UtilsModule {
}
}

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"
);
}
}

fn require_valid_token_id(&self, token_id: &TokenIdentifier) {
require!(token_id.is_valid_esdt_identifier(), "Invalid token ID");
}
Expand Down
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 @@ -44,6 +44,9 @@ 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
1 change: 1 addition & 0 deletions dex/farm-with-locked-rewards/src/external_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub trait ExternalInteractionsModule:
+ 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
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 @@ -30,6 +30,7 @@ pub trait Farm:
+ 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
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.

3 changes: 3 additions & 0 deletions dex/farm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ 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
1 change: 1 addition & 0 deletions dex/farm/src/external_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub trait ExternalInteractionsModule:
+ 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
1 change: 1 addition & 0 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub trait Farm:
+ 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
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.

3 changes: 3 additions & 0 deletions farm-staking/farm-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ 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
1 change: 1 addition & 0 deletions farm-staking/farm-staking/src/external_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub trait ExternalInteractionsModule:
+ pausable::PausableModule
+ permissions_module::PermissionsModule
+ permissions_hub_module::PermissionsHubModule
+ original_owner_helper::OriginalOwnerHelperModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ farm_base_impl::base_farm_init::BaseFarmInitModule
+ farm_base_impl::base_farm_validation::BaseFarmValidationModule
Expand Down
1 change: 1 addition & 0 deletions farm-staking/farm-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub trait FarmStaking:
+ pausable::PausableModule
+ permissions_module::PermissionsModule
+ permissions_hub_module::PermissionsHubModule
+ original_owner_helper::OriginalOwnerHelperModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ farm_base_impl::base_farm_init::BaseFarmInitModule
+ farm_base_impl::base_farm_validation::BaseFarmValidationModule
Expand Down
10 changes: 10 additions & 0 deletions farm-staking/farm-staking/wasm/Cargo.lock

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

0 comments on commit a2f526f

Please sign in to comment.