Skip to content

Commit

Permalink
Add env variable support to aderyn.toml (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
TilakMaddy authored Mar 10, 2025
1 parent 798bf95 commit 0b0458b
Show file tree
Hide file tree
Showing 19 changed files with 284 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/reports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- sablier
- adhoc-sol-files-workflow
- nft-workflow
- nft-workflow-env
- ccip-functions-report
- hardhat-playground-report
- prb-math-report
Expand Down Expand Up @@ -95,6 +96,10 @@ jobs:
cargo run -- -o ./reports/nft-workflow-report.md --src src/ ./tests/foundry-nft-f23 --skip-update-check
diff ./reports/nft-report.md ./reports/nft-workflow-report.md
;;
nft-workflow-env)
cargo run -- -o ./reports/nft-workflow-report-icm.md ./tests/foundry-nft-f23-icm --skip-update-check
diff ./reports/nft-report-icm.md ./reports/nft-workflow-report-icm.md
;;
ccip-functions-report)
cargo run -- -o reports/ccip-functions-report-workflow.md tests/ccip-contracts/contracts --src src/v0.8/functions/ -x "tests/,test/,mocks/" --skip-update-check
diff ./reports/ccip-functions-report.md ./reports/ccip-functions-report-workflow.md
Expand Down
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@
[submodule "tests/2024-07-templegold"]
path = tests/2024-07-templegold
url = https://github.com/Cyfrin/2024-07-templegold.git
[submodule "tests/foundry-nft-f23-icm/lib/forge-std"]
path = tests/foundry-nft-f23-icm/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "tests/foundry-nft-f23-icm/lib/openzeppelin-contracts"]
path = tests/foundry-nft-f23-icm/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
6 changes: 6 additions & 0 deletions aderyn/templates/aderyn.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ root = "."
# That would be the result of calling `foundry remappings`
# Example:
# remappings = ["@oz/contracts=lib/openzeppelin-contracts/contracts"]

# Environment variables that would help aderyn detect src, etc.
# In a medium sized foundry project, the profile can determine the values to be read from `foundry.toml`. For example, if different profiles have different src declaration in `foundry.toml`, these env variables can help decide which ones to read.

[env]
# FOUNDRY_PROFILE = "profile_name"
27 changes: 25 additions & 2 deletions aderyn_driver/src/config_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
fs,
collections::HashMap,
env, fs,
path::{Path, PathBuf},
};

Expand All @@ -16,6 +17,7 @@ pub struct AderynConfig {
pub exclude: Option<Vec<String>>,
pub remappings: Option<Vec<String>>,
pub include: Option<Vec<String>>,
pub env: Option<HashMap<String, String>>,
}

/// Load the aderyn.toml file and deserialize it to AderynConfig
Expand All @@ -38,9 +40,20 @@ fn load_aderyn_config(root: &Path) -> Result<AderynConfig, String> {
clear_empty_vectors(&mut config.remappings);
clear_empty_vectors(&mut config.include);

load_env_variables_from_config(&config);

Ok(config)
}

fn load_env_variables_from_config(config: &AderynConfig) {
// Load env variables
if let Some(map) = config.env.clone() {
map.iter().for_each(|(k, v)| {
env::set_var(k, v);
})
}
}

fn clear_empty_vectors<T>(vec: &mut Option<Vec<T>>) {
if let Some(v) = vec {
if v.is_empty() {
Expand Down Expand Up @@ -216,26 +229,34 @@ fn interpret_foundry_config(

#[cfg(test)]
mod tests {
use std::path::PathBuf;
use std::{collections::HashMap, env, path::PathBuf};

use cyfrin_foundry_config::RelativeRemapping;

use crate::config_helpers::load_env_variables_from_config;

#[test]
fn test_interpret_aderyn_config_correctly_appends_and_replaces() {
let env =
HashMap::from_iter(vec![("FOUNDRY_PROFILE".to_string(), "ENV_VAR_VALUE".to_string())]);

let config = super::AderynConfig {
version: 1,
root: Some("CONFIG_ROOT".to_string()),
src: Some("CONFIG_SRC".to_string()),
exclude: Some(vec!["CONFIG_EXCLUDE".to_string()]),
remappings: Some(vec!["CONFIG_REMAPPINGS".to_string()]),
include: Some(vec!["CONFIG_SCOPE".to_string()]),
env: Some(env),
};

let root = std::path::Path::new("ARG_ROOT");
let src = Some(vec!["ARG_SRC".to_string()]);
let exclude = Some(vec!["ARG_EXCLUDE_1".to_string(), "ARG_EXCLUDE_2".to_string()]);
let remappings = Some(vec!["ARG_REMAPPINGS_1".to_string(), "ARG_REMAPPINGS_2".to_string()]);
let include = Some(vec!["ARG_SCOPE_1".to_string(), "ARG_SCOPE_2".to_string()]);

load_env_variables_from_config(&config);
let result =
super::interpret_aderyn_config(config, root, &src, &exclude, &remappings, &include);
assert_eq!(result.0, std::path::Path::new("ARG_ROOT/CONFIG_ROOT"));
Expand Down Expand Up @@ -264,6 +285,8 @@ mod tests {
"CONFIG_SCOPE".to_string()
])
);

assert_eq!(env::var("FOUNDRY_PROFILE").unwrap(), "ENV_VAR_VALUE");
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions cli/reportgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ cargo run -- tests/2024-07-templegold/protocol -o reports/templegold-report.md -

cargo run -- tests/hardhat-js-playground -o reports/hardhat-playground-report.md --skip-update-check &

cargo run -- ./tests/foundry-nft-f23-icm -o ./reports/nft-report-icm.md --skip-update-check &

##### JSON REPORTS ########

# Basic report.json
Expand Down
37 changes: 37 additions & 0 deletions reports/nft-report-icm.md

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

34 changes: 34 additions & 0 deletions tests/foundry-nft-f23-icm/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: test

on: 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: Run Forge build
run: |
forge --version
forge build --sizes
id: build

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

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

# Docs
docs/

# Dotenv file
.env
broadcast
66 changes: 66 additions & 0 deletions tests/foundry-nft-f23-icm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## 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
```
8 changes: 8 additions & 0 deletions tests/foundry-nft-f23-icm/aderyn.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Aderyn Configuration File
# Help Aderyn work with more granular control

# DO NOT CHANGE version below. As of now, only 1 is supported
version = 1

[env]
FOUNDRY_PROFILE = "icm"
13 changes: 13 additions & 0 deletions tests/foundry-nft-f23-icm/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
remappings = ["@oz/contracts=lib/openzeppelin-contracts/contracts", "icm/=src/inner-core-modules"]

[profile.icm]
src = "src/inner-core-modules"
out = "out"
libs = ["lib"]
remappings = ["@oz/contracts=lib/openzeppelin-contracts/contracts", "icm/=src/inner-core-modules"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions tests/foundry-nft-f23-icm/lib/forge-std
Submodule forge-std added at 3b20d6
1 change: 1 addition & 0 deletions tests/foundry-nft-f23-icm/lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at acd4ff
7 changes: 7 additions & 0 deletions tests/foundry-nft-f23-icm/remappings
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@oz/contracts/=lib/openzeppelin-contracts/contracts/
icm/=src/inner-core-modules/
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/
erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/
forge-std/=lib/forge-std/src/
openzeppelin-contracts/=lib/openzeppelin-contracts/
23 changes: 23 additions & 0 deletions tests/foundry-nft-f23-icm/src/BasicNft.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.25;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "icm/ICM.sol"; // icm is remapped to src/inner-core-modules
import "./Initializer.sol";

contract BasicNft is ICM, ERC721 {

uint256 public s_tokenId;

constructor() ERC721(PROJECT_NAME, PROJECT_SYMBOL) {
Initializer initializer = new Initializer();
s_tokenId = initializer.get_start_token_id();
}

function mint() public {
_safeMint(msg.sender, s_tokenId);
s_tokenId++;
}

}
9 changes: 9 additions & 0 deletions tests/foundry-nft-f23-icm/src/F1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.10;

import "./F2.sol";

contract F1 {

}
7 changes: 7 additions & 0 deletions tests/foundry-nft-f23-icm/src/F2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.21;

contract F2 {

}
11 changes: 11 additions & 0 deletions tests/foundry-nft-f23-icm/src/Initializer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.25;

import "@oz/contracts/token/ERC721/ERC721.sol";

contract Initializer {
function get_start_token_id() public pure returns(uint256) {
return 10;
}
}
8 changes: 8 additions & 0 deletions tests/foundry-nft-f23-icm/src/inner-core-modules/ICM.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.25;

contract ICM {
string public PROJECT_NAME = "Dogie";
string public PROJECT_SYMBOL = "DOG";
}

0 comments on commit 0b0458b

Please sign in to comment.