Skip to content

Commit

Permalink
upgradeContractsByTemplate endpoint+code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Nov 20, 2023
1 parent 0a9e5ea commit 42ffe7e
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 287 deletions.
3 changes: 3 additions & 0 deletions contracts/proxy-deployer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ features = ["esdt-token-payment-legacy-decode"]
[dev-dependencies.multiversx-sc-scenario]
version = "=0.43.4"

[dependencies.multiversx-sc-modules]
version = "0.43.4"

[dev-dependencies.adder]
path = "../adder"
192 changes: 0 additions & 192 deletions contracts/proxy-deployer/src/address_to_id_mapper.rs

This file was deleted.

67 changes: 53 additions & 14 deletions contracts/proxy-deployer/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,68 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use crate::address_to_id_mapper::{AddressId, AddressToIdMapper};
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi)]
pub struct OngoingUpgradeOperation<M: ManagedTypeApi> {
pub template_address: ManagedAddress<M>,
pub arguments: ManagedArgBuffer<M>,
pub contracts_remaining: ManagedVec<M, ManagedAddress<M>>,
}

impl<M: ManagedTypeApi> OngoingUpgradeOperation<M> {
#[inline]
pub fn new(
template_address: ManagedAddress<M>,
arguments: ManagedArgBuffer<M>,
contracts_remaining: ManagedVec<M, ManagedAddress<M>>,
) -> Self {
OngoingUpgradeOperation {
template_address,
arguments,
contracts_remaining,
}
}
}

#[multiversx_sc::module]
pub trait ConfigModule {
#[only_owner]
#[endpoint(addContractTemplate)]
fn add_contract_template(&self, template_address: ManagedAddress) -> AddressId {
#[endpoint(addDeployerToBlacklist)]
fn add_deployer_to_blacklist(&self, blacklisted_address: ManagedAddress) {
require!(
self.blockchain().is_smart_contract(&template_address),
"Invalid template address"
self.deployers_list().contains(&blacklisted_address),
"The address is not a deployer"
);

self.address_ids().insert_new(&template_address)
require!(
!self
.blacklisted_deployers_list()
.contains(&blacklisted_address),
"Address already blacklisted"
);
self.blacklisted_deployers_list()
.insert(blacklisted_address);
}

#[only_owner]
#[endpoint(removeContractTemplate)]
fn remove_contract_template(&self, address_id: AddressId) {
#[endpoint(removeDeployerFromBlacklist)]
fn remove_deployer_from_blacklist(&self, address: ManagedAddress) {
require!(
self.address_ids().contains_id(address_id),
"Invalid address id"
self.blacklisted_deployers_list().contains(&address),
"Address is not blacklisted"
);

self.address_ids().remove_by_id(address_id);
self.blacklisted_deployers_list().swap_remove(&address);
}

#[storage_mapper("addressIds")]
fn address_ids(&self) -> AddressToIdMapper<Self::Api>;
#[view(getAllDeployedContractsByTemplate)]
#[storage_mapper("deployedContractsByTemplate")]
fn deployed_contracts_list_by_template(
&self,
template_address: &ManagedAddress,
) -> SingleValueMapper<ManagedVec<ManagedAddress>>;

#[view(getOngoingUpgradeOperations)]
#[storage_mapper("ongoingUpgradeOperation")]
fn ongoing_upgrade_operation(&self) -> SingleValueMapper<OngoingUpgradeOperation<Self::Api>>;

#[view(getAllDeployers)]
#[storage_mapper("deployersList")]
Expand All @@ -39,4 +74,8 @@ pub trait ConfigModule {
&self,
deployer_address: &ManagedAddress,
) -> UnorderedSetMapper<ManagedAddress>;

#[view(getAllBlacklistedDeployers)]
#[storage_mapper("blacklistedDeployersList")]
fn blacklisted_deployers_list(&self) -> UnorderedSetMapper<ManagedAddress>;
}
Loading

0 comments on commit 42ffe7e

Please sign in to comment.