Skip to content

Commit

Permalink
Merge pull request #205 from multiversx/phase-four
Browse files Browse the repository at this point in the history
Sovereign Forge Phase Four
  • Loading branch information
andreiblt1304 authored Dec 11, 2024
2 parents a78f490 + 8412739 commit 467a819
Show file tree
Hide file tree
Showing 17 changed files with 323 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

15 changes: 12 additions & 3 deletions chain-factory/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,23 @@ pub trait FactoryModule: only_admin::OnlyAdminModule {
let source_address = self.fee_market_template().get();
let metadata = self.blockchain().get_code_metadata(&source_address);

self.tx()
let fee_market_address = self
.tx()
.typed(FeeMarketProxy)
.init(esdt_safe_address, fee)
.init(&esdt_safe_address, fee)
.gas(60_000_000)
.from_source(source_address)
.code_metadata(metadata)
.returns(ReturnsNewManagedAddress)
.sync_call()
.sync_call();

self.tx()
.to(&esdt_safe_address)
.typed(EsdtSafeProxy)
.set_fee_market_address(&fee_market_address)
.sync_call();

fee_market_address
}

#[only_admin]
Expand Down
13 changes: 13 additions & 0 deletions common/proxies/src/sovereign_forge_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ where
.original_result()
}

pub fn deploy_phase_four<
Arg0: ProxyArg<Option<super::fee_market_proxy::FeeStruct<Env::Api>>>,
>(
self,
fee: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("deployPhaseFour")
.argument(&fee)
.original_result()
}

pub fn chain_factories<
Arg0: ProxyArg<u32>,
>(
Expand Down
3 changes: 3 additions & 0 deletions sovereign-forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ path = "../chain-config"
[dependencies.header-verifier]
path = "../header-verifier"

[dependencies.fee-market]
path = "../fee-market"

[dependencies.esdt-safe]
path = "../esdt-safe"

Expand Down
73 changes: 65 additions & 8 deletions sovereign-forge/interactor/src/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ mod config;
use config::Config;
use multiversx_sc_snippets::{imports::*, sdk::bech32};
use proxies::{
chain_config_proxy::ChainConfigContractProxy, chain_factory_proxy::ChainFactoryContractProxy,
esdt_safe_proxy::EsdtSafeProxy, header_verifier_proxy::HeaderverifierProxy,
chain_config_proxy::ChainConfigContractProxy,
chain_factory_proxy::ChainFactoryContractProxy,
esdt_safe_proxy::EsdtSafeProxy,
fee_market_proxy::{FeeMarketProxy, FeeStruct},
header_verifier_proxy::HeaderverifierProxy,
sovereign_forge_proxy::SovereignForgeProxy,
};
use serde::{Deserialize, Serialize};
Expand All @@ -20,6 +23,7 @@ const CHAIN_CONFIG_CODE_PATH: &str = "../../chain-config/output/chain-config.mxs
const CHAIN_FACTORY_CODE_PATH: &str = "../../chain-factory/output/chain-factory.mxsc.json";
const HEADER_VERIFIER_CODE_PATH: &str = "../../header-verifier/output/header-verifier.mxsc.json";
const ESDT_SAFE_CODE_PATH: &str = "../../esdt-safe/output/esdt-safe.mxsc.json";
const FEE_MARKET_CODE_PATH: &str = "../../fee-market/output/fee-market.mxsc.json";

pub async fn sovereign_forge_cli() {
env_logger::init();
Expand Down Expand Up @@ -50,6 +54,7 @@ pub struct State {
factory_address: Option<Bech32Address>,
header_verifier_address: Option<Bech32Address>,
esdt_safe_address: Option<Bech32Address>,
fee_market_address: Option<Bech32Address>,
}

impl State {
Expand All @@ -65,30 +70,36 @@ impl State {
}
}

/// Sets the contract address
/// Sets the Sovereign-Forge contract address
pub fn set_address(&mut self, address: Bech32Address) {
self.contract_address = Some(address);
}

/// Sets the contract address
/// Sets the Chain-Config contract address
pub fn set_config_template(&mut self, address: Bech32Address) {
self.config_address = Some(address);
}

/// Sets the contract address
/// Sets the Chain-Factory contract address
pub fn set_factory_template(&mut self, address: Bech32Address) {
self.factory_address = Some(address);
}

/// Sets the contract address
/// Sets the Header-Verifier contract address
pub fn set_header_verifier_address(&mut self, address: Bech32Address) {
self.header_verifier_address = Some(address);
}

/// Sets the Esdt-Safe contract address
pub fn set_esdt_safe_address(&mut self, address: Bech32Address) {
self.esdt_safe_address = Some(address);
}

/// Sets the Fee-Market contract address
pub fn set_fee_market_address(&mut self, address: Bech32Address) {
self.fee_market_address = Some(address);
}

/// Returns the contract address
pub fn current_address(&self) -> &Bech32Address {
self.contract_address
Expand Down Expand Up @@ -172,6 +183,8 @@ impl ContractInteract {
self.convert_address_to_managed(self.state.config_address.clone());
let esdt_safe_managed_address =
self.convert_address_to_managed(self.state.esdt_safe_address.clone());
let fee_market_mananged_address =
self.convert_address_to_managed(self.state.fee_market_address.clone());

let new_address = self
.interactor
Expand All @@ -184,7 +197,7 @@ impl ContractInteract {
config_managed_address,
header_verifier_managed_address,
esdt_safe_managed_address,
forge_managed_address, // USE ACTUAL FEE-MARKET TEMPLATE
fee_market_mananged_address,
)
.code(MxscPath::new(CHAIN_FACTORY_CODE_PATH))
.returns(ReturnsNewAddress)
Expand Down Expand Up @@ -278,8 +291,35 @@ impl ContractInteract {
new_address_bech32.clone(),
));

println!("new Header-Verifier address: {new_address_bech32}");
println!("new ESDT-Safe address: {new_address_bech32}");
}

pub async fn deploy_fee_market_template(&mut self) {
let esdt_safe_managed_address =
self.convert_address_to_managed(self.state.esdt_safe_address.clone());
let fee: Option<FeeStruct<StaticApi>> = None;

let new_address = self
.interactor
.tx()
.from(&self.wallet_address)
.gas(80_000_000u64)
.typed(FeeMarketProxy)
.init(esdt_safe_managed_address, fee)
.returns(ReturnsNewAddress)
.code(MxscPath::new(FEE_MARKET_CODE_PATH))
.run()
.await;

let new_address_bech32 = bech32::encode(&new_address);
self.state
.set_fee_market_address(Bech32Address::from_bech32_string(
new_address_bech32.clone(),
));

println!("new Fee-Market address: {new_address_bech32}");
}

pub async fn upgrade(&mut self) {
let response = self
.interactor
Expand Down Expand Up @@ -425,6 +465,23 @@ impl ContractInteract {
println!("Result: {response:?}");
}

pub async fn deploy_phase_four(&mut self) {
let fee: Option<FeeStruct<StaticApi>> = None;

let response = self
.interactor
.tx()
.from(&self.wallet_address)
.to(self.state.current_address())
.gas(80_000_000u64)
.typed(SovereignForgeProxy)
.deploy_phase_four(fee)
.returns(ReturnsResultUnmanaged)
.run()
.await;

println!("Result: {response:?}");
}
pub async fn chain_factories(&mut self) {
let shard_id = 0u32;

Expand Down
5 changes: 0 additions & 5 deletions sovereign-forge/interactor/state.toml

This file was deleted.

2 changes: 2 additions & 0 deletions sovereign-forge/interactor/tests/interact_cs_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ async fn deploy_test_sovereign_forge_cs() {
interactor.deploy_header_verifier_template().await;
interactor.deploy_chain_config_template().await;
interactor.deploy_esdt_safe_template().await;
interactor.deploy_fee_market_template().await;
interactor.deploy_chain_factory().await;

interactor.register_token_handler(1).await;
Expand All @@ -24,4 +25,5 @@ async fn deploy_test_sovereign_forge_cs() {
interactor.deploy_phase_one().await;
interactor.deploy_phase_two().await;
interactor.deploy_phase_three().await;
interactor.deploy_phase_four().await;
}
4 changes: 4 additions & 0 deletions sovereign-forge/sc-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ add-labels = ["sovereign-forge-external-view"]

[[proxy]]
path = "../common/proxies/src/sovereign_forge_proxy.rs"

[[proxy.path-rename]]
from = "proxies::fee_market_proxy::FeeStruct<Env::Api>"
to = "super::fee_market_proxy::FeeStruct<Env::Api>"
16 changes: 15 additions & 1 deletion sovereign-forge/src/common/sc_deploy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::err_msg;
use multiversx_sc::types::{MultiValueEncoded, ReturnsResult};
use proxies::chain_factory_proxy::ChainFactoryContractProxy;
use proxies::{chain_factory_proxy::ChainFactoryContractProxy, fee_market_proxy::FeeStruct};
use transaction::StakeMultiArg;

#[multiversx_sc::module]
Expand Down Expand Up @@ -49,4 +49,18 @@ pub trait ScDeployModule: super::utils::UtilsModule + super::storage::StorageMod
.returns(ReturnsResult)
.sync_call()
}

#[inline]
fn deploy_fee_market(
&self,
esdt_safe_address: &ManagedAddress,
fee: Option<FeeStruct<Self::Api>>,
) -> ManagedAddress {
self.tx()
.to(self.get_chain_factory_address())
.typed(ChainFactoryContractProxy)
.deploy_fee_market(esdt_safe_address, fee)
.returns(ReturnsResult)
.sync_call()
}
}
7 changes: 7 additions & 0 deletions sovereign-forge/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ pub enum ScArray {

#[multiversx_sc::module]
pub trait UtilsModule: super::storage::StorageModule {
fn require_phase_three_completed(&self, caller: &ManagedAddress) {
require!(
self.is_contract_deployed(caller, ScArray::ESDTSafe),
"The ESDT-Safe SC is not deployed, you skipped the third phase"
);
}

fn require_phase_two_completed(&self, caller: &ManagedAddress) {
require!(
self.is_contract_deployed(caller, ScArray::HeaderVerifier),
Expand Down
21 changes: 21 additions & 0 deletions sovereign-forge/src/phases.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::err_msg;
use core::ops::Deref;
use proxies::fee_market_proxy::FeeStruct;
use transaction::StakeMultiArg;

use multiversx_sc::{require, types::MultiValueEncoded};
Expand Down Expand Up @@ -127,4 +128,24 @@ pub trait PhasesModule:
self.sovereign_deployed_contracts(&self.sovereigns_mapper(&caller).get())
.insert(esdt_safe_contract_info);
}

#[endpoint(deployPhaseFour)]
fn deploy_phase_four(&self, fee: Option<FeeStruct<Self::Api>>) {
let caller = self.blockchain().get_caller();

self.require_phase_three_completed(&caller);
require!(
!self.is_contract_deployed(&caller, ScArray::FeeMarket),
"The Fee-Market SC is already deployed"
);

let esdt_safe_address = self.get_contract_address(&caller, ScArray::ESDTSafe);

let fee_market_address = self.deploy_fee_market(&esdt_safe_address, fee);

let fee_market_contract_info = ContractInfo::new(ScArray::FeeMarket, fee_market_address);

self.sovereign_deployed_contracts(&self.sovereigns_mapper(&caller).get())
.insert(fee_market_contract_info);
}
}
Loading

0 comments on commit 467a819

Please sign in to comment.