From e4ff3b63ee0a18906390c342639be707e8838c57 Mon Sep 17 00:00:00 2001 From: MSG <59928086+MSghais@users.noreply.github.com> Date: Thu, 20 Jun 2024 16:48:42 +0200 Subject: [PATCH] event escrow for deposit claim and cancel (#184) * event escrow for deposit claim and cancel * review: key deposit_id and emit in end of function * import transfer event * format scarb check * key index + transfer event if recipient alredy link * fmt + starknet address on events * rename to nostr_recipient --- onchain/src/social/deposit.cairo | 104 ++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/onchain/src/social/deposit.cairo b/onchain/src/social/deposit.cairo index 0a6488ad..69f95120 100644 --- a/onchain/src/social/deposit.cairo +++ b/onchain/src/social/deposit.cairo @@ -40,7 +40,6 @@ pub trait IDepositEscrow { fn claim(ref self: TContractState, request: SocialRequest); } - #[starknet::contract] pub mod DepositEscrow { use core::num::traits::Zero; @@ -55,7 +54,7 @@ pub mod DepositEscrow { }; use super::{ - Deposit, DepositId, DepositResult, IDepositEscrow, NostrPublicKey, DepositIdEncodeImpl + Deposit, DepositId, DepositResult, IDepositEscrow, NostrPublicKey, DepositIdEncodeImpl, }; impl DepositDefault of Default { @@ -78,6 +77,65 @@ pub mod DepositEscrow { nostr_to_sn: LegacyMap } + #[derive(Drop, starknet::Event)] + struct ClaimEvent { + #[key] + deposit_id: DepositId, + #[key] + sender: ContractAddress, + #[key] + nostr_recipient: NostrPublicKey, + #[key] + starknet_recipient: ContractAddress, + amount: u256, + token_address: ContractAddress, + } + + #[derive(Drop, starknet::Event)] + struct DepositEvent { + #[key] + deposit_id: DepositId, + #[key] + sender: ContractAddress, + #[key] + nostr_recipient: NostrPublicKey, + amount: u256, + token_address: ContractAddress, + } + + #[derive(Drop, starknet::Event)] + struct CancelEvent { + #[key] + deposit_id: DepositId, + #[key] + sender: ContractAddress, + #[key] + nostr_recipient: NostrPublicKey, + amount: u256, + token_address: ContractAddress, + } + + #[derive(Drop, starknet::Event)] + struct TransferEvent { + #[key] + sender: ContractAddress, + #[key] + nostr_recipient: NostrPublicKey, + #[key] + starknet_recipient: ContractAddress, + amount: u256, + token_address: ContractAddress, + } + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + ClaimEvent: ClaimEvent, + DepositEvent: DepositEvent, + CancelEvent: CancelEvent, + TransferEvent: TransferEvent, + } + #[constructor] fn constructor(ref self: ContractState) { self.next_deposit_id.write(1); @@ -101,6 +159,16 @@ pub mod DepositEscrow { if (!recipient.is_zero()) { let erc20 = IERC20Dispatcher { contract_address: token_address }; erc20.transfer_from(get_caller_address(), recipient, amount); + self + .emit( + TransferEvent { + sender: get_caller_address(), + nostr_recipient, + starknet_recipient: recipient, + amount: amount, + token_address: token_address + } + ); return DepositResult::Transfer(recipient); } @@ -122,6 +190,16 @@ pub mod DepositEscrow { ttl: get_block_timestamp() + timelock } ); + self + .emit( + DepositEvent { + deposit_id, + sender: get_caller_address(), + nostr_recipient, + amount: amount, + token_address: token_address + } + ); DepositResult::Deposit(deposit_id) } @@ -137,8 +215,17 @@ pub mod DepositEscrow { let erc20 = IERC20Dispatcher { contract_address: deposit.token_address }; erc20.transfer(get_caller_address(), deposit.amount); - self.deposits.write(deposit_id, Default::default()); + self + .emit( + CancelEvent { + deposit_id, + sender: get_caller_address(), + nostr_recipient: deposit.recipient, + amount: deposit.amount, + token_address: deposit.token_address + } + ); } fn claim(ref self: ContractState, request: SocialRequest) { @@ -153,6 +240,17 @@ pub mod DepositEscrow { self.nostr_to_sn.write(request.public_key, get_caller_address()); self.deposits.write(deposit_id, Default::default()); + self + .emit( + ClaimEvent { + deposit_id, + sender: get_caller_address(), + nostr_recipient: request.public_key, + amount: deposit.amount, + starknet_recipient: get_caller_address(), + token_address: deposit.token_address + } + ); } } }