Skip to content

Commit

Permalink
Merge branch 'stylus' into call-flush
Browse files Browse the repository at this point in the history
  • Loading branch information
rachel-bousfield authored Sep 1, 2023
2 parents 1131d75 + 109b6c7 commit eea8c0d
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 70 deletions.
5 changes: 5 additions & 0 deletions .clabot
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"contributors": "https://api.github.com/repos/OffchainLabs/clabot-config/contents/nitro-contributors.json",
"message": "We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please sign the linked documents below to get yourself added. https://na3.docusign.net/Member/PowerFormSigning.aspx?PowerFormId=b15c81cc-b5ea-42a6-9107-3992526f2898&env=na3&acct=6e152afc-6284-44af-a4c1-d8ef291db402&v=2",
"label": "s"
}
43 changes: 43 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
on:
push:
branches: [stylus]
pull_request:

name: check
jobs:
fmt:
runs-on: ubuntu-latest
name: stable / fmt
steps:
- uses: actions/checkout@v3
with:
submodules: true
- name: Install stable
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: cargo fmt --check
uses: actions-rs/cargo@v1
with:
command: fmt
args: --check
clippy:
runs-on: ubuntu-latest
name: ${{ matrix.toolchain }} / clippy
strategy:
fail-fast: false
matrix:
toolchain: [stable, beta]
steps:
- uses: actions/checkout@v3
with:
submodules: true
- name: Install ${{ matrix.toolchain }}
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain }}
components: clippy
- name: cargo clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
33 changes: 33 additions & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: linux
on:
push:
branches:
- stylus
pull_request:

jobs:
test:
runs-on: ubuntu-latest
name: (${{ matrix.target }}, ${{ matrix.cfg_release_channel }})
env:
CFG_RELEASE_CHANNEL: ${{ matrix.cfg_release_channel }}
strategy:
matrix:
target: [
x86_64-unknown-linux-gnu,
]
cfg_release_channel: [nightly, stable]

steps:
- name: checkout
uses: actions/checkout@v3

# Run build
- name: install rustup
run: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup-init.sh
sh rustup-init.sh -y --default-toolchain none
rustup target add ${{ matrix.target }}
- name: Build and Test
run: ./ci/build_and_test.sh
34 changes: 34 additions & 0 deletions .github/workflows/mac.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: mac
on:
push:
branches:
- stylus
pull_request:

jobs:
test:
runs-on: macos-latest
name: (${{ matrix.target }}, ${{ matrix.cfg_release_channel }})
env:
CFG_RELEASE_CHANNEL: ${{ matrix.cfg_release_channel }}
strategy:
fail-fast: false
matrix:
target: [
aarch64-apple-darwin,
]
cfg_release_channel: [nightly, stable]

steps:
- name: checkout
uses: actions/checkout@v3

# Run build
- name: install rustup
run: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup-init.sh
sh rustup-init.sh -y --default-toolchain none
rustup target add ${{ matrix.target }}
- name: Build and Test
run: ./ci/build_and_test.sh
24 changes: 21 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["stylus-sdk", "stylus-proc"]
resolver = "2"

[workspace.package]
version = "0.1.0"
version = "0.1.2"
edition = "2021"
authors = ["Offchain Labs"]
license = "MIT OR Apache-2.0"
Expand Down
39 changes: 32 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,68 @@
<br />
<p align="center">
<a href="https://arbitrum.io/">
<img src="https://thereisatoken.com/media/stylus-logo.svg" alt="Logo" width="100%" height="80">
<img src="https://arbitrum.io/assets/stylus/stylus_with_paint_bg.png" alt="Logo" width="100%">
</a>

<h3 align="center">The Stylus SDK</h3>

<p align="center">
<a href="https://developer.arbitrum.io/"><strong>Rust programs on Arbitrum »</strong></a>
<a href="https://developer.arbitrum.io/"><strong>Rust contracts on Arbitrum »</strong></a>
<br />
</p>
</p>

## Overview

The Stylus SDK enables smart contract developers to write programs for **Arbitrum chains** written in the [Rust](https://www.rust-lang.org/tools/install) programming language. Stylus programs are compiled to [WebAssembly](https://webassembly.org/) and can then deployed onchain to be executed alongside Solidity smart contracts. Stylus programs are not only orders of magnitude cheaper and faster but also enable what was thought to be previously impossible for WebAssembly: **EVM-interoperability**.
The Stylus SDK enables smart contract developers to write programs for **Arbitrum chains** written in the [Rust](https://www.rust-lang.org/tools/install) programming language. Stylus programs are compiled to [WebAssembly](https://webassembly.org/) and can then be deployed on-chain to execute alongside Solidity smart contracts. Stylus programs are not only orders of magnitude cheaper and faster but also enable what was thought to be previously impossible for WebAssembly: **EVM-interoperability**.

For information about deploying Rust smart contracts, see the [Cargo Stylus CLI Tool](CargoStylus). For more information about Stylus, see [Stylus: A Gentle Introduction](https://docs.arbitrum.io/stylus/stylus-gentle-introduction). For a simpler intro to Stylus Rust development, see the [Quick Start guide](https://docs.arbitrum.io/stylus/stylus-quickstart).

Comprehensive documentation on the Rust SDK can be found [here](https://docs.arbitrum.io/stylus/rust-sdk-guide).

## Feature highlights

The SDK makes it easy to develop Ethereum ABI-equivalent Stylus contracts in Rust. It provides a full suite of types and shortcuts that abstract away the details of Ethereum's storage layout, making it easy to _just write Rust_.
The SDK makes it easy to develop Ethereum ABI-equivalent Stylus contracts in Rust. It provides a full suite of types and shortcuts that abstract away the details of Ethereum's storage layout, making it easy to _just write Rust_. For an in depth exploration of the features, please see comprehensive [Feature Overview][overview].

Some of the features available in the SDK include, but are not limited to:
Some of the features available in the SDK include:

- **Generic**, storage-backed Rust types for programming **Ethereum-equivalent** smart contracts
- Simple macros for writing Solidity structs and **entrypoints** that get converted to SDK-types internally
- Powerful **primitive types** backed by the feature-rich [alloy-rs/crate](https://github.com/alloy-rs/core)

Rust programs implemented with the Stylus SDK can **call and be called** by Solidity smart contracts through Ethereum ABIs and also share the same storage layout.

![Image](example.png)
```rust
use stylus_sdk::{alloy_primitives::U256, prelude::*};

// Generate Solidity-equivalent, Rust structs backed by storage.
sol_storage! {
#[entrypoint]
pub struct Counter {
uint256 number;
}
}

#[external]
impl Counter {
// Gets the number value from storage.
pub fn number(&self) -> Result<U256, Vec<u8>> {
Ok(self.number.get())
}
// Sets a number in storage to a user-specified value.
pub fn set_number(&mut self, new_number: U256) -> Result<(), Vec<u8>> {
self.number.set(new_number);
Ok(())
}
}
```

Additionally, the Stylus SDK supports `#[no_std]` for contracts that wish to opt out of the standard library. In fact, the entire SDK is available from `#[no_std]`, so no special feature flag is required. This can be helpful for reducing binary size, and may be preferable in pure-compute use cases like cryptography.

Most users will want to use the standard library, which is available since the Stylus VM supports `rustc`'s `wasm32-unknown-unknown` target triple. In the future we may add `wasm32-wasi` too, along with floating point and SIMD, which the Stylus VM does not yet support.

[overview]: https://docs.arbitrum.io/stylus/reference/rust-sdk-guide

## Don't know Rust?

The Stylus VM supports more than just Rust. In fact, any programming language that compiles down to WebAssembly could in principle be deployed to Stylus-enabled chains. The table below includes the official ports of the SDK, with more coming soon.
Expand All @@ -59,7 +84,7 @@ Want to write your own? [Join us in the `#stylus` channel on discord][discord]!

## Developing Stylus Programs

The Stylus SDK is just one of the building blocks in creating and deploying WebAssebmly programs to Arbitrum chains. To create a new Stylus project from a hello-world example and deploy it onchain, check out some of our tools below:
The Stylus SDK is just one of the building blocks in creating and deploying WebAssembly programs to Arbitrum chains. To create a new Stylus project from a hello-world example and deploy it onchain, check out some of our tools below:

| Repo | Use cases | License |
|:-----------------|:----------------------------|:------------------|
Expand Down
18 changes: 18 additions & 0 deletions ci/build_and_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -euo pipefail

export RUSTFLAGS="-D warnings"
export RUSTFMT_CI=1

# Print version information
rustc -Vv
cargo -V

# Build and test main crate
if [ "$CFG_RELEASE_CHANNEL" == "nightly" ]; then
cargo build --locked --all-features
else
cargo build --locked
fi
cargo test --all-features
Binary file removed example.png
Binary file not shown.
7 changes: 7 additions & 0 deletions examples/erc20/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build]
target = "wasm32-unknown-unknown"

[target.wasm32-unknown-unknown]
rustflags = [
"-C", "link-arg=-zstack-size=32768",
]
4 changes: 2 additions & 2 deletions examples/erc20/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ sol_interface! {
#[inherit(Erc20<WethParams>)]
impl Weth {
#[payable]
pub fn mint(&mut self) -> Result<(), Vec<u8>> {
pub fn deposit(&mut self) -> Result<(), Vec<u8>> {
self.erc20.mint(msg::sender(), msg::value());
Ok(())
}

pub fn burn(&mut self, amount: U256) -> Result<(), Vec<u8>> {
pub fn withdraw(&mut self, amount: U256) -> Result<(), Vec<u8>> {
self.erc20.burn(msg::sender(), amount)?;

// send the user their funds
Expand Down
4 changes: 3 additions & 1 deletion stylus-proc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "stylus-proc"
keywords = ["arbitrum", "ethereum"]
keywords = ["arbitrum", "ethereum", "stylus", "alloy"]
description = "Procedural macros for stylus-sdk"
readme = "../README.md"

authors.workspace = true
edition.workspace = true
Expand Down
5 changes: 4 additions & 1 deletion stylus-proc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ pub fn solidity_type_info(ty: &Type) -> (Cow<'static, str>, Cow<'static, str>) {
Type::Address(_, _) => simple!(Address),
Type::String(_) => simple!(String),
Type::Bytes(_) => simple!(Bytes),
Type::FixedBytes(_, size) => (path!("FixedBytes<{size}>"), abi!("bytes[{size}]")),
Type::FixedBytes(_, size) => (
"stylus_sdk::abi::FixedBytesSolType<{size}>".into(),
abi!("bytes[{size}]"),
),
Type::Uint(_, size) => {
let size = size.unwrap_or(NonZeroU16::new(256).unwrap());
(path!("Uint<{size}>"), abi!("uint{size}"))
Expand Down
6 changes: 4 additions & 2 deletions stylus-sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "stylus-sdk"
keywords = ["arbitrum", "ethereum"]
keywords = ["arbitrum", "ethereum", "stylus", "alloy"]
description = "Rust smart contracts with Arbitrum Stylus"
readme = "../README.md"

authors.workspace = true
edition.workspace = true
Expand All @@ -21,7 +23,7 @@ regex = { workspace = true, optional = true }
fnv.workspace = true

# local deps
stylus-proc.workspace = true
stylus-proc = "0.1.2"

[dev-dependencies]
paste.workspace = true
Expand Down
Loading

0 comments on commit eea8c0d

Please sign in to comment.