Skip to content

Commit

Permalink
wip: ci tests with schema
Browse files Browse the repository at this point in the history
  • Loading branch information
NanezX committed Nov 3, 2023
1 parent c8caa12 commit 0ea6b1f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 82 deletions.
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"

50 changes: 37 additions & 13 deletions subgraph/cli/deploy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,46 @@ use std::fs;

use crate::utils::run_cmd;

pub struct Config<'a> {
// contracts address
pub contract_address: &'a String,
// block-number
pub block_number: u64,
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(config: Config) -> anyhow::Result<bool> {
pub fn deploy_subgraph(config: DeployArgs) -> anyhow::Result<()> {
let subgraph_template = "subgraph.template.yaml";
let output_path = "subgraph.yaml";
// let root_dir = "./";
let end_point = "http://localhost:8020/";
let subgraph_name = "test/test";

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

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

Expand All @@ -30,7 +53,8 @@ pub fn deploy(config: Config) -> anyhow::Result<bool> {
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 && npx graph build"]);
let is_built = run_cmd("bash", &["-c", "npx graph codegen"]);
if !is_built {
return Err(anyhow!("Failed to build subgraph"));
}
Expand Down Expand Up @@ -62,5 +86,5 @@ pub fn deploy(config: Config) -> anyhow::Result<bool> {
return Err(anyhow!("Failed to deploy subgraph"));
}

Ok(true)
Ok(())
}
66 changes: 24 additions & 42 deletions subgraph/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// extern crate url;
mod deploy;
mod utils;
use clap::{Args, Parser, Subcommand};
use clap::{Parser, Subcommand};

use deploy::{deploy, Config};
// use colored::*;
use deploy::{deploy_subgraph, DeployArgs};
use utils::run_cmd;

#[derive(Parser)]
Expand All @@ -21,62 +23,42 @@ pub enum Subgraph {
#[command(about = "Test the rain subgraph")]
Test,
#[command(about = "Deploy the rain subgraph")]
Deploy(DeployCommand),
Deploy(DeployArgs),
}

#[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,
}

fn main() {
fn main() -> Result<(), anyhow::Error> {
let args = Cli::parse();

match args.subgraph {
Subgraph::Install => {
run_cmd("npm", &["install"]);

Ok(())
}

Subgraph::Build => {
// Use a arbitrary address to put the endpoint up
let config = Config {
contract_address: &"0x0000000000000000000000000000000000000000".to_string(),
block_number: 0,
};

let _ = deploy(config);

// Get the schema from the endpoint
// "tests/subgraph/query/schema.json",
run_cmd(
"graphql-client",
&[
"introspect-schema",
"--output",
"schema.json",
"http://localhost:8000/subgraphs/name/test/test",
],
);
run_cmd("npm", &["run", "codegen"]);
run_cmd("npm", &["run", "build"]);

()
Ok(())
}

Subgraph::Test => {
println!("Hello tests 🧪");
todo!("Test CI does not implemented");
run_cmd("nix", &["run", ".#ci-test"]);

Ok(())
}
Subgraph::Deploy(args) => {
println!("🚀 Hello, deploy with: {:?}", args);
todo!("Deploy CI does not implemented");
println!("\n🚀 Hello deploy");
let _ = deploy_subgraph(args);

// if args.url.scheme() != "http" && args.url.scheme() != "https" {
// eprintln!("Error: Invalid URL provided");
// std::process::exit(1);
// }

Ok(())

}
}
}
55 changes: 28 additions & 27 deletions subgraph/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

in rec {
packages = rec {
install = pkgs.writeShellScriptBin "install" ("npm install");

build = pkgs.writeShellScriptBin "build" ("npm run codegen && npm run build");

# ERC20Mock is not present here. It's hardcoded because It's just a ERC20 contract with a mint method.
concrete-contracts = ["AuthoringMetaGetter" "OrderBook" "RainterpreterExpressionDeployerNP" "RainterpreterNP" "RainterpreterStore"];

Expand All @@ -41,6 +37,22 @@
echo Removed duplicated at: $contract_path
'';

# The graphql file can generate the schema.json file needed for testing
# Of course, this need a graph node at localhost to work
gen-sg-schema = ''
# Use a arbitrary address to put the endpoint up
cargo run deploy \
--name test/test \
--url http://localhost:8020 \
--network localhost \
--block 0 \
--address 0x0000000000000000000000000000000000000000
${graphql_client} introspect-schema \
--output tests/subgraph/query/schema.json \
http://localhost:8000/subgraphs/name/test/test
'';

init-setup = pkgs.writeShellScriptBin "init-setup" (''
# NOTE: This should be called after `npm install`
Expand All @@ -50,9 +62,14 @@
# Copying the new abis into the SG abi folder
cp ../out/OrderBook.sol/OrderBook.json ./abis/
cp ../out/ERC20.sol/ERC20.json ./abis/ERC20.json
'' + pkgs.lib.concatStrings (map copy-abis concrete-contracts) + (remove-duplicate)
'' + pkgs.lib.concatStrings (map copy-abis concrete-contracts)
+ (remove-duplicate)
);

run-anvil = pkgs.writeShellScriptBin "run-anvil" (''
anvil -m "$(cat ./test-mnemonic)"
'');

docker-up = pkgs.writeShellScriptBin "docker-up" ''
docker-compose -f docker/docker-compose.yaml up --build -d
'';
Expand All @@ -61,36 +78,20 @@
docker-compose -f docker/docker-compose.yaml down
'';

check-args = pkgs.writeShellScriptBin "check-args" (''
echo "All parameters: $@"
echo "First parameter: $1"
echo "Second parameter: $2"
'');

run-anvil = pkgs.writeShellScriptBin "run-anvil" (''
anvil -m "$(cat ./test-mnemonic)"
'');

check = pkgs.writeShellScriptBin "check" (''
${graphql_client}
'');

strong-anvil = pkgs.writeShellScriptBin "strong-anvil" (''
anvil -m "$(cat ./test-mnemonic)" --code-size-limit 36864
'');

end-anvil = pkgs.writeShellScriptBin "end-anvil" (''
kill -9 $(lsof -t -i :8545)
'');

ci-test = pkgs.writeShellScriptBin "ci-test" (''
# This build is for generate the schema.json
cargo run build;
ls
# This build is for generate the schema.json.
# This in case the subgraph schema.graphql have changes when running tests
${gen-sg-schema}
# Run tests in single thread
cargo test -- --test-threads=1 --nocapture;
'');

default = install;
default = init-setup;
};
}
);
Expand Down

0 comments on commit 0ea6b1f

Please sign in to comment.