Skip to main content

A gentle introduction to Stylus

In a nutshell:

  • Stylus lets you write smart contracts in programming languages that compile to WASM, such as Rust, C, C++, and others, allowing you to use their ecosystem of libraries and tools. Language and tooling support exist for Rust. You can try the SDK and CLI with the quickstart.
  • Solidity contracts and Stylus contracts are interoperable. In Solidity, you can call a Rust program and vice versa, thanks to a second, coequal WASM virtual machine.
  • Stylus contracts offer reduced gas costs for memory and compute-intensive operations because WASM programs execute more efficiently than EVM bytecode for these workloads.

What's Stylus?

Stylus is an upgrade to Arbitrum Nitro (ArbOS 32), the tech stack powering Arbitrum One, Arbitrum Nova, and Arbitrum chains. This upgrade adds a second, coequal virtual machine to the EVM, where EVM contracts continue to behave exactly as they would in Ethereum. This paradigm is called MultiVM because it is additive to existing functionality.

Stylus gives you MultiVM

This second virtual machine executes WebAssembly (WASM) rather than EVM bytecode. WASM is a binary format used in web standards and browsers for efficient computation. It is designed to be portable and human-readable, with sandboxed execution environments for security. Working with WASM is nothing new for Arbitrum chains. Ever since the Nitro upgrade, WASM has been a fundamental component of Arbitrum's fraud proofs.

With a WASM VM, any programming language compilable to WASM is within Stylus's scope. While many popular programming languages can compile into WASM, some compilers are more suitable for smart contract development than others, like Rust, C, and C++. Other languages like Go, Sway, Move, and Cairo are also supported. Languages that include their own runtimes, like Python and JavaScript, are more complex for Stylus to support, although not impossible. WASM programs tend to be more efficient than EVM bytecode for memory-intensive applications. This efficiency comes from mature compiler toolchains for languages like Rust and C, which have benefited from decades of optimization work. The WASM runtime also executes faster than the EVM interpreter. Third-party contributions in the form of libraries for new and existing languages are welcome.

How Stylus works

Bringing a Stylus program to life involves four stages: coding, activation, execution, and proving. The following sections describe each step.

Coding

You write your smart contract in any programming language that compiles to WASM. Rust has the most developed support with an open-source SDK for smart contract development. C and C++ are also supported, so you can deploy existing contracts in those languages onchain with minimal modifications.

The Stylus SDK for Rust provides the development framework and language features for smart contract development. It also provides access to EVM-specific functionality that smart contract developers use.

Activation

Once you've written your contract, you compile it to WASM using the Stylus CLI (or another compiler like Clang for C/C++). Then you post the compiled WASM onchain.

Before your contract can be called, it goes through an activation process. During activation, the WASM gets compiled down to a node's native machine code (like ARM or x86). This step also applies safety checks, including gas metering, depth-checking, and memory charging, to ensure your program runs safely and can be fraud-proven.

Stylus measures computational costs using ink instead of gas. Ink works like gas but is thousands of times smaller. WASM executes faster than the EVM, so a single EVM operation takes as long as thousands of WASM operations. A finer-grained unit makes pricing more precise.

note

Stylus contracts need to be reactivated once per year (365 days) or after any Stylus upgrade. You can do this using cargo-stylus or the ArbWasm precompile. If a contract isn't reactivated, it becomes uncallable.

Execution

When your Stylus program runs, it executes in a fork of Wasmer, a WebAssembly runtime. Because Wasmer compiles to native machine code, it executes faster than Geth's EVM bytecode interpreter. This performance difference is the source of reduced gas costs for compute-intensive operations.

EVM contracts continue to work exactly as before. When a contract is called, the system checks whether it's an EVM contract or a WASM program and routes it to the appropriate runtime. Solidity and WASM contracts can call each other, so the language a contract was written in doesn't affect interoperability.

Proving

Stylus builds on Nitro's fraud-proving technology. In normal operation, execution compiles to native code for speed. But if there's a dispute, the execution history compiles to WASM so validators can run interactive fraud proofs on Ethereum.

This is what makes Stylus possible: Nitro can already replay and verify disputes using WASM. Stylus extends this capability to verify not just execution history, but also the WASM programs you deploy. The result is a system where any program compiled to WASM can be deterministically fraud-proven. For more details, see the Nitro architecture documentation.

Use cases

Stylus can integrate into existing Solidity projects by calling a Stylus contract to optimize specific parts of your app, or you can build an entire app with Stylus. Developers can also port existing applications written in Rust, C, or C++ to run onchain with minimal modifications. Here are some use cases where Stylus may be a good fit:

  • Onchain verification with zero-knowledge proofs: Reduce gas costs for onchain verification using zero-knowledge proving systems. See case study for an example implementation.
  • DeFi instruments: Implement custom pricing curves for AMMs, synthetic assets, options, and futures with onchain computation. You can extend existing protocols (such as Uniswap V4 hooks) or build your own.
  • Memory and compute-intensive applications: Build onchain games, generative art, or other applications that benefit from reduced memory costs. You can write the entire application in Stylus or optimize specific parts of existing Solidity contracts.
  • Cryptographic applications: Implement applications that require advanced cryptography or other compute-heavy operations that would be cost-prohibitive in Solidity.

Getting started

  1. Follow the quickstart to deploy your first Stylus contract, and explore the Rust SDK documentation.
  2. Join the Stylus Developer Telegram group and Arbitrum Discord for community support.
  3. Browse the Awesome Stylus repository for community-contributed projects, examples, and tools.
  4. Subscribe to the Stylus Saturdays newsletter for tutorials and technical content.