|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { existsSync, readFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import open from "open"; |
| 5 | +import ora, { type Ora } from "ora"; |
| 6 | +import { parse } from "toml"; |
| 7 | +import { createThirdwebClient } from "../../../client/client.js"; |
| 8 | +import { upload } from "../../../storage/upload.js"; |
| 9 | + |
| 10 | +const THIRDWEB_URL = "https://thirdweb.com"; |
| 11 | + |
| 12 | +export async function publishStylus(secretKey?: string) { |
| 13 | + const spinner = ora("Checking if this is a Stylus project...").start(); |
| 14 | + const uri = await buildStylus(spinner, secretKey); |
| 15 | + |
| 16 | + const url = getUrl(uri, "publish").toString(); |
| 17 | + spinner.succeed(`Upload complete, navigate to ${url}`); |
| 18 | + await open(url); |
| 19 | +} |
| 20 | + |
| 21 | +export async function deployStylus(secretKey?: string) { |
| 22 | + const spinner = ora("Checking if this is a Stylus project...").start(); |
| 23 | + const uri = await buildStylus(spinner, secretKey); |
| 24 | + |
| 25 | + const url = getUrl(uri, "deploy").toString(); |
| 26 | + spinner.succeed(`Upload complete, navigate to ${url}`); |
| 27 | + await open(url); |
| 28 | +} |
| 29 | + |
| 30 | +async function buildStylus(spinner: Ora, secretKey?: string) { |
| 31 | + if (!secretKey) { |
| 32 | + spinner.fail("Error: Secret key is required."); |
| 33 | + process.exit(1); |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + // Step 1: Validate stylus project |
| 38 | + const root = process.cwd(); |
| 39 | + if (!root) { |
| 40 | + spinner.fail("Error: No package directory found."); |
| 41 | + process.exit(1); |
| 42 | + } |
| 43 | + |
| 44 | + const cargoTomlPath = join(root, "Cargo.toml"); |
| 45 | + if (!existsSync(cargoTomlPath)) { |
| 46 | + spinner.fail("Error: No Cargo.toml found. Not a Stylus/Rust project."); |
| 47 | + process.exit(1); |
| 48 | + } |
| 49 | + |
| 50 | + const cargoToml = readFileSync(cargoTomlPath, "utf8"); |
| 51 | + const parsedCargoToml = parse(cargoToml); |
| 52 | + if (!parsedCargoToml.dependencies?.["stylus-sdk"]) { |
| 53 | + spinner.fail( |
| 54 | + "Error: Not a Stylus project. Missing stylus-sdk dependency.", |
| 55 | + ); |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | + |
| 59 | + spinner.succeed("Stylus project detected."); |
| 60 | + |
| 61 | + // Step 2: Run stylus command to generate initcode |
| 62 | + spinner.start("Generating initcode..."); |
| 63 | + const initcodeResult = spawnSync("cargo", ["stylus", "get-initcode"], { |
| 64 | + encoding: "utf-8", |
| 65 | + }); |
| 66 | + if (initcodeResult.status !== 0) { |
| 67 | + spinner.fail("Failed to generate initcode."); |
| 68 | + process.exit(1); |
| 69 | + } |
| 70 | + |
| 71 | + const initcode = extractBytecode(initcodeResult.stdout); |
| 72 | + if (!initcode) { |
| 73 | + spinner.fail("Failed to generate initcode."); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | + spinner.succeed("Initcode generated."); |
| 77 | + |
| 78 | + // Step 3: Run stylus command to generate abi |
| 79 | + spinner.start("Generating ABI..."); |
| 80 | + const abiResult = spawnSync("cargo", ["stylus", "export-abi", "--json"], { |
| 81 | + encoding: "utf-8", |
| 82 | + }); |
| 83 | + if (abiResult.status !== 0) { |
| 84 | + spinner.fail("Failed to generate ABI."); |
| 85 | + process.exit(1); |
| 86 | + } |
| 87 | + |
| 88 | + const abiContent = abiResult.stdout.trim(); |
| 89 | + if (!abiContent) { |
| 90 | + spinner.fail("Failed to generate ABI."); |
| 91 | + process.exit(1); |
| 92 | + } |
| 93 | + spinner.succeed("ABI generated."); |
| 94 | + |
| 95 | + // Step 4: Process the output |
| 96 | + const contractName = extractContractNameFromExportAbi(abiContent); |
| 97 | + if (!contractName) { |
| 98 | + spinner.fail("Error: Could not determine contract name from ABI output."); |
| 99 | + process.exit(1); |
| 100 | + } |
| 101 | + |
| 102 | + let cleanedAbi = ""; |
| 103 | + try { |
| 104 | + const jsonMatch = abiContent.match(/\[.*\]/s); |
| 105 | + if (jsonMatch) { |
| 106 | + cleanedAbi = jsonMatch[0]; |
| 107 | + } else { |
| 108 | + throw new Error("No valid JSON ABI found in the file."); |
| 109 | + } |
| 110 | + } catch (error) { |
| 111 | + spinner.fail("Error: ABI file contains invalid format."); |
| 112 | + console.error(error); |
| 113 | + process.exit(1); |
| 114 | + } |
| 115 | + |
| 116 | + const metadata = { |
| 117 | + compiler: {}, |
| 118 | + language: "rust", |
| 119 | + output: { |
| 120 | + abi: JSON.parse(cleanedAbi), |
| 121 | + devdoc: {}, |
| 122 | + userdoc: {}, |
| 123 | + }, |
| 124 | + settings: { |
| 125 | + compilationTarget: { |
| 126 | + "src/main.rs": contractName, |
| 127 | + }, |
| 128 | + }, |
| 129 | + sources: {}, |
| 130 | + }; |
| 131 | + spinner.succeed("Stylus contract exported successfully."); |
| 132 | + |
| 133 | + // Step 5: Upload to IPFS |
| 134 | + spinner.start("Uploading to IPFS..."); |
| 135 | + const client = createThirdwebClient({ |
| 136 | + secretKey, |
| 137 | + }); |
| 138 | + |
| 139 | + const metadataUri = await upload({ |
| 140 | + client, |
| 141 | + files: [metadata], |
| 142 | + }); |
| 143 | + |
| 144 | + const bytecodeUri = await upload({ |
| 145 | + client, |
| 146 | + files: [initcode], |
| 147 | + }); |
| 148 | + |
| 149 | + const uri = await upload({ |
| 150 | + client, |
| 151 | + files: [ |
| 152 | + { |
| 153 | + name: contractName, |
| 154 | + metadataUri, |
| 155 | + bytecodeUri, |
| 156 | + analytics: { |
| 157 | + command: "publish-stylus", |
| 158 | + contract_name: contractName, |
| 159 | + cli_version: "", |
| 160 | + project_type: "stylus", |
| 161 | + }, |
| 162 | + compilers: { |
| 163 | + stylus: [ |
| 164 | + { compilerVersion: "", evmVersion: "", metadataUri, bytecodeUri }, |
| 165 | + ], |
| 166 | + }, |
| 167 | + }, |
| 168 | + ], |
| 169 | + }); |
| 170 | + spinner.succeed("Upload complete"); |
| 171 | + |
| 172 | + return uri; |
| 173 | + } catch (error) { |
| 174 | + spinner.fail(`Error: ${error}`); |
| 175 | + process.exit(1); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +function extractContractNameFromExportAbi(abiRawOutput: string): string | null { |
| 180 | + const match = abiRawOutput.match(/<stdin>:(I[A-Za-z0-9_]+)/); |
| 181 | + if (match?.[1]) { |
| 182 | + return match[1].replace(/^I/, ""); |
| 183 | + } |
| 184 | + return null; |
| 185 | +} |
| 186 | + |
| 187 | +function getUrl(hash: string, command: string) { |
| 188 | + const url = new URL( |
| 189 | + `${THIRDWEB_URL}/contracts/${command}/${encodeURIComponent(hash.replace("ipfs://", ""))}`, |
| 190 | + ); |
| 191 | + |
| 192 | + return url; |
| 193 | +} |
| 194 | + |
| 195 | +function extractBytecode(rawOutput: string): string { |
| 196 | + const hexStart = rawOutput.indexOf("7f000000"); |
| 197 | + if (hexStart === -1) { |
| 198 | + throw new Error("Could not find start of bytecode"); |
| 199 | + } |
| 200 | + return rawOutput.slice(hexStart).trim(); |
| 201 | +} |
0 commit comments