File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ // SPDX-License-Identifier: MIT
2+ pragma solidity ^ 0.8.0 ;
3+
4+ contract TokenVault {
5+ mapping (address => uint256 ) public balances;
6+ uint256 public totalValue;
7+
8+ // A simple, unoptimized deposit function
9+ function deposit (uint256 amount ) public payable {
10+ require (msg .value == amount, "Must send exact ETH amount " );
11+ balances[msg .sender ] += amount;
12+ totalValue += amount;
13+ // ... imagine logic for interacting with a separate ERC20 token here
14+ }
15+
16+ // A slightly complex function for optimization comparison
17+ function _transferEth (address recipient , uint256 amount ) internal {
18+ // Option 1: Low-level call (more gas efficient for simple transfers)
19+ (bool success , ) = recipient.call {value: amount}("" );
20+ require (success, "ETH transfer failed " );
21+ }
22+
23+ function withdrawOptimized (uint256 amount ) public {
24+ require (balances[msg .sender ] >= amount, "Insufficient balance " );
25+ balances[msg .sender ] -= amount;
26+ totalValue -= amount;
27+ _transferEth (msg .sender , amount); // Uses the optimized internal transfer
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments