Writing the Simple Storage Contract
In this section, we will write a simple storage contract to demonstrate how to read from and write to storage in Arbitrum Stylus.
Create a New Project
Use the cargo-stylus
command to create a new project:
Edit the Contract Code
Open the src/lib.rs
file and replace the content with the following code:
Explanation:
Allocator:
Using
mini_alloc
for an efficient WASM allocator.
Imports:
Importing necessary items from the Stylus SDK, including
U256
fromalloy_primitives
and common traits and macros from theprelude
.
Storage Definition:
Using the
sol_storage!
macro to define theStorage
struct with a singleuint256
field.
External Methods:
get_value
: Returns the current value from storage.set_value
: Sets the storage value to a new user-specified value.mul_value
: Multiplies the current storage value by a user-specified value.add_value
: Adds a user-specified value to the current storage value.increment
: Increments the current storage value by 1.
Deploying and Testing the Contract
Check the Contract:
Deploy the Contract:
Ensure you have a valid private key saved in a file (e.g.,
private_key.txt
). The file should contain only the private key without any extra characters or spaces.
Last updated