From 20b2b38317dcb9a2c094165a7bb107c466594222 Mon Sep 17 00:00:00 2001 From: Augustus Chang Date: Thu, 14 Nov 2024 12:22:09 -0500 Subject: [PATCH] add storage contract --- .../contracts/aggregator_consumer/Scarb.toml | 4 +-- .../aggregator_consumer/src/lib.cairo | 1 + .../aggregator_consumer/src/storage.cairo | 27 +++++++++++++++++++ .../tests/test_storage.cairo | 25 +++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 examples/contracts/aggregator_consumer/src/storage.cairo create mode 100644 examples/contracts/aggregator_consumer/tests/test_storage.cairo diff --git a/examples/contracts/aggregator_consumer/Scarb.toml b/examples/contracts/aggregator_consumer/Scarb.toml index 8da76d784..23bffc0aa 100644 --- a/examples/contracts/aggregator_consumer/Scarb.toml +++ b/examples/contracts/aggregator_consumer/Scarb.toml @@ -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 diff --git a/examples/contracts/aggregator_consumer/src/lib.cairo b/examples/contracts/aggregator_consumer/src/lib.cairo index ea174388f..c807e9e78 100644 --- a/examples/contracts/aggregator_consumer/src/lib.cairo +++ b/examples/contracts/aggregator_consumer/src/lib.cairo @@ -1,2 +1,3 @@ pub mod ocr2; +pub mod storage; diff --git a/examples/contracts/aggregator_consumer/src/storage.cairo b/examples/contracts/aggregator_consumer/src/storage.cairo new file mode 100644 index 000000000..66c911a29 --- /dev/null +++ b/examples/contracts/aggregator_consumer/src/storage.cairo @@ -0,0 +1,27 @@ +#[starknet::interface] +pub trait IStorage { + 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 { + fn store(ref self: ContractState, value: u256) { + self.value.write(value) + } + + fn retrieve(self: @ContractState) -> u256 { + self.value.read() + } + } +} diff --git a/examples/contracts/aggregator_consumer/tests/test_storage.cairo b/examples/contracts/aggregator_consumer/tests/test_storage.cairo new file mode 100644 index 000000000..1ea2f22b6 --- /dev/null +++ b/examples/contracts/aggregator_consumer/tests/test_storage.cairo @@ -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'); +} +