diff --git a/subgraph/Cargo.lock b/subgraph/Cargo.lock index f9235a0bf..d01950ca2 100644 --- a/subgraph/Cargo.lock +++ b/subgraph/Cargo.lock @@ -3308,6 +3308,7 @@ dependencies = [ "thiserror", "tiny-keccak", "tokio", + "url", "web3", ] diff --git a/subgraph/Cargo.toml b/subgraph/Cargo.toml index 8d55cafb8..0a014f64b 100644 --- a/subgraph/Cargo.toml +++ b/subgraph/Cargo.toml @@ -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" diff --git a/subgraph/cli/deploy/mod.rs b/subgraph/cli/deploy/mod.rs index 9a0836907..a8113ec25 100644 --- a/subgraph/cli/deploy/mod.rs +++ b/subgraph/cli/deploy/mod.rs @@ -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, } -pub fn deploy(config: Config) -> anyhow::Result { +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(); @@ -30,7 +53,8 @@ pub fn deploy(config: Config) -> anyhow::Result { 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")); } @@ -62,5 +86,5 @@ pub fn deploy(config: Config) -> anyhow::Result { return Err(anyhow!("Failed to deploy subgraph")); } - Ok(true) + Ok(()) } diff --git a/subgraph/cli/main.rs b/subgraph/cli/main.rs index db8952d2a..d6c44acc7 100644 --- a/subgraph/cli/main.rs +++ b/subgraph/cli/main.rs @@ -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)] @@ -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(()) + } } } diff --git a/subgraph/flake.nix b/subgraph/flake.nix index ac45071e5..a855b21fe 100644 --- a/subgraph/flake.nix +++ b/subgraph/flake.nix @@ -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"]; @@ -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` @@ -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 ''; @@ -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; }; } );