-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #967 from multiversx/farm-on-behalf-features
[MEX-580] Add on behalf features for farm SC
- Loading branch information
Showing
58 changed files
with
3,097 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
32 changes: 32 additions & 0 deletions
32
common/modules/permissions_hub_module/src/permissions_hub_module.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.