Skip to content

Commit

Permalink
Merge pull request #23 from 0xChqrles/feat/setup-tests
Browse files Browse the repository at this point in the history
[CONTRACTS] setup registry tests
  • Loading branch information
0xChqrles authored Aug 14, 2024
2 parents 1e64df8 + 160d3ff commit e909846
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 8 deletions.
7 changes: 6 additions & 1 deletion contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
name = "zkramp"
version = "0.1.0"
edition = "2023_11"
cairo-version = "2.7.0"
scarb-version = "2.7.0"

[dependencies]
starknet = "2.6.4"
starknet = "2.7.0"
openzeppelin = { git = "https://github.com/openzeppelin/cairo-contracts", tag = "v0.15.0" }

[dev-dependencies]
cairo_test = "2.7.0"

[tool.fmt]
sort-module-level-items = true

Expand Down
6 changes: 6 additions & 0 deletions contracts/src/components/registry.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
pub mod interface;
pub mod registry;

#[cfg(test)]
pub mod registry_mock;

#[cfg(test)]
pub mod registry_test;
29 changes: 22 additions & 7 deletions contracts/src/components/registry/registry.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
#[starknet::interface]
trait IRegisterContract<ContractState> {
fn register_user(ref self: ContractState, revolut_ID: felt252);
}

#[starknet::component]
mod RegistryComponent {
use starknet::storage::Map;
pub mod RegistryComponent {
use core::num::traits::Zero;
use starknet::storage::Map;
use starknet::{ContractAddress, get_caller_address};
use zkramp::components::registry::interface::OffchainId;
use zkramp::components::registry::interface;

//
// Storage
//

#[storage]
struct Storage {
Registry_registrations: Map::<(ContractAddress, OffchainId), bool>,
}

//
// Errors
//

pub mod Errors {
pub const ZERO_ADDRESS_CALLER: felt252 = 'Caller is the zero address';
}

//
// Registry impl
//

#[embeddable_as(RegistryImpl)]
impl Registry<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>,
Expand All @@ -30,6 +42,9 @@ mod RegistryComponent {
fn register(ref self: ComponentState<TContractState>, offchain_id: OffchainId) {
let caller = get_caller_address();

// verify caller
assert(caller.is_non_zero(), Errors::ZERO_ADDRESS_CALLER);

// TODO: caller a processor to verify the proof of registration

// save registration
Expand Down
51 changes: 51 additions & 0 deletions contracts/src/components/registry/registry_mock.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::registry::RegistryComponent;

#[starknet::contract]
pub mod RegistryMock {
use starknet::ContractAddress;
use starknet::account::Call;
use zkramp::components::registry::interface::IRegistry;
use zkramp::components::registry::registry::RegistryComponent;

component!(path: RegistryComponent, storage: registry, event: RegistryEvent);

// Registry
#[abi(embed_v0)]
impl RegistryImpl = RegistryComponent::RegistryImpl<ContractState>;

//
// Storage
//

#[storage]
struct Storage {
#[substorage(v0)]
registry: RegistryComponent::Storage,
}

//
// Events
//

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
RegistryEvent: RegistryComponent::Event,
}

//
// Constructor
//

#[constructor]
fn constructor(ref self: ContractState) {
// Nothing to be done
}
}

type ComponentState = RegistryComponent::ComponentState<RegistryMock::ContractState>;

fn COMPONENT() -> ComponentState {
RegistryComponent::component_state_for_testing()
}
24 changes: 24 additions & 0 deletions contracts/src/components/registry/registry_test.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use zkramp::components::registry::interface::{IRegistryDispatcher, IRegistryDispatcherTrait};
use zkramp::components::registry::registry_mock::RegistryMock;
use zkramp::tests::utils;
use zkramp::tests::constants;

/// Deploys the registry mock contract.
fn setup_contracts() -> IRegistryDispatcher {
// deploy registry
let registry_contract_address = utils::deploy(RegistryMock::TEST_CLASS_HASH, calldata: array![]);

IRegistryDispatcher { contract_address: registry_contract_address }
}

//
// Externals
//

#[test]
#[should_panic(expected: ('Caller is the zero address', 'ENTRYPOINT_FAILED'))]
fn test_register_from_zero() {
let registry = setup_contracts();

registry.register(offchain_id: constants::REVOLUT_ID());
}
3 changes: 3 additions & 0 deletions contracts/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod components;
pub mod contracts;
pub mod utils;

#[cfg(test)]
pub mod tests;
2 changes: 2 additions & 0 deletions contracts/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod constants;
pub mod utils;
9 changes: 9 additions & 0 deletions contracts/src/tests/constants.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use zkramp::components::registry::interface::OffchainId;

pub fn REVTAG() -> ByteArray {
"my wonderfull, incredible but also very long, revtag"
}

pub fn REVOLUT_ID() -> OffchainId {
OffchainId::Revolut(REVTAG())
}
10 changes: 10 additions & 0 deletions contracts/src/tests/utils.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use starknet::{SyscallResultTrait, syscalls};

pub fn deploy(contract_class_hash: felt252, calldata: Array<felt252>) -> starknet::ContractAddress {
let (address, _) = syscalls::deploy_syscall(
contract_class_hash.try_into().unwrap(), 0, calldata.span(), false
)
.unwrap_syscall();

address
}

0 comments on commit e909846

Please sign in to comment.