Skip to content

Commit

Permalink
aderyn and keyexistsjson update
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickAlphaC committed May 13, 2024
1 parent dacf76f commit 7bf9165
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 72 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ A repo to get the most recent deployment from a given environment in foundry. Th

It will look through your `broadcast` folder at your most recent deployment.

- [foundry-devops](#foundry-devops)
- [Getting Started](#getting-started)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Contributing](#contributing)
- [Testing](#testing)
- [foundry-devops](#foundry-devops)
- [Getting Started](#getting-started)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Contributing](#contributing)
- [Testing](#testing)

# Getting Started

Expand Down Expand Up @@ -65,6 +65,12 @@ function interactWithPreviouslyDeployedContracts() public {
}
```

3. To work with aderyn as a script, also set `ffi` to true in your `foundry.toml`:

```
ffi = true
```

# Contributing

PRs are welcome!
Expand Down
6 changes: 5 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
src = "src"
out = "out"
libs = ["lib"]
fs_permissions = [{ access = "read", path = "./broadcast" }, { access = "read", path = "./reports" }]
fs_permissions = [
{ access = "read", path = "./broadcast" },
{ access = "read", path = "./reports" },
]
# ffi = true

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
19 changes: 2 additions & 17 deletions src/AderynRunnerTool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ import {console} from "forge-std/console.sol";
import {stdJson} from "forge-std/StdJson.sol";
import {console} from "forge-std/console.sol";


struct IssueCount {
int256 critical;
int256 high;
int256 medium;
int256 low;
int256 nc;
}

library AderynRunnerTool {

using stdJson for string;

Vm public constant vm = Vm(address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))));
Expand All @@ -39,18 +34,8 @@ library AderynRunnerTool {

function get_issue_count(string memory filePath) public view returns (IssueCount memory) {
string memory json = vm.readFile(filePath);
int256 critical = json.readInt("$.issue_count.critical");
int256 high = json.readInt("$.issue_count.high");
int256 medium = json.readInt("$.issue_count.medium");
int256 low = json.readInt("$.issue_count.low");
int256 nc = json.readInt("$.issue_count.nc");
return IssueCount({
critical: critical,
high: high,
medium: medium,
low: low,
nc:nc
});
return IssueCount({high: high, low: low});
}

}
}
10 changes: 5 additions & 5 deletions src/DevOpsTools.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {StdCheatsSafe} from "forge-std/StdCheats.sol";
import {console} from "forge-std/console.sol";
import {StringUtils} from "./StringUtils.sol";


library DevOpsTools {
using stdJson for string;
using StringUtils for string;

Vm public constant vm = Vm(address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))));
Vm public constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

string public constant RELATIVE_BROADCAST_PATH = "./broadcast";

Expand Down Expand Up @@ -70,12 +69,13 @@ library DevOpsTools {
view
returns (address)
{
for(uint256 i = 0; vm.keyExists(json, string.concat("$.transactions[", vm.toString(i), "]")); i++) {
for (uint256 i = 0; vm.keyExistsJson(json, string.concat("$.transactions[", vm.toString(i), "]")); i++) {
string memory contractNamePath = string.concat("$.transactions[", vm.toString(i), "].contractName");
if (vm.keyExists(json, contractNamePath)) {
if (vm.keyExistsJson(json, contractNamePath)) {
string memory deployedContractName = json.readString(contractNamePath);
if (deployedContractName.isEqualTo(contractName)) {
latestAddress = json.readAddress(string.concat("$.transactions[", vm.toString(i), "].contractAddress"));
latestAddress =
json.readAddress(string.concat("$.transactions[", vm.toString(i), "].contractAddress"));
}
}
}
Expand Down
18 changes: 5 additions & 13 deletions test/AderynRunnerTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,26 @@ import "../src/AderynRunnerTool.sol";
import {console} from "forge-std/console.sol";

contract AderynRunnerTest is Test {

function setUp() public {

// Path to aderyn executable in your local computer
string memory ADERYN_PATH = "../aderyn/target/release/aderyn";
string memory ADERYN_PATH = "aderyn";

// Test if markdown creation works
AderynRunnerTool.run_with(ADERYN_PATH, "./reports/my_report.md");

// Test if json output works
AderynRunnerTool.run_with(ADERYN_PATH, "./reports/my_report.json");
}

// function testDefaultsAderynRunnerWorks() public {
// AderynRunnerTool.run_with_defaults();
// }

// forge-config: default.ffi = true
function testAderynThereAreNoCriticalErrors() public view {
IssueCount memory ic = AderynRunnerTool.get_issue_count("./reports/my_report.json");
assert(ic.critical == 0);
assert(ic.high == 0);
}

// forge-config: default.ffi = true
function testAderynThereAreLessThan10MediumOrLessSevereErrors() public view {
IssueCount memory ic = AderynRunnerTool.get_issue_count("./reports/my_report.json");
assert(ic.medium < 10);
assert(ic.low < 10);
assert(ic.nc < 10);
}

}
}
36 changes: 8 additions & 28 deletions test/DevOpsToolsTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,43 @@ import {DevOpsTools} from "../src/DevOpsTools.sol";
contract DevOpsToolsTest is Test {
string public constant SEARCH_PATH = "broadcast";

function testGetMostRecentlyDeployedContract() public {
function testGetMostRecentlyDeployedContract() public view {
string memory contractName = "Stuff";
uint256 chainId = 31337;
address expectedAddress = 0x5FbDB2315678afecb367f032d93F642f64180aa3;
address mostRecentDeployment = DevOpsTools.get_most_recent_deployment(
contractName,
chainId,
SEARCH_PATH
);
address mostRecentDeployment = DevOpsTools.get_most_recent_deployment(contractName, chainId, SEARCH_PATH);
assertEq(mostRecentDeployment, expectedAddress);
}

function testGetMostRecentlyDeployedEvenWhenMultipleAreDeployed() public {
function testGetMostRecentlyDeployedEvenWhenMultipleAreDeployed() public view {
string memory contractName = "FundMe";
uint256 chainId = 1234;
address expectedAddress = 0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9;
address mostRecentDeployment = DevOpsTools.get_most_recent_deployment(
contractName,
chainId,
SEARCH_PATH
);
address mostRecentDeployment = DevOpsTools.get_most_recent_deployment(contractName, chainId, SEARCH_PATH);
assertEq(mostRecentDeployment, expectedAddress);
}

function testExpectRevertIfNoRun() public {
string memory contractName = "FundMe";
uint256 chainId = 9999;
vm.expectRevert("No deployment artifacts were found for specified chain");
DevOpsTools.get_most_recent_deployment(
contractName,
chainId,
SEARCH_PATH
);
DevOpsTools.get_most_recent_deployment(contractName, chainId, SEARCH_PATH);
}

function testExpectRevertIfNoDeployment() public {
string memory contractName = "MissingContract";
uint256 chainId = 1234;
vm.expectRevert("No contract deployed");
DevOpsTools.get_most_recent_deployment(
contractName,
chainId,
SEARCH_PATH
);
DevOpsTools.get_most_recent_deployment(contractName, chainId, SEARCH_PATH);
}

// All other tests use what appear to be legacy broadcast files
// This one uses the newer type with no rpc property
function testNonLegacyBroadcast() public {
function testNonLegacyBroadcast() public view {
string memory contractName = "NewStuff";
uint256 chainId = 31337;
address expectedAddress = 0x5FbDB2315678afecb367f032d93F642f64180aa3;
address mostRecentDeployment = DevOpsTools.get_most_recent_deployment(
contractName,
chainId,
SEARCH_PATH
);
address mostRecentDeployment = DevOpsTools.get_most_recent_deployment(contractName, chainId, SEARCH_PATH);
assertEq(mostRecentDeployment, expectedAddress);
}
}

0 comments on commit 7bf9165

Please sign in to comment.