diff --git a/api/src/handlers/mod.rs b/api/src/handlers/mod.rs index ec3af3cd..81bd9ce8 100644 --- a/api/src/handlers/mod.rs +++ b/api/src/handlers/mod.rs @@ -7,16 +7,83 @@ pub mod types; use crate::handlers::compile::do_compile; use crate::handlers::compiler_version::do_compiler_version; -use crate::handlers::types::{ApiCommand, ApiCommandResult}; +use crate::handlers::types::{ApiCommand, ApiCommandResult, HealthCheckResponse}; use crate::types::ApiError; +use crate::utils::lib::{get_file_path, init_parent_directories, ARTIFACTS_ROOT}; +use rocket::tokio; +use std::path::PathBuf; +use std::str::FromStr; use tracing::info; use tracing::instrument; +use uuid::Uuid; #[instrument] #[get("/health")] -pub async fn health() -> &'static str { +pub async fn health() -> HealthCheckResponse { info!("/health"); - "OK" + + let file_content = generate_mock_solidity_file_content(); + let version = String::from("latest"); + let path_uuid = generate_remix_file_path(); + let local_path = get_file_path(&version, &path_uuid); + let path = PathBuf::from_str(&path_uuid).unwrap(); + + let artifact_folder = PathBuf::from(ARTIFACTS_ROOT) + .join(version.clone()) + .join(path_uuid.clone()); + + // create file directory from file path + init_parent_directories(local_path.clone()).await; + if tokio::fs::write(&local_path, file_content).await.is_err() { + return HealthCheckResponse::error("Failed to write file"); + } + + let result = do_compile(version, path.clone()).await; + + // cleanup + if tokio::fs::remove_dir_all(local_path.parent().unwrap().parent().unwrap()) + .await + .is_err() + { + return HealthCheckResponse::error("Failed to remove directory from local path"); + } + + println!("Artifacts : {:?}", artifact_folder); + if tokio::fs::remove_dir_all(artifact_folder.parent().unwrap()) + .await + .is_err() + { + return HealthCheckResponse::error("Failed to remove directory from artifact path"); + } + + if result.is_ok() { + HealthCheckResponse::ok() + } else { + HealthCheckResponse::error("Failed to compile") + } +} + +pub fn generate_mock_solidity_file_content() -> String { + r#" + pragma solidity ^0.8.0; + + contract SimpleStorage { + uint256 storedData; + + function set(uint256 x) public { + storedData = x; + } + + function get() public view returns (uint256) { + return storedData; + } + } + "# + .to_string() +} + +pub fn generate_remix_file_path() -> String { + format!("{}/{}", Uuid::new_v4(), "SimpleStorage.sol") } #[instrument] diff --git a/api/src/handlers/types.rs b/api/src/handlers/types.rs index df398661..75346df1 100644 --- a/api/src/handlers/types.rs +++ b/api/src/handlers/types.rs @@ -1,3 +1,6 @@ +use rocket::http::Status; +use rocket::response::Responder; +use rocket::Request; use serde::{Deserialize, Serialize}; use std::path::PathBuf; @@ -47,3 +50,31 @@ pub enum ApiCommandResult { #[allow(dead_code)] Shutdown, } + +pub struct HealthCheckResponse(pub Result<(), &'static str>); + +impl<'r, 'o: 'r> Responder<'r, 'o> for HealthCheckResponse { + fn respond_to(self, request: &'r Request<'_>) -> rocket::response::Result<'o> { + match self.0 { + Ok(_) => { + Ok(rocket::response::status::Custom(Status { code: 200 }, "OK") + .respond_to(request)?) + } + Err(_) => Ok(rocket::response::status::Custom( + Status { code: 500 }, + "Internal Server Error", + ) + .respond_to(request)?), + } + } +} + +impl HealthCheckResponse { + pub fn ok() -> Self { + HealthCheckResponse(Ok(())) + } + + pub fn error(value: &'static str) -> Self { + HealthCheckResponse(Err(value)) + } +} diff --git a/plugin/src/index.tsx b/plugin/src/index.tsx index daf21046..14ec330a 100644 --- a/plugin/src/index.tsx +++ b/plugin/src/index.tsx @@ -7,7 +7,6 @@ import { createWeb3Modal } from '@web3modal/wagmi/react' import { configureChains, createConfig, WagmiConfig } from 'wagmi' import { publicProvider } from 'wagmi/providers/public' import { CoinbaseWalletConnector } from 'wagmi/connectors/coinbaseWallet' - import { zkSync, zkSyncSepoliaTestnet } from 'viem/chains' import { WalletConnectConnector } from 'wagmi/connectors/walletConnect' import { InjectedConnector } from 'wagmi/connectors/injected' @@ -16,14 +15,15 @@ import { EIP6963Connector, walletConnectProvider } from '@web3modal/wagmi' const projectId: string = import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID // TODO who owns this? make sure nethermind owns this const { - chains, publicClient -} = configureChains([zkSyncSepoliaTestnet, zkSync], [walletConnectProvider({ projectId }), publicProvider()]) +} = configureChains([zkSync, zkSyncSepoliaTestnet], [walletConnectProvider({ projectId }), publicProvider()]) + +const chains = [zkSyncSepoliaTestnet, zkSync] const metadata = { name: 'zkSync remix plugin', description: 'zkSync remix plugin', - url: '', + url: 'https://remix.ethereum.org', icons: ['https://avatars.githubusercontent.com/u/37784886'] }