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

generating schema.json from ci #37

Merged
merged 20 commits into from
Nov 7, 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
8 changes: 4 additions & 4 deletions .github/workflows/subgraph-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- name: Forge shallow install
run: forge install --shallow

- name: Start docker container
run: docker-compose -f subgraph/docker/docker-compose.yaml up -d

- name: Initialize setup
working-directory: ./subgraph
run: nix run .#init-setup
Expand All @@ -30,16 +33,13 @@ jobs:
with:
toolchain: stable

- name: Start docker container
run: docker-compose -f subgraph/docker/docker-compose.yaml up -d

- name: install npm dependencies
working-directory: ./subgraph
run: npm install

- name: Run test
working-directory: ./subgraph
run: RUST_TEST_THREADS=1 cargo test
run: nix run .#ci-test

- name: Stop docker container
run: docker-compose -f subgraph/docker/docker-compose.yaml down
1 change: 1 addition & 0 deletions subgraph/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/subgraph.yaml
/abis/*.json


# Rust generated files
/tests/generated/abigen
# Ignore all JSON files
Expand Down
1 change: 1 addition & 0 deletions subgraph/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 subgraph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ once_cell = "1.18.0"
minicbor = "0.20.0"
tiny-keccak = "2.0.2"
bigdecimal = "0.4.2"
url = "2.4.1"

94 changes: 94 additions & 0 deletions subgraph/cli/deploy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use anyhow::anyhow;
use mustache::MapBuilder;
use std::fs;
// use std::{fs, process::Command};

use crate::utils::run_cmd;

use clap::Args;
use url::Url;

#[derive(Args, Debug)]
pub struct DeployArgs {
/// Subgraph name (eg: User/SubgraphName)
#[arg(long = "name")]
pub subgraph_name: String,

/// Endpoint URL where the subgraph will be deployed
#[arg(long)]
pub url: Url,

/// Network that the subgraph will index
#[arg(long)]
pub network: String,

/// Block number where the subgraph will start indexing
#[arg(long = "block")]
pub block_number: u32,

/// Contract address that the subgraph will be indexing (Assuming one address)
#[arg(long)]
pub address: String,

/// (Optional) Subgraph token to deploy the subgraph
#[arg(long)]
pub key: Option<String>,
}

pub fn deploy_subgraph(config: DeployArgs) -> anyhow::Result<()> {
if config.url.scheme() != "http" && config.url.scheme() != "https" {
return Err(anyhow!("Invalid URL provided"));
}

let subgraph_template = "subgraph.template.yaml";
let output_path = "subgraph.yaml";

let end_point = config.url.as_str();
let subgraph_name = config.subgraph_name;

let data = MapBuilder::new()
.insert_str("network", config.network)
.insert_str("orderbook", config.address)
.insert_str("blockNumber", config.block_number.to_string())
.build();

let template = fs::read_to_string(subgraph_template)?;
let renderd = mustache::compile_str(&template)?.render_data_to_string(&data)?;
let _ = fs::write(output_path, renderd)?;

// Generate the subgraph code
// let is_built = run_cmd("bash", &["-c", "npx graph codegen && npx graph build"]);
let is_built = run_cmd("bash", &["-c", "npx graph codegen"]);
if !is_built {
return Err(anyhow!("Failed to build subgraph"));
}

// Create the endpoint node
let is_node_up = run_cmd(
"bash",
&[
"-c",
&format!("npx graph create --node {} {}", end_point, subgraph_name),
],
);
if !is_node_up {
return Err(anyhow!("Failed to create subgraph endpoint node"));
}

// Deploy Subgraph to the endpoint
let is_deploy = run_cmd(
"bash",
&[
"-c",
&format!(
"npx graph deploy --node {} --ipfs http://localhost:5001 {} --version-label 1",
end_point, subgraph_name
),
],
);
if !is_deploy {
return Err(anyhow!("Failed to deploy subgraph"));
}

Ok(())
}
142 changes: 46 additions & 96 deletions subgraph/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use clap::{Args, Parser, Subcommand};
// extern crate url;
mod deploy;
mod utils;
use clap::{Parser, Subcommand};

use colored::*;
use std::env;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
use std::thread;
use deploy::{deploy_subgraph, DeployArgs};
use utils::run_cmd;

#[derive(Parser)]
#[clap(author, version, about)]
Expand All @@ -21,112 +23,60 @@ pub enum Subgraph {
#[command(about = "Test the rain subgraph")]
Test,
#[command(about = "Deploy the rain subgraph")]
Deploy(DeployCommand),
}

#[derive(Args, Debug)]
pub struct DeployCommand {
/// Endpoint URL where the subgraph will be deployed
url: String,
/// Subgraph token to deploy the subgraph
key: String,
/// Network that the subgraph will index
network: String,
/// Block number where the subgraph will start indexing
block_number: String,
/// Contract address that the subgraph will be indexing (Assuming one address)
address: String,
Deploy(DeployArgs),
}

// This function will work onthe working directory
fn run_cmd(main_cmd: &str, args: &[&str]) -> bool {
// Get the current working directory
let current_dir = env::current_dir().expect("Failed to get current directory");

// Create a new Command to run
let mut cmd = Command::new(main_cmd);

// Add the arguments
cmd.args(args);

// Set the directory from where the command wil run
cmd.current_dir(&current_dir);

// Tell what to do when try to print the process
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());

let full_cmd = format!("{} {}", main_cmd, args.join(" "));

println!("{} {}", "Running:".green(), full_cmd.blue());

// Execute the command
let mut child = cmd
.spawn()
.expect(format!("Failed to run: {}", full_cmd).as_str());
fn main() -> Result<(), anyhow::Error> {
let args = Cli::parse();

// Read and print stdout in a separate thread
let stdout_child = child.stdout.take().expect("Failed to get stdout");
let stdout_reader = BufReader::new(stdout_child);
match args.subgraph {
Subgraph::Install => {
let resp = run_cmd("npm", &["install"]);

let stdout_handle = thread::spawn({
move || {
for line in stdout_reader.lines() {
if let Ok(line) = line {
println!("{}", line);
}
if !resp {
eprintln!("{}", "Error: Failed at npm install".red());
std::process::exit(1);
}
}
});

// Read and print stderr in the main thread
let stderr_reader = BufReader::new(child.stderr.take().expect("Failed to get stderr"));
for line in stderr_reader.lines() {
if let Ok(line) = line {
eprintln!("{}", line);
Ok(())
}
}

// Wait for the command to finish and get the exit status
let status = child
.wait()
.expect(format!("Failed to wait: {}", full_cmd).as_str());

// Wait for the stdout thread to finish
stdout_handle.join().expect("Failed to join stdout thread");

if status.success() {
println!("✅ {} {}", full_cmd.blue(), "completed".green());
return true;
} else {
eprintln!(
"❌ {} {}",
full_cmd.blue(),
format!("failed with exit code: {}", status.code().unwrap_or(-1)).red()
);

return false;
}
}

fn main() {
let args = Cli::parse();
Subgraph::Build => {
let resp = run_cmd("npm", &["run", "codegen"]);
if !resp {
eprintln!("{}", "Error: Failed at npm run codegen".red());
std::process::exit(1);
}

match args.subgraph {
Subgraph::Install => {
run_cmd("npm", &["install"]);
}
let resp = run_cmd("npm", &["run", "build"]);
if !resp {
eprintln!("{}", "Error: Failed at npm run build".red());
std::process::exit(1);
}

Subgraph::Build => {
println!("{}", "Generating subgraph code".blue());
run_cmd("npx", &["graph", "codegen"]);
Ok(())
}

Subgraph::Test => {
println!("Hello tests 🧪");
let resp = run_cmd("nix", &["run", ".#ci-test"]);
if !resp {
std::process::exit(1);
}

Ok(())
}

Subgraph::Deploy(args) => {
println!("🚀 Hello, deploy with: {:?}", args);
match deploy_subgraph(args) {
Ok(_) => {
return Ok(());
}
Err(err) => {
// Error occurred, print the error message and exit
eprintln!("Error: {}", err);
std::process::exit(1);
}
}
}
}
}
76 changes: 76 additions & 0 deletions subgraph/cli/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use colored::*;
use std::env;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
use std::thread;

// This function will work onthe working directory
pub fn run_cmd(main_cmd: &str, args: &[&str]) -> bool {
// Get the current working directory
let current_dir = env::current_dir().expect("Failed to get current directory");

// Create a new Command to run
let mut cmd = Command::new(main_cmd);

// Add the arguments
cmd.args(args);

// Set the directory from where the command wil run
cmd.current_dir(&current_dir);

// Tell what to do when try to print the process
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());

let full_cmd = format!("{} {}", main_cmd, args.join(" "));

println!("{} {}\n", "Running:".green(), full_cmd.blue());

// Execute the command
let mut child = cmd
.spawn()
.expect(format!("Failed to run: {}", full_cmd).as_str());

// Read and print stdout in a separate thread
let stdout_child = child.stdout.take().expect("Failed to get stdout");
let stdout_reader = BufReader::new(stdout_child);

let stdout_handle = thread::spawn({
move || {
for line in stdout_reader.lines() {
if let Ok(line) = line {
println!("{}", line);
}
}
}
});

// Read and print stderr in the main thread
let stderr_reader = BufReader::new(child.stderr.take().expect("Failed to get stderr"));
for line in stderr_reader.lines() {
if let Ok(line) = line {
eprintln!("{}", line);
}
}

// Wait for the command to finish and get the exit status
let status = child
.wait()
.expect(format!("Failed to wait: {}", full_cmd).as_str());

// Wait for the stdout thread to finish
stdout_handle.join().expect("Failed to join stdout thread");

if status.success() {
println!("✅ {} {}\n", full_cmd.blue(), "completed".green());
return true;
} else {
eprintln!(
"❌ {} {}",
full_cmd.blue(),
format!("failed with exit code: {}\n", status.code().unwrap_or(-1)).red()
);

return false;
}
}
Loading
Loading