Skip to content

Commit

Permalink
Add foundry for invariant testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiascaricato committed May 23, 2023
1 parent d454365 commit f376059
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 1 deletion.
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
15 changes: 15 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[profile.default]
src = 'contracts'
out = 'out'
libs = ['node_modules', 'lib']
test = 'test'
cache_path = 'cache_forge'
solc = '0.8.18'

[fuzz]
runs = 1_000
depth = 100

# Extreme Fuzzing CI Profile
[profile.ci]
runs = 100_000
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HardhatUserConfig } from 'hardhat/config';
import '@nomicfoundation/hardhat-toolbox';
import '@nomicfoundation/hardhat-chai-matchers';
import "@nomicfoundation/hardhat-foundry";
import '@openzeppelin/hardhat-upgrades';
import dotenv from 'dotenv';

Expand Down
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at fc560f
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
"url": "git+https://github.com/mountainprotocol/tokens.git"
},
"author": "Mati <mattiascaricato@gmail.com>",
"license": "UNLICENSED",
"license": "MIT",
"bugs": {
"url": "https://github.com/mountainprotocol/tokens/issues"
},
"homepage": "https://github.com/mountainprotocol/tokens#readme",
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^1.0.6",
"@nomicfoundation/hardhat-foundry": "^1.0.1",
"@nomicfoundation/hardhat-toolbox": "^2.0.1",
"@openzeppelin/hardhat-upgrades": "^1.22.1",
"@openzeppelin/test-helpers": "^0.5.16",
Expand Down
7 changes: 7 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ensdomains/=node_modules/@ensdomains/
@openzeppelin/contracts/=node_modules/@openzeppelin/contracts/
@openzeppelin/contracts-upgradeable/=node_modules/@openzeppelin/contracts-upgradeable/
ds-test/=lib/forge-std/lib/ds-test/src/
eth-gas-reporter/=node_modules/eth-gas-reporter/
forge-std/=lib/forge-std/src/
hardhat/=node_modules/hardhat/
59 changes: 59 additions & 0 deletions test/USDMTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

import {Test} from "forge-std/Test.sol";
import {console} from "forge-std/console.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

import "../contracts/USDM.sol";

contract UUPSProxy is ERC1967Proxy {
constructor(address _implementation, bytes memory _data)
ERC1967Proxy(_implementation, _data)
{}
}

contract Handler {
USDM public usdm;

constructor(USDM _usdm) {
usdm = _usdm;
}
}

contract USDMInvariants is Test {
USDM implementation;
UUPSProxy proxy;
USDM usdm;
Handler handler;

function setUp() public {
// deploy implementation contract
implementation = new USDM();

// deploy proxy contract and point it to implementation
proxy = new UUPSProxy(address(implementation), "");

// wrap in ABI to support easier calls
usdm = USDM(address(proxy));

usdm.initialize("Mountain Protocol USD", "USDM", 0);

handler = new Handler(usdm);

// targetContract(address(handler));
// targetContract(address(implementation));
}

function invariant_totalSupply() public {
assertEq(usdm.balanceOf(address(this)), usdm.totalSupply());
}

function invariant_totalShares() public {
assertEq(usdm.sharesOf(address(this)), usdm.totalShares());
}

function invariant_totalShares_totalSupply() public {
assertEq(usdm.totalSupply(), usdm.totalShares());
}
}

0 comments on commit f376059

Please sign in to comment.