Skip to content

Commit

Permalink
Merge pull request #200 from multiversx/cf-only-admin
Browse files Browse the repository at this point in the history
Added `only_admin` anotation for Chain-Factory endpoints
  • Loading branch information
andreiblt1304 authored Dec 9, 2024
2 parents debe677 + d316ac2 commit 70fd828
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
13 changes: 7 additions & 6 deletions chain-factory/src/factory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use multiversx_sc::imports::*;
use multiversx_sc_modules::only_admin;
use proxies::{
chain_config_proxy::ChainConfigContractProxy,
enshrine_esdt_safe_proxy::EnshrineEsdtSafeProxy,
Expand All @@ -9,8 +10,8 @@ use transaction::StakeMultiArg;
multiversx_sc::derive_imports!();

#[multiversx_sc::module]
pub trait FactoryModule {
#[only_owner]
pub trait FactoryModule: only_admin::OnlyAdminModule {
#[only_admin]
#[endpoint(deploySovereignChainConfigContract)]
fn deploy_sovereign_chain_config_contract(
&self,
Expand Down Expand Up @@ -39,7 +40,7 @@ pub trait FactoryModule {
.sync_call()
}

#[only_owner]
#[only_admin]
#[endpoint(deployHeaderVerifier)]
fn deploy_header_verifier(
&self,
Expand All @@ -58,7 +59,7 @@ pub trait FactoryModule {
.sync_call()
}

#[only_owner]
#[only_admin]
#[endpoint(deployEnshrineEsdtSafe)]
fn deploy_enshrine_esdt_safe(
&self,
Expand All @@ -85,7 +86,7 @@ pub trait FactoryModule {
.sync_call()
}

#[only_owner]
#[only_admin]
#[endpoint(deployFeeMarket)]
fn deploy_fee_market(
&self,
Expand All @@ -105,7 +106,7 @@ pub trait FactoryModule {
.sync_call()
}

#[only_owner]
#[only_admin]
#[endpoint(completeSetupPhase)]
fn complete_setup_phase(&self, _contract_address: ManagedAddress) {
// TODO: will have to call each contract's endpoint to finish setup phase
Expand Down
9 changes: 8 additions & 1 deletion chain-factory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
#![no_std]

use multiversx_sc_modules::only_admin;

multiversx_sc::imports!();

pub mod factory;

#[multiversx_sc::contract]
pub trait ChainFactoryContract: factory::FactoryModule + utils::UtilsModule {
pub trait ChainFactoryContract:
factory::FactoryModule + utils::UtilsModule + only_admin::OnlyAdminModule
{
#[init]
fn init(
&self,
sovereign_forge_address: ManagedAddress,
chain_config_template: ManagedAddress,
header_verifier_template: ManagedAddress,
cross_chain_operation_template: ManagedAddress,
fee_market_template: ManagedAddress,
) {
self.require_sc_address(&sovereign_forge_address);
self.require_sc_address(&chain_config_template);
self.require_sc_address(&header_verifier_template);
self.require_sc_address(&cross_chain_operation_template);
self.require_sc_address(&fee_market_template);

self.add_admin(sovereign_forge_address);
self.chain_config_template().set(chain_config_template);
self.header_verifier_template()
.set(header_verifier_template);
Expand Down
13 changes: 9 additions & 4 deletions chain-factory/tests/chain_factory_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const CODE_PATH: MxscPath = MxscPath::new("output/chain-factory.mxsc.json");
const CONFIG_ADDRESS: TestSCAddress = TestSCAddress::new("chain-config");
const CONFIG_CODE_PATH: MxscPath = MxscPath::new("../chain-config/output/chain-factory.mxsc.json");

const HEADER_ADDRESS: TestSCAddress = TestSCAddress::new("header-verifier");
const ESDT_SAFE_ADDRESS: TestSCAddress = TestSCAddress::new("esdt-safe");
const FEE_MARKET_ADDRESS: TestSCAddress = TestSCAddress::new("esdt-safe");

const OWNER: TestAddress = TestAddress::new("owner");
const OWNER_BALANCE: u64 = 100_000_000_000;

Expand Down Expand Up @@ -68,9 +72,10 @@ impl ChainFactoryTestState {
.typed(ChainFactoryContractProxy)
.init(
CONFIG_ADDRESS,
FACTORY_ADDRESS,
FACTORY_ADDRESS,
FACTORY_ADDRESS,
CONFIG_ADDRESS,
HEADER_ADDRESS,
ESDT_SAFE_ADDRESS,
FEE_MARKET_ADDRESS,
)
.code(CODE_PATH)
.new_address(FACTORY_ADDRESS)
Expand All @@ -88,7 +93,7 @@ impl ChainFactoryTestState {
let transaction = self
.world
.tx()
.from(OWNER)
.from(CONFIG_ADDRESS)
.to(FACTORY_ADDRESS)
.typed(ChainFactoryContractProxy)
.deploy_sovereign_chain_config_contract(
Expand Down
8 changes: 6 additions & 2 deletions chain-factory/wasm-chain-factory-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 5
// Endpoints: 9
// Async Callback (empty): 1
// Total number of exported functions: 8
// Total number of exported functions: 12

#![no_std]

Expand All @@ -25,6 +25,10 @@ multiversx_sc_wasm_adapter::endpoints! {
deployEnshrineEsdtSafe => deploy_enshrine_esdt_safe
deployFeeMarket => deploy_fee_market
completeSetupPhase => complete_setup_phase
isAdmin => is_admin
addAdmin => add_admin
removeAdmin => remove_admin
getAdmins => admins
)
}

Expand Down
8 changes: 6 additions & 2 deletions chain-factory/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 5
// Endpoints: 9
// Async Callback (empty): 1
// Total number of exported functions: 8
// Total number of exported functions: 12

#![no_std]

Expand All @@ -25,6 +25,10 @@ multiversx_sc_wasm_adapter::endpoints! {
deployEnshrineEsdtSafe => deploy_enshrine_esdt_safe
deployFeeMarket => deploy_fee_market
completeSetupPhase => complete_setup_phase
isAdmin => is_admin
addAdmin => add_admin
removeAdmin => remove_admin
getAdmins => admins
)
}

Expand Down
59 changes: 55 additions & 4 deletions common/proxies/src/chain_factory_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ where
Arg1: ProxyArg<ManagedAddress<Env::Api>>,
Arg2: ProxyArg<ManagedAddress<Env::Api>>,
Arg3: ProxyArg<ManagedAddress<Env::Api>>,
Arg4: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
chain_config_template: Arg0,
header_verifier_template: Arg1,
cross_chain_operation_template: Arg2,
fee_market_template: Arg3,
sovereign_forge_address: Arg0,
chain_config_template: Arg1,
header_verifier_template: Arg2,
cross_chain_operation_template: Arg3,
fee_market_template: Arg4,
) -> TxTypedDeploy<Env, From, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_deploy()
.argument(&sovereign_forge_address)
.argument(&chain_config_template)
.argument(&header_verifier_template)
.argument(&cross_chain_operation_template)
Expand Down Expand Up @@ -179,4 +182,52 @@ where
.argument(&_contract_address)
.original_result()
}

pub fn is_admin<
Arg0: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
address: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, bool> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("isAdmin")
.argument(&address)
.original_result()
}

pub fn add_admin<
Arg0: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
address: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("addAdmin")
.argument(&address)
.original_result()
}

pub fn remove_admin<
Arg0: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
address: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("removeAdmin")
.argument(&address)
.original_result()
}

pub fn admins(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, MultiValueEncoded<Env::Api, ManagedAddress<Env::Api>>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getAdmins")
.original_result()
}
}
1 change: 1 addition & 0 deletions token-handler/tests/token_handler_blackbox_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl TokenHandlerTestState {
FACTORY_ADDRESS,
FACTORY_ADDRESS,
FACTORY_ADDRESS,
FACTORY_ADDRESS,
)
.code(FACTORY_CODE_PATH)
.new_address(FACTORY_ADDRESS)
Expand Down

0 comments on commit 70fd828

Please sign in to comment.