diff --git a/contracts/src/components/escrow.cairo b/contracts/src/components/escrow.cairo index 66137bd..fc733ae 100644 --- a/contracts/src/components/escrow.cairo +++ b/contracts/src/components/escrow.cairo @@ -1,4 +1,3 @@ pub mod escrow; pub mod interface; - diff --git a/contracts/src/components/escrow/escrow.cairo b/contracts/src/components/escrow/escrow.cairo index d4ef1ac..4a1d8a4 100644 --- a/contracts/src/components/escrow/escrow.cairo +++ b/contracts/src/components/escrow/escrow.cairo @@ -13,6 +13,8 @@ pub mod EscrowComponent { struct Storage { // (owner, token) -> amount deposits: Map::<(ContractAddress, ContractAddress), u256>, + // token -> escrow address + escrow_contract_addresses: Map::, } // @@ -38,8 +40,20 @@ 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 + transfer_erc20(from, escrow_contract_address, token, amount); + self.deposits.write((from, token), amount + locked_amount); } @@ -50,18 +64,21 @@ 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 // 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(from, to, token, amount); + transfer_erc20(escrow_contract_address, to, token, amount); - // update locked amount + // update locked amount self.deposits.write((from, token), locked_amount - amount); } }