How to add a new programming language to Stylus
Arbitrum Stylus is a new technology developed for Arbitrum chains which gives smart contract developers superpowers. With Stylus, developers can write EVM-compatible smart contracts in many different programming languages, and reap massive performance gains. Stylus slashes fees, with performance gains ranging from 10-70x, and memory efficiency gains as high as 100-500x.
This is possible thanks to WebAssembly technology, which all Stylus contracts compile to. Stylus smart contracts live under the same Ethereum state trie in Arbitrum nodes, and can fully interoperate with Solidity or Vyper EVM smart contracts. With Stylus, developers can write smart contracts in Rust that talk to Solidity and vice versa without any limitations.
Today, the Stylus testnet also comes with 2 officially supported SDKs for developers to write contracts in the Rust or C programming languages.
However, anyone can add support for new languages in Stylus. As long as a programming language can compile to WebAssembly, Stylus will let you use it to write EVM-compatible smart contracts. Note that in order to be deployed onchain, your compiled program must fit under the 24Kb brotli-compressed limit, and should meet Stylus gas metering requirements.
In this document, we go over how we added support for the up-and-coming Zig programming language, which is meant to be a spiritual successor to C that comes with great performance and memory safety within 20 lines of code.
Why Zig?
- Zig contains memory safety guardrails, requiring developers to think hard about manual memory allocation in a prudent manner
- Zig is a C equivalent language, and its tooling is also a C compiler. This means C projects can incrementally adopt Zig when refactoring
- Zig is lightning fast and produces small binaries, making it suitable for blockchain applications
Programs written in Zig and deployed to Stylus have a tiny footprint and will have gas costs comparable, if not equal to, C programs.
Requirements
- Download and install Zig 0.11.0
- Install Rust, which we'll need for the Stylus CLI tool to deploy our program to the Stylus testnet
We'll also be using Rust to run an example script that can call our Zig contract on the Stylus testnet using the popular ethers-rs library.
Once Rust is installed, also install the Stylus CLI tool with
RUSTFLAGS="-C link-args=-rdynamic" cargo install --force cargo-stylus
Using Zig with Stylus
First, let's clone the repository:
git clone https://github.com/offchainlabs/zig-on-stylus && cd zig-on-stylus