Skip to content

Commit

Permalink
first commit : bug:external contracts not found for selectors: `openz…
Browse files Browse the repository at this point in the history
…eppelin::presets::ERC20Upgradeable`
  • Loading branch information
chachaleo committed Sep 9, 2024
1 parent b7c8bb3 commit d82d5f3
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 9 deletions.
2 changes: 1 addition & 1 deletion contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
target
.snfoundry_cache/
.snfoundry_cache
1 change: 1 addition & 0 deletions contracts/.snfoundry_cache/.prev_tests_failed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zkramp::components::escrow::escrow_test::test_lock_unlock
1 change: 1 addition & 0 deletions contracts/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scarb 2.7.0
1 change: 1 addition & 0 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sort-module-level-items = true
[[target.starknet-contract]]
sierra = true
casm = true
build-external-contracts = ["openzeppelin::presets::ERC20Upgradeable"]

[scripts]
test = "snforge test"
6 changes: 6 additions & 0 deletions contracts/src/components/escrow.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pub mod escrow;

#[cfg(test)]
pub mod escrow_mock;

#[cfg(test)]
pub mod escrow_test;
pub mod interface;

2 changes: 1 addition & 1 deletion contracts/src/components/escrow/escrow.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub mod EscrowComponent {
// Escrow impl
//

#[embeddable_as(RegistryImpl)]
#[embeddable_as(EscrowImpl)]
impl Escrow<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>,
> of interface::IEscrow<ComponentState<TContractState>> {
Expand Down
52 changes: 52 additions & 0 deletions contracts/src/components/escrow/escrow_mock.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::escrow::EscrowComponent;

#[starknet::contract]
pub mod EscrowMock {
use starknet::ContractAddress;
use starknet::account::Call;
use zkramp::components::escrow::escrow::EscrowComponent;
use zkramp::components::escrow::interface::IEscrow;

component!(path: EscrowComponent, storage: escrow, event: EscrowEvent);

// Escrow
#[abi(embed_v0)]
impl EscrowImpl = EscrowComponent::EscrowImpl<ContractState>;

//
// Storage
//

#[storage]
struct Storage {
#[substorage(v0)]
escrow: EscrowComponent::Storage,
}

//
// Events
//

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

//
// Constructor
//

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

pub type ComponentState = EscrowComponent::ComponentState<EscrowMock::ContractState>;

pub impl TestingStateDefault of Default<ComponentState> {
fn default() -> ComponentState {
EscrowComponent::component_state_for_testing()
}
}
31 changes: 31 additions & 0 deletions contracts/src/components/escrow/escrow_test.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use core::starknet::get_contract_address;

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

use zkramp::components::escrow::escrow::EscrowComponent::EscrowImpl;
use zkramp::components::escrow::escrow_mock::{EscrowMock, TestingStateDefault, ComponentState};

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


//
// Externals
//

#[test]
fn test_lock_unlock() {
let token_dispatcher = utils::setup_erc20(recipient: get_contract_address());

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

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

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

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

assert_eq!(escrow.deposits.read((constants::OWNER(), token_dispatcher.contract_address)), 0);
}

15 changes: 15 additions & 0 deletions contracts/src/tests/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ pub fn REVOLUT_ID() -> OffchainId {
pub fn CALLER() -> ContractAddress {
contract_address_const::<'caller'>()
}

pub fn OWNER() -> ContractAddress {
contract_address_const::<'owner'>()
}

pub const SUPPLY: u256 = 1_000_000_000_000_000_000; // 1 ETH


pub fn NAME() -> ByteArray {
"NAME"
}

pub fn SYMBOL() -> ByteArray {
"SYMBOL"
}
33 changes: 26 additions & 7 deletions contracts/src/tests/utils.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
use starknet::{SyscallResultTrait, syscalls};
use core::traits::TryInto;

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();
use openzeppelin::presets::interfaces::{
ERC20UpgradeableABIDispatcher, ERC20UpgradeableABIDispatcherTrait
};

address
use openzeppelin::utils::serde::SerializedAppend;

use snforge_std::{declare, ContractClassTrait};
use starknet::ContractAddress;

use super::constants;


pub fn setup_erc20(recipient: ContractAddress) -> ERC20UpgradeableABIDispatcher {
let mut calldata = array![];

calldata.append_serde(constants::NAME());
calldata.append_serde(constants::SYMBOL());
calldata.append_serde(constants::SUPPLY); // 1 ETH
calldata.append_serde(recipient);
calldata.append_serde(recipient);

let contract = declare("ERC20Upgradeable").unwrap();
let (contract_address, _) = contract.deploy(@calldata).unwrap();

ERC20UpgradeableABIDispatcher { contract_address: contract_address }
}

0 comments on commit d82d5f3

Please sign in to comment.