Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
6boris committed Oct 7, 2023
1 parent fd5de10 commit 22677c9
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 234 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lib/** linguist-vendored
foundry/lib/** linguist-vendored
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ all: fmt test
@echo Run all scripts ...

fmt:
yarn lint && yarn lint:sol && yarn prettier:write
yarn prettier:write && yarn lint && yarn lint:sol

test:
forge test
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
# Foundry Template
# Awesome Web3 Contracts

## Install Dependency

```yarn
yarn
```

```bash

git submodule update --rebase --remote
# OR
forge install OpenZeppelin/openzeppelin-contracts@v5.0.0 --no-commit
forge install foundry-rs/forge-std@v1.7.1 --no-commit
forge install PaulRBerg/prb-test@v0.6.5 --no-commit
```

```bash
# run test case
forge test

anvil
# run local node
anvil -f sepolia

# run script
forge script foundry/script/Deploy.s.sol --fork-url http://localhost:8545 --broadcast
```

## Ethernaut

Ethernaut is a Web3 / Solidity based adversarial game inspired by overthewire.org, running on the Ethernaut virtual
machine. Each level is a smart contract that needs to be hacked.

| Status | Level | Docs | Video | Note |
| :----: | :-------------------- | :----------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------: | :--: |
|| [0.Hello Ethernaut]() | ... | [YouTube](https://www.youtube.com/watch?v=BE0J7I13CPo)[BILIBILI](https://www.bilibili.com/video/BV1GV411w7bk) | ... |
|| [1.Fallback]() | [Mirror](https://mirror.xyz/leekdev.eth/fQn4QQF3iIwhOdolLmQpTTB5M7R9RWcvFaRFLdTodPM) | [YouTube](https://www.youtube.com/watch?v=6SZcd4bbTjQ)[BILIBILI](https://www.bilibili.com/video/BV1qu411u7Rn) | ... |
|| [2.Fallout]() | [Mirror](https://mirror.xyz/leekdev.eth/GuFMbyyOnKqJOp49vRugrk_EWr2-JUIc9y-kJVWONeg) | [YouTube](https://www.youtube.com/watch?v=Q2EYC1a4VVM)[BILIBILI](https://www.bilibili.com/video/BV1TC4y1o7Up/) | ... |
| | [3.CoinFlip]() | ... | ... | ... |
| | [4.Telephone]() | ... | ... | ... |
| | [5.Token]() | ... | ... | ... |
| | [6.Delegate]() | ... | ... | ... |
| | [7.Force]() | ... | ... | ... |
| | [8.Vault]() | ... | ... | ... |
| | [9.King]() | ... | ... | ... |
| | [10.Reentrance]() | ... | ... | ... |
Empty file added contracts/.gitkeep
Empty file.
86 changes: 3 additions & 83 deletions contracts/Ethernaut/00_Ethernaut.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { Test } from "forge-std/Test.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";

abstract contract Level is Ownable {
abstract contract Level is Ownable(msg.sender) {
function createInstance(address _player) public payable virtual returns (address);
function validateInstance(address payable _instance, address _player) public virtual returns (bool);
}

abstract contract Ethernaut is Ownable {
// string public name;

// constructor(string memory _name) {
// _name;
// }
contract Ethernaut is Ownable(msg.sender) {
// ----------------------------------
// Owner interaction
// ----------------------------------
Expand Down Expand Up @@ -78,77 +72,3 @@ abstract contract Ethernaut is Ownable {
return false; // Return data - not possible to read emitted events via solidity
}
}

abstract contract BaseTest is Test, Ethernaut {
// Utilities internal utilities;
Ethernaut private ethernaut;
Level internal levelFactory;
address payable internal levelAddress;

address payable[] internal users;
uint256 private userCount;
uint256 private userInitialFunds = 100 ether;
string[] private userLabels;

// address payable internal owner;
address payable internal player;

constructor() {
userCount = 2;
userInitialFunds = 5 ether;

// userLabels = new string[](2);
// userLabels.push("Owner");
// userLabels.push("Player");
}

function setUp() public virtual {
require(address(levelFactory) != address(0), "level not setup");

// utilities = new Utilities();
// ethernaut = Ethernaut();
registerLevel(levelFactory);

if (userCount > 0) {
// check which one we need to call
// users = utilities.createUsers(userCount, userInitialFunds, userLabels);
// owner = users[0];
// player = users[1];
}
}

function createLevelInstance(bool fromPlayer) external payable returns (address) {
if (fromPlayer) {
vm.prank(player);
}
return ethernaut.createLevelInstance{ value: msg.value }(levelFactory);
}

function runLevel() public {
// run the exploit
setupLevel();

// run the exploit
exploitLevel();

// verify the exploit
checkSuccess();
}

function setupLevel() internal virtual {
/* IMPLEMENT YOUR EXPLOIT */
}

function exploitLevel() internal virtual {
/* IMPLEMENT YOUR EXPLOIT */
}

function checkSuccess() internal {
/* CHECK SUCCESS */
vm.startPrank(player);
bool success = ethernaut.submitLevelInstance(payable(levelAddress));
assertTrue(success, "Solution is not solving the level");

vm.stopPrank();
}
}
4 changes: 2 additions & 2 deletions contracts/Ethernaut/01_Fallback.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
Expand Down Expand Up @@ -54,7 +54,7 @@ contract Fallback {
}
}

abstract contract FallbackFactory is Level {
contract FallbackFactory is Level {
function createInstance(address _player) public payable override returns (address) {
_player;
Fallback instance = new Fallback();
Expand Down
52 changes: 52 additions & 0 deletions contracts/Ethernaut/02_Fallout.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { Level } from "./00_Ethernaut.sol";

contract Fallout {
using Math for uint256;

mapping(address account => uint256 balance) private allocations;
address payable public owner;

function Fal1out() public payable {
owner = payable(msg.sender); // Type issues must be payable address
allocations[owner] = msg.value;
}

modifier onlyOwner() {
require(msg.sender == owner, "caller is not the owner");
_;
}

function allocate() public payable {
(, allocations[msg.sender]) = allocations[msg.sender].tryAdd(msg.value);
}

function sendAllocation(address payable allocator) public {
require(allocations[allocator] > 0, "not have enough balance");
allocator.transfer(allocations[allocator]);
}

function collectAllocations() public onlyOwner {
payable(msg.sender).transfer(address(this).balance); // Type issues must be payable address
}

function allocatorBalance(address allocator) public view returns (uint256) {
return allocations[allocator];
}
}

contract FalloutFactory is Level {
function createInstance(address _player) public payable override returns (address) {
_player;
Fallout instance = new Fallout();
return address(instance);
}

function validateInstance(address payable _instance, address _player) public view override returns (bool) {
Fallout instance = Fallout(_instance);
return instance.owner() == _player;
}
}
8 changes: 0 additions & 8 deletions contracts/Foo.sol

This file was deleted.

32 changes: 19 additions & 13 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
evm_version = "paris" # See https://www.evmdiff.com/features?name=PUSH0&kind=opcode
fuzz = { runs = 1_000 }
gas_reports = ["*"]
optimizer = true
optimizer_runs = 10_000
solc = "0.8.21"
src = "contracts"
libs = ["foundry/lib"]
test = "foundry/test"
script = "foundry/script"
out = "foundry/out"
broadcast = 'foundry/broadcast'
cache_path = 'foundry/cache'
optimizer = true
optimizer_runs = 10_000

solc = "0.8.21"



[profile.ci]
Expand Down Expand Up @@ -48,13 +46,21 @@
wrap_comments = true

[rpc_endpoints]
arbitrum_one = "https://arbitrum-mainnet.infura.io/v3/${API_KEY_INFURA}"
avalanche = "https://avalanche-mainnet.infura.io/v3/${API_KEY_INFURA}"
bnb_smart_chain = "https://bsc-dataseed.binance.org"
gnosis_chain = "https://rpc.gnosischain.com"
goerli = "https://goerli.infura.io/v3/${API_KEY_INFURA}"
localhost = "http://localhost:8545"

mainnet = "https://eth-mainnet.g.alchemy.com/v2/${API_KEY_ALCHEMY}"
optimism = "https://optimism-mainnet.infura.io/v3/${API_KEY_INFURA}"
polygon = "https://polygon-mainnet.infura.io/v3/${API_KEY_INFURA}"
sepolia = "https://sepolia.infura.io/v3/${API_KEY_INFURA}"
goerli = "https://eth-goerli.g.alchemy.com/v2/${API_KEY_ALCHEMY}"
sepolia = "https://eth-sepolia.g.alchemy.com/v2/${API_KEY_ALCHEMY}"

polygon = "https://polygon-mainnet.g.alchemy.com/v2/${API_KEY_ALCHEMY}"
mumbai = "https://polygon-mumbai.g.alchemy.com/v2/${API_KEY_ALCHEMY}"

base_mainnet = "https://base-mainnet.g.alchemy.com/v2/${API_KEY_ALCHEMY}"
base_goerli = "https://base-goerli.g.alchemy.com/v2/${API_KEY_ALCHEMY}"

arbitrum_mainnet = "https://arb-mainnet.g.alchemy.com/v2/${API_KEY_ALCHEMY}"
arbitrum_goerli = "https://arb-goerli.g.alchemy.com/v2/${API_KEY_ALCHEMY}"

optimism_mainnet = "https://opt-mainnet.g.alchemy.com/v2/${API_KEY_ALCHEMY}"
optimism_goerli = "https://opt-goerli.g.alchemy.com/v2/${API_KEY_ALCHEMY}"

1 change: 0 additions & 1 deletion foundry/lib/prb-test
Submodule prb-test deleted from 2ece87
Empty file added foundry/script/.gitkeep
Empty file.
41 changes: 0 additions & 41 deletions foundry/script/Base.s.sol

This file was deleted.

13 changes: 0 additions & 13 deletions foundry/script/Deploy.s.sol

This file was deleted.

Empty file added foundry/test/.gitkeep
Empty file.
Loading

0 comments on commit 22677c9

Please sign in to comment.