Created
September 27, 2025 09:15
-
-
Save sarvagnakadiya/390493eff944ee1d4270734518edd04f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/bin/zsh | |
| set -euo pipefail | |
| # Usage: ./kiteup.sh <ContractName> | |
| # Example: ./kiteup.sh SimpleStaking | |
| if [ $# -lt 1 ]; then | |
| echo "Usage: $0 <ContractName>" >&2 | |
| exit 1 | |
| fi | |
| CONTRACT_NAME="$1" | |
| ROOT_DIR="$(cd "$(dirname "$0")" && pwd)" | |
| OUT_DIR="$ROOT_DIR/out" | |
| echo "[INFO] Searching artifact for $CONTRACT_NAME..." >&2 | |
| artifact_path="" | |
| # Prefer exact folder match first | |
| if [ -d "$OUT_DIR/$CONTRACT_NAME.sol" ]; then | |
| if [ -f "$OUT_DIR/$CONTRACT_NAME.sol/$CONTRACT_NAME.json" ]; then | |
| artifact_path="$OUT_DIR/$CONTRACT_NAME.sol/$CONTRACT_NAME.json" | |
| fi | |
| fi | |
| # If not found, glob search | |
| if [ -z "$artifact_path" ]; then | |
| match_count=0 | |
| while IFS= read -r -d '' f; do | |
| artifact_path="$f" | |
| match_count=$((match_count + 1)) | |
| done < <(find "$OUT_DIR" -type f -name "$CONTRACT_NAME.json" -print0) | |
| if [ $match_count -eq 0 ]; then | |
| echo "[ERROR] No artifact found for '$CONTRACT_NAME' under $OUT_DIR" >&2 | |
| exit 2 | |
| fi | |
| if [ $match_count -gt 1 ]; then | |
| echo "[WARN] Multiple artifacts found. Using first: $artifact_path" >&2 | |
| fi | |
| fi | |
| echo "[INFO] Artifact path: $artifact_path" >&2 | |
| if ! command -v jq >/dev/null 2>&1; then | |
| echo "[ERROR] jq is required but not installed." >&2 | |
| exit 3 | |
| fi | |
| # Extract fields | |
| abi=$(jq -c '.abi' "$artifact_path") | |
| bytecode=$(jq -r '.bytecode.object // .bytecode // ""' "$artifact_path") | |
| compiler=$(jq -r '.metadata.compiler.version // .rawMetadata.compiler.version // ""' "$artifact_path") | |
| # Resolve source file path (look into src/) | |
| echo "[INFO] Resolving source file..." >&2 | |
| source_abs=$(grep -RIl --include='*.sol' --exclude-dir={node_modules,lib,out} \ | |
| "contract $CONTRACT_NAME\b" "$ROOT_DIR/src" 2>/dev/null | head -n1 || true) | |
| if [ -z "$source_abs" ]; then | |
| echo "[WARN] Could not find source file for contract $CONTRACT_NAME" >&2 | |
| fi | |
| # Extract settings | |
| optimizer_enabled=$(jq -r '(.metadata.settings.optimizer.enabled // .rawMetadata.settings.optimizer.enabled // false)' "$artifact_path") | |
| optimizer_runs=$(jq -r '(.metadata.settings.optimizer.runs // .rawMetadata.settings.optimizer.runs // 200)' "$artifact_path") | |
| evm_version=$(jq -r '(.metadata.settings.evmVersion // .rawMetadata.settings.evmVersion // "")' "$artifact_path") | |
| remappings=$(jq -c '(.metadata.settings.remappings // .rawMetadata.settings.remappings // [])' "$artifact_path") | |
| # Normalize optimizer values | |
| if [ "$optimizer_enabled" = "true" ] || [ "$optimizer_enabled" = "false" ]; then | |
| optimizer_json=$(jq -n --argjson en "$optimizer_enabled" --argjson rn "$optimizer_runs" '{enabled: $en, runs: $rn}') | |
| else | |
| optimizer_json=$(jq -n --argjson en false --argjson rn 200 '{enabled: $en, runs: $rn}') | |
| fi | |
| # Output final JSON | |
| echo "[INFO] Building final JSON..." >&2 | |
| jq -n \ | |
| --arg name "$CONTRACT_NAME" \ | |
| --arg path "$artifact_path" \ | |
| --arg bytecode "$bytecode" \ | |
| --arg compiler "$compiler" \ | |
| --arg sourcePath "$source_abs" \ | |
| --rawfile source "${source_abs:-/dev/null}" \ | |
| --argjson abi "$abi" \ | |
| --arg evmVersion "$evm_version" \ | |
| --argjson remappings "$remappings" \ | |
| --argjson optimizer "$optimizer_json" \ | |
| '{ | |
| name: $name, | |
| artifactPath: $path, | |
| compilerVersion: $compiler, | |
| abi: $abi, | |
| bytecode: $bytecode, | |
| sourcePath: $sourcePath, | |
| source: $source, | |
| settings: { | |
| evmVersion: $evmVersion, | |
| remappings: $remappings, | |
| optimizer: $optimizer | |
| } | |
| }' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment