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 string varibles #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 9 additions & 2 deletions example_code/basic_examples/variables/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -13,6 +13,7 @@ pub struct Contract {
initialized: StorageBool,
owner: StorageAddress,
max_supply: StorageU256,
base_uri: StorageString,
}

#[public]
Expand All @@ -32,10 +33,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<u8>> {
pub fn do_something(&self) -> Result<(), Vec<u8>> {
// Local variables are not saved to the blockchain
// 16-bit Rust integer
let _i = 456_u16;
Expand All @@ -45,9 +50,11 @@ impl Contract {
// Here are some global variables
let _timestamp = block::timestamp();
let _amount = msg::value();
let _base_uri = self.base_uri.get_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to add this here. This section is for global variables. We are already showing examples for state variables in the previous function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put here is because we don't mention how to get value from string type state, maybe we add another read-only function for this and also put how to get owner and max_supply there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that, or have a specific "section" for reading those (something under a different comment, since that one suggests those are "global variables")


console!("Local variables: {_i}, {_j}");
console!("Global variables: {_timestamp}, {_amount}");
console!("Base uri: {_base_uri}");

Ok(())
}
Expand Down
11 changes: 9 additions & 2 deletions src/app/basic_examples/variables/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -42,6 +42,7 @@ pub struct Contract {
initialized: StorageBool,
owner: StorageAddress,
max_supply: StorageU256,
base_uri: StorageString,
}

#[public]
Expand All @@ -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<u8>> {
pub fn do_something(&self) -> Result<(), Vec<u8>> {
// Local variables are not saved to the blockchain
// 16-bit Rust integer
let _i = 456_u16;
Expand All @@ -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(())
}
Expand Down
Loading