From 2d7b5d2e8dd7962eb4c6062af8a7edb377e441ad Mon Sep 17 00:00:00 2001 From: Jason-Wanxt Date: Mon, 6 Jan 2025 15:01:46 +0800 Subject: [PATCH 1/2] add string varbibles --- example_code/basic_examples/variables/src/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/example_code/basic_examples/variables/src/lib.rs b/example_code/basic_examples/variables/src/lib.rs index e6b882e..996b273 100644 --- a/example_code/basic_examples/variables/src/lib.rs +++ b/example_code/basic_examples/variables/src/lib.rs @@ -4,7 +4,7 @@ extern crate alloc; use stylus_sdk::alloy_primitives::{U16, U256}; use stylus_sdk::prelude::*; -use stylus_sdk::storage::{StorageAddress, StorageBool, StorageU256}; +use stylus_sdk::storage::{StorageAddress, StorageBool, StorageU256, StorageString}; use stylus_sdk::{block, console, msg}; #[storage] @@ -13,6 +13,7 @@ pub struct Contract { initialized: StorageBool, owner: StorageAddress, max_supply: StorageU256, + base_uri: StorageString, } #[public] @@ -32,6 +33,10 @@ impl Contract { self.owner.set(msg::sender()); self.max_supply.set(U256::from(10_000)); + // We set the base URI for the contract + // Unlike other variables, string needs to use .set_str() to set the value + self.base_uri.set_str("https://stylus-by-example.org/".to_string()); + Ok(()) } @@ -51,4 +56,9 @@ impl Contract { Ok(()) } + + pub fn get_base_uri(&self) -> Result> { + // Unlike other variables, string needs to use .get_string() to get the value + Ok(self.base_uri.get_string()) + } } From 74a9652e3dc44e0295bdd3ae52fa71d6b2e1f039 Mon Sep 17 00:00:00 2001 From: Jason-Wanxt Date: Mon, 6 Jan 2025 15:16:14 +0800 Subject: [PATCH 2/2] reset --- example_code/basic_examples/variables/src/lib.rs | 9 +++------ src/app/basic_examples/variables/page.mdx | 11 +++++++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/example_code/basic_examples/variables/src/lib.rs b/example_code/basic_examples/variables/src/lib.rs index 996b273..06fe029 100644 --- a/example_code/basic_examples/variables/src/lib.rs +++ b/example_code/basic_examples/variables/src/lib.rs @@ -40,7 +40,7 @@ impl Contract { Ok(()) } - pub fn do_something() -> Result<(), Vec> { + pub fn do_something(&self) -> Result<(), Vec> { // Local variables are not saved to the blockchain // 16-bit Rust integer let _i = 456_u16; @@ -50,15 +50,12 @@ impl Contract { // Here are some global variables let _timestamp = block::timestamp(); let _amount = msg::value(); + let _base_uri = self.base_uri.get_string(); console!("Local variables: {_i}, {_j}"); console!("Global variables: {_timestamp}, {_amount}"); + console!("Base uri: {_base_uri}"); Ok(()) } - - pub fn get_base_uri(&self) -> Result> { - // Unlike other variables, string needs to use .get_string() to get the value - Ok(self.base_uri.get_string()) - } } diff --git a/src/app/basic_examples/variables/page.mdx b/src/app/basic_examples/variables/page.mdx index a6eff08..92fd27b 100644 --- a/src/app/basic_examples/variables/page.mdx +++ b/src/app/basic_examples/variables/page.mdx @@ -33,7 +33,7 @@ extern crate alloc; use stylus_sdk::alloy_primitives::{U16, U256}; use stylus_sdk::prelude::*; -use stylus_sdk::storage::{StorageAddress, StorageBool, StorageU256}; +use stylus_sdk::storage::{StorageAddress, StorageBool, StorageU256, StorageString}; use stylus_sdk::{block, console, msg}; #[storage] @@ -42,6 +42,7 @@ pub struct Contract { initialized: StorageBool, owner: StorageAddress, max_supply: StorageU256, + base_uri: StorageString, } #[public] @@ -61,10 +62,14 @@ impl Contract { self.owner.set(msg::sender()); self.max_supply.set(U256::from(10_000)); + // We set the base URI for the contract + // Unlike other variables, string needs to use .set_str() to set the value + self.base_uri.set_str("https://stylus-by-example.org/".to_string()); + Ok(()) } - pub fn do_something() -> Result<(), Vec> { + pub fn do_something(&self) -> Result<(), Vec> { // Local variables are not saved to the blockchain // 16-bit Rust integer let _i = 456_u16; @@ -74,9 +79,11 @@ impl Contract { // Here are some global variables let _timestamp = block::timestamp(); let _amount = msg::value(); + let _base_uri = self.base_uri.get_string(); console!("Local variables: {_i}, {_j}"); console!("Global variables: {_timestamp}, {_amount}"); + console!("Base uri: {_base_uri}"); Ok(()) }