Dispe(rs)e, basically disperse but more rusty

Overview

Dispe(rs)e

banner
Dispe(rs)e, basically disperse but more rusty

License: AGPL-3-0


Quick Start πŸ’¨

create environment files

cp .pk.example .pk && cp .env.example .env

deploy the contract

cargo stylus deploy --private-key-path=.pk

run the script

❯ source .env ; cargo run ./examples/counter.rs

New to Stylus πŸ‘‡πŸΎ

Install Rust, and then install the Stylus CLI tool with Cargo

RUSTFLAGS="-C link-args=-rdynamic" cargo install --force cargo-stylus

Add the wasm32-unknown-unknown build target to your Rust compiler:

rustup target add wasm32-unknown-unknown

You should now have it available as a Cargo subcommand:

cargo stylus --help

Then, clone the template:

git clone https://github.com/OffchainLabs/stylus-hello-world && cd stylus-hello-world

Testnet Information

All testnet information, including faucets and RPC endpoints can be found here.

ABI Export

You can export the Solidity ABI for your program by using the cargo stylus tool as follows:

cargo stylus export-abi

which outputs:

/**
 * This file was automatically generated by Stylus and represents a Rust program.
 * For more information, please see [The Stylus SDK](https://github.com/OffchainLabs/stylus-sdk-rs).
 */

interface Counter {
    function setNumber(uint256 new_number) external;

    function increment() external;
}

Exporting ABIs uses a feature that is enabled by default in your Cargo.toml:

[features]
export-abi = ["stylus-sdk/export-abi"]

Deploying

You can use the cargo stylus command to also deploy your program to the Stylus testnet. We can use the tool to first check our program compiles to valid WASM for Stylus and will succeed a deployment onchain without transacting. By default, this will use the Stylus testnet public RPC endpoint. See here for Stylus testnet information

cargo stylus check

If successful, you should see:

Finished release [optimized] target(s) in 1.88s
Reading WASM file at stylus-hello-world/target/wasm32-unknown-unknown/release/stylus-hello-world.wasm
Compressed WASM size: 8.9 KB
Program succeeded Stylus onchain activation checks with Stylus version: 1

Next, we can estimate the gas costs to deploy and activate our program before we send our transaction. Check out the cargo-stylus README to see the different wallet options for this step:

cargo stylus deploy \
  --private-key-path=<PRIVKEY_FILE_PATH> \
  --estimate-gas-only

You will then see the estimated gas cost for deploying before transacting:

Deploying program to address e43a32b54e48c7ec0d3d9ed2d628783c23d65020
Estimated gas for deployment: 1874876

The above only estimates gas for the deployment tx by default. To estimate gas for activation, first deploy your program using --mode=deploy-only, and then run cargo stylus deploy with the --estimate-gas-only flag, --mode=activate-only, and specify --activate-program-address.

Here's how to deploy:

cargo stylus deploy \
  --private-key-path=<PRIVKEY_FILE_PATH>

The CLI will send 2 transactions to deploy and activate your program onchain.

Compressed WASM size: 8.9 KB
Deploying program to address 0x457b1ba688e9854bdbed2f473f7510c476a3da09
Estimated gas: 1973450
Submitting tx...
Confirmed tx 0x42db…7311, gas used 1973450
Activating program at address 0x457b1ba688e9854bdbed2f473f7510c476a3da09
Estimated gas: 14044638
Submitting tx...
Confirmed tx 0x0bdb…3307, gas used 14044638

Once both steps are successful, you can interact with your program as you would with any Ethereum smart contract.

Calling Your Program

This template includes an example of how to call and transact with your program in Rust using ethers-rs under the examples/counter.rs. However, your programs are also Ethereum ABI equivalent if using the Stylus SDK. They can be called and transacted with using any other Ethereum tooling.

By using the program address from your deployment step above, and your wallet, you can attempt to call the counter program and increase its value in storage:

abigen!(
    Counter,
    r#"[
        function number() external view returns (uint256)
        function setNumber(uint256 number) external
        function increment() external
    ]"#
);
let counter = Counter::new(address, client);
let num = counter.number().call().await;
println!("Counter number value = {:?}", num);

let _ = counter.increment().send().await?.await?;
println!("Successfully incremented counter via a tx");

let num = counter.number().call().await;
println!("New counter number value = {:?}", num);

To run it, set the following env vars or place them in a .env file this project, then:

STYLUS_PROGRAM_ADDRESS=
PRIV_KEY_PATH=
RPC_URL=https://stylus-testnet.arbitrum.io/rpc

Next, run:

cargo run --example counter --target=

Where you can find YOUR_ARCHITECTURE by running rustc -vV | grep host. For M1 Apple computers, for example, this is aarch64-apple-darwin and for most Linux x86 it is x86_64-unknown-linux-gnu

Build Options

By default, the cargo stylus tool will build your project for WASM using sensible optimizations, but you can control how this gets compiled by seeing the full README for cargo stylus. If you wish to optimize the size of your compiled WASM, see the different options available here.

Peeking Under the Hood

The stylus-sdk contains many features for writing Stylus programs in Rust. It also provides helpful macros to make the experience for Solidity developers easier. These macros expand your code into pure Rust code that can then be compiled to WASM. If you want to see what the stylus-hello-world boilerplate expands into, you can use cargo expand to see the pure Rust code that will be deployed onchain.

First, run cargo install cargo-expand if you don't have the subcommand already, then:

cargo expand --all-features --release --target=

Where you can find YOUR_ARCHITECTURE by running rustc -vV | grep host. For M1 Apple computers, for example, this is aarch64-apple-darwin.

License

This project is fully open source, including an Apache-2.0 or MIT license at your choosing under your own copyright.

You might also like...
A more intuitive version of du in rust
A more intuitive version of du in rust

A more intuitive version of du in rust

πŸ¦€ Syncing dotfiles and more

DT DT allows you to sync/populate configuration files at will. It currently provides a CLI tool dt-cli. Usage The command line interface dt-cli takes

A more free Rust-Iterator.
A more free Rust-Iterator.

CURSOR A more free Rust-Iterator. | Examples | Docs | Latest Note | [dependencies] cursor = "1.0.0" Example use cursor::*; fn example7() - u8 {

A rust binary that will flip one or more bits of a file (mostly for messing with images for fun).
A rust binary that will flip one or more bits of a file (mostly for messing with images for fun).

file-bitflipper A rust binary that will flip one or more bits of a file (mostly for messing with images for fun). Example (bitflipped bentley) Usage $

A list of crates with snippets used by me to learn more about Rust.

my-rust-examples This is a list of crates used by me to learn Rust. How to execute You can use a dependency called cargo-play: cargo install cargo-pla

Tools - The Rome Toolchain. A linter, compiler, bundler, and more for JavaScript, TypeScript, HTML, Markdown, and CSS.
Tools - The Rome Toolchain. A linter, compiler, bundler, and more for JavaScript, TypeScript, HTML, Markdown, and CSS.

Rome is currently being rewritten in Rust. Read more about it in our latest blog post. The documentation below is out of date and available for poster

βš™οΈ A curated list of static analysis (SAST) tools for all programming languages, config files, build tools, and more.
βš™οΈ A curated list of static analysis (SAST) tools for all programming languages, config files, build tools, and more.

This repository lists static analysis tools for all programming languages, build tools, config files and more. The official website, analysis-tools.de

🧠 A command-line utility for switching git branches more easily. Switch branches interactively or use a fuzzy search to find that long-forgotten branch name.
🧠 A command-line utility for switching git branches more easily. Switch branches interactively or use a fuzzy search to find that long-forgotten branch name.

git-smart-checkout A git command extension for switching git branches more efficiently. About Interactively switch branches or fuzzy search for that f

Library to write x64 Assembly code from Rust, more properly. Designed for the nasm assembler

x64asm Library to write x64 Assembly code from Rust, more properly. Designed for the nasm assembler How to use let mut f = Formatter::new(false); // f

Owner
Aaron Abu Usama
πŸ‘¨πŸ½β€πŸ’»
Aaron Abu Usama
A reasonable web framework. Others are faster, but this is likely to be more economical.

intercity A web framework for people wanting to build microservices good and build other stuff good too. Intercity is a reasonable web framework. Othe

Tim McNamara 3 Nov 2, 2023
rusty-donut - ASCII raymarching inside a terminal

ASCII raymarching inside a terminal

drip 14 Feb 9, 2022
⌚ A command-line tool (and library) for the rusty Swatch Internet Time.

⌚ A command-line tool (and library) for the rusty Swatch Internet Time. Comes with XBar/Swiftbar support.

Gil Desmarais 4 Jul 18, 2022
πŸŽ„ My Rusty Solutions to AOC 2021

?? Advent-Of-Code-2021 My Rusty Solutions to Advent Of Code 2021 Day 01: Sonar Sweep [A, B] Day 02: Dive! [A, B] Day 03: Binary Diagnostic [A, B] Day

Connor Slade 13 Dec 22, 2022
My Rusty Solutions For Advent of Code

My Rusty Solutions For Advent of Code Name Part 1 Part 2 Language Day 1 Snack Calorie Counting βœ… βœ… Rust Day 2 Rock Paper Scissors βœ… βœ… Rust Day 3 Food

Abhijit Biswas 2 Dec 18, 2022
Rusty fast cross-platform 2D drawing library

Bly Rusty fast cross-platform 2D graphics library Concept Easy to use Bly is easy to use and yet can be called from various windowing libraries using

null 4 Feb 27, 2023
Rusty Ekko - Sleep Obfuscation in Rust

Ekko in Rust A small sleep obfuscation technique that uses the CreateTimerQueueTimer Win32 API function ported from C https://github.com/Cracked5pider

null 116 Apr 10, 2023
Rusty Shellcode Reflective DLL Injection (sRDI) - A small reflective loader in Rust 4KB in size for generating position-independent code (PIC) in Rust.

Shellcode Reflective DLL Injection (sRDI) Shellcode reflective DLL injection (sRDI) is a process injection technique that allows us to convert a given

null 242 Jul 5, 2023
REC2 (Rusty External Command and Control) is client and server tool allowing auditor to execute command from VirusTotal and Mastodon APIs written in Rust. πŸ¦€

Information: REC2 is an old personal project (early 2023) that I didn't continue development on. It's part of a list of projects that helped me to lea

Quentin Texier (g0h4n) 104 Oct 7, 2023
Warp is a blazingly fast, Rust-based terminal that makes you and your team more productive at running, debugging, and deploying code and infrastructure.

Warp is a blazingly fast, Rust-based terminal that makes you and your team more productive at running, debugging, and deploying code and infrastructure.

Warp 10.4k Jan 4, 2023