Skip to content

Commit

Permalink
Merge pull request #913 from multiversx/hooks-updated
Browse files Browse the repository at this point in the history
Hooks updated
  • Loading branch information
dorin-iancu authored May 20, 2024
2 parents b8092a3 + 5ac1de2 commit b60d549
Show file tree
Hide file tree
Showing 91 changed files with 5,897 additions and 85 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
push:
branches: [ main, feat/* ]
branches: [ main, feat/*, rc/* ]
pull_request:
branches: [ main, feat/* ]
branches: [ main, feat/*, rc/* ]
workflow_dispatch:

permissions:
Expand Down Expand Up @@ -77,4 +77,4 @@ jobs:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body-file: ./coverage.md
edit-mode: replace
edit-mode: replace
68 changes: 68 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ members = [
"dex/pair-mock",
"dex/pair-mock/meta",

"dex/sample-hooks/pair-hooks-sample",
"dex/sample-hooks/pair-hooks-sample/meta",

"energy-integration/energy-factory-mock",
"energy-integration/energy-factory-mock/meta",
"energy-integration/energy-update",
Expand All @@ -32,6 +35,8 @@ members = [

"farm-staking/farm-staking",
"farm-staking/farm-staking/meta",
"farm-staking/farm-staking-nft",
"farm-staking/farm-staking-nft/meta",
"farm-staking/farm-staking-proxy",
"farm-staking/farm-staking-proxy/meta",
"farm-staking/metabonding-staking",
Expand Down
15 changes: 15 additions & 0 deletions common/modules/banned_addresses/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "banned_addresses"
version = "0.0.0"
authors = ["Dorin Iancu <[email protected]>"]
edition = "2021"

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

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

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

multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait BannedAddressModule: permissions_module::PermissionsModule {
#[endpoint(addBannedAddress)]
fn add_banned_address(&self, addresses: MultiValueEncoded<ManagedAddress>) {
self.require_caller_has_owner_or_admin_permissions();

let mapper = self.banned_addresses();
for address in addresses {
mapper.add(&address);
}
}

#[endpoint(removeBannedAddress)]
fn remove_banned_address(&self, addresses: MultiValueEncoded<ManagedAddress>) {
self.require_caller_has_owner_or_admin_permissions();

let mapper = self.banned_addresses();
for address in addresses {
mapper.remove(&address);
}
}

fn require_not_banned_address(&self, address: &ManagedAddress) {
require!(
!self.banned_addresses().contains(address),
"Cannot add hook for this address"
);
}

#[storage_mapper("bannedAddresses")]
fn banned_addresses(&self) -> WhitelistMapper<ManagedAddress>;
}
1 change: 0 additions & 1 deletion common/modules/farm/farm_base_impl/src/base_traits_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ pub trait FarmContract {
}
}

#[inline]
fn increase_user_farm_position(
sc: &Self::FarmSc,
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
Expand Down
27 changes: 25 additions & 2 deletions common/modules/farm/farm_base_impl/src/enter_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use contexts::{
enter_farm_context::EnterFarmContext,
storage_cache::{FarmContracTraitBounds, StorageCache},
};
use fixed_supply_token::FixedSupplyToken;

pub struct InternalEnterFarmResult<'a, C, T>
where
Expand Down Expand Up @@ -34,6 +35,21 @@ pub trait BaseEnterFarmModule:
&self,
caller: ManagedAddress,
payments: PaymentsVec<Self::Api>,
) -> InternalEnterFarmResult<Self, FC::AttributesType> {
let mut result = self.enter_farm_base_no_token_create::<FC>(caller, payments);
let new_farm_token_payment = self.farm_token().nft_create(
result.new_farm_token.payment.amount,
&result.new_farm_token.attributes,
);
result.new_farm_token.payment = new_farm_token_payment;

result
}

fn enter_farm_base_no_token_create<FC: FarmContract<FarmSc = Self>>(
&self,
caller: ManagedAddress,
payments: PaymentsVec<Self::Api>,
) -> InternalEnterFarmResult<Self, FC::AttributesType> {
let mut storage_cache = StorageCache::new(self);
self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id);
Expand All @@ -55,7 +71,6 @@ pub trait BaseEnterFarmModule:
&caller,
&enter_farm_context.farming_token_payment.amount,
);

FC::generate_aggregated_rewards(self, &mut storage_cache);

storage_cache.farm_token_supply += &enter_farm_context.farming_token_payment.amount;
Expand All @@ -67,11 +82,19 @@ pub trait BaseEnterFarmModule:
enter_farm_context.farming_token_payment.amount.clone(),
storage_cache.reward_per_share.clone(),
);
let new_farm_token = self.merge_and_create_token(
let new_token_attributes = self.merge_attributes_from_payments(
base_attributes,
&enter_farm_context.additional_farm_tokens,
&farm_token_mapper,
);
let new_farm_token = PaymentAttributesPair {
payment: EsdtTokenPayment::new(
storage_cache.farm_token_id.clone(),
0,
new_token_attributes.get_total_supply(),
),
attributes: new_token_attributes,
};

self.send()
.esdt_local_burn_multi(&enter_farm_context.additional_farm_tokens);
Expand Down
30 changes: 27 additions & 3 deletions common/modules/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,42 @@ pub trait UtilsModule {
payments.clone_value()
}

fn pop_first_payment(
fn pop_first_payment(&self, payments: &mut PaymentsVec<Self::Api>) -> EsdtTokenPayment {
require!(!payments.is_empty(), ERR_EMPTY_PAYMENTS);

let first_payment = payments.get(0);
payments.remove(0);

first_payment
}

fn pop_or_return_payment(
&self,
payments: &mut PaymentsVec<Self::Api>,
) -> EsdtTokenPayment<Self::Api> {
require!(!payments.is_empty(), ERR_EMPTY_PAYMENTS);
payment_if_empty: EsdtTokenPayment,
) -> EsdtTokenPayment {
if payments.is_empty() {
return payment_if_empty;
}

let first_payment = payments.get(0);
payments.remove(0);

first_payment
}

fn push_if_non_zero_payment(
&self,
payments: &mut PaymentsVec<Self::Api>,
new_payment: EsdtTokenPayment,
) {
if new_payment.amount == 0 {
return;
}

payments.push(new_payment);
}

fn get_attributes_as_part_of_fixed_supply<T: FixedSupplyToken<Self::Api> + TopDecode>(
&self,
payment: &EsdtTokenPayment,
Expand Down
9 changes: 9 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.

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 dex/pair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ path = "../../energy-integration/fees-collector"
[dependencies.utils]
path = "../../common/modules/utils"

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

[dependencies.itertools]
version = "0.10.1"
default-features = false
Expand Down
7 changes: 7 additions & 0 deletions dex/pair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod fee;
mod liquidity_pool;
pub mod locking_wrapper;
pub mod pair_actions;
pub mod pair_hooks;
pub mod safe_price;
pub mod safe_price_view;

Expand Down Expand Up @@ -45,6 +46,9 @@ pub trait Pair<ContractReader>:
+ pair_actions::swap::SwapModule
+ pair_actions::views::ViewsModule
+ pair_actions::common_methods::CommonMethodsModule
+ pair_hooks::change_hooks::ChangeHooksModule
+ pair_hooks::call_hook::CallHookModule
+ banned_addresses::BannedAddressModule
+ utils::UtilsModule
{
#[init]
Expand Down Expand Up @@ -97,6 +101,9 @@ pub trait Pair<ContractReader>:
);
self.add_permissions_for_all(admins, Permissions::ADMIN);
};

let sc_address = self.blockchain().get_sc_address();
self.banned_addresses().add(&sc_address);
}

#[endpoint]
Expand Down
Loading

0 comments on commit b60d549

Please sign in to comment.