Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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

- 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
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ states/

# Optional
.env
.env.local
.env.local

# Foundry
.env.foundry
out/
cache/

# VSCode
.vscode/

# Node
node_modules/
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "lib/openzeppelin-contracts-upgradeable"]
path = lib/openzeppelin-contracts-upgradeable
url = https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable
73 changes: 58 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,66 @@
# Smart Modules
## Foundry

**Smart Modules** is a curated collection of Solidity components built for developers who want to master real-world smart contract engineering.
**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

This repository is part of an [Solidity Bootcamp](https://bootcamp.solidity.university) program by [Solidity University](https://solidity.university), covering core topics like:
Foundry consists of:

- ✅ Multisig wallets
- ✅ Gasless transactions
- ✅ EIP-712 signatures
- ✅ TBA
- **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

> 🚧 **This repository is under active development.** Expect new modules, upgrades, and educational content in upcoming commits.
https://book.getfoundry.sh/

---
## Usage

## 🚀 Getting Started
### Build

```bash
git clone https://github.com/solidity-university/smart-modules.git
cd smart-modules
forge install
forge 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
```
7 changes: 7 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
solc_version = "0.8.30"

# 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 77041d
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at e4f702
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts-upgradeable
26 changes: 26 additions & 0 deletions src/AccessManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

import "@openzeppelin/contracts/access/AccessControl.sol";
import "./Roles.sol";

contract AccessManager is AccessControl {
using Roles for bytes32;

constructor() {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_setRoleAdmin(Roles.MULTISIG_ADMIN_ROLE, DEFAULT_ADMIN_ROLE);
}

function addMultisigAdmin(address _multisigAdmin) external onlyRole(DEFAULT_ADMIN_ROLE) {
_grantRole(Roles.MULTISIG_ADMIN_ROLE, _multisigAdmin);
}

function removeMultisigAdmin(address _multisigAdmin) external onlyRole(DEFAULT_ADMIN_ROLE) {
_revokeRole(Roles.MULTISIG_ADMIN_ROLE, _multisigAdmin);
}

function isMultisigAdmin(address _address) external view returns (bool) {
return hasRole(Roles.MULTISIG_ADMIN_ROLE, _address);
}
}
68 changes: 68 additions & 0 deletions src/EIP712Swap.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

import "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "./LiquidityPool.sol";
import "./ISwap.sol";

contract EIP712Swap is EIP712 {
using ECDSA for bytes32;

mapping(address => uint256) private _nonces;

bytes32 private constant SWAP_TYPEHASH = keccak256(
"SwapRequest(address pool,address sender,address tokenIn,address tokenOut,uint256 amountIn,uint256 minAmountOut,uint256 nonce,uint256 deadline)"
);

error InvalidSignature();
error ExpiredSwapRequest();
error InvalidNonce();

constructor() EIP712("EIP712Swap", "1") {}

function getDomainSeparator() public view returns (bytes32) {
return _domainSeparatorV4();
}

function getNonce(address _sender) public view returns (uint256) {
return _nonces[_sender];
}

function verify(ISwap.SwapRequest memory _swapRequest, bytes memory _signature) public view returns (bool) {
bytes32 digest = _hashTypedDataV4(
keccak256(
abi.encode(
SWAP_TYPEHASH,
_swapRequest.pool,
_swapRequest.sender,
_swapRequest.tokenIn,
_swapRequest.tokenOut,
_swapRequest.amountIn,
_swapRequest.minAmountOut,
_swapRequest.nonce,
_swapRequest.deadline
)
)
);
address signer = digest.recover(_signature);
return signer == _swapRequest.sender;
}

function executeSwap(ISwap.SwapRequest memory _swapRequest, bytes memory _signature) public returns (bool) {
if (!verify(_swapRequest, _signature)) revert InvalidSignature();
if (_swapRequest.deadline < block.timestamp) revert ExpiredSwapRequest();
if (_swapRequest.nonce != _nonces[_swapRequest.sender]) revert InvalidNonce();

_nonces[_swapRequest.sender]++;
LiquidityPool(_swapRequest.pool).swap(
_swapRequest.sender,
_swapRequest.tokenIn,
_swapRequest.tokenOut,
_swapRequest.amountIn,
_swapRequest.minAmountOut
);

return true;
}
}
30 changes: 30 additions & 0 deletions src/FeeManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

import "./ISwap.sol";

contract FeeManager is Initializable, AccessControlUpgradeable, UUPSUpgradeable {
uint256 public constant FEE_DENOMINATOR = 10000;

uint256 public fee;

function initialize(uint256 _fee) external initializer {
_disableInitializers();
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
fee = _fee;
}

function _authorizeUpgrade(address newImplementation) internal override onlyRole(DEFAULT_ADMIN_ROLE) {}

function setFee(uint256 _fee) external onlyRole(DEFAULT_ADMIN_ROLE) {
fee = _fee;
}

function getFee(ISwap.SwapParams memory swapParams) external view returns (uint256) {
return (swapParams.amount0 * fee) / FEE_DENOMINATOR;
}
}
38 changes: 38 additions & 0 deletions src/ISwap.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

interface ISwap {
/// @notice Parameters for the swap
/// @param token0 The address of the first token
/// @param token1 The address of the second token
/// @param amount0 The amount of the first token to swap
/// @param reserveToken0 The reserve of the first token
/// @param reserveToken1 The reserve of the second token
struct SwapParams {
address token0;
address token1;
uint256 amount0;
uint256 reserveToken0;
uint256 reserveToken1;
}

/// @notice Parameters for the swap request
/// @param pool The address of the pool
/// @param sender The address of the sender
/// @param tokenIn The address of the token to swap in
/// @param tokenOut The address of the token to swap out
/// @param amountIn The amount of the token to swap in
/// @param minAmountOut The minimum amount of the token to swap out
/// @param nonce The nonce of the swap
/// @param deadline The deadline of the swap
struct SwapRequest {
address pool;
address sender;
address tokenIn;
address tokenOut;
uint256 amountIn;
uint256 minAmountOut;
uint256 nonce;
uint256 deadline;
}
}
Loading
Loading