Skip to content

Commit

Permalink
test: ch00/variables
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Apr 12, 2024
1 parent a768477 commit fcb534d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
19 changes: 19 additions & 0 deletions listings/getting-started/variables/src/global_variables.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pub trait IGlobalExample<TContractState> {
fn foo(ref self: TContractState);
}

// ANCHOR: contract
#[starknet::contract]
pub mod GlobalExample {
// import the required functions from the starknet core library
Expand All @@ -19,3 +21,20 @@ pub mod GlobalExample {
}
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod test {
use super::GlobalExample;
use starknet::SyscallResultTrait;
use starknet::syscalls::deploy_syscall;

#[test]
fn test_can_deploy() {
let (_contract_address, _) = deploy_syscall(
GlobalExample::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
// Not much to test
}
}
3 changes: 0 additions & 3 deletions listings/getting-started/variables/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
mod storage_variables;
mod local_variables;
mod global_variables;

#[cfg(test)]
mod tests;
29 changes: 28 additions & 1 deletion listings/getting-started/variables/src/local_variables.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub trait ILocalVariablesExample<TContractState> {
fn do_something(self: @TContractState, value: u32) -> u32;
}

// ANCHOR: contract
#[starknet::contract]
pub mod LocalVariablesExample {
#[storage]
Expand All @@ -11,7 +12,8 @@ pub mod LocalVariablesExample {
#[abi(embed_v0)]
impl LocalVariablesExample of super::ILocalVariablesExample<ContractState> {
fn do_something(self: @ContractState, value: u32) -> u32 {
// This variable is local to the current block. It can't be accessed once it goes out of scope.
// This variable is local to the current block.
// It can't be accessed once it goes out of scope.
let increment = 10;

{
Expand All @@ -20,6 +22,31 @@ pub mod LocalVariablesExample {
let sum = value + increment;
sum
}
// We can't access the variable `sum` here, as it's out of scope.
}
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod test {
use super::{
LocalVariablesExample, ILocalVariablesExampleDispatcher,
ILocalVariablesExampleDispatcherTrait
};
use starknet::SyscallResultTrait;
use starknet::syscalls::deploy_syscall;

#[test]
fn test_can_deploy_and_do_something() {
let (contract_address, _) = deploy_syscall(
LocalVariablesExample::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();

let contract = ILocalVariablesExampleDispatcher { contract_address };
let value = 10;
let res = contract.do_something(value);
assert_eq!(res, value + 10);
}
}
36 changes: 35 additions & 1 deletion listings/getting-started/variables/src/storage_variables.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub trait IStorageVariableExample<TContractState> {
fn get(self: @TContractState) -> u32;
}

// ANCHOR: contract
#[starknet::contract]
pub mod StorageVariablesExample {
// All storage variables are contained in a struct called Storage
Expand All @@ -16,7 +17,8 @@ pub mod StorageVariablesExample {

#[abi(embed_v0)]
impl StorageVariablesExample of super::IStorageVariableExample<ContractState> {
// Write to storage variables by sending a transaction that calls an external function
// Write to storage variables by sending a transaction
// that calls an external function
fn set(ref self: ContractState, value: u32) {
self.value.write(value);
}
Expand All @@ -27,3 +29,35 @@ pub mod StorageVariablesExample {
}
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod test {
use super::{
StorageVariablesExample, StorageVariablesExample::valueContractMemberStateTrait,
IStorageVariableExampleDispatcher, IStorageVariableExampleDispatcherTrait
};
use starknet::SyscallResultTrait;
use starknet::syscalls::deploy_syscall;
use starknet::testing::set_contract_address;

#[test]
fn test_can_deploy_and_mutate_storage() {
let (contract_address, _) = deploy_syscall(
StorageVariablesExample::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();

let contract = IStorageVariableExampleDispatcher { contract_address };

let initial_value = 10;

contract.set(initial_value);
assert_eq!(contract.get(), initial_value);

// With contract state directly
let state = StorageVariablesExample::contract_state_for_testing();
set_contract_address(contract_address);
assert_eq!(state.value.read(), initial_value);
}
}
2 changes: 0 additions & 2 deletions listings/getting-started/variables/src/tests.cairo

This file was deleted.

12 changes: 6 additions & 6 deletions src/ch00/basics/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ Local variables are stored in memory and are not stored on the blockchain. This
Here's a simple example of a contract with only local variables:

```rust
{{#include ../../../listings/getting-started/variables/src/local_variables.cairo}}
{{#rustdoc_include ../../../listings/getting-started/variables/src/local_variables.cairo:contract}}
```

## Storage Variables

Storage variables are persistent data stored on the blockchain. They can be accessed from one execution to another, allowing the contract to remember and update information over time.
Storage variables are persistent data stored on the blockchain. They can be accessed from one execution to another, allowing the contract to remember and update information over time. See [Storage](./storage.md) for more information.

To write or update a storage variable, you need to interact with the contract through an external entrypoint by sending a transaction.

Expand All @@ -35,17 +35,17 @@ On the other hand, you can read state variables, for free, without any transacti
Here's a simple example of a contract with one storage variable:

```rust
{{#include ../../../listings/getting-started/variables/src/storage_variables.cairo}}
{{#rustdoc_include ../../../listings/getting-started/variables/src/storage_variables.cairo:contract}}
```

## Global Variables

Global variables are predefined variables that provide information about the blockchain and the current execution environment. They can be accessed at any time and from anywhere!

In Starknet, you can access global variables by using specific functions contained in the starknet core libraries.
In Starknet, you can access global variables by using specific functions from the starknet core library.

For example, the `get_caller_address` function returns the address of the caller of the current transaction, and the `get_contract_address` function returns the address of the current contract.

```rust
{{#include ../../../listings/getting-started/variables/src/global_variables.cairo}}
```
{{#rustdoc_include ../../../listings/getting-started/variables/src/global_variables.cairo:contract}}
```

0 comments on commit fcb534d

Please sign in to comment.