Skip to content

Commit 0fb48fa

Browse files
committed
added tokenVault contract
1 parent 589e6a7 commit 0fb48fa

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/TokenVault.sol

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)