Replies: 1 comment
-
Yeah CEI is an industry standard that should always be followed but if your situation requires otherwise then do so as long as you are sure of what you are doing and you know why you are doing it. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I'm currently studying Smart Contract Security, specifically the lesson 21. Recon Continued (again) from the course. Patrick mentioned that functions should follow the Check-Effects-Interaction (CEI) pattern. However, I’ve encountered a potential issue when applying this in the following scenario:
The function I’m working with is designed to lock tokens in a vault and emit a
Deposit
event, which triggers an L2 minting process. Off-chain services are listening for this event and will mint the corresponding tokens on L2. Patrick said that the function should strictly follow the CEI pattern, where theemit
happens before the actual transfer (usingsafeTransferFrom
). Here’s the code snippet:However, my concern is that emitting the
Deposit
event before callingsafeTransferFrom
could be potentially problematic.The issue is that by emitting the
Deposit
event before the transfer, there’s a risk that someone listening to this event (on L2, for example) could mint tokens there without the actual transfer being successful on L1. If the transfer later fails or is reverted, the event has already been emitted, leading to a potential inconsistency between the layers.My question is:
Wouldn't it be dangerous to emit the event before ensuring the success of the transfer? By emitting it early, isn’t there a chance that listeners (such as L2 nodes) could mint tokens based on the event even if the transfer fails, causing discrepancies between the layers?
I'm trying to follow best practices but want to make sure I’m not missing anything. Any insights or suggestions on how to handle this would be greatly appreciated!
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions