Skip to content

Commit

Permalink
Merge pull request #17 from multiversx/chain-config
Browse files Browse the repository at this point in the history
Chain config
  • Loading branch information
dorin-iancu authored Jan 17, 2024
2 parents 738baf5 + ed6d20b commit 9db99fb
Show file tree
Hide file tree
Showing 19 changed files with 584 additions and 27 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[workspace]
members = [
"chain-config",
"chain-config/meta",
"chain-factory",
"chain-factory/meta",
"esdt-safe",
Expand Down
7 changes: 7 additions & 0 deletions chain-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/
*/target/

# The mxpy output
/output*/
21 changes: 21 additions & 0 deletions chain-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "chain-config"
version = "0.0.0"
authors = [ "you",]
edition = "2018"
publish = false

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

[dev-dependencies]
num-bigint = "0.4.2"

[dependencies.multiversx-sc]
version = "=0.43.5"

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

[dev-dependencies.multiversx-sc-scenario]
version = "=0.43.5"
14 changes: 14 additions & 0 deletions chain-config/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "chain-config-meta"
version = "0.0.0"
edition = "2018"
publish = false
authors = [ "you",]

[dev-dependencies]

[dependencies.chain-config]
path = ".."

[dependencies.multiversx-sc-meta]
version = "=0.43.5"
3 changes: 3 additions & 0 deletions chain-config/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta::cli_main::<chain_config::AbiProvider>();
}
3 changes: 3 additions & 0 deletions chain-config/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
40 changes: 40 additions & 0 deletions chain-config/src/bridge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
multiversx_sc::imports!();

mod bridge_proxy {
multiversx_sc::imports!();

#[multiversx_sc::proxy]
pub trait BridgeProxy {
#[init]
fn init(&self, min_valid_signers: u32, signers: MultiValueEncoded<ManagedAddress>);
}
}

#[multiversx_sc::module]
pub trait BridgeModule {
#[only_owner]
#[endpoint(deployBridge)]
fn deploy_bridge(
&self,
code: ManagedBuffer,
min_valid_signers: u32,
signers: MultiValueEncoded<ManagedAddress>,
) {
require!(self.bridge_address().is_empty(), "Bridge already deployed");

let metadata =
CodeMetadata::PAYABLE_BY_SC | CodeMetadata::UPGRADEABLE | CodeMetadata::READABLE;
let (sc_address, _) = self
.bridge_proxy()
.init(min_valid_signers, signers)
.deploy_contract::<IgnoreValue>(&code, metadata);

self.bridge_address().set(sc_address);
}

#[proxy]
fn bridge_proxy(&self) -> bridge_proxy::Proxy<Self::Api>;

#[storage_mapper("bridgeAddress")]
fn bridge_address(&self) -> SingleValueMapper<ManagedAddress>;
}
49 changes: 49 additions & 0 deletions chain-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![no_std]

use validator_rules::TokenIdAmountPair;

multiversx_sc::imports!();

pub mod bridge;
pub mod validator_rules;

pub type StakeMultiArg<M> = MultiValue2<TokenIdentifier<M>, BigUint<M>>;

#[multiversx_sc::contract]
pub trait ChainConfigContract:
bridge::BridgeModule
+ validator_rules::ValidatorRulesModule
+ multiversx_sc_modules::only_admin::OnlyAdminModule
{
#[init]
fn init(
&self,
min_validators: usize,
max_validators: usize,
min_stake: BigUint,
admin: ManagedAddress,
additional_stake_required: MultiValueEncoded<StakeMultiArg<Self::Api>>,
) {
require!(
min_validators <= max_validators,
"Invalid min/max validator numbers"
);

let mut additional_stake_vec = ManagedVec::new();
for multi_value in additional_stake_required {
let (token_id, amount) = multi_value.into_tuple();
let value = TokenIdAmountPair { token_id, amount };

additional_stake_vec.push(value);
}

self.min_validators().set(min_validators);
self.max_validators().set(max_validators);
self.min_stake().set(min_stake);
self.add_admin(admin);
self.additional_stake_required().set(additional_stake_vec);
}

#[endpoint]
fn upgrade(&self) {}
}
38 changes: 38 additions & 0 deletions chain-config/src/validator_rules.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

// TODO: What to fill here?
pub enum SlashableOffenses {}

#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem)]
pub struct TokenIdAmountPair<M: ManagedTypeApi> {
pub token_id: TokenIdentifier<M>,
pub amount: BigUint<M>,
}

#[multiversx_sc::module]
pub trait ValidatorRulesModule {
#[view(getMinValidators)]
#[storage_mapper("minValidators")]
fn min_validators(&self) -> SingleValueMapper<usize>;

#[view(getMaxValidators)]
#[storage_mapper("maxValidators")]
fn max_validators(&self) -> SingleValueMapper<usize>;

// TODO: Read user stake and verify
#[view(getMinStake)]
#[storage_mapper("minStake")]
fn min_stake(&self) -> SingleValueMapper<BigUint>;

// TODO: Read user stake and verify
#[view(getAdditionalStakeRequired)]
#[storage_mapper("additionalStakeRequired")]
fn additional_stake_required(
&self,
) -> SingleValueMapper<ManagedVec<TokenIdAmountPair<Self::Api>>>;

#[view(wasPreviouslySlashed)]
#[storage_mapper("wasPreviouslySlashed")]
fn was_previously_slashed(&self, validator: &ManagedAddress) -> SingleValueMapper<bool>;
}
Loading

0 comments on commit 9db99fb

Please sign in to comment.