Skip to content

Commit

Permalink
Merge pull request #198 from multiversx/interactor-refactoring
Browse files Browse the repository at this point in the history
Interactor file structure refactor
  • Loading branch information
sergiuosvat authored Dec 3, 2024
2 parents 181415b + e0d1c94 commit bfefa37
Show file tree
Hide file tree
Showing 21 changed files with 548 additions and 1,203 deletions.
90 changes: 51 additions & 39 deletions Cargo.lock

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

23 changes: 23 additions & 0 deletions common/interactor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "interactor"
version = "0.1.0"
edition = "2021"

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

[dependencies]
toml = "0.8.6"

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

[dependencies.multiversx-sc-snippets]
version = "=0.54.5"

[dependencies.serde]
version = "1.0"
features = ["derive"]



4 changes: 4 additions & 0 deletions common/interactor/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const TOKEN_ID: &[u8] = b"SVT-805b28";
pub const WHITELIST_TOKEN_ID: &[u8] = b"CHOCOLATE-daf625";
pub const TOKEN_ID_FOR_EXECUTE: &[u8] = b"x-SOV-101252";
pub const WHITELISTED_TOKEN_ID: &[u8] = b"CHOCOLATE-daf625";
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@
use serde::Deserialize;
use std::io::Read;


/// Config file
const CONFIG_FILE: &str = "config.toml";


#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChainType {
Real,
Simulator,
}
}

/// Contract Interact configuration
#[derive(Debug, Deserialize)]
pub struct Config {
pub gateway_uri: String,
pub chain_type: ChainType,
}

}

impl Config {
// Deserializes config from file
pub fn new() -> Self {
pub fn load_config() -> Self {
let mut file = std::fs::File::open(CONFIG_FILE).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
Expand All @@ -36,7 +33,7 @@ impl Config {
Config {
gateway_uri: "http://localhost:8085".to_owned(),
chain_type: ChainType::Simulator,
}
}
}

// Returns the gateway URI
Expand All @@ -49,8 +46,6 @@ impl Config {
match self.chain_type {
ChainType::Real => false,
ChainType::Simulator => true,
}
}
}
}


}
90 changes: 90 additions & 0 deletions common/interactor/src/interactor_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use multiversx_sc_snippets::imports::*;
use serde::{Deserialize, Serialize};
use std::{
io::{Read, Write},
path::Path,
};

const STATE_FILE: &str = "state.toml";

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct State {
esdt_safe_address: Option<Bech32Address>,
header_verifier_address: Option<Bech32Address>,
fee_market_address: Option<Bech32Address>,
token_handler_address: Option<Bech32Address>,
testing_sc_address: Option<Bech32Address>,
}

impl State {
// Deserializes state from file
pub fn load_state() -> Self {
if Path::new(STATE_FILE).exists() {
let mut file = std::fs::File::open(STATE_FILE).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
toml::from_str(&content).unwrap()
} else {
Self::default()
}
}

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

pub fn set_header_verifier_address(&mut self, address: Bech32Address) {
self.header_verifier_address = Some(address);
}

pub fn set_fee_market_address(&mut self, address: Bech32Address) {
self.fee_market_address = Some(address);
}

pub fn set_token_handler_address(&mut self, address: Bech32Address) {
self.token_handler_address = Some(address);
}

pub fn set_testing_sc_address(&mut self, address: Bech32Address) {
self.testing_sc_address = Some(address);
}

/// Returns the contract address
pub fn esdt_safe_address(&self) -> &Bech32Address {
self.esdt_safe_address
.as_ref()
.expect("no known esdt_safe contract, deploy first")
}

pub fn get_header_verifier_address(&self) -> &Bech32Address {
self.header_verifier_address
.as_ref()
.expect("no known header verifier, deploy first")
}

pub fn get_fee_market_address(&self) -> &Bech32Address {
self.fee_market_address
.as_ref()
.expect("no known fee market, deploy first")
}

pub fn get_token_handler_address(&self) -> &Bech32Address {
self.token_handler_address
.as_ref()
.expect("no known token handler, deploy first")
}

pub fn get_testing_sc_address(&self) -> Address {
self.testing_sc_address.clone().unwrap().to_address()
}
}

impl Drop for State {
// Serializes state to file
fn drop(&mut self) {
let mut file = std::fs::File::create(STATE_FILE).unwrap();
file.write_all(toml::to_string(self).unwrap().as_bytes())
.unwrap();
}
}
3 changes: 3 additions & 0 deletions common/interactor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod interactor_config;
pub mod interactor_state;
pub mod constants;
2 changes: 2 additions & 0 deletions enshrine-esdt-safe/interactor/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Pem files are used for interactions, but shouldn't be committed
*.pem
# State files are used for interactions, but shouldn't be committed
*state.toml
15 changes: 12 additions & 3 deletions enshrine-esdt-safe/interactor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
[[bin]]
name = "rust-interact-esdt-safe"
path = "src/interactor_main.rs"
name = "enshrine_esdt_safe_interactor"
path = "src/enshrine_esdt_safe_interactor_main.rs"

[package]
name = "rust-interact-esdt-safe"
name = "enshrine_esdt_safe_interactor"
version = "0.0.0"
authors = ["you"]
edition = "2021"
publish = false

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

[dependencies]
toml = "0.8.6"

Expand All @@ -30,6 +33,9 @@ path = "../../common/setup-phase"
[dependencies.token-whitelist]
path = "../../common/token-whitelist"

[dependencies.interactor]
path = "../../common/interactor"

[dependencies.utils]
path = "../../common/utils"

Expand Down Expand Up @@ -58,3 +64,6 @@ features = ["derive"]
[dependencies.serde]
version = "1.0"
features = ["derive"]

[features]
chain-simulator-tests = []
Loading

0 comments on commit bfefa37

Please sign in to comment.