Skip to content

Commit

Permalink
feat : escrow component (#45)
Browse files Browse the repository at this point in the history
* feat : escrow component

* review fix : lock_from

* review changes

---------

Co-authored-by: Chqrles <[email protected]>
  • Loading branch information
chachaleo and 0xChqrles authored Sep 4, 2024
1 parent a2722f6 commit bea3338
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions contracts/src/components.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod escrow;
pub mod processors;
pub mod registry;
3 changes: 3 additions & 0 deletions contracts/src/components/escrow.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod escrow;
pub mod interface;

76 changes: 76 additions & 0 deletions contracts/src/components/escrow/escrow.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#[starknet::component]
pub mod EscrowComponent {
use openzeppelin_token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use starknet::storage::Map;
use starknet::{ContractAddress, get_contract_address};
use zkramp::components::escrow::interface;

//
// Storage
//

#[storage]
struct Storage {
// (owner, token) -> amount
deposits: Map::<(ContractAddress, ContractAddress), u256>,
}

//
// Errors
//

pub mod Errors {
pub const PROOF_OF_DEPOSIT_FAILED: felt252 = 'Proof of deposit failed';
pub const INSUFFICIENT_BALANCE: felt252 = 'Insufficient deposit balance';
}

//
// Escrow impl
//

#[embeddable_as(RegistryImpl)]
impl Escrow<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>,
> of interface::IEscrow<ComponentState<TContractState>> {
fn lock_from(
ref self: ComponentState<TContractState>,
from: ContractAddress,
token: ContractAddress,
amount: u256
) {
let locked_amount = self.deposits.read((from, token));

// transfers funds to escrow
let erc20_dispatcher = IERC20Dispatcher { contract_address: token };

erc20_dispatcher.transfer_from(from, get_contract_address(), amount);

self.deposits.write((from, token), amount + locked_amount);
}

fn unlock_to(
ref self: ComponentState<TContractState>,
from: ContractAddress,
to: ContractAddress,
token: ContractAddress,
amount: u256
) {
let locked_amount = self.deposits.read((from, token));

// TODO
// check for proof of deposit
assert(true, Errors::PROOF_OF_DEPOSIT_FAILED);

// check deposit balance
assert(locked_amount >= amount, Errors::INSUFFICIENT_BALANCE);

// transfert of the amount to `to`
let erc20_dispatcher = IERC20Dispatcher { contract_address: token };

erc20_dispatcher.transfer_from(get_contract_address(), to, amount);

// update locked amount
self.deposits.write((from, token), locked_amount - amount);
}
}
}
15 changes: 15 additions & 0 deletions contracts/src/components/escrow/interface.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use core::hash::HashStateExTrait;
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
);
}

0 comments on commit bea3338

Please sign in to comment.