Skip to content

memester-xyz/solenv

Repository files navigation

solenv

Load .env files in Solidity scripts/tests

Github Actions

⚠️ Note ⚠️

Foundry recently shipped default dotenv parsing. It's in active development but soon the solenv library will no longer be needed. Yay for upstreaming!

Installation

forge install memester-xyz/solenv

Usage

Firstly, it's very important that you do not commit your .env file. It should go without saying but make sure to add it to your .gitignore file! This repo has committed the .env and .env.test files only for examples and tests.

  1. Add this import to your script or test:
import {Solenv} from "solenv/Solenv.sol";
  1. Call .config() somewhere. It defaults to using .env in your project root, but you can pass another string as a parameter to load another file in instead.
// Inside a test
function setUp() public {
    Solenv.config();
}

// Inside a script, load a file with a different name
function run() public {
    Solenv.config(".env.prod");

    // Continue with your script...
}
  1. You can then use the standard "env" cheatcodes in order to read your variables. e.g. envString, envUint, envBool, etc.
string memory apiKey = vm.envString("API_KEY");
uint256 retries = vm.envUint("RETRIES");
bool ouputLogs = vm.envBool("OUTPUT_LOGS");
  1. You must enable ffi in order to use the library. You can either pass the --ffi flag to any forge commands you run (e.g. forge script Script --ffi), or you can add ffi = true to your foundry.toml file.

Notes

  • Comments start with # and must be on a newline
  • If you set a key twice, the last value in the file is used
  • It assumes you are running on a UNIX based machine with sh, cast and xxd installed.

Example

We have example usage for both tests and scripts.

To see the script in action, you can run:

forge script SolenvScript

Contributing

Clone this repo and run:

forge install

Make sure all tests pass, add new ones if needed:

forge test

Why?

Forge scripting is becoming more popular. With solenv your scripts are even more powerful and natural to work with.

Development

This project uses Foundry. See the book for instructions on how to install and use Foundry.

About

Dotenv parser for solidity & foundry

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages