-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from rainprotocol/2023-11-01-generate-schema-json
generating schema.json from ci
- Loading branch information
Showing
12 changed files
with
285 additions
and
502 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
/subgraph.yaml | ||
/abis/*.json | ||
|
||
|
||
# Rust generated files | ||
/tests/generated/abigen | ||
# Ignore all JSON files | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¤t_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; | ||
} | ||
} |
Oops, something went wrong.