Skip to content

Commit

Permalink
add mini proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
guoshijiang committed Dec 10, 2024
1 parent f967519 commit 94962a0
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 23 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Show Forge version
run: |
forge --version
- name: Run Forge fmt
run: |
forge fmt --check
id: fmt

- name: Run Forge build
run: |
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
.idea
lib
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

68 changes: 66 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,66 @@
# miniproxy
miniproxy
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
6 changes: 6 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 1eea5b
23 changes: 23 additions & 0 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "forge-std/Script.sol";
import "../src/LogicContract.sol";
import "../src/MiniProxyFactory.sol";

contract Deploy is Script {
function run() external {
vm.startBroadcast();

LogicContract logicContract = new LogicContract();
console.log("Logic contract deployed at:", address(logicContract));

MiniProxyFactory miniProxyFactory = new MiniProxyFactory();
console.log("ProxyFactory contract deployed at:", address(miniProxyFactory));

address proxy = miniProxyFactory.createProxy(address(logicContract));
console.log("Proxy contract deployed at:", proxy);

vm.stopBroadcast();
}
}
10 changes: 10 additions & 0 deletions src/LogicContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

contract LogicContract {
uint256 public value;

function setValue(uint256 _value) external {
value = _value;
}
}
22 changes: 22 additions & 0 deletions src/MiniProxyFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

contract MiniProxyFactory {
event ProxyCreated(address proxyAddress);

function createProxy(address implementation) external returns (address) {
bytes memory bytecode = abi.encodePacked(
hex"3d602d80600a3d3981f3",
hex"363d3d373d3d3d363d73",
implementation,
hex"5af43d82803e903d91602b57fd5bf3"
);
address proxy;
assembly {
proxy := create(0, add(bytecode, 0x20), mload(bytecode))
}
require(proxy != address(0), "Proxy creation failed");
emit ProxyCreated(proxy);
return proxy;
}
}
25 changes: 25 additions & 0 deletions test/ProxyTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../src/LogicContract.sol";
import "../src/MiniProxyFactory.sol";

contract ProxyTest is Test {
LogicContract public logic;
MiniProxyFactory public factory;
LogicContract public proxyLogic;

function setUp() public {
logic = new LogicContract();
factory = new MiniProxyFactory();
address proxy = factory.createProxy(address(logic));
proxyLogic = LogicContract(proxy);
}

function testSetValueThroughProxy() public {
proxyLogic.setValue(42);
uint256 value = proxyLogic.value();
assertEq(value, 42);
}
}

0 comments on commit 94962a0

Please sign in to comment.