Skip to content
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

Add Stylus script example to the template #25

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4ca0150
feat(scripts)!: add to example
reo101 Mar 18, 2024
805a573
fix(scripts)!: escape `scripts`' `Cargo` projects
reo101 Mar 18, 2024
51ebac7
feat(README): mention how to use `stylus` `script`
reo101 Mar 18, 2024
8afb9a0
fix(gitignore): correctly `.keep` secrets
reo101 Mar 18, 2024
09811b8
chore(scripts)!: better names for `common` exports
reo101 Mar 19, 2024
b41556d
feat(scripts)!: `.env` -> `Stylus.toml`
reo101 Mar 19, 2024
0ebc67d
feat(scripts)!: add `deploy_and_increment` script
reo101 Mar 19, 2024
e4a27a3
chore: leave only one script, fix typos, alter script
vikinatora Mar 19, 2024
d980b58
chore: improve private_key docs and remove secret logic from .gitignore
vikinatora Mar 19, 2024
0e9ab8b
chore: further improve docs and example
reo101 Mar 20, 2024
6ce69a1
Merge pull request #3 from LimeChain/LimeChain/scripts-vik
reo101 Mar 20, 2024
28d390c
chore(README): reflect on scripts' changes
reo101 Mar 20, 2024
d0d97cf
chore(README): remove old `--example` example
reo101 Mar 20, 2024
4735e54
chore(README): add a few more language annotations
reo101 Mar 21, 2024
6c42434
chore: address comments
reo101 Mar 21, 2024
0827db4
chore: `concrete` -> `specific`
reo101 Mar 21, 2024
89a8561
feat(scripts)!: use exported `common` module
reo101 Mar 22, 2024
5d14603
feat(scripts): bring back test
reo101 Mar 25, 2024
e431b28
feat(scripts): use updated `cargo_stylus`
reo101 Mar 26, 2024
a57066e
chore(cargo_stylus): update reference
reo101 Mar 26, 2024
6f08bc3
Merge branch 'OffchainLabs/main' into 'LimeChain/stylus-script-common…
reo101 Mar 26, 2024
ce6bcd6
Merge pull request #4 from LimeChain/LimeChain/stylus-script-common-e…
reo101 Mar 26, 2024
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# .env
/.env

# Contract artifacts
/target

# Scripts artifacts
/scripts/target
52 changes: 9 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract Counter {

To set up more minimal example that still uses the Stylus SDK, use `cargo stylus new --minimal <YOUR_PROJECT_NAME>` under [OffchainLabs/cargo-stylus](https://github.com/OffchainLabs/cargo-stylus).

## Quick Start
## Quick Start

Install [Rust](https://www.rust-lang.org/tools/install), and then install the Stylus CLI tool with Cargo

Expand All @@ -33,7 +33,7 @@ RUSTFLAGS="-C link-args=-rdynamic" cargo install --force cargo-stylus

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

```
```bash
rustup target add wasm32-unknown-unknown
```

Expand All @@ -45,7 +45,7 @@ cargo stylus --help

Then, clone the template:

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

Expand Down Expand Up @@ -142,48 +142,14 @@ 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](https://github.com/gakonst/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.**
## Deploying + Interacting with `scripts`

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:
You can instead opt-in to using `cargo stylus script` and run the included `deploy_and_increment` script at [`./scripts/deploy_and_increment/`](./scripts/deploy_and_increment/).

```rs
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=<the onchain address of your deployed program>
PRIV_KEY_PATH=<the file path for your priv key to transact with>
RPC_URL=https://stylus-testnet.arbitrum.io/rpc
```

Next, run:

```
cargo run --example counter --target=<YOUR_ARCHITECTURE>
```

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`
This can be done either by manually `cd`-ing into `./scripts/deploy_and_increment/` and running `cargo run`, or by running `cargo stylus script run deploy_and_increment` from anywhere in the repository.

> [!NOTE]
> This requires you to setup the credentials for `TESTNET`, see [`./Stylus.toml`](./Stylus.toml)
## 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](https://github.com/OffchainLabs/cargo-stylus). If you wish to optimize the size of your compiled WASM, see the different options available [here](https://github.com/OffchainLabs/cargo-stylus/blob/main/OPTIMIZING_BINARIES.md).
Expand All @@ -194,7 +160,7 @@ The [stylus-sdk](https://github.com/OffchainLabs/stylus-sdk-rs) contains many fe

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

```
```bash
cargo expand --all-features --release --target=<YOUR_ARCHITECTURE>
```

Expand Down
14 changes: 14 additions & 0 deletions Stylus.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Configuration file used by Stylus scripts

[networks.testnet]
private_key = ""
# Alternatively, you can pass a (absolute or relative) path to your PK
# private_key_path = "./secrets/pk"
rpc_url = "https://stylus-testnet.arbitrum.io/rpc"
# You can specify as many extra variables as you want, they are also brought in
increment_number = "4"

[networks.mainnet]
private_key = ""
# private_key_path = ""
rpc_url = "https://arb1.arbitrum.io/rpc"
73 changes: 0 additions & 73 deletions examples/counter.rs

This file was deleted.

Loading