Skip to content

Commit

Permalink
add storage contract
Browse files Browse the repository at this point in the history
  • Loading branch information
augustbleeds committed Nov 14, 2024
1 parent 16426aa commit 20b2b38
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/contracts/aggregator_consumer/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ name = "aggregator_consumer"
version = "0.1.0"
cairo-version = "2.6.3"

# [scripts]
# test = "snforge test"
[scripts]
test = "snforge test"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

Expand Down
1 change: 1 addition & 0 deletions examples/contracts/aggregator_consumer/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod ocr2;
pub mod storage;

27 changes: 27 additions & 0 deletions examples/contracts/aggregator_consumer/src/storage.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#[starknet::interface]
pub trait IStorage<TContractState> {
fn store(ref self: TContractState, value: u256);
fn retrieve(self: @TContractState) -> u256;
}

#[starknet::contract]
mod Storage {
#[storage]
struct Storage {
value: u256
}

#[constructor]
fn constructor(ref self: ContractState) {}

#[abi(embed_v0)]
impl StorageImpl of super::IStorage<ContractState> {
fn store(ref self: ContractState, value: u256) {
self.value.write(value)
}

fn retrieve(self: @ContractState) -> u256 {
self.value.read()
}
}
}
25 changes: 25 additions & 0 deletions examples/contracts/aggregator_consumer/tests/test_storage.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use starknet::contract_address_const;
use starknet::get_caller_address;
use starknet::ContractAddress;

use aggregator_consumer::storage::{IStorageDispatcherTrait, IStorageDispatcher};

use snforge_std::{declare, ContractClassTrait};

fn deploy_storage() -> (ContractAddress, IStorageDispatcher) {
let mut calldata = ArrayTrait::new();
let (contract_address, _) = declare("Storage").unwrap().deploy(@calldata).unwrap();
(contract_address, IStorageDispatcher { contract_address: contract_address })
}

#[test]
fn test_store_and_retrieve() {
let (_, storage) = deploy_storage();
let desired_value: u256 = 145;
storage.store(desired_value);

let actual_value = storage.retrieve();

assert(actual_value == desired_value, 'values should equal');
}

0 comments on commit 20b2b38

Please sign in to comment.