-
Notifications
You must be signed in to change notification settings - Fork 621
Support stylus contracts publish and deploy #6495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
ccef0f4
Support stylus contracts publish and deploy
kumaryash90 0169660
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 62f629c
append and resolve ipfs uri in calldata
kumaryash90 8196f90
clean up, append uri data to all deploy tx
kumaryash90 97683d2
merge main
kumaryash90 8a7d38c
lint
kumaryash90 8213dfb
resolve implementation
kumaryash90 3f70fad
compute address with extra data
kumaryash90 6ec21e8
fix
kumaryash90 25b674b
update commands
kumaryash90 c5c3e51
activation tx for stylus contracts
kumaryash90 6621e9f
append uri data for stylus only
kumaryash90 a409e18
deploy command
kumaryash90 09dbb3b
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 b38b57a
create command
kumaryash90 a516eff
lock
kumaryash90 4e513bc
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 fbe5954
fix arb wasm address
kumaryash90 267d634
remove decoding logic, fetch from contract-api
kumaryash90 01faa22
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 244ba4a
changeset
kumaryash90 8b20f6d
estimate data fee for activation
kumaryash90 24c1b86
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 253a8dd
cleanup
kumaryash90 1fe5b15
static version of deps
kumaryash90 5505a65
lock
kumaryash90 10c1261
parse cargo toml
kumaryash90 b5a1825
lock
kumaryash90 cb07dba
check for error, cleanup
kumaryash90 d90dda7
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "thirdweb": patch | ||
| --- | ||
|
|
||
| Support Stylus contracts in CLI and SDK |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [ | ||
| "function activateProgram(address program) returns (uint16,uint256)" | ||
| ] |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import { spawnSync } from "node:child_process"; | ||
| import { existsSync, readFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import open from "open"; | ||
| import ora, { type Ora } from "ora"; | ||
| import { parse } from "toml"; | ||
| import { createThirdwebClient } from "../../../client/client.js"; | ||
| import { upload } from "../../../storage/upload.js"; | ||
|
|
||
| const THIRDWEB_URL = "https://thirdweb.com"; | ||
|
|
||
| export async function publishStylus(secretKey?: string) { | ||
| const spinner = ora("Checking if this is a Stylus project...").start(); | ||
| const uri = await buildStylus(spinner, secretKey); | ||
|
|
||
| const url = getUrl(uri, "publish").toString(); | ||
| spinner.succeed(`Upload complete, navigate to ${url}`); | ||
| await open(url); | ||
| } | ||
|
|
||
| export async function deployStylus(secretKey?: string) { | ||
| const spinner = ora("Checking if this is a Stylus project...").start(); | ||
| const uri = await buildStylus(spinner, secretKey); | ||
|
|
||
| const url = getUrl(uri, "deploy").toString(); | ||
| spinner.succeed(`Upload complete, navigate to ${url}`); | ||
| await open(url); | ||
| } | ||
|
|
||
| async function buildStylus(spinner: Ora, secretKey?: string) { | ||
| if (!secretKey) { | ||
| spinner.fail("Error: Secret key is required."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| try { | ||
| // Step 1: Validate stylus project | ||
| const root = process.cwd(); | ||
| if (!root) { | ||
| spinner.fail("Error: No package directory found."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const cargoTomlPath = join(root, "Cargo.toml"); | ||
| if (!existsSync(cargoTomlPath)) { | ||
| spinner.fail("Error: No Cargo.toml found. Not a Stylus/Rust project."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const cargoToml = readFileSync(cargoTomlPath, "utf8"); | ||
| const parsedCargoToml = parse(cargoToml); | ||
| if (!parsedCargoToml.dependencies?.["stylus-sdk"]) { | ||
| spinner.fail( | ||
| "Error: Not a Stylus project. Missing stylus-sdk dependency.", | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| spinner.succeed("Stylus project detected."); | ||
|
|
||
| // Step 2: Run stylus command to generate initcode | ||
| spinner.start("Generating initcode..."); | ||
| const initcodeResult = spawnSync("cargo", ["stylus", "get-initcode"], { | ||
| encoding: "utf-8", | ||
| }); | ||
| const initcode = extractBytecode(initcodeResult.stdout); | ||
|
||
|
|
||
| if (!initcode) { | ||
| spinner.fail("Failed to generate initcode."); | ||
| process.exit(1); | ||
| } | ||
| spinner.succeed("Initcode generated."); | ||
|
|
||
| // Step 3: Run stylus command to generate abi | ||
| spinner.start("Generating ABI..."); | ||
| const abiResult = spawnSync("cargo", ["stylus", "export-abi", "--json"], { | ||
| encoding: "utf-8", | ||
| }); | ||
|
|
||
| const abiContent = abiResult.stdout.trim(); | ||
kumaryash90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!abiContent) { | ||
| spinner.fail("Failed to generate ABI."); | ||
| process.exit(1); | ||
| } | ||
| spinner.succeed("ABI generated."); | ||
|
|
||
| // Step 4: Process the output | ||
| const contractName = extractContractNameFromExportAbi(abiContent); | ||
| if (!contractName) { | ||
| spinner.fail("Error: Could not determine contract name from ABI output."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let cleanedAbi = ""; | ||
| try { | ||
| const jsonMatch = abiContent.match(/\[.*\]/s); | ||
| if (jsonMatch) { | ||
| cleanedAbi = jsonMatch[0]; | ||
| } else { | ||
| throw new Error("No valid JSON ABI found in the file."); | ||
| } | ||
| } catch (error) { | ||
| spinner.fail("Error: ABI file contains invalid format."); | ||
| console.error(error); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const metadata = { | ||
| compiler: {}, | ||
| language: "rust", | ||
| output: { | ||
| abi: JSON.parse(cleanedAbi), | ||
| devdoc: {}, | ||
| userdoc: {}, | ||
| }, | ||
| settings: { | ||
| compilationTarget: { | ||
| "src/main.rs": contractName, | ||
| }, | ||
| }, | ||
| sources: {}, | ||
| }; | ||
| spinner.succeed("ABI cleaned and saved."); | ||
|
||
| spinner.succeed("Stylus contract exported successfully."); | ||
|
|
||
| // Step 5: Upload to IPFS | ||
| spinner.start("Uploading to IPFS..."); | ||
| const client = createThirdwebClient({ | ||
| secretKey, | ||
| }); | ||
|
|
||
| const metadataUri = await upload({ | ||
| client, | ||
| files: [metadata], | ||
| }); | ||
|
|
||
| const bytecodeUri = await upload({ | ||
| client, | ||
| files: [initcode], | ||
| }); | ||
|
|
||
| const uri = await upload({ | ||
| client, | ||
| files: [ | ||
| { | ||
| name: contractName, | ||
| metadataUri, | ||
| bytecodeUri, | ||
| analytics: { | ||
| command: "publish-stylus", | ||
| contract_name: contractName, | ||
| cli_version: "", | ||
| project_type: "stylus", | ||
| }, | ||
| compilers: { | ||
| stylus: [ | ||
| { compilerVersion: "", evmVersion: "", metadataUri, bytecodeUri }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
| spinner.succeed("Upload complete"); | ||
|
|
||
| return uri; | ||
| } catch (error) { | ||
| spinner.fail(`Error: ${error}`); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| function extractContractNameFromExportAbi(abiRawOutput: string): string | null { | ||
| const match = abiRawOutput.match(/<stdin>:(I[A-Za-z0-9_]+)/); | ||
| if (match?.[1]) { | ||
| return match[1].replace(/^I/, ""); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function getUrl(hash: string, command: string) { | ||
| const url = new URL( | ||
| `${THIRDWEB_URL}/contracts/${command}/${encodeURIComponent(hash.replace("ipfs://", ""))}`, | ||
| ); | ||
|
|
||
| return url; | ||
| } | ||
|
|
||
| function extractBytecode(rawOutput: string): string { | ||
| const hexStart = rawOutput.indexOf("7f000000"); | ||
| if (hexStart === -1) { | ||
| throw new Error("Could not find start of bytecode"); | ||
| } | ||
| return rawOutput.slice(hexStart).trim(); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.