# Complete Z Chain local setup: RPC node + contract deploy + config sync. # Uses Docker Besu when available; otherwise Hardhat node chainId 900002. $ErrorActionPreference = "Stop" $Root = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path $RpcPort = if ($env:Z_CHAIN_RPC_PORT) { $env:Z_CHAIN_RPC_PORT } else { "8546" } $RpcUrl = "http://127.0.0.1:$RpcPort" $NodeLog = Join-Path $Root "logs\z-chain-node.log" $ErrLog = Join-Path $Root "logs\z-chain-node.err.log" $PidFile = Join-Path $Root "logs\z-chain-node.pid" Set-Location $Root New-Item -ItemType Directory -Force -Path (Join-Path $Root "logs") | Out-Null function Test-RpcReady { try { $body = '{"jsonrpc":"2.0","id":1,"method":"eth_chainId","params":[]}' $r = Invoke-RestMethod -Uri $RpcUrl -Method Post -Body $body -ContentType "application/json" -TimeoutSec 2 return ($r.result -eq "0xdbba2") } catch { return $false } } function Stop-ZChainNode { if (Test-Path $PidFile) { $oldPid = Get-Content $PidFile -ErrorAction SilentlyContinue if ($oldPid) { Stop-Process -Id $oldPid -Force -ErrorAction SilentlyContinue } Remove-Item $PidFile -Force -ErrorAction SilentlyContinue } try { docker rm -f z-chain-besu 2>$null | Out-Null } catch {} $conn = Get-NetTCPConnection -LocalPort $RpcPort -ErrorAction SilentlyContinue | Select-Object -First 1 if ($conn -and $conn.OwningProcess) { Stop-Process -Id $conn.OwningProcess -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 } } $dockerOk = $false try { docker info 2>&1 | Out-Null if ($LASTEXITCODE -eq 0) { $dockerOk = $true } } catch {} Stop-ZChainNode if ($dockerOk) { Write-Host "Starting Z Chain via Docker Besu ..." & "$PSScriptRoot\bootstrap-z-chain-local.ps1" } else { $env:Z_CHAIN_LOCAL = "1" $hardhat = Join-Path $Root "node_modules\.bin\hardhat.cmd" $nodeCmd = "set Z_CHAIN_LOCAL=1&& `"$hardhat`" node --port $RpcPort --hostname 127.0.0.1" $nodeProc = Start-Process -FilePath "cmd.exe" -ArgumentList @("/c", $nodeCmd) -WorkingDirectory $Root -PassThru -WindowStyle Hidden -RedirectStandardOutput $NodeLog -RedirectStandardError $ErrLog $nodeProc.Id | Set-Content $PidFile Start-Sleep -Seconds 8 } $deadline = (Get-Date).AddSeconds(90) while (-not (Test-RpcReady)) { if ((Get-Date) -gt $deadline) { Write-Error "Z Chain RPC not ready at $RpcUrl after 90s. See $NodeLog and $ErrLog" } Start-Sleep -Seconds 2 } Write-Host "Z Chain RPC ready at $RpcUrl" $env:CHAIN_900002_RPC_URL = $RpcUrl $env:Z_CHAIN_LOCAL = "1" if (-not $env:PRIVATE_KEY) { $env:PRIVATE_KEY = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" } Write-Host "Compiling Z Chain contracts ..." npx hardhat compile --config hardhat.zchain.config.js 2>&1 | Out-Null if ($LASTEXITCODE -ne 0) { throw "zchain compile failed" } Write-Host "Deploying Z Chain contracts ..." npx hardhat run scripts/deployment/deploy-z-chain-contracts.js --network zchain --config hardhat.zchain.config.js if ($LASTEXITCODE -ne 0) { throw "contract deploy failed" } Write-Host "" Write-Host "Z Chain setup complete." Write-Host " RPC: $RpcUrl" Write-Host " Set VITE_ENABLE_Z_WALLET=true and VITE_RPC_URL_900002=$RpcUrl" Write-Host " Updated config/z-chain-deployed.v1.json"