Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : escrow component #45

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

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

//
// Storage
//

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

//
// 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 erc20_dispatcher = IERC20Dispatcher { contract_address: token };

let balance = erc20_dispatcher.balance_of(from);

assert(balance >= amount, Errors::INSUFFICIENT_BALANCE);

let locked_amount = self.deposits.read((from, token));

// Retreives escrow address for the token `token``
let escrow_contract_address = self.escrow_contract_addresses.read(token);

// Transfers funds to escrow
transfer_erc20(from, escrow_contract_address, token, 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 escrow_contract_address = self.escrow_contract_addresses.read(token);

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 `amount` from `from` to `to`
transfer_erc20(escrow_contract_address, to, token, amount);

// update locked amount
self.deposits.write((from, token), locked_amount - amount);
}
}

//
// Internals
//

fn transfer_erc20(
from: ContractAddress, token: ContractAddress, to: ContractAddress, amount: u256
) {
let erc20_dispatcher = IERC20Dispatcher { contract_address: token };

erc20_dispatcher.transfer_from(from, to, 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
);
}

Loading