Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Sep 4, 2024
1 parent a244047 commit c254aa5
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 deletions contracts/src/components/escrow/escrow.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[starknet::component]
pub mod EscrowComponent {
use openzeppelin_token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use starknet::ContractAddress;
use starknet::storage::Map;
use starknet::{ContractAddress, get_contract_address};
use zkramp::components::escrow::interface;

//
Expand All @@ -13,8 +13,6 @@ pub mod EscrowComponent {
struct Storage {
// (owner, token) -> amount
deposits: Map::<(ContractAddress, ContractAddress), u256>,
// token -> escrow address
escrow_contract_addresses: Map::<ContractAddress, ContractAddress>,
}

//
Expand All @@ -40,19 +38,12 @@ pub mod EscrowComponent {
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
let erc20_dispatcher = IERC20Dispatcher { contract_address: token };

// Transfers funds to escrow
transfer_erc20(from, escrow_contract_address, token, amount);
erc20_dispatcher.transfer_from(from, get_contract_address(), amount);

self.deposits.write((from, token), amount + locked_amount);
}
Expand All @@ -64,8 +55,6 @@ pub mod EscrowComponent {
token: ContractAddress,
amount: u256
) {
let escrow_contract_address = self.escrow_contract_addresses.read(token);

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

// TODO
Expand All @@ -75,23 +64,13 @@ pub mod EscrowComponent {
// 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);
// 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);
}
}

//
// 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);
}
}

0 comments on commit c254aa5

Please sign in to comment.