Skip to content

Commit

Permalink
event escrow for deposit claim and cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
MSghais committed Jun 18, 2024
1 parent acca6ab commit 9641e15
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions onchain/src/social/deposit.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ pub trait IDepositEscrow<TContractState> {
fn claim(ref self: TContractState, request: SocialRequest<DepositId>);
}

#[derive(Drop, starknet::Event)]
struct ClaimEvent {
#[key]
sender: ContractAddress,
amount: u256,
token_address: ContractAddress,
recipient: NostrPublicKey,
}

#[derive(Drop, starknet::Event)]
struct DepositEvent {
#[key]
sender: ContractAddress,
amount: u256,
token_address: ContractAddress,
recipient: NostrPublicKey,
}

#[derive(Drop, starknet::Event)]
struct CancelEvent {
#[key]
sender: ContractAddress,
amount: u256,
token_address: ContractAddress,
recipient: NostrPublicKey,
}


#[starknet::contract]
pub mod DepositEscrow {
Expand All @@ -55,7 +82,8 @@ pub mod DepositEscrow {
};

use super::{
Deposit, DepositId, DepositResult, IDepositEscrow, NostrPublicKey, DepositIdEncodeImpl
Deposit, DepositId, DepositResult, IDepositEscrow, NostrPublicKey, DepositIdEncodeImpl,
CancelEvent, DepositEvent, ClaimEvent
};

impl DepositDefault of Default<Deposit> {
Expand All @@ -78,6 +106,14 @@ pub mod DepositEscrow {
nostr_to_sn: LegacyMap<NostrPublicKey, ContractAddress>
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
ClaimEvent: ClaimEvent,
DepositEvent: DepositEvent,
CancelEvent: CancelEvent,
}

#[constructor]
fn constructor(ref self: ContractState) {
self.next_deposit_id.write(1);
Expand Down Expand Up @@ -122,6 +158,7 @@ pub mod DepositEscrow {
ttl: get_block_timestamp() + timelock
}
);
self.emit(DepositEvent { sender:get_caller_address(), recipient:nostr_recipient, amount:amount, token_address:token_address });

DepositResult::Deposit(deposit_id)
}
Expand All @@ -137,7 +174,7 @@ pub mod DepositEscrow {
let erc20 = IERC20Dispatcher { contract_address: deposit.token_address };

erc20.transfer(get_caller_address(), deposit.amount);

self.emit(CancelEvent { sender:get_caller_address(), recipient:deposit.recipient, amount:deposit.amount, token_address:deposit.token_address });
self.deposits.write(deposit_id, Default::default());
}

Expand All @@ -152,6 +189,7 @@ pub mod DepositEscrow {
erc20.transfer(get_caller_address(), deposit.amount);

self.nostr_to_sn.write(request.public_key, get_caller_address());
self.emit(ClaimEvent { sender:get_caller_address(), recipient:request.public_key, amount:deposit.amount, token_address:deposit.token_address });
self.deposits.write(deposit_id, Default::default());
}
}
Expand Down

0 comments on commit 9641e15

Please sign in to comment.