create-stylus 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/templates/base/.gitignore.template.mjs +2 -2
- package/templates/base/dist/cli.js +683 -0
- package/templates/base/dist/cli.js.map +1 -0
- package/templates/base/package.json +2 -1
- package/templates/base/packages/nextjs/.gitignore.template.mjs +3 -3
- package/templates/base/packages/nextjs/scaffold.config.ts +2 -2
- package/templates/base/packages/stylus/.env.example +5 -2
- package/templates/base/packages/stylus/.gitignore.template.mjs +3 -3
- package/templates/base/packages/stylus/package.json +0 -1
- package/templates/base/packages/stylus/scripts/deploy.ts +29 -12
- package/templates/base/packages/stylus/scripts/deploy_contract.ts +34 -43
- package/templates/base/packages/stylus/scripts/deploy_wrapper.ts +4 -44
- package/templates/base/packages/stylus/scripts/export_abi.ts +13 -13
- package/templates/base/packages/stylus/scripts/utils/command.ts +18 -31
- package/templates/base/packages/stylus/scripts/utils/contract.ts +23 -14
- package/templates/base/packages/stylus/scripts/utils/deployment.ts +169 -45
- package/templates/base/packages/stylus/scripts/utils/network.ts +27 -7
- package/templates/base/packages/stylus/scripts/utils/type.ts +13 -10
- package/templates/base/packages/stylus/your-contract/Cargo.lock +35 -18
- package/templates/base/packages/stylus/your-contract/Cargo.toml +3 -1
- package/templates/base/packages/stylus/your-contract/src/lib.rs +51 -21
- package/templates/base/readme.md +207 -42
- package/templates/base/yarn.lock +1 -2
- package/templates/base/packages/stylus/README.md +0 -263
- package/templates/base/packages/stylus/header.png +0 -0
- package/templates/base/packages/stylus/scripts/deploy_all_contracts.ts +0 -59
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-

|
|
2
|
-
|
|
3
|
-
# Stylus Hello World
|
|
4
|
-
|
|
5
|
-
Project starter template for writing Arbitrum Stylus programs in Rust using the [stylus-sdk](https://github.com/OffchainLabs/stylus-sdk-rs). It includes a Rust implementation of a basic counter Ethereum smart contract:
|
|
6
|
-
|
|
7
|
-
```js
|
|
8
|
-
// SPDX-License-Identifier: UNLICENSED
|
|
9
|
-
pragma solidity ^0.8.13;
|
|
10
|
-
|
|
11
|
-
contract Counter {
|
|
12
|
-
uint256 public number;
|
|
13
|
-
|
|
14
|
-
function setNumber(uint256 newNumber) public {
|
|
15
|
-
number = newNumber;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function increment() public {
|
|
19
|
-
number++;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
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).
|
|
25
|
-
|
|
26
|
-
## Quick Start
|
|
27
|
-
|
|
28
|
-
Install [Rust](https://www.rust-lang.org/tools/install), and then install the Stylus CLI tool with Cargo
|
|
29
|
-
|
|
30
|
-
```bash
|
|
31
|
-
cargo install --force cargo-stylus cargo-stylus-check
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
Add the `wasm32-unknown-unknown` build target to your Rust compiler:
|
|
35
|
-
|
|
36
|
-
```
|
|
37
|
-
rustup target add wasm32-unknown-unknown
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
You should now have it available as a Cargo subcommand:
|
|
41
|
-
|
|
42
|
-
```bash
|
|
43
|
-
cargo stylus --help
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
Then, clone the template:
|
|
47
|
-
|
|
48
|
-
```
|
|
49
|
-
git clone https://github.com/OffchainLabs/stylus-hello-world && cd stylus-hello-world
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
### Testnet Information
|
|
53
|
-
|
|
54
|
-
All testnet information, including faucets and RPC endpoints can be found [here](https://docs.arbitrum.io/stylus/reference/testnet-information).
|
|
55
|
-
|
|
56
|
-
### ABI Export
|
|
57
|
-
|
|
58
|
-
You can export the Solidity ABI for your program by using the `cargo stylus` tool as follows:
|
|
59
|
-
|
|
60
|
-
```bash
|
|
61
|
-
cargo stylus export-abi
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
which outputs:
|
|
65
|
-
|
|
66
|
-
```js
|
|
67
|
-
/**
|
|
68
|
-
* This file was automatically generated by Stylus and represents a Rust program.
|
|
69
|
-
* For more information, please see [The Stylus SDK](https://github.com/OffchainLabs/stylus-sdk-rs).
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
|
-
// SPDX-License-Identifier: MIT-OR-APACHE-2.0
|
|
73
|
-
pragma solidity ^0.8.23;
|
|
74
|
-
|
|
75
|
-
interface ICounter {
|
|
76
|
-
function number() external view returns (uint256);
|
|
77
|
-
|
|
78
|
-
function setNumber(uint256 new_number) external;
|
|
79
|
-
|
|
80
|
-
function mulNumber(uint256 new_number) external;
|
|
81
|
-
|
|
82
|
-
function addNumber(uint256 new_number) external;
|
|
83
|
-
|
|
84
|
-
function increment() external;
|
|
85
|
-
}
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
Exporting ABIs uses a feature that is enabled by default in your Cargo.toml:
|
|
89
|
-
|
|
90
|
-
```toml
|
|
91
|
-
[features]
|
|
92
|
-
export-abi = ["stylus-sdk/export-abi"]
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## Deploying
|
|
96
|
-
|
|
97
|
-
You can use the `cargo stylus` command to also deploy your program to the Stylus testnet. We can use the tool to first check
|
|
98
|
-
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](https://docs.arbitrum.io/stylus/reference/testnet-information)
|
|
99
|
-
|
|
100
|
-
```bash
|
|
101
|
-
cargo stylus check
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
If successful, you should see:
|
|
105
|
-
|
|
106
|
-
```bash
|
|
107
|
-
Finished release [optimized] target(s) in 1.88s
|
|
108
|
-
Reading WASM file at stylus-hello-world/target/wasm32-unknown-unknown/release/stylus-hello-world.wasm
|
|
109
|
-
Compressed WASM size: 8.9 KB
|
|
110
|
-
Program succeeded Stylus onchain activation checks with Stylus version: 1
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
Next, we can estimate the gas costs to deploy and activate our program before we send our transaction. Check out the [cargo-stylus](https://github.com/OffchainLabs/cargo-stylus) README to see the different wallet options for this step:
|
|
114
|
-
|
|
115
|
-
```bash
|
|
116
|
-
cargo stylus deploy \
|
|
117
|
-
--private-key-path=<PRIVKEY_FILE_PATH> \
|
|
118
|
-
--estimate-gas
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
You will then see the estimated gas cost for deploying before transacting:
|
|
122
|
-
|
|
123
|
-
```bash
|
|
124
|
-
Deploying program to address e43a32b54e48c7ec0d3d9ed2d628783c23d65020
|
|
125
|
-
Estimated gas for deployment: 1874876
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
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` flag, `--mode=activate-only`, and specify `--activate-program-address`.
|
|
129
|
-
|
|
130
|
-
Here's how to deploy:
|
|
131
|
-
|
|
132
|
-
```bash
|
|
133
|
-
cargo stylus deploy \
|
|
134
|
-
--private-key-path=<PRIVKEY_FILE_PATH>
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
The CLI will send 2 transactions to deploy and activate your program onchain.
|
|
138
|
-
|
|
139
|
-
```bash
|
|
140
|
-
Compressed WASM size: 8.9 KB
|
|
141
|
-
Deploying program to address 0x457b1ba688e9854bdbed2f473f7510c476a3da09
|
|
142
|
-
Estimated gas: 1973450
|
|
143
|
-
Submitting tx...
|
|
144
|
-
Confirmed tx 0x42db…7311, gas used 1973450
|
|
145
|
-
Activating program at address 0x457b1ba688e9854bdbed2f473f7510c476a3da09
|
|
146
|
-
Estimated gas: 14044638
|
|
147
|
-
Submitting tx...
|
|
148
|
-
Confirmed tx 0x0bdb…3307, gas used 14044638
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
Once both steps are successful, you can interact with your program as you would with any Ethereum smart contract.
|
|
152
|
-
|
|
153
|
-
## Network Configuration
|
|
154
|
-
|
|
155
|
-
This template includes enhanced deployment scripts that support automatic RPC URL resolution from viem's chain definitions. You can now specify a network name instead of manually providing RPC URLs.
|
|
156
|
-
|
|
157
|
-
### Using Network Names
|
|
158
|
-
|
|
159
|
-
Instead of setting `RPC_URL` in your environment, you can use the `NETWORK` environment variable:
|
|
160
|
-
|
|
161
|
-
```bash
|
|
162
|
-
# Deploy to Arbitrum One (mainnet)
|
|
163
|
-
NETWORK=arbitrum yarn deploy
|
|
164
|
-
NETWORK=mainnet yarn deploy # alias for arbitrum
|
|
165
|
-
|
|
166
|
-
# Deploy to Arbitrum Sepolia testnet
|
|
167
|
-
NETWORK=arbitrumSepolia yarn deploy
|
|
168
|
-
NETWORK=testnet yarn deploy # alias for arbitrumSepolia
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
### Available Networks
|
|
172
|
-
|
|
173
|
-
This template supports Arbitrum networks only. You can test which networks are available and their RPC URLs:
|
|
174
|
-
|
|
175
|
-
```bash
|
|
176
|
-
yarn test:networks
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
This will show you all supported networks and their corresponding RPC endpoints.
|
|
180
|
-
|
|
181
|
-
### Fallback Behavior
|
|
182
|
-
|
|
183
|
-
If a network name is not supported (only `arbitrum`, `arbitrumSepolia`, `mainnet`, and `testnet` are supported), the system will:
|
|
184
|
-
|
|
185
|
-
1. Show a warning message indicating the network isn't supported
|
|
186
|
-
2. Fall back to the `RPC_URL` environment variable (or default to `http://localhost:8547`)
|
|
187
|
-
3. Continue with deployment using the fallback endpoint
|
|
188
|
-
|
|
189
|
-
### Environment Variables
|
|
190
|
-
|
|
191
|
-
You can still use the traditional approach with `RPC_URL`:
|
|
192
|
-
|
|
193
|
-
```bash
|
|
194
|
-
RPC_URL=https://your-custom-rpc.com yarn deploy
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
Or combine both (network takes precedence):
|
|
198
|
-
|
|
199
|
-
```bash
|
|
200
|
-
NETWORK=mainnet RPC_URL=https://fallback-rpc.com yarn deploy
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
## Calling Your Program
|
|
204
|
-
|
|
205
|
-
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.**
|
|
206
|
-
|
|
207
|
-
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:
|
|
208
|
-
|
|
209
|
-
```rs
|
|
210
|
-
abigen!(
|
|
211
|
-
Counter,
|
|
212
|
-
r#"[
|
|
213
|
-
function number() external view returns (uint256)
|
|
214
|
-
function setNumber(uint256 number) external
|
|
215
|
-
function increment() external
|
|
216
|
-
]"#
|
|
217
|
-
);
|
|
218
|
-
let counter = Counter::new(address, client);
|
|
219
|
-
let num = counter.number().call().await;
|
|
220
|
-
println!("Counter number value = {:?}", num);
|
|
221
|
-
|
|
222
|
-
let _ = counter.increment().send().await?.await?;
|
|
223
|
-
println!("Successfully incremented counter via a tx");
|
|
224
|
-
|
|
225
|
-
let num = counter.number().call().await;
|
|
226
|
-
println!("New counter number value = {:?}", num);
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
Before running, set the following env vars or place them in a `.env` file (see: [.env.example](./.env.example)) in this project:
|
|
230
|
-
|
|
231
|
-
```
|
|
232
|
-
RPC_URL=https://sepolia-rollup.arbitrum.io/rpc
|
|
233
|
-
STYLUS_CONTRACT_ADDRESS=<the onchain address of your deployed program>
|
|
234
|
-
PRIV_KEY_PATH=<the file path for your priv key to transact with>
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
Next, run:
|
|
238
|
-
|
|
239
|
-
```
|
|
240
|
-
cargo run --example counter --target=<YOUR_ARCHITECTURE>
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
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`
|
|
244
|
-
|
|
245
|
-
## Build Options
|
|
246
|
-
|
|
247
|
-
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).
|
|
248
|
-
|
|
249
|
-
## Peeking Under the Hood
|
|
250
|
-
|
|
251
|
-
The [stylus-sdk](https://github.com/OffchainLabs/stylus-sdk-rs) 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.
|
|
252
|
-
|
|
253
|
-
First, run `cargo install cargo-expand` if you don't have the subcommand already, then:
|
|
254
|
-
|
|
255
|
-
```
|
|
256
|
-
cargo expand --all-features --release --target=<YOUR_ARCHITECTURE>
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
Where you can find `YOUR_ARCHITECTURE` by running `rustc -vV | grep host`. For M1 Apple computers, for example, this is `aarch64-apple-darwin`.
|
|
260
|
-
|
|
261
|
-
## License
|
|
262
|
-
|
|
263
|
-
This project is fully open source, including an Apache-2.0 or MIT license at your choosing under your own copyright.
|
|
Binary file
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import * as path from "path";
|
|
3
|
-
import deployStylusContract from "./deploy_contract";
|
|
4
|
-
import {
|
|
5
|
-
clearDeploymentDir,
|
|
6
|
-
ensureDeploymentDirectory,
|
|
7
|
-
getDeploymentConfig,
|
|
8
|
-
isContractFolder,
|
|
9
|
-
printDeployedAddresses,
|
|
10
|
-
} from "./utils/";
|
|
11
|
-
import { DeployOptions } from "./utils/type";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Deploy all contracts in the stylus folder
|
|
15
|
-
* @param deployOptions - The deploy options
|
|
16
|
-
* @returns void
|
|
17
|
-
*/
|
|
18
|
-
export default async function deployAllStylusContracts(
|
|
19
|
-
deployOptions: DeployOptions,
|
|
20
|
-
) {
|
|
21
|
-
const contractsRoot = ".";
|
|
22
|
-
const entries = fs.readdirSync(contractsRoot, { withFileTypes: true });
|
|
23
|
-
|
|
24
|
-
const config = getDeploymentConfig(deployOptions);
|
|
25
|
-
clearDeploymentDir();
|
|
26
|
-
ensureDeploymentDirectory(config.deploymentDir);
|
|
27
|
-
|
|
28
|
-
console.log(`📄 Deploying all contracts`);
|
|
29
|
-
console.log(`📡 Using endpoint: ${config.chain?.rpcUrl}`);
|
|
30
|
-
if (config.chain) {
|
|
31
|
-
console.log(`🌐 Network specified: ${config.chain?.name}`);
|
|
32
|
-
}
|
|
33
|
-
console.log(`🔑 Using private key: ${config.privateKey.substring(0, 10)}...`);
|
|
34
|
-
console.log(`📁 Deployment directory: ${config.deploymentDir}`);
|
|
35
|
-
|
|
36
|
-
for (const entry of entries) {
|
|
37
|
-
if (!entry.isDirectory()) continue;
|
|
38
|
-
const contractFolder = path.join(contractsRoot, entry.name);
|
|
39
|
-
if (isContractFolder(contractFolder)) {
|
|
40
|
-
try {
|
|
41
|
-
await deployStylusContract(
|
|
42
|
-
{
|
|
43
|
-
contract: entry.name,
|
|
44
|
-
...deployOptions,
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
isSingleCommand: false,
|
|
48
|
-
},
|
|
49
|
-
);
|
|
50
|
-
} catch (e) {
|
|
51
|
-
console.error(`❌ Failed to deploy contract in: ${contractFolder}`);
|
|
52
|
-
console.error(e);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
console.log("\n\n");
|
|
58
|
-
printDeployedAddresses(config.deploymentDir);
|
|
59
|
-
}
|