diff --git a/contracts/account/src/contract/account.cairo b/contracts/account/src/contract/account.cairo index 87542e0..8626a98 100644 --- a/contracts/account/src/contract/account.cairo +++ b/contracts/account/src/contract/account.cairo @@ -16,7 +16,6 @@ mod Account { use starknet::{ClassHash, ContractAddress, get_caller_address}; use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait}; - component!(path: AccountComponent, storage: account, event: AccountEvent); component!(path: SRC5Component, storage: src5, event: SRC5Event); component!(path: SRC9Component, storage: src9, event: SRC9Event); @@ -26,8 +25,7 @@ mod Account { #[abi(embed_v0)] impl AccountMixinImpl = AccountComponent::AccountMixinImpl; #[abi(embed_v0)] - impl OutsideExecutionV2Impl = - SRC9Component::OutsideExecutionV2Impl; + impl OutsideExecutionV2Impl = SRC9Component::OutsideExecutionV2Impl; // Internal impl AccountInternalImpl = AccountComponent::InternalImpl; @@ -46,12 +44,12 @@ mod Account { upgradeable: UpgradeableComponent::Storage, // Custom storage for SyncPayment functionality fiat_balance: Map<(ContractAddress, felt252), u128>, // (user, currency) => balance - token_address: Map, // symbol => token_address + token_address: Map, // symbol => token_address default_fiat_currency: felt252, liquidity_bridge: ContractAddress, initialized: bool, public_key: felt252, - approved_tokens: Map, // symbol => token_address + approved_tokens: Map, // symbol => token_address } #[event] @@ -66,6 +64,8 @@ mod Account { #[flat] UpgradeableEvent: UpgradeableComponent::Event, TokenApproved: TokenApproved, + FiatDeposit: FiatDeposit, + FiatWithdrawal: FiatWithdrawal, } #[derive(Drop, starknet::Event)] @@ -76,6 +76,21 @@ mod Account { pub amount: u128, } + #[derive(Drop, starknet::Event)] + pub struct FiatDeposit { + pub user: ContractAddress, + pub currency: felt252, + pub amount: u128, + } + + #[derive(Drop, starknet::Event)] + pub struct FiatWithdrawal { + pub account_address: ContractAddress, + pub currency: felt252, + pub amount: u128, + pub recipient: ContractAddress, + } + #[constructor] fn constructor(ref self: ContractState, public_key: felt252) { self.account.initializer(public_key); @@ -85,7 +100,6 @@ mod Account { // // Upgradeable // - #[abi(embed_v0)] impl UpgradeableImpl of IUpgradeable { fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { @@ -94,7 +108,6 @@ mod Account { } } - // contract impl #[abi(embed_v0)] impl AccountImpl of Iaccount { @@ -103,7 +116,6 @@ mod Account { let caller = get_caller_address(); let currency = self.default_fiat_currency.read(); let current_balance = self.fiat_balance.read((caller, currency)); - self .fiat_balance .write((caller, currency), current_balance + amount.try_into().unwrap()); @@ -129,13 +141,12 @@ mod Account { assert(token_address != 0.try_into().unwrap(), 'Token address cannot be 0'); self.approved_tokens.write(symbol, token_address); - + let bridge_address = self.liquidity_bridge.read(); - + let token_dispatcher = IERC20Dispatcher { contract_address: token_address }; - token_dispatcher.approve(bridge_address, 10000000000000000000); - + self.emit(TokenApproved { user: get_caller_address(), symbol, @@ -159,6 +170,25 @@ mod Account { fn get_initialized_status(self: @ContractState) -> bool { self.initialized.read() } + + // --- New functions for fiat handling --- + fn deposit_fiat(ref self: ContractState, currency: felt252, amount: u128) { + assert(currency != 0, 'Currency must be provided'); + assert(amount > 0, 'Amount must be > 0'); + let caller = get_caller_address(); + let current_balance = self.fiat_balance.read((caller, currency)); + self.fiat_balance.write((caller, currency), current_balance + amount); + self.emit(FiatDeposit { user: caller, currency, amount }); + } + + fn withdraw_fiat(ref self: ContractState, currency: felt252, amount: u128, recipient: ContractAddress) { + assert(currency != 0, 'Currency must be provided'); + assert(amount > 0, 'Amount must be > 0'); + let caller = get_caller_address(); + let balance = self.fiat_balance.read((caller, currency)); + assert(balance >= amount, 'Insufficient balance'); + self.fiat_balance.write((caller, currency), balance - amount); + self.emit(FiatWithdrawal { account_address: caller, currency, amount, recipient }); + } } } -