From 6bcf5ba04f3887c7e454352a658fe0680ea2c8fc Mon Sep 17 00:00:00 2001 From: Nilesh Gupta Date: Wed, 20 Nov 2024 17:37:50 +0530 Subject: [PATCH] fix: fixed addDelegate and removeDelegate emitting events when not necessary --- src/lib.cairo | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/lib.cairo b/src/lib.cairo index ae40474..3ce4c97 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -348,14 +348,20 @@ pub mod PushComm { fn add_delegate(ref self: ContractState, delegate: ContractAddress) { let channel = get_caller_address(); - self.delegated_notification_senders.entry(channel).write(delegate, true); - self.emit(AddDelegate { channel: channel, delegate: delegate }); + + if !self.delegated_notification_senders.entry(channel).entry(delegate).read() { + self.delegated_notification_senders.entry(channel).write(delegate, true); + self.emit(AddDelegate { channel: channel, delegate: delegate }); + } } fn remove_delegate(ref self: ContractState, delegate: ContractAddress) { let channel = get_caller_address(); - self.delegated_notification_senders.entry(channel).write(delegate, false); - self.emit(RemoveDelegate { channel: channel, delegate: delegate }); + + if self.delegated_notification_senders.entry(channel).entry(delegate).read() { + self.delegated_notification_senders.entry(channel).write(delegate, false); + self.emit(RemoveDelegate { channel: channel, delegate: delegate }); + } } fn send_notification(