Skip to content

Commit

Permalink
feat(setup_states): new yaml reader, new read_config_state and types …
Browse files Browse the repository at this point in the history
…adjusts for generalization
  • Loading branch information
willyrgf committed Jan 21, 2024
1 parent 8787f19 commit ee53a2b
Show file tree
Hide file tree
Showing 14 changed files with 738 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Rust
name: Rust build & tests

on: [push]

Expand Down
57 changes: 29 additions & 28 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions mfm_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ zeroize = "1.7.0"
serde_json = "1.0.108"
serde_derive = "1.0.193"
anyhow = "1.0.75"
serde_yaml = "0.9.30"
2 changes: 1 addition & 1 deletion mfm_core/src/config/authentication/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ use crate::password::{deserialize_safe_password, SafePassword};
pub struct Wallet {
#[serde(deserialize_with = "deserialize_safe_password")]
pub private_key: SafePassword,
pub not_encrypted: bool,
pub not_encrypted: Option<bool>,
pub env_password: Option<String>,
}
1 change: 1 addition & 0 deletions mfm_core/src/config/dexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Kind {
UniswapV2,
}
Expand Down
10 changes: 5 additions & 5 deletions mfm_core/src/config/network.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::token::Token;
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Kind {
EVM,
Evm,
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
Expand All @@ -14,11 +14,11 @@ pub struct Network {
pub symbol: String,
pub decimals: Option<u8>,
pub chain_id: u32,
pub node_url: String,
pub node_url_failover: Option<String>,
pub node_url_http: Option<String>,
pub node_url_grpc: Option<String>,
pub blockexplorer_url: Option<String>,
pub min_balance_coin: f64,
pub wrapped_asset: Option<Token>,
pub wrapped_token: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
Expand Down
9 changes: 6 additions & 3 deletions mfm_core/src/config/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Kind {
ERC20,
Erc20,
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct TokenNetwork {
pub name: String,
pub kind: Kind,
pub network_id: String,
pub address: String,
pub slippage: f64,
pub path_asset: String,
pub path_token: String,
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct TokenNetworks(HashMap<String, TokenNetwork>);

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Token {
pub kind: Kind,
// TODO: rethink tokens to be any kind of token, but each
// chain/network will may have an different token kind.
pub networks: TokenNetworks,
}

Expand Down
8 changes: 5 additions & 3 deletions mfm_core/src/contexts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::config::Config;
use mfm_machine::state::Label;
use serde_derive::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ConfigSource {
TomlFile(String),
YamlFile(String),
}
pub const CONFIG_SOURCE_CTX: Label = Label("config_source_ctx");

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct ReadConfig {
pub struct ConfigCtx {
pub config_source: ConfigSource,
pub config: Config,
}
pub const READ_CONFIG: &str = "read_config";
pub const CONFIG_CTX: Label = Label("config_ctx");
16 changes: 15 additions & 1 deletion mfm_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
use std::{fs::File, io::Read};

pub mod config;
pub mod contexts;
pub mod hidden;
pub mod operations;
pub mod password;
pub mod states;
pub mod tasks;

use anyhow::Error;
use serde::de::DeserializeOwned;

fn read_yaml<T: DeserializeOwned>(path: String) -> Result<T, Error> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;

let instance: T = serde_yaml::from_str(&contents)?;
Ok(instance)
}
File renamed without changes.
Loading

0 comments on commit ee53a2b

Please sign in to comment.