Skip to content

Commit

Permalink
additionnal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Sep 10, 2024
1 parent 7ae2f89 commit 65963ba
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 55 deletions.
1 change: 0 additions & 1 deletion contracts/.snfoundry_cache/.prev_tests_failed
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
zkramp::components::escrow::escrow_test::test_lock_unlock
2 changes: 1 addition & 1 deletion contracts/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.7.0
scarb 2.8.0
5 changes: 1 addition & 4 deletions contracts/src/components/escrow/escrow.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ pub mod EscrowComponent {
TContractState, +HasComponent<TContractState>, +Drop<TContractState>,
> of interface::IEscrow<ComponentState<TContractState>> {
fn lock_from(
ref self: ComponentState<TContractState>,
from: ContractAddress,
token: ContractAddress,
amount: u256
ref self: ComponentState<TContractState>, from: ContractAddress, token: ContractAddress, amount: u256
) {
let locked_amount = self.deposits.read((from, token));

Expand Down
141 changes: 132 additions & 9 deletions contracts/src/components/escrow/escrow_test.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use core::starknet::get_contract_address;

use openzeppelin::presets::interfaces::ERC20UpgradeableABIDispatcherTrait;

use zkramp::components::escrow::escrow::EscrowComponent::EscrowImpl;
use snforge_std::{start_cheat_caller_address, EventSpyAssertionsTrait, spy_events};
use zkramp::components::escrow::escrow::EscrowComponent::{Event, Locked, UnLocked, EscrowImpl};
use zkramp::components::escrow::escrow_mock::{TestingStateDefault, ComponentState};

use zkramp::tests::constants;
use zkramp::tests::utils;

Expand All @@ -13,18 +12,142 @@ use zkramp::tests::utils;
// Externals
//

#[test]
fn test_lock() {
let mut spy = spy_events();
let token_dispatcher = utils::setup_erc20(recipient: constants::OWNER());
start_cheat_caller_address(token_dispatcher.contract_address, constants::OWNER());

token_dispatcher.transfer(constants::SPENDER(), 100);

start_cheat_caller_address(token_dispatcher.contract_address, constants::SPENDER());

token_dispatcher.approve(constants::RECIPIENT(), 42);
start_cheat_caller_address(token_dispatcher.contract_address, constants::RECIPIENT());

let mut escrow: ComponentState = Default::default();

escrow.lock_from(constants::SPENDER(), token_dispatcher.contract_address, 42);

assert_eq!(token_dispatcher.balance_of(constants::SPENDER()), 58);
assert_eq!(token_dispatcher.allowance(constants::SPENDER(), constants::RECIPIENT()), 0);

// test event emission
spy
.assert_emitted(
@array![
(
get_contract_address(),
Event::Locked(
Locked { token: token_dispatcher.contract_address, from: constants::SPENDER(), amount: 42 }
)
)
]
)
}


#[test]
fn test_lock_unlock() {
let token_dispatcher = utils::setup_erc20(recipient: get_contract_address());
let mut spy = spy_events();
let token_dispatcher = utils::setup_erc20(recipient: constants::OWNER());
start_cheat_caller_address(token_dispatcher.contract_address, constants::OWNER());

token_dispatcher.transfer(constants::OWNER(), 100);
token_dispatcher.transfer(constants::SPENDER(), 100);

start_cheat_caller_address(token_dispatcher.contract_address, constants::SPENDER());

token_dispatcher.approve(constants::RECIPIENT(), 42);
start_cheat_caller_address(token_dispatcher.contract_address, constants::RECIPIENT());

let mut escrow: ComponentState = Default::default();

escrow.lock_from(constants::OWNER(), token_dispatcher.contract_address, 42);
escrow.lock_from(constants::SPENDER(), token_dispatcher.contract_address, 42);

start_cheat_caller_address(token_dispatcher.contract_address, get_contract_address());

escrow
.unlock_to(constants::OWNER(), constants::CALLER(), token_dispatcher.contract_address, 42);
token_dispatcher.approve(constants::RECIPIENT(), 42);

start_cheat_caller_address(token_dispatcher.contract_address, constants::RECIPIENT());
escrow.unlock_to(constants::SPENDER(), constants::RECIPIENT(), token_dispatcher.contract_address, 42);

assert_eq!(token_dispatcher.balance_of(constants::SPENDER()), 58);
assert_eq!(token_dispatcher.balance_of(constants::RECIPIENT()), 42);
assert_eq!(token_dispatcher.allowance(constants::SPENDER(), constants::RECIPIENT()), 0);
assert_eq!(escrow.deposits.read((constants::SPENDER(), token_dispatcher.contract_address)), 0);

// test event emission
spy
.assert_emitted(
@array![
(
get_contract_address(),
Event::Locked(
Locked { token: token_dispatcher.contract_address, from: constants::SPENDER(), amount: 42 }
)
)
]
);

spy
.assert_emitted(
@array![
(
get_contract_address(),
Event::UnLocked(
UnLocked {
token: token_dispatcher.contract_address,
from: constants::SPENDER(),
to: constants::RECIPIENT(),
amount: 42
}
)
)
]
)
}


#[test]
#[should_panic(expected: 'Insufficient deposit balance')]
fn test_lock_unlock_greater_than_balance() {
let token_dispatcher = utils::setup_erc20(recipient: constants::OWNER());
start_cheat_caller_address(token_dispatcher.contract_address, constants::OWNER());

token_dispatcher.transfer(constants::SPENDER(), 1000);

start_cheat_caller_address(token_dispatcher.contract_address, constants::SPENDER());

token_dispatcher.approve(constants::RECIPIENT(), 42);
start_cheat_caller_address(token_dispatcher.contract_address, constants::RECIPIENT());

let mut escrow: ComponentState = Default::default();

escrow.lock_from(constants::SPENDER(), token_dispatcher.contract_address, 42);

start_cheat_caller_address(token_dispatcher.contract_address, get_contract_address());

token_dispatcher.approve(constants::RECIPIENT(), 42);

start_cheat_caller_address(token_dispatcher.contract_address, constants::RECIPIENT());
escrow.unlock_to(constants::SPENDER(), constants::RECIPIENT(), token_dispatcher.contract_address, 420);
}

#[test]
#[should_panic(expected: 'ERC20: insufficient allowance')]
fn test_lock_from_unallowed_caller() {
let token_dispatcher = utils::setup_erc20(recipient: constants::OWNER());
start_cheat_caller_address(token_dispatcher.contract_address, constants::OWNER());

token_dispatcher.transfer(constants::SPENDER(), 100);

start_cheat_caller_address(token_dispatcher.contract_address, constants::SPENDER());

token_dispatcher.approve(constants::RECIPIENT(), 42);

start_cheat_caller_address(token_dispatcher.contract_address, constants::CALLER());

let mut escrow: ComponentState = Default::default();

assert_eq!(escrow.deposits.read((constants::OWNER(), token_dispatcher.contract_address)), 0);
escrow.lock_from(constants::SPENDER(), token_dispatcher.contract_address, 42);
}
8 changes: 1 addition & 7 deletions contracts/src/components/escrow/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,5 @@ use starknet::ContractAddress;
#[starknet::interface]
pub trait IEscrow<TState> {
fn lock_from(ref self: TState, from: ContractAddress, token: ContractAddress, amount: u256);
fn unlock_to(
ref self: TState,
from: ContractAddress,
to: ContractAddress,
token: ContractAddress,
amount: u256
);
fn unlock_to(ref self: TState, from: ContractAddress, to: ContractAddress, token: ContractAddress, amount: u256);
}
12 changes: 3 additions & 9 deletions contracts/src/components/processors/tls/tls.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ pub mod TLSProcessorComponent {
///
/// @param timestamp_buffer The timestamp buffer for validated TLS calls
///
fn set_timestamp_buffer(
ref self: ComponentState<TContractState>, timestamp_buffer: felt252
) {
fn set_timestamp_buffer(ref self: ComponentState<TContractState>, timestamp_buffer: felt252) {
// assert only owner
let mut ownable_component = get_dep_component!(@self, Ownable);
ownable_component.assert_only_owner();
Expand Down Expand Up @@ -101,9 +99,7 @@ pub mod TLSProcessorComponent {
assert(expected_endpoint == endpoint, Errors::BAD_ENDPOINT);
}

fn _validate_TLS_host(
self: @ComponentState<TContractState>, expected_host: ByteArray, host: ByteArray
) {
fn _validate_TLS_host(self: @ComponentState<TContractState>, expected_host: ByteArray, host: ByteArray) {
assert(expected_host == host, Errors::BAD_HOST);
}

Expand All @@ -114,9 +110,7 @@ pub mod TLSProcessorComponent {
nullifier_registry.add_nullifier(:nullifier);
}

fn _validate_signature(
self: @ComponentState<TContractState>,
) { // TODO: verifiy signature, can we use SNIP-12?
fn _validate_signature(self: @ComponentState<TContractState>,) { // TODO: verifiy signature, can we use SNIP-12?
}
}
}
4 changes: 1 addition & 3 deletions contracts/src/components/registry/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ pub enum OffchainId {

#[starknet::interface]
pub trait IRegistry<TState> {
fn is_registered(
self: @TState, contract_address: ContractAddress, offchain_id: OffchainId
) -> bool;
fn is_registered(self: @TState, contract_address: ContractAddress, offchain_id: OffchainId) -> bool;
fn register(ref self: TState, offchain_id: OffchainId);
}
4 changes: 1 addition & 3 deletions contracts/src/components/registry/registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ pub mod RegistryComponent {
TContractState, +HasComponent<TContractState>, +Drop<TContractState>,
> of interface::IRegistry<ComponentState<TContractState>> {
fn is_registered(
self: @ComponentState<TContractState>,
contract_address: ContractAddress,
offchain_id: OffchainId
self: @ComponentState<TContractState>, contract_address: ContractAddress, offchain_id: OffchainId
) -> bool {
self.Registry_registrations.read((contract_address, offchain_id))
}
Expand Down
8 changes: 2 additions & 6 deletions contracts/src/components/registry/registry_test.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use core::starknet::ContractAddress;
use snforge_std::{start_cheat_caller_address, EventSpyAssertionsTrait, spy_events, test_address};
use zkramp::components::registry::registry::{
RegistryComponent::{Event, RegistrationEvent, RegistryImpl}
};
use zkramp::components::registry::registry::{RegistryComponent::{Event, RegistrationEvent, RegistryImpl}};
use zkramp::components::registry::registry_mock::{TestingStateDefault, ComponentState};
use zkramp::tests::constants;

Expand Down Expand Up @@ -40,9 +38,7 @@ fn test_registration_event() {
(
test_address,
Event::RegistrationEvent(
RegistrationEvent {
caller: constants::CALLER(), offchain_id: constants::REVOLUT_ID()
}
RegistrationEvent { caller: constants::CALLER(), offchain_id: constants::REVOLUT_ID() }
)
)
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ pub mod NullifierRegistry {
#[cfg(test)]
mod NullifierRegistry_tests {
use core::traits::Into;
use snforge_std::{
declare, ContractClassTrait, start_cheat_caller_address, stop_cheat_caller_address
};
use snforge_std::{declare, ContractClassTrait, start_cheat_caller_address, stop_cheat_caller_address};
use starknet::{ContractAddress};
use zkramp::contracts::nullifier_registry::interface::{
INullifierRegistryDispatcher, INullifierRegistryDispatcherTrait
Expand All @@ -145,9 +143,7 @@ mod NullifierRegistry_tests {
fn deploy_NullifierRegistry(owner: felt252) -> ContractAddress {
let mut nullifier_constructor_calldata = array![owner];
let mut nullifier_contract = declare("NullifierRegistry").unwrap();
let (contract_address, _) = nullifier_contract
.deploy(@nullifier_constructor_calldata)
.unwrap();
let (contract_address, _) = nullifier_contract.deploy(@nullifier_constructor_calldata).unwrap();

contract_address
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ pub mod RevolutSendProcessor {
self.ownable.initializer(:owner);

// initialize TLS processor
self
.tls_processor
.initializer(:ramp_address, :nullifier_registry, :timestamp_buffer, :enpoint, :host);
self.tls_processor.initializer(:ramp_address, :nullifier_registry, :timestamp_buffer, :enpoint, :host);
}
}
8 changes: 8 additions & 0 deletions contracts/src/tests/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ pub fn CALLER() -> ContractAddress {
contract_address_const::<'caller'>()
}

pub fn SPENDER() -> ContractAddress {
contract_address_const::<'soender'>()
}

pub fn RECIPIENT() -> ContractAddress {
contract_address_const::<'recipient'>()
}

pub fn OWNER() -> ContractAddress {
contract_address_const::<'owner'>()
}
Expand Down
4 changes: 1 addition & 3 deletions contracts/src/utils/hash.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use core::hash::{HashStateTrait, Hash};

// Care must be taken when using this implementation: Serde of the type T must be safe for hashing.
// This means that no two values of type T have the same serialization.
pub(crate) impl HashSerializable<
T, S, +Serde<T>, +HashStateTrait<S>, +Drop<T>, +Drop<S>
> of Hash<T, S> {
pub(crate) impl HashSerializable<T, S, +Serde<T>, +HashStateTrait<S>, +Drop<T>, +Drop<S>> of Hash<T, S> {
fn update_state(mut state: S, value: T) -> S {
let mut arr = array![];
Serde::serialize(@value, ref arr);
Expand Down

0 comments on commit 65963ba

Please sign in to comment.