Skip to content

Commit 861ac0f

Browse files
KaelSenseiclaude
andcommitted
fix: emit events before external calls in remaining contracts
Apply check-effects-interactions pattern to: - CrowdFund.unpledge() - CustomError.withdraw() - Payable.withdraw() and withdrawTo() - SendingEther.sendViaSend() - TokenLocker.withdraw() - UniswapV3Swap.swapExactInputSingle() All 945 tests passing. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent 6beb9c7 commit 861ac0f

6 files changed

Lines changed: 20 additions & 13 deletions

File tree

src/basic/CustomError.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ contract CustomError {
5757

5858
balances[msg.sender] = balance - amount;
5959

60+
// Emit event before external call
61+
emit Withdrawal(msg.sender, amount);
62+
6063
(bool success,) = payable(msg.sender).call{value: amount}("");
6164
if (!success) revert("Transfer failed");
62-
63-
emit Withdrawal(msg.sender, amount);
6465
}
6566

6667
/// @notice Transfer balance to another address using custom errors

src/basic/Payable.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,21 @@ contract Payable {
5353
function withdraw() external {
5454
require(msg.sender == owner, "Not owner");
5555
uint256 balance = address(this).balance;
56+
// Emit event before external call
57+
emit EtherSent(owner, balance);
5658
(bool success,) = payable(owner).call{value: balance}("");
5759
require(success, "Transfer failed");
58-
emit EtherSent(owner, balance);
5960
}
6061

6162
/// @notice Withdraw to specific address
6263
function withdrawTo(address payable to, uint256 amount) external {
6364
require(msg.sender == owner, "Not owner");
6465
require(to != address(0), "Zero address");
6566
require(amount <= address(this).balance, "Insufficient balance");
67+
// Emit event before external call
68+
emit EtherSent(to, amount);
6669
(bool success,) = to.call{value: amount}("");
6770
require(success, "Transfer failed");
68-
emit EtherSent(to, amount);
6971
}
7072
}
7173

src/basic/SendingEther.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ contract SendingEther {
2828
/// @notice Send via send (returns bool, 2300 gas)
2929
function sendViaSend(uint256 amount) external payable returns (bool) {
3030
require(msg.value == amount, "Value mismatch");
31+
// Emit event before external call
32+
emit Sent(receiver, amount, "send");
3133
bool success = receiver.send(amount);
3234
require(success, "Send failed");
33-
emit Sent(receiver, amount, "send");
3435
return success;
3536
}
3637

src/defi/CrowdFund.sol

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,16 @@ contract CrowdFund {
8888
function unpledge(uint256 amount) external {
8989
require(amount > 0, "Cannot unpledge 0");
9090
require(pledges[msg.sender] >= amount, "Insufficient pledge");
91-
91+
9292
// Update state
9393
pledges[msg.sender] -= amount;
9494
totalPledged -= amount;
95-
95+
96+
// Emit event before external call
97+
emit Unpledged(msg.sender, amount);
98+
9699
// Transfer tokens back to pledger
97100
if (!token.transfer(msg.sender, amount)) revert TransferFailed();
98-
99-
emit Unpledged(msg.sender, amount);
100101
}
101102

102103
/// @notice Claim funds if goal is met (only creator)

src/defi/TokenLocker.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ contract TokenLocker {
8686

8787
lock.withdrawn = true;
8888

89-
if (!IERC20Locker(lock.token).transfer(msg.sender, lock.amount)) revert TransferFailed();
90-
89+
// Emit event before external call
9190
emit Withdrawn(lockId, msg.sender, lock.amount);
91+
92+
if (!IERC20Locker(lock.token).transfer(msg.sender, lock.amount)) revert TransferFailed();
9293
}
9394

9495
/// @notice Get the details of a lock

src/defi/UniswapV3Swap.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ contract UniswapV3Swap {
9898
sqrtPriceLimitX96: 0
9999
});
100100

101-
amountOut = router.exactInputSingle(params);
102-
101+
// Emit event before external call
103102
emit SwapCompleted(tokenIn, tokenOut, amountIn, amountOut);
103+
104+
amountOut = router.exactInputSingle(params);
104105
}
105106

106107
/// @notice Execute exact output single hop swap

0 commit comments

Comments
 (0)