Skip to content

Commit

Permalink
Merge pull request #977 from multiversx/actual-impl-auto-buyback-and-…
Browse files Browse the repository at this point in the history
…burn

Impl auto buyback and burn
  • Loading branch information
dorin-iancu authored Dec 10, 2024
2 parents 8080e9a + 6747154 commit c59b61f
Show file tree
Hide file tree
Showing 36 changed files with 1,279 additions and 837 deletions.
6 changes: 3 additions & 3 deletions common/modules/farm/farm_base_impl/src/base_farm_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ pub trait BaseFarmInitModule:
self.farming_token_id().set_if_empty(&farming_token_id);

if !owner.is_zero() {
self.add_permissions(owner, Permissions::OWNER | Permissions::PAUSE);
self.add_permissions(&owner, Permissions::OWNER | Permissions::PAUSE);
}

let caller = self.blockchain().get_caller();
if admins.is_empty() {
// backwards compatibility
let all_permissions = Permissions::OWNER | Permissions::ADMIN | Permissions::PAUSE;
self.add_permissions(caller, all_permissions);
self.add_permissions(&caller, all_permissions);
} else {
self.add_permissions(caller, Permissions::OWNER | Permissions::PAUSE);
self.add_permissions(&caller, Permissions::OWNER | Permissions::PAUSE);
self.add_permissions_for_all(admins, Permissions::ADMIN);
};
}
Expand Down
2 changes: 1 addition & 1 deletion common/modules/pausable/src/pausable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait PausableModule: permissions_module::PermissionsModule {
self.require_caller_has_owner_permissions();

for address in address_list {
self.remove_permissions(address, Permissions::PAUSE);
self.remove_permissions(&address, Permissions::PAUSE);
}
}

Expand Down
22 changes: 11 additions & 11 deletions common/modules/permissions_module/src/permissions_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@ pub trait PermissionsModule {
#[endpoint(addAdmin)]
fn add_admin_endpoint(&self, address: ManagedAddress) {
self.require_caller_has_owner_permissions();
self.add_permissions(address, Permissions::ADMIN);
self.add_permissions(&address, Permissions::ADMIN);
}

#[endpoint(removeAdmin)]
fn remove_admin_endpoint(&self, address: ManagedAddress) {
self.require_caller_has_owner_permissions();
self.remove_permissions(address, Permissions::ADMIN);
self.remove_permissions(&address, Permissions::ADMIN);
}

#[only_owner]
#[endpoint(updateOwnerOrAdmin)]
fn update_owner_or_admin_endpoint(&self, previous_owner: ManagedAddress) {
let caller = self.blockchain().get_caller();
let previous_owner_permissions = self.permissions(previous_owner.clone()).get();
let previous_owner_permissions = self.permissions(&previous_owner).get();

self.permissions(previous_owner).clear();
self.permissions(caller).set(previous_owner_permissions);
self.permissions(&previous_owner).clear();
self.permissions(&caller).set(previous_owner_permissions);
}

fn set_permissions(&self, address: ManagedAddress, permissions: Permissions) {
fn set_permissions(&self, address: &ManagedAddress, permissions: Permissions) {
self.permissions(address).set(permissions);
}

fn add_permissions(&self, address: ManagedAddress, new_permissions: Permissions) {
fn add_permissions(&self, address: &ManagedAddress, new_permissions: Permissions) {
self.permissions(address).update(|permissions| {
permissions.insert(new_permissions);
});
}

fn remove_permissions(&self, address: ManagedAddress, permissions_to_remove: Permissions) {
fn remove_permissions(&self, address: &ManagedAddress, permissions_to_remove: Permissions) {
self.permissions(address).update(|permissions| {
permissions.remove(permissions_to_remove);
});
Expand All @@ -54,13 +54,13 @@ pub trait PermissionsModule {
permissions: Permissions,
) {
for address in addresses {
self.add_permissions(address, permissions.clone());
self.add_permissions(&address, permissions.clone());
}
}

fn require_caller_any_of(&self, permissions: Permissions) {
let caller = self.blockchain().get_caller();
let caller_permissions = self.permissions(caller).get();
let caller_permissions = self.permissions(&caller).get();
require!(
caller_permissions.intersects(permissions),
ERROR_PERMISSION_DENIED
Expand All @@ -85,5 +85,5 @@ pub trait PermissionsModule {

#[view(getPermissions)]
#[storage_mapper("permissions")]
fn permissions(&self, address: ManagedAddress) -> SingleValueMapper<Permissions>;
fn permissions(&self, address: &ManagedAddress) -> SingleValueMapper<Permissions>;
}
2 changes: 1 addition & 1 deletion dex/farm/tests/farm_multi_user_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ fn farm_change_boosted_yields_factors_test() {
BoostedYieldsConfig::new(current_week - 1, default_factors.clone());
assert_eq!(expected_config, sc.boosted_yields_config().get());

sc.add_permissions(managed_address!(&farm_addr), Permissions::all());
sc.add_permissions(&managed_address!(&farm_addr), Permissions::all());
sc.set_boosted_yields_factors(
managed_biguint!(1u64),
managed_biguint!(1u64),
Expand Down
138 changes: 138 additions & 0 deletions dex/pair/src/fee/endpoints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use common_structs::{Percent, TokenPair};

use crate::{
config::MAX_PERCENTAGE, ERROR_ALREADY_FEE_DEST, ERROR_ALREADY_WHITELISTED,
ERROR_BAD_TOKEN_FEE_DEST, ERROR_NOT_FEE_DEST, ERROR_NOT_WHITELISTED,
ERROR_PAIR_ALREADY_TRUSTED, ERROR_PAIR_NOT_TRUSTED, ERROR_SAME_TOKENS,
};

multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait EndpointsModule:
crate::config::ConfigModule
+ crate::liquidity_pool::LiquidityPoolModule
+ crate::amm::AmmModule
+ token_send::TokenSendModule
+ permissions_module::PermissionsModule
+ pausable::PausableModule
+ super::storage::StorageModule
{
#[endpoint(whitelist)]
fn whitelist_endpoint(&self, address: ManagedAddress) {
self.require_caller_has_owner_permissions();

let is_new = self.whitelist().insert(address);
require!(is_new, ERROR_ALREADY_WHITELISTED);
}

#[endpoint(removeWhitelist)]
fn remove_whitelist(&self, address: ManagedAddress) {
self.require_caller_has_owner_permissions();

let is_removed = self.whitelist().remove(&address);
require!(is_removed, ERROR_NOT_WHITELISTED);
}

#[endpoint(addTrustedSwapPair)]
fn add_trusted_swap_pair(
&self,
pair_address: ManagedAddress,
first_token: TokenIdentifier,
second_token: TokenIdentifier,
) {
self.require_caller_has_owner_permissions();
require!(first_token != second_token, ERROR_SAME_TOKENS);

let token_pair = TokenPair {
first_token,
second_token,
};
let is_new = self
.trusted_swap_pair()
.insert(token_pair, pair_address)
.is_none();
require!(is_new, ERROR_PAIR_ALREADY_TRUSTED);
}

#[endpoint(removeTrustedSwapPair)]
fn remove_trusted_swap_pair(
&self,
first_token: TokenIdentifier,
second_token: TokenIdentifier,
) {
self.require_caller_has_owner_permissions();

let token_pair = TokenPair {
first_token: first_token.clone(),
second_token: second_token.clone(),
};

let mut is_removed = self.trusted_swap_pair().remove(&token_pair).is_some();
if !is_removed {
let token_pair_reversed = TokenPair {
first_token: second_token,
second_token: first_token,
};
is_removed = self
.trusted_swap_pair()
.remove(&token_pair_reversed)
.is_some();
require!(is_removed, ERROR_PAIR_NOT_TRUSTED);
}
}

/// `fees_collector_cut_percentage` of the special fees are sent to the fees_collector_address SC
///
/// For example, if special fees is 5%, and fees_collector_cut_percentage is 10%,
/// then of the 5%, 10% are reserved, and only the rest are split between other pair contracts.
#[endpoint(setupFeesCollector)]
fn setup_fees_collector(
&self,
fees_collector_address: ManagedAddress,
fees_collector_cut_percentage: Percent,
) {
self.require_caller_has_owner_permissions();
require!(
self.blockchain().is_smart_contract(&fees_collector_address),
"Invalid fees collector address"
);
require!(
fees_collector_cut_percentage > 0 && fees_collector_cut_percentage <= MAX_PERCENTAGE,
"Invalid fees percentage"
);

self.fees_collector_address().set(&fees_collector_address);
self.fees_collector_cut_percentage()
.set(fees_collector_cut_percentage);
}

#[endpoint(setFeeOn)]
fn set_fee_on(&self, fee_to_address: ManagedAddress, fee_token: TokenIdentifier) {
self.require_caller_has_owner_permissions();

let is_dest = self
.destination_map()
.keys()
.any(|dest_address| dest_address == fee_to_address);
require!(!is_dest, ERROR_ALREADY_FEE_DEST);

let _ = self.destination_map().insert(fee_to_address, fee_token);
}

#[endpoint(setFeeOn)]
fn set_fee_off(&self, fee_to_address: ManagedAddress, fee_token: TokenIdentifier) {
self.require_caller_has_owner_permissions();

let is_dest = self
.destination_map()
.keys()
.any(|dest_address| dest_address == fee_to_address);
require!(is_dest, ERROR_NOT_FEE_DEST);

let dest_fee_token = self.destination_map().get(&fee_to_address).unwrap();
require!(fee_token == dest_fee_token, ERROR_BAD_TOKEN_FEE_DEST);

let _ = self.destination_map().remove(&fee_to_address);
}
}
Loading

0 comments on commit c59b61f

Please sign in to comment.