Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init functionality by adding a small CLI #16

Merged
merged 6 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[workspace]
members = [
"bin/artemis",
"bin/cli",
"crates/artemis-core",
"crates/generator",
"crates/strategies/*",
"crates/clients/*"
]
Expand All @@ -13,3 +15,4 @@ panic = 'abort'

[profile.dev]
panic = 'abort'

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

anyhow = "1.0.70"
generator = { path = "../../crates/generator" }
clap = { version = "4.2.5", features = ["derive"] }


11 changes: 11 additions & 0 deletions bin/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use anyhow::{Error, Result};
use clap::Parser;
use generator::parser::StrategyParser;

fn main() -> Result<(), Error> {
let strategy_parser = StrategyParser::parse();

strategy_parser.generate()?;

Ok(())
}
14 changes: 14 additions & 0 deletions crates/generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "generator"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
quote = "1.0.27"
clap = { version = "4.2.5", features = ["derive"] }
anyhow = "1.0.70"
convert_case = "0.6.0"


82 changes: 82 additions & 0 deletions crates/generator/src/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use quote::__private::TokenStream;
use quote::quote;

// Generate struct that implements required traits
pub fn generate_strategy(name: &str) -> TokenStream {
let struct_name = quote::format_ident!("{}", name);
let generic_type = quote::format_ident!("M");

quote! {
use async_trait::async_trait;
use std::sync::Arc;

use anyhow::Result;
use artemis_core::types::Strategy;
use ethers::providers::Middleware;

use super::types::{Action, Config, Event};

pub struct #struct_name<#generic_type> {
client: Arc<#generic_type>,
}

impl<#generic_type: Middleware + 'static> #struct_name<#generic_type> {
pub fn new(client: Arc<#generic_type>, config: Config) -> Self {
Self { client }
}
}

#[async_trait]
impl<#generic_type: Middleware + 'static> Strategy<Event, Action> for #struct_name<#generic_type> {
async fn sync_state(&mut self) -> Result<()> {
Ok(())
}

async fn process_event(&mut self, event: Event) -> Option<Action> {
match event {}
}
}

impl<#generic_type: Middleware + 'static> #struct_name<#generic_type> {
}
}
}

pub fn generate_types() -> TokenStream {
let event_definition = quote! {
#[derive(Debug, Clone)]
pub enum Event {}
};

let action_definition = quote! {
#[derive(Debug, Clone)]
pub enum Action {}
};

let config_definition = quote! {
#[derive(Debug, Clone)]
pub struct Config {}
};

quote! {
#event_definition
#action_definition
#config_definition
}
}

// Generate lib file
pub fn generate_lib() -> TokenStream {
quote! {
pub mod strategy;
pub mod types;
pub mod constants;
}
}

// Generate constants file
pub fn generate_constants() -> TokenStream {
quote! {
//add required constants here
}
}
2 changes: 2 additions & 0 deletions crates/generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod init;
pub mod parser;
Loading