Skip to content

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 30 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ccef0f4
Support stylus contracts publish and deploy
kumaryash90 Mar 18, 2025
0169660
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 Mar 18, 2025
62f629c
append and resolve ipfs uri in calldata
kumaryash90 Mar 18, 2025
8196f90
clean up, append uri data to all deploy tx
kumaryash90 Mar 26, 2025
97683d2
merge main
kumaryash90 Mar 26, 2025
8a7d38c
lint
kumaryash90 Mar 26, 2025
8213dfb
resolve implementation
kumaryash90 Mar 26, 2025
3f70fad
compute address with extra data
kumaryash90 Mar 26, 2025
6ec21e8
fix
kumaryash90 Mar 27, 2025
25b674b
update commands
kumaryash90 Mar 27, 2025
c5c3e51
activation tx for stylus contracts
kumaryash90 Mar 27, 2025
6621e9f
append uri data for stylus only
kumaryash90 Mar 27, 2025
a409e18
deploy command
kumaryash90 Mar 27, 2025
09dbb3b
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 Mar 27, 2025
b38b57a
create command
kumaryash90 Mar 27, 2025
a516eff
lock
kumaryash90 Mar 27, 2025
4e513bc
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 Mar 27, 2025
fbe5954
fix arb wasm address
kumaryash90 Mar 27, 2025
267d634
remove decoding logic, fetch from contract-api
kumaryash90 Mar 27, 2025
01faa22
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 Mar 27, 2025
244ba4a
changeset
kumaryash90 Mar 27, 2025
8b20f6d
estimate data fee for activation
kumaryash90 Mar 28, 2025
24c1b86
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 Mar 28, 2025
253a8dd
cleanup
kumaryash90 Mar 28, 2025
1fe5b15
static version of deps
kumaryash90 Mar 28, 2025
5505a65
lock
kumaryash90 Mar 28, 2025
10c1261
parse cargo toml
kumaryash90 Mar 28, 2025
b5a1825
lock
kumaryash90 Mar 28, 2025
cb07dba
check for error, cleanup
kumaryash90 Mar 28, 2025
d90dda7
Merge branch 'main' into yash/stylus-publish-deploy
kumaryash90 Mar 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-bugs-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Support Stylus contracts in CLI and SDK
85 changes: 25 additions & 60 deletions packages/thirdweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,66 +137,26 @@
},
"typesVersions": {
"*": {
"adapters/*": [
"./dist/types/exports/adapters/*.d.ts"
],
"auth": [
"./dist/types/exports/auth.d.ts"
],
"chains": [
"./dist/types/exports/chains.d.ts"
],
"contract": [
"./dist/types/exports/contract.d.ts"
],
"deploys": [
"./dist/types/exports/deploys.d.ts"
],
"event": [
"./dist/types/exports/event.d.ts"
],
"extensions/*": [
"./dist/types/exports/extensions/*.d.ts"
],
"pay": [
"./dist/types/exports/pay.d.ts"
],
"react": [
"./dist/types/exports/react.d.ts"
],
"react-native": [
"./dist/types/exports/react-native.d.ts"
],
"rpc": [
"./dist/types/exports/rpc.d.ts"
],
"storage": [
"./dist/types/exports/storage.d.ts"
],
"transaction": [
"./dist/types/exports/transaction.d.ts"
],
"utils": [
"./dist/types/exports/utils.d.ts"
],
"wallets": [
"./dist/types/exports/wallets.d.ts"
],
"wallets/*": [
"./dist/types/exports/wallets/*.d.ts"
],
"modules": [
"./dist/types/exports/modules.d.ts"
],
"social": [
"./dist/types/exports/social.d.ts"
],
"ai": [
"./dist/types/exports/ai.d.ts"
],
"bridge": [
"./dist/types/exports/bridge.d.ts"
]
"adapters/*": ["./dist/types/exports/adapters/*.d.ts"],
"auth": ["./dist/types/exports/auth.d.ts"],
"chains": ["./dist/types/exports/chains.d.ts"],
"contract": ["./dist/types/exports/contract.d.ts"],
"deploys": ["./dist/types/exports/deploys.d.ts"],
"event": ["./dist/types/exports/event.d.ts"],
"extensions/*": ["./dist/types/exports/extensions/*.d.ts"],
"pay": ["./dist/types/exports/pay.d.ts"],
"react": ["./dist/types/exports/react.d.ts"],
"react-native": ["./dist/types/exports/react-native.d.ts"],
"rpc": ["./dist/types/exports/rpc.d.ts"],
"storage": ["./dist/types/exports/storage.d.ts"],
"transaction": ["./dist/types/exports/transaction.d.ts"],
"utils": ["./dist/types/exports/utils.d.ts"],
"wallets": ["./dist/types/exports/wallets.d.ts"],
"wallets/*": ["./dist/types/exports/wallets/*.d.ts"],
"modules": ["./dist/types/exports/modules.d.ts"],
"social": ["./dist/types/exports/social.d.ts"],
"ai": ["./dist/types/exports/ai.d.ts"],
"bridge": ["./dist/types/exports/bridge.d.ts"]
}
},
"browser": {
Expand Down Expand Up @@ -233,7 +193,11 @@
"fuse.js": "7.1.0",
"input-otp": "^1.4.1",
"mipd": "0.0.7",
"open": "10.1.0",
"ora": "8.2.0",
"ox": "0.6.10",
"prompts": "2.4.2",
"toml": "3.0.0",
"uqr": "0.1.2",
"viem": "2.23.10"
},
Expand Down Expand Up @@ -358,6 +322,7 @@
"@testing-library/react": "^16.2.0",
"@testing-library/user-event": "^14.6.1",
"@types/cross-spawn": "^6.0.6",
"@types/prompts": "2.4.9",
"@types/react": "19.0.10",
"@viem/anvil": "0.0.10",
"@vitejs/plugin-react": "^4.3.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"function activateProgram(address program) returns (uint16,uint256)"
]
25 changes: 25 additions & 0 deletions packages/thirdweb/src/cli/bin.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ import {
generate,
isValidChainIdAndContractAddress,
} from "./commands/generate/generate.js";
import { deployStylus, publishStylus } from "./commands/stylus/builder.js";
import { createStylusProject } from "./commands/stylus/create.js";

// skip the first two args?
const [, , command = "", ...rest] = process.argv;

let secretKey: string | undefined;
const keyIndex = rest.indexOf("-k");
if (keyIndex !== -1 && rest.length > keyIndex + 1) {
secretKey = rest[keyIndex + 1];
rest.splice(keyIndex, 2);
}

async function main() {
switch (command) {
case "generate": {
Expand All @@ -20,6 +30,21 @@ async function main() {
break;
}

case "publish-stylus": {
await publishStylus(secretKey);
break;
}

case "deploy-stylus": {
await deployStylus(secretKey);
break;
}

case "create-stylus": {
await createStylusProject();
break;
}

case "login": {
// Not implemented yet
console.info(
Expand Down
201 changes: 201 additions & 0 deletions packages/thirdweb/src/cli/commands/stylus/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
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",
});
if (initcodeResult.status !== 0) {
spinner.fail("Failed to generate initcode.");
process.exit(1);
}

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",
});
if (abiResult.status !== 0) {
spinner.fail("Failed to generate ABI.");
process.exit(1);
}

const abiContent = abiResult.stdout.trim();
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("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();
}
Loading