Skip to content

Commit

Permalink
Add command to deploy chronicle. (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch authored Feb 3, 2025
1 parent 646ddfd commit b95f6a0
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 48 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ runtime/target/srtool
# secrets
.env
gcp-key.json

#Temp files
prices.csv
4 changes: 0 additions & 4 deletions config/envs/development/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ networks:
batch_gas_limit: 10000
gmp_margin: 0.0
shard_task_limit: 50
symbol: "ETH"
token_decimals: 18
route_gas_limit: 10000000
route_base_fee: 1400000000
shard_size: 1
Expand All @@ -108,8 +106,6 @@ networks:
batch_gas_limit: 10000
gmp_margin: 0.0
shard_task_limit: 50
symbol: "SBY"
token_decimals: 18
route_gas_limit: 10000000
route_base_fee: 1400000000
shard_size: 1
Expand Down
7 changes: 7 additions & 0 deletions config/envs/development/prices.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
network_id,symbol,usd_price
0,TT,0.0032616625404162376
1,TT,0.0032616625404162376
2,TT,0.0032616625404162376
3,TT,0.0032616625404162376
4,ETH,0.0032616625404162376
5,ASTR,0.0032616625404162376
4 changes: 0 additions & 4 deletions config/envs/integration/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ networks:
batch_gas_limit: 10000
gmp_margin: 0.0
shard_task_limit: 50
symbol: "ETH"
token_decimals: 18
route_gas_limit: 10000000
route_base_fee: 1400000000
shard_size: 3
Expand All @@ -39,8 +37,6 @@ networks:
batch_gas_limit: 10000
gmp_margin: 0.0
shard_task_limit: 50
symbol: "SBY"
token_decimals: 18
route_gas_limit: 10000000
route_base_fee: 1400000000
shard_size: 3
Expand Down
3 changes: 3 additions & 0 deletions config/envs/integration/prices.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
network_id,symbol,usd_price
10,ETH,0.003683198722878368
11,ASTR,0.003683198722878368
4 changes: 0 additions & 4 deletions config/envs/local/local-evm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ networks:
batch_gas_limit: 10000000
gmp_margin: 0.0
shard_task_limit: 50
symbol: "ETH"
token_decimals: 18
route_gas_limit: 10000000
route_base_fee: 1400000000
shard_size: 1
Expand All @@ -39,8 +37,6 @@ networks:
batch_gas_limit: 10000000
gmp_margin: 0.0
shard_task_limit: 50
symbol: "ETH"
token_decimals: 18
route_gas_limit: 10000000
route_base_fee: 1400000000
shard_size: 1
Expand Down
42 changes: 30 additions & 12 deletions tc-cli/src/bin/slack_bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use tokio::net::TcpListener;
#[derive(Clone, Debug, Eq, PartialEq)]
enum Command {
TcCli { tag: String, args: String },
RuntimeUpgrade { branch: String },
RuntimeUpgrade,
DeployChronicles { tag: String },
}

impl std::fmt::Display for Command {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::TcCli { tag, args } => write!(f, "/tc-cli tag={tag} {args}"),
Self::RuntimeUpgrade { branch } => write!(f, "/runtime-upgrade branch={branch}"),
Self::RuntimeUpgrade => write!(f, "/runtime-upgrade"),
Self::DeployChronicles { tag } => write!(f, "/deploy-chronicles tag={tag}"),
}
}
Expand All @@ -44,21 +44,30 @@ impl Command {
fn inputs(&self, env: Env) -> serde_json::Value {
match self {
Self::TcCli { tag, args } => {
json!({ "version": tag, "args": args, "environment": env.to_string() })
json!({
"environment": env.to_string(),
"version": tag,
"args": args,
})
},
Self::RuntimeUpgrade { branch } => {
json!({ "branch": branch, "environment": env.to_string() })
Self::RuntimeUpgrade => {
json!({
"environment": env.to_string(),
})
},
Self::DeployChronicles { tag } => {
json!({ "version": tag, "environment": env.to_string() })
json!({
"environment": env.to_string(),
"version": tag,
})
},
}
}

fn json(&self, env: Env) -> serde_json::Value {
fn json(&self, branch: &str, env: Env) -> serde_json::Value {
let inputs = self.inputs(env);
serde_json::json!({
"ref": "development",
"ref": branch,
"inputs": inputs,
})
}
Expand All @@ -67,12 +76,18 @@ impl Command {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Env {
Development,
Integration,
Testnet,
Mainnet,
}

impl std::fmt::Display for Env {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Development => write!(f, "development"),
Self::Integration => write!(f, "integration"),
Self::Testnet => write!(f, "testnet"),
Self::Mainnet => write!(f, "mainnet"),
}
}
}
Expand All @@ -92,12 +107,12 @@ impl GithubState {
Ok(Self { client, token, user_agent })
}

async fn trigger_workflow(&self, command: &Command, env: Env) -> Result<()> {
async fn trigger_workflow(&self, command: &Command, branch: &str, env: Env) -> Result<()> {
tracing::info!("triggering {command} in {env}");
let request = self
.client
.post(command.url())
.json(&command.json(env))
.json(&command.json(branch, env))
.bearer_auth(&self.token)
.header("Accept", "application/vnd.github+json")
.header("X-Github-Api-Version", "2022-11-28")
Expand Down Expand Up @@ -136,19 +151,22 @@ async fn command_event(
.join(" ");
let command = match event.command.0.as_str() {
"/tc-cli" => Command::TcCli { tag: tag.into(), args },
"/runtime-upgrade" => Command::RuntimeUpgrade { branch: branch.into() },
"/runtime-upgrade" => Command::RuntimeUpgrade,
"/deploy-chronicles" => Command::DeployChronicles { tag: tag.into() },
_ => {
return slack_error(format!("unknown command {}", &event.command.0));
},
};
let env = match event.channel_id.0.as_str() {
"C08A621SKRR" => Env::Development,
"C08BK21RPHA" => Env::Integration,
"C08B20E4NEB" => Env::Testnet,
"C08BV777PG9" => Env::Mainnet,
_ => {
return slack_error(format!("unknown channel {}", &event.channel_id.0));
},
};
if let Err(err) = gh.trigger_workflow(&command, env).await {
if let Err(err) = gh.trigger_workflow(&command, branch, env).await {
return slack_error(format!("triggering workflow failed: {err}"));
}
axum::Json(
Expand Down
49 changes: 38 additions & 11 deletions tc-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ pub struct Config {

impl Config {
pub fn from_env(path: PathBuf, config: &str) -> Result<Self> {
let config = if let Ok(config) = std::env::var("CONFIG") {
config
} else {
let config = path.join(config);
std::fs::read_to_string(&config)
.with_context(|| format!("failed to read config file {}", config.display()))?
};
let yaml = serde_yaml::from_str(&config).context("failed to parse config file")?;
let config_path = path.join(config);
let config = std::fs::read_to_string(&config_path)
.with_context(|| format!("failed to read config file {}", config_path.display()))?;
let yaml = serde_yaml::from_str(&config)
.with_context(|| format!("failed to parse config file {}", config_path.display()))?;
Ok(Self { path, yaml })
}

Expand Down Expand Up @@ -87,6 +84,7 @@ impl Config {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct ConfigYaml {
config: GlobalConfig,
contracts: HashMap<Backend, ContractsConfig>,
Expand All @@ -95,13 +93,15 @@ struct ConfigYaml {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GlobalConfig {
prices_path: PathBuf,
pub chronicle_timechain_funds: String,
pub timechain_url: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct ContractsConfig {
additional_params: PathBuf,
proxy: PathBuf,
Expand All @@ -118,6 +118,7 @@ pub struct Contracts {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NetworkConfig {
pub backend: Backend,
pub blockchain: String,
Expand All @@ -139,11 +140,37 @@ pub struct NetworkConfig {
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;

#[test]
fn make_sure_envs_parse() {
fn make_sure_envs_parse() -> Result<()> {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../config/envs");
Config::from_env(root.join("local"), "local-grpc.yaml").unwrap();
Config::from_env(root.join("development"), "config.yaml").unwrap();
let envs = std::fs::read_dir(&root)?;
for env in envs {
let env_dir = env?;
if !env_dir.file_type()?.is_dir() {
continue;
}
let mut networks = HashSet::new();
let mut prices = HashSet::new();
for config in std::fs::read_dir(env_dir.path())? {
let config = config?;
if !config.file_type()?.is_file() {
continue;
}
let config = config.file_name().into_string().unwrap();
if !config.ends_with(".yaml") {
continue;
}
let config = Config::from_env(env_dir.path(), &config).unwrap();
networks.extend(config.networks().keys().copied());
let prices_path = config.prices();
let prices_csv =
crate::gas_price_calculator::read_csv_token_prices(&prices_path).unwrap();
prices.extend(prices_csv.keys().copied());
}
assert_eq!(prices, networks, "{}", env_dir.path().display());
}
Ok(())
}
}
24 changes: 14 additions & 10 deletions tc-cli/src/gas_price_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use reqwest::header::{HeaderMap, HeaderValue};
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::File;
use std::path::Path;
use time_primitives::NetworkId;

#[derive(Clone, Deserialize)]
Expand Down Expand Up @@ -130,6 +131,18 @@ pub fn convert_bigint_to_u128(value: &BigUint) -> Result<u128> {
.ok_or_else(|| anyhow::anyhow!("Could not convert bigint to u128"))
}

pub fn read_csv_token_prices(price_path: &Path) -> Result<HashMap<NetworkId, (String, f64)>> {
let mut rdr = Reader::from_path(price_path)
.with_context(|| format!("failed to open {}", price_path.display()))?;

let mut network_map: HashMap<NetworkId, (String, f64)> = HashMap::new();
for result in rdr.deserialize() {
let record: NetworkPrice = result?;
network_map.insert(record.network_id, (record.symbol, record.usd_price));
}
Ok(network_map)
}

impl Tc {
pub async fn fetch_token_prices(&self) -> Result<()> {
let env = CoinMarketCap::from_env()?;
Expand Down Expand Up @@ -170,16 +183,7 @@ impl Tc {
}

pub fn read_csv_token_prices(&self) -> Result<HashMap<NetworkId, (String, f64)>> {
let price_path = self.config.prices();
let mut rdr = Reader::from_path(&price_path)
.with_context(|| format!("failed to open {}", price_path.display()))?;

let mut network_map: HashMap<NetworkId, (String, f64)> = HashMap::new();
for result in rdr.deserialize() {
let record: NetworkPrice = result?;
network_map.insert(record.network_id, (record.symbol, record.usd_price));
}
Ok(network_map)
read_csv_token_prices(&self.config.prices())
}

pub fn calculate_relative_price(
Expand Down
6 changes: 6 additions & 0 deletions tc-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ enum Command {
path: PathBuf,
},
Deploy,
DeployChronicle {
url: String,
},
UnregisterMember {
member: String,
},
Expand Down Expand Up @@ -292,6 +295,9 @@ async fn real_main() -> Result<()> {
Command::Deploy => {
tc.deploy().await?;
},
Command::DeployChronicle { url } => {
tc.deploy_chronicle(&url).await?;
},
Command::UnregisterMember { member } => {
let member = tc.parse_address(None, &member)?;
tc.unregister_member(member.into()).await?;
Expand Down

0 comments on commit b95f6a0

Please sign in to comment.