Skip to content

Commit

Permalink
feat(repo): add slither to lint code (#1)
Browse files Browse the repository at this point in the history
* feat: add slither 

* fix: resolve bugs checked by slither 

* feat: add mythril

* ci: add scopes

* ci: add lint ci
  • Loading branch information
phenix3443 authored Aug 25, 2023
1 parent 4685017 commit fd8b9ec
Show file tree
Hide file tree
Showing 38 changed files with 249 additions and 193 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/lint-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ jobs:
with:
scopes: |
repo
ci
lint
doc
test
script
requireScope: true
17 changes: 14 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ on:
pull_request:

jobs:
solidity:
name: Lint Solidity Code
solhint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -17,5 +16,17 @@ jobs:
- name: Install pnpm dependencies
uses: ./.github/actions/install-pnpm-dependencies

- name: lint commit msg
- name: lint solidity code
run: pnpm lint:sol

commitlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: wagoid/commitlint-github-action@v5

slihter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: crytic/[email protected]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ lib
out
cache
broadcast
.vscode/slither-results.json
5 changes: 3 additions & 2 deletions .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"func-param-name-mixedcase": "warn",
"modifier-name-mixedcase": "warn",
"ordering": "warn",
"compiler-version": ["error", "^0.8.20"],
"func-visibility": ["warn", { "ignoreConstructors": true }]
"compiler-version": ["error", "^0.8.18"],
"func-visibility": ["warn", { "ignoreConstructors": true }],
"immutable-vars-naming": "off"
}
}
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"tintinweb.solidity-visual-auditor",
"foxundermoon.shell-format",
"timonwong.shellcheck",
"remisa.shellman"
"remisa.shellman",
"trailofbits.slither-vscode"
]
}
9 changes: 6 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"cSpell.words": ["commitlint", "mixedcase", "solhint"],
"solidity.compileUsingRemoteVersion": "v0.8.21+commit.d9974bed",
"cSpell.words": ["commitlint", "mixedcase", "remappings", "solhint"],

"[solidity]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
},
"solidity.compileUsingRemoteVersion": "v0.8.18+commit.87f61d96",
"slither.solcPath": "",
"slither.hiddenDetectors": []
}
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@
[![Lint](https://github.com/phenix3443/contract_starter/actions/workflows/lint.yml/badge.svg)](https://github.com/phenix3443/contract_starter/actions/workflows/lint.yml)

solidity contract template repository, configured as [use solidity to develop smart contract](https://blog.panghuli.cn/posts/ethereum/solidity/).

## prepare

you should install following tools:

- pnpm
- pip3
- foundry

```shell
pnpm init
pnpm install:slither
```
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ out = 'out'
libs = ["node_modules", "lib"]

# solc
solc_version = '0.8.20'
solc_version = '0.8.18'

# fmt
[fmt]
Expand Down
12 changes: 12 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# slither
pip3 install slither-analyzer
pip3 install solc-select
solc-select install 0.8.18
solc-select use 0.8.18

#mythril
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup default nightly
pip3 install mythril
12 changes: 12 additions & 0 deletions mythril.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"remappings": [
"ds-test/=lib/ds-test/src/",
"forge-std/=lib/forge-std/src/",
"@openzeppelin/contracts=node_modules/@openzeppelin/contracts",
"@openzeppelin/contracts-upgradeable=node_modules/@openzeppelin/contracts-upgradeable"
],
"optimizer": {
"enabled": true,
"runs": 200
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"build": "forge build",
"test": "forge test",
"lint:sol": "solhint src/**/*.sol script/**/*.sol test/**/*.sol",
"format:sol": "prettier --write --plugin=prettier-plugin-solidity src/**/*.sol script/**/*.sol test/**/*.sol"
"slither": "slither .",
"mythril": "myth analyze src/**/*.sol script/**/*.sol",
"format:sol": "prettier --write --plugin=prettier-plugin-solidity src/**/*.sol script/**/*.sol test/**/*.sol",
"init": "sh init.sh"
},
"repository": {
"type": "git",
Expand Down
22 changes: 22 additions & 0 deletions script/DeployTPCounterV1.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.18;

import "forge-std/Script.sol"; // solhint-disable
import {TPCounterV1} from "../src/TPCounterV1.sol";
import {DeployTPScript} from "./DeployTPScript.s.sol";

contract DeployTPCounterV1 is DeployTPScript {
address private immutable _deployer;

constructor() DeployTPScript(vm.envUint("PRIVATE_KEY")) {
_deployer = vm.envAddress("DEPLOYER");
}

//slither-disable-next-line reentrancy-no-eth
function _run() internal override deploy(_deployer) {
TPCounterV1 c = new TPCounterV1();
implementation = address(c);
data = bytes.concat(c.initialize.selector);
}
}
21 changes: 21 additions & 0 deletions script/DeployTPCounterV2.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.18;

import "forge-std/Script.sol"; // solhint-disable

import {TPCounterV2} from "../src/TPCounterV2.sol";
import {DeployTPScript} from "./DeployTPScript.s.sol";

contract DeployTPCounterV2 is DeployTPScript {
constructor() DeployTPScript(vm.envUint("PRIVATE_KEY")) {
proxyAddress = vm.envAddress("PROXY");
}

//slither-disable-next-line reentrancy-no-eth
function _run() internal override upgrade {
TPCounterV2 c = new TPCounterV2();
implementation = address(c);
data = bytes.concat(c.upgradeVersion.selector);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;
pragma solidity ^0.8.18;

import "forge-std/Script.sol"; // solhint-disable-line
// solhint-disable-next-line
import {ITransparentUpgradeableProxy, TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

abstract contract DeployScript is Script {
uint256 public privateKey;
abstract contract DeployTPScript is Script {
uint256 public immutable privateKey;
address public implementation;
bytes public data;
address public proxyAddress;

error InvalidAddress(string reason);

modifier deploy(address deployer) {
_;
if (deployer == address(0)) {
revert InvalidAddress("deployer address can not be zero");
}
_;
if (implementation == address(0)) {
revert InvalidAddress("implementation address can not be zero");
}
Expand All @@ -28,10 +28,10 @@ abstract contract DeployScript is Script {
}

modifier upgrade() {
_;
if (proxyAddress == address(0)) {
revert InvalidAddress("proxy address can not be zero");
}
_;
ITransparentUpgradeableProxy proxy = ITransparentUpgradeableProxy(
proxyAddress
);
Expand All @@ -41,6 +41,10 @@ abstract contract DeployScript is Script {
proxy.upgradeToAndCall(implementation, data);
}

constructor(uint256 pkey) {
privateKey = pkey;
}

function run() external {
vm.startBroadcast(privateKey);
_run();
Expand Down
18 changes: 18 additions & 0 deletions script/DeployUUPSCounterV1.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.18;

import "forge-std/Script.sol"; // solhint-disable
import {UUPSCounterV1} from "../src/UUPSCounterV1.sol";
import {DeployUUPSScript} from "./DeployUUPSScript.s.sol";

contract DeployUUPSCounterV1 is DeployUUPSScript {
constructor() DeployUUPSScript(vm.envUint("PRIVATE_KEY")) {}

//slither-disable-next-line reentrancy-no-eth
function _run() internal override deploy {
UUPSCounterV1 c = new UUPSCounterV1();
implementation = address(c);
data = bytes.concat(c.initialize.selector);
}
}
20 changes: 20 additions & 0 deletions script/DeployUUPSCounterV2.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.18;

import "forge-std/Script.sol"; // solhint-disable-line
import {UUPSCounterV2} from "../src/UUPSCounterV2.sol";
import {DeployUUPSScript} from "./DeployUUPSScript.s.sol";

contract DeployUUPSCounterV2 is DeployUUPSScript {
constructor() DeployUUPSScript(vm.envUint("PRIVATE_KEY")) {
proxyAddress = vm.envAddress("PROXY");
}

//slither-disable-next-line reentrancy-no-eth
function _run() internal override upgrade {
UUPSCounterV2 c = new UUPSCounterV2();
implementation = address(c);
data = bytes.concat(c.upgradeVersion.selector);
}
}
12 changes: 8 additions & 4 deletions script/uups/DeployScript.s.sol → script/DeployUUPSScript.s.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;
pragma solidity ^0.8.18;

import "forge-std/Script.sol"; // solhint-disable
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";

abstract contract DeployScript is Script {
uint256 public privateKey;
abstract contract DeployUUPSScript is Script {
uint256 public immutable privateKey;
address public implementation;
bytes public data;
address public proxyAddress;
Expand All @@ -23,17 +23,21 @@ abstract contract DeployScript is Script {
}

modifier upgrade() {
_;
if (proxyAddress == address(0)) {
revert InvalidAddress("proxy address can not be zero");
}
_;
if (implementation == address(0)) {
revert InvalidAddress("implementation address can not be zero");
}
UUPSUpgradeable proxy = UUPSUpgradeable(proxyAddress);
proxy.upgradeToAndCall(address(implementation), data);
}

constructor(uint256 pkey) {
privateKey = pkey;
}

function run() external {
vm.startBroadcast(privateKey);
_run();
Expand Down
2 changes: 2 additions & 0 deletions script/deploy_tp_counter_v1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
source .env && forge script script/transparent/DeployTPCounterV1.s.sol --rpc-url ${RPC_URL} --broadcast -vvvv
2 changes: 2 additions & 0 deletions script/deploy_tp_counter_v2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
source .env && forge script script/transparent/DeployTPCounterV2.s.sol --rpc-url ${RPC_URL} --broadcast -vvvv
2 changes: 2 additions & 0 deletions script/deploy_uups_counter_v1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
source .env && forge script script/uups/DeployUUPSCounterV1.s.sol --rpc-url ${RPC_URL} --broadcast -vvvv
3 changes: 3 additions & 0 deletions script/deploy_uups_counter_v2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# shellcheck disable=all
source .env && forge script script/uups/DeployUUPSCounterV2.s.sol --rpc-url ${RPC_URL} --broadcast -vvvv
21 changes: 0 additions & 21 deletions script/transparent/DeployCounterV1.s.sol

This file was deleted.

21 changes: 0 additions & 21 deletions script/transparent/DeployCounterV2.s.sol

This file was deleted.

2 changes: 0 additions & 2 deletions script/transparent/deploy_counter_v1.sh

This file was deleted.

2 changes: 0 additions & 2 deletions script/transparent/deploy_counter_v2.sh

This file was deleted.

Loading

0 comments on commit fd8b9ec

Please sign in to comment.